Open History to see the last commit
1import {
2 McpCapabilityUnavailableError,
3 OrchestratorMcpEnvironmentTarget,
4 OrchestratorMcpFailure,
5 PositiveInt,
6 PullRequestCheck,
7 PullRequestCommentKind,
8 PullRequestChecksState,
9 PullRequestInvolvement,
10 PullRequestListState,
11 PullRequestMergeability,
12 PullRequestMergeMethod,
13 PullRequestReviewVerdict,
14 PullRequestReviewDecision,
15 PullRequestReviewGuide,
16 PullRequestReviewGuideRun,
17 PullRequestState,
18 PullRequestUnavailableError,
19 PullRequestVisibility,
20 PullRequestWatchUntil,
21 ThreadPullRequestLinkSource,
22 TrimmedNonEmptyString,
23 ThreadId,
24} from "@t3tools/contracts";
25import * as Schema from "effect/Schema";
26import * as McpSchema from "effect/ai/McpSchema";
27import * as Tool from "effect/ai/Tool";
28import * as Toolkit from "effect/ai/Toolkit";
29
30import * as McpInvocationContext from "../../McpInvocationContext.ts";
31import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
32import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
33import * as GitManager from "../../../git/GitManager.ts";
34import * as ProjectService from "../../../project/ProjectService.ts";
35import * as PullRequestWatchService from "../../../pullRequest/PullRequestWatchService.ts";
36import {
37 PullRequestUrlInvalidError,
38 PullRequestTargetIncompleteError,
39 PullRequestHostRequiredError,
40 PullRequestUrlUnavailableError,
41} from "../../../pullRequest/pullRequestTarget.ts";
42import * as PullRequestService from "../../../pullRequest/PullRequestService.ts";
43import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
44
45const dependencies = [
46 McpInvocationContext.McpInvocationContext,
47 ThreadManagementService.ThreadManagementService,
48 Orchestrator.OrchestratorV2,
49 ProjectService.ProjectService,
50];
51
52const forgeDependencies = [...dependencies, PullRequestService.PullRequestService];
53
54const guideDependencies = [
55 ...dependencies,
56 PullRequestReviewGuideService.PullRequestReviewGuideService,
57];
58
59const watchDependencies = [...dependencies, PullRequestWatchService.PullRequestWatchService];
60
61export const PullRequestTargetInput = Schema.Struct({
62 environmentId: OrchestratorMcpEnvironmentTarget,
63 threadId: Schema.optional(
64 ThreadId.annotate({
65 description: "Thread. Default: this thread.",
66 }),
67 ),
68 url: Schema.optional(TrimmedNonEmptyString),
69 repository: Schema.optional(
70 TrimmedNonEmptyString.annotate({
71 description: "With `number`, when no `url`.",
72 }),
73 ),
74 number: Schema.optional(
75 PositiveInt.annotate({
76 description: "With `repository`, when no `url`.",
77 }),
78 ),
79 host: Schema.optional(
80 TrimmedNonEmptyString.annotate({
81 description: "Default: this project's host.",
82 }),
83 ),
84});
85export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
86
87export class PullRequestThreadRequiredError extends Schema.TaggedError<PullRequestThreadRequiredError>()(
88 "PullRequestThreadRequiredError",
89 {},
90) {
91 override get message(): string {
92 return "Pass threadId: this MCP client is not running inside a T3 thread.";
93 }
94}
95
96export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
97 "PullRequestLinkFailedError",
98 { cause: Schema.Defect() },
99) {
100 override get message(): string {
101 return "Could not link the pull request.";
102 }
103}
104
105export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
106 "PullRequestUnlinkFailedError",
107 { cause: Schema.Defect() },
108) {
109 override get message(): string {
110 return "Could not unlink the pull request.";
111 }
112}
113
114export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
115 "PullRequestListFailedError",
116 { cause: Schema.Defect() },
117) {
118 override get message(): string {
119 return "Could not list the pull request.";
120 }
121}
122
123export class PullRequestProjectNotFoundError extends Schema.TaggedError<PullRequestProjectNotFoundError>()(
124 "PullRequestProjectNotFoundError",
125 { repository: Schema.String },
126) {
127 override get message(): string {
128 return `No project in this environment is a checkout of ${this.repository}, so its pull requests cannot be read here. Add one with t3_project_create.`;
129 }
130}
131
132export class PullRequestGuideFailedError extends Schema.TaggedError<PullRequestGuideFailedError>()(
133 "PullRequestGuideFailedError",
134 { detail: Schema.String, cause: Schema.optional(Schema.Defect()) },
135) {
136 override get message(): string {
137 return this.detail;
138 }
139}
140
141export class PullRequestForgeError extends Schema.TaggedError<PullRequestForgeError>()(
142 "PullRequestForgeError",
143 { detail: Schema.optional(Schema.String), cause: Schema.optional(Schema.Defect()) },
144) {
145 override get message(): string {
146 return this.detail ?? "Could not read this thread's project.";
147 }
148}
149
150export class PullRequestBranchHasNoneError extends Schema.TaggedError<PullRequestBranchHasNoneError>()(
151 "PullRequestBranchHasNoneError",
152 {},
153) {
154 override get message(): string {
155 return "This thread's branch has no known PR. Pass number, or open one with pull_request_create.";
156 }
157}
158
159export class PullRequestCursorInvalidError extends Schema.TaggedError<PullRequestCursorInvalidError>()(
160 "PullRequestCursorInvalidError",
161 {},
162) {
163 override get message(): string {
164 return "This cursor is not from this tool. Pass a nextCursor from its last result.";
165 }
166}
167
168export class PullRequestNothingToDoError extends Schema.TaggedError<PullRequestNothingToDoError>()(
169 "PullRequestNothingToDoError",
170 { detail: Schema.String },
171) {
172 override get message(): string {
173 return this.detail;
174 }
175}
176
177export const PullRequestToolError = Schema.Union([
178 OrchestratorMcpFailure,
179 McpCapabilityUnavailableError,
180 PullRequestUrlInvalidError,
181 PullRequestTargetIncompleteError,
182 PullRequestHostRequiredError,
183 PullRequestUrlUnavailableError,
184 PullRequestThreadRequiredError,
185 PullRequestLinkFailedError,
186 PullRequestUnlinkFailedError,
187 PullRequestListFailedError,
188 PullRequestWatchService.PullRequestWatchFailedError,
189 PullRequestUnavailableError,
190 PullRequestWatchService.PullRequestWatchFromSubagentError,
191 PullRequestProjectNotFoundError,
192 PullRequestGuideFailedError,
193 PullRequestForgeError,
194 PullRequestBranchHasNoneError,
195 PullRequestNothingToDoError,
196 PullRequestCursorInvalidError,
197]);
198export type PullRequestToolError = typeof PullRequestToolError.Type;
199
200const PullRequestIdentity = {
201 host: Schema.String,
202 repository: Schema.String,
203 number: Schema.Int,
204 url: Schema.String,
205};
206
207/** A delegated child's report is how its parent learns the PR, so the link result asks for it. */
208export const CHILD_PULL_REQUEST_REPORT_NEXT =
209 "End your reply with: PR <full URL>, head <SHA>, checks and reviews <state>.";
210
211export const LinkPullRequestResult = Schema.Struct({
212 ...PullRequestIdentity,
213 alreadyLinked: Schema.Boolean,
214 next: Schema.optional(Schema.String),
215});
216export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
217
218const PullRequestNumbersByRepository = Schema.Array(
219 Schema.Struct({
220 host: Schema.String,
221 repository: Schema.String,
222 numbers: Schema.Array(Schema.Int),
223 }),
224);
225
226export const LinkPullRequestsResult = Schema.Struct({
227 linked: PullRequestNumbersByRepository,
228 alreadyLinked: PullRequestNumbersByRepository,
229 linkedCount: Schema.Int,
230 invalidUrls: Schema.optional(Schema.Array(Schema.String)),
231 next: Schema.optional(Schema.String),
232});
233export type LinkPullRequestsResult = typeof LinkPullRequestsResult.Type;
234
235export const UnlinkPullRequestResult = Schema.Struct({
236 host: Schema.String,
237 repository: Schema.String,
238 number: Schema.Int,
239 wasLinked: Schema.Boolean,
240});
241export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
242
243export const UnlinkPullRequestsResult = Schema.Struct({
244 unlinked: Schema.Array(
245 Schema.Struct({
246 host: Schema.String,
247 repository: Schema.String,
248 state: PullRequestState,
249 numbers: Schema.Array(Schema.Int),
250 }),
251 ),
252 unlinkedCount: Schema.Int,
253 remaining: Schema.Int,
254});
255export type UnlinkPullRequestsResult = typeof UnlinkPullRequestsResult.Type;
256
257export const WatchPullRequestResult = Schema.Struct({
258 ...PullRequestIdentity,
259 watching: Schema.Boolean,
260 wasWatching: Schema.Boolean,
261 state: Schema.optional(
262 Schema.Literals(["merged", "closed"]).annotate({
263 description: "The PR already ended, so nothing was watched.",
264 }),
265 ),
266 next: Schema.optional(Schema.String),
267});
268export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
269
270export const alreadyEndedWatchNext = (state: "merged" | "closed") =>
271 `This PR is already ${state}, so there is nothing to watch. Report that and end your turn.`;
272
273export const ThreadPullRequestEntry = Schema.Struct({
274 ...PullRequestIdentity,
275 source: ThreadPullRequestLinkSource,
276 watching: Schema.Boolean,
277 state: Schema.NullOr(PullRequestState),
278 title: Schema.NullOr(Schema.String),
279 headBranch: Schema.NullOr(Schema.String),
280 baseBranch: Schema.NullOr(Schema.String),
281 isDraft: Schema.NullOr(Schema.Boolean),
282 stack: Schema.NullOr(
283 Schema.Struct({
284 kind: Schema.Literals(["native", "derived"]),
285 /** 1-based layer from the bottom; siblings share a layer. */
286 position: Schema.Int,
287 size: Schema.Int,
288 }),
289 ),
290});
291export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
292
293export const ListThreadPullRequestsResult = Schema.Struct({
294 pullRequests: Schema.Array(ThreadPullRequestEntry),
295 chains: Schema.Array(
296 Schema.Struct({
297 kind: Schema.Literals(["native", "derived"]),
298 /** Bottom to top; each pull request comes before the ones built on it. */
299 numbers: Schema.Array(Schema.Int),
300 }),
301 ),
302 /** Pass as `cursor` for the next page; null on the last page. */
303 nextCursor: Schema.NullOr(Schema.String),
304});
305export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
306
307const LinkPullRequestTool = Tool.make("link_pull_request", {
308 description:
309 "Link a PR to this thread. T3 shows its status and settles the thread on merge. For several PRs, use `link_pull_requests`.",
310 parameters: PullRequestTargetInput,
311 success: LinkPullRequestResult,
312 failure: PullRequestToolError,
313 dependencies,
314})
315 .annotate(Tool.Title, "Link pull request to thread")
316 .annotate(Tool.Readonly, false)
317 .annotate(Tool.Destructive, false)
318 .annotate(Tool.Idempotent, true)
319 .annotate(Tool.OpenWorld, false);
320
321const LinkPullRequestsTool = Tool.make("link_pull_requests", {
322 description:
323 "Link several PRs to a thread. Use it instead of one `link_pull_request` per PR. URLs that are not PRs come back in `invalidUrls`.",
324 parameters: Schema.Struct({
325 environmentId: OrchestratorMcpEnvironmentTarget,
326 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
327 urls: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1)),
328 }),
329 success: LinkPullRequestsResult,
330 failure: PullRequestToolError,
331 dependencies,
332})
333 .annotate(Tool.Title, "Link pull requests to thread")
334 .annotate(Tool.Readonly, false)
335 .annotate(Tool.Destructive, false)
336 .annotate(Tool.Idempotent, true)
337 .annotate(Tool.OpenWorld, false);
338
339const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
340 description:
341 "Remove a PR link from this thread. Succeeds with `wasLinked=false` when it was not linked.",
342 parameters: PullRequestTargetInput,
343 success: UnlinkPullRequestResult,
344 failure: PullRequestToolError,
345 dependencies,
346})
347 .annotate(Tool.Title, "Unlink pull request from thread")
348 .annotate(Tool.Readonly, false)
349 .annotate(Tool.Destructive, true)
350 .annotate(Tool.Idempotent, true)
351 .annotate(Tool.OpenWorld, false);
352
353const UnlinkPullRequestsTool = Tool.make("unlink_pull_requests", {
354 description:
355 "Unlink a thread's PRs whose last known state is in `states`. Use it instead of one `unlink_pull_request` per PR. PRs with no known state stay linked.",
356 parameters: Schema.Struct({
357 environmentId: OrchestratorMcpEnvironmentTarget,
358 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
359 states: Schema.Array(PullRequestState).check(Schema.isMinLength(1)),
360 }),
361 success: UnlinkPullRequestsResult,
362 failure: PullRequestToolError,
363 dependencies,
364})
365 .annotate(Tool.Title, "Unlink pull requests by state")
366 .annotate(Tool.Readonly, false)
367 .annotate(Tool.Destructive, true)
368 .annotate(Tool.Idempotent, true)
369 .annotate(Tool.OpenWorld, false);
370
371const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
372 description:
373 "List a thread's PRs, with last known state and stack order (bottom to top). Not an end-of-work check.",
374 parameters: Schema.Struct({
375 environmentId: OrchestratorMcpEnvironmentTarget,
376 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
377 states: Schema.optional(Schema.Array(PullRequestState)),
378 cursor: Schema.optional(Schema.String),
379 }),
380 success: ListThreadPullRequestsResult,
381 failure: PullRequestToolError,
382 dependencies,
383})
384 .annotate(Tool.Title, "List thread pull requests")
385 .annotate(Tool.Readonly, true)
386 .annotate(Tool.Destructive, false)
387 .annotate(Tool.Idempotent, true)
388 .annotate(Tool.OpenWorld, false);
389
390const WatchPullRequestInput = Schema.Struct({
391 ...PullRequestTargetInput.fields,
392 until: Schema.optional(
393 PullRequestWatchUntil.annotate({
394 description:
395 'How long this thread stays working. Omit to keep the current value; a new watch starts at "checks". "checks": until checks finish, for a PR you hand to the user. "merged": until it merges, closes or is unwatched, when you act on it yourself, such as merging after approval.',
396 }),
397 ),
398});
399
400const WatchPullRequestTool = Tool.make("watch_pull_request", {
401 description:
402 "Watch an open PR for this thread, linking it if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, conflicts, and once on merge or close unless you did it via CLI. Handle old comments first; only new ones wake you. After this call, end your turn. The watch stays on: do not call this again on a wake. A wake is not a merge decision. Check readiness yourself.",
403 parameters: WatchPullRequestInput,
404 success: WatchPullRequestResult,
405 failure: PullRequestToolError,
406 dependencies: watchDependencies,
407})
408 .annotate(Tool.Title, "Watch pull request")
409 .annotate(Tool.Readonly, false)
410 .annotate(Tool.Destructive, false)
411 .annotate(Tool.Idempotent, true)
412 .annotate(Tool.OpenWorld, false)
413 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
414
415const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
416 description: "Stop watching a PR for this thread. It stays linked.",
417 parameters: PullRequestTargetInput,
418 success: WatchPullRequestResult,
419 failure: PullRequestToolError,
420 dependencies: watchDependencies,
421})
422 .annotate(Tool.Title, "Stop watching pull request")
423 .annotate(Tool.Readonly, false)
424 .annotate(Tool.Destructive, false)
425 .annotate(Tool.Idempotent, true)
426 .annotate(Tool.OpenWorld, false)
427 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
428
429const GuideTarget = Schema.Struct({
430 url: PullRequestTargetInput.fields.url,
431 repository: PullRequestTargetInput.fields.repository,
432 number: PullRequestTargetInput.fields.number,
433 host: PullRequestTargetInput.fields.host,
434});
435
436const GuideOutcome = Schema.Struct({
437 ...PullRequestIdentity,
438 outcome: Schema.Literals(["started", "running", "failed"]).annotate({
439 description: "started: a run began. running: joined a run for this head. failed: see detail.",
440 }),
441 detail: Schema.NullOr(Schema.String),
442});
443
444export const WritePullRequestGuidesResult = Schema.Struct({
445 guides: Schema.Array(GuideOutcome),
446});
447export type WritePullRequestGuidesResult = typeof WritePullRequestGuidesResult.Type;
448
449export const ReadPullRequestGuideResult = Schema.Struct({
450 ...PullRequestIdentity,
451 guide: Schema.NullOr(PullRequestReviewGuide),
452 run: Schema.NullOr(PullRequestReviewGuideRun),
453 currentHeadSha: Schema.NullOr(Schema.String),
454 stale: Schema.Boolean.annotate({
455 description: "True when the PR head moved after the guide was written.",
456 }),
457});
458export type ReadPullRequestGuideResult = typeof ReadPullRequestGuideResult.Type;
459
460const WritePullRequestGuidesTool = Tool.make("write_pull_request_guides", {
461 description:
462 "Write review guides for up to 20 PRs (the tour in the PR Guide tab). Returns once runs start. Each run takes minutes in a temporary checkout. A run for the same head is joined. Read with `read_pull_request_guide`. Fails while guides are off.",
463 parameters: Schema.Struct({
464 environmentId: OrchestratorMcpEnvironmentTarget,
465 pullRequests: Schema.Array(GuideTarget).check(Schema.isMinLength(1), Schema.isMaxLength(20)),
466 }),
467 success: WritePullRequestGuidesResult,
468 failure: PullRequestToolError,
469 dependencies: guideDependencies,
470})
471 .annotate(Tool.Title, "Write pull request guides")
472 .annotate(Tool.Readonly, false)
473 .annotate(Tool.Destructive, false)
474 .annotate(Tool.Idempotent, false)
475 .annotate(Tool.OpenWorld, true);
476
477const ReadPullRequestGuideTool = Tool.make("read_pull_request_guide", {
478 description:
479 "Read a PR's stored review guide and run state. `guide` is null when none exists. Never starts a run.",
480 parameters: Schema.Struct({
481 environmentId: OrchestratorMcpEnvironmentTarget,
482 ...GuideTarget.fields,
483 }),
484 success: ReadPullRequestGuideResult,
485 failure: PullRequestToolError,
486 dependencies: guideDependencies,
487})
488 .annotate(Tool.Title, "Read pull request guide")
489 .annotate(Tool.Readonly, true)
490 .annotate(Tool.Destructive, false)
491 .annotate(Tool.Idempotent, true)
492 .annotate(Tool.OpenWorld, true);
493
494export const ThreadPullRequestInput = Schema.Struct({
495 environmentId: OrchestratorMcpEnvironmentTarget,
496 number: Schema.optional(PositiveInt.annotate({ description: "Default: the branch's PR." })),
497 repository: Schema.optional(TrimmedNonEmptyString),
498});
499export type ThreadPullRequestInput = typeof ThreadPullRequestInput.Type;
500
501export const PullRequestReadSection = Schema.Literals([
502 "body",
503 "checks",
504 "threads",
505 "comments",
506 "diff",
507]);
508export type PullRequestReadSection = typeof PullRequestReadSection.Type;
509
510const ReadComment = Schema.Struct({
511 author: Schema.NullOr(Schema.String),
512 body: Schema.String,
513 createdAt: Schema.String,
514});
515
516export const ReadPullRequestResult = Schema.Struct({
517 ...PullRequestIdentity,
518 title: Schema.String,
519 state: PullRequestState,
520 isDraft: Schema.Boolean,
521 visibility: Schema.optional(PullRequestVisibility).annotate({
522 description: "Only on a host with private PRs.",
523 }),
524 author: Schema.NullOr(Schema.String),
525 headBranch: Schema.String,
526 baseBranch: Schema.String,
527 headSha: Schema.NullOr(Schema.String),
528 mergeability: PullRequestMergeability,
529 behindBy: Schema.NullOr(Schema.Int),
530 autoMergeEnabled: Schema.NullOr(Schema.Boolean),
531 checks: Schema.Struct({
532 passed: Schema.Int,
533 pending: Schema.Int,
534 failed: Schema.Array(Schema.String),
535 incomplete: Schema.optional(Schema.Literal(true)).annotate({
536 description: "The host could not list some checks; checkRuns says which.",
537 }),
538 }),
539 unresolvedThreads: Schema.Int,
540 unresolvedThreadsTruncated: Schema.optional(Schema.Literal(true)).annotate({
541 description: "unresolvedThreads is a lower bound.",
542 }),
543 body: Schema.optional(Schema.String),
544 checkRuns: Schema.optional(Schema.Array(PullRequestCheck)),
545 conversation: Schema.optional(
546 Schema.Struct({
547 reviewThreads: Schema.optional(
548 Schema.Array(
549 Schema.Struct({
550 id: Schema.String,
551 path: Schema.String,
552 line: Schema.NullOr(Schema.Int),
553 isResolved: Schema.Boolean,
554 isOutdated: Schema.Boolean,
555 comments: Schema.Array(ReadComment),
556 moreComments: Schema.Boolean,
557 }),
558 ),
559 ),
560 comments: Schema.optional(
561 Schema.Array(
562 Schema.Struct({
563 ...ReadComment.fields,
564 id: Schema.String,
565 kind: PullRequestCommentKind,
566 path: Schema.NullOr(Schema.String),
567 reviewState: Schema.NullOr(Schema.String),
568 }),
569 ),
570 ),
571 truncated: Schema.Boolean,
572 nextCursor: Schema.NullOr(Schema.String),
573 }),
574 ),
575 diff: Schema.optional(
576 Schema.Struct({
577 patch: Schema.String,
578 truncated: Schema.Boolean,
579 nextCursor: Schema.NullOr(Schema.String),
580 }),
581 ),
582});
583export type ReadPullRequestResult = typeof ReadPullRequestResult.Type;
584
585export const ListPullRequestsResult = Schema.Struct({
586 pullRequests: Schema.Array(
587 Schema.Struct({
588 number: Schema.Int,
589 title: Schema.String,
590 url: Schema.String,
591 author: Schema.NullOr(Schema.String),
592 headBranch: Schema.String,
593 baseBranch: Schema.String,
594 state: PullRequestState,
595 isDraft: Schema.Boolean,
596 reviewDecision: Schema.NullOr(PullRequestReviewDecision),
597 checksState: Schema.NullOr(PullRequestChecksState),
598 updatedAt: Schema.String,
599 }),
600 ),
601 truncated: Schema.Boolean,
602 nextCursor: Schema.NullOr(Schema.String),
603});
604export type ListPullRequestsResult = typeof ListPullRequestsResult.Type;
605
606export const CreatePullRequestResult = Schema.Struct({
607 ...PullRequestIdentity,
608 created: Schema.Boolean.annotate({
609 description: "False when the branch already had an open PR.",
610 }),
611 next: Schema.optional(Schema.String),
612});
613export type CreatePullRequestResult = typeof CreatePullRequestResult.Type;
614
615export const RespondToPullRequestResult = Schema.Struct({
616 ...PullRequestIdentity,
617 replied: Schema.Boolean,
618 resolved: Schema.NullOr(Schema.Boolean),
619});
620export type RespondToPullRequestResult = typeof RespondToPullRequestResult.Type;
621
622export const MergePullRequestResult = Schema.Struct({
623 ...PullRequestIdentity,
624 state: PullRequestState.annotate({
625 description: "open while auto-merge or a merge queue waits for checks.",
626 }),
627});
628export type MergePullRequestResult = typeof MergePullRequestResult.Type;
629
630const ReadPullRequestTool = Tool.make("pull_request_read", {
631 description: "Read a PR's state and check counts. `include` adds detail.",
632 parameters: Schema.Struct({
633 ...ThreadPullRequestInput.fields,
634 include: Schema.optional(
635 Schema.Array(PullRequestReadSection).annotate({
636 description: "threads: unresolved review threads.",
637 }),
638 ),
639 includeResolved: Schema.optional(Schema.Boolean),
640 cursor: Schema.optional(TrimmedNonEmptyString),
641 }),
642 success: ReadPullRequestResult,
643 failure: PullRequestToolError,
644 dependencies: forgeDependencies,
645})
646 .annotate(Tool.Title, "Read pull request")
647 .annotate(Tool.Readonly, true)
648 .annotate(Tool.Destructive, false)
649 .annotate(Tool.Idempotent, true)
650 .annotate(Tool.OpenWorld, true)
651 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
652
653const ListPullRequestsTool = Tool.make("pull_request_list", {
654 description: "List PRs in this thread's repository, recently updated first.",
655 parameters: Schema.Struct({
656 environmentId: OrchestratorMcpEnvironmentTarget,
657 state: Schema.optional(PullRequestListState.annotate({ description: "Default: open." })),
658 involvement: Schema.optional(PullRequestInvolvement),
659 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
660 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
661 cursor: Schema.optional(TrimmedNonEmptyString),
662 }),
663 success: ListPullRequestsResult,
664 failure: PullRequestToolError,
665 dependencies: forgeDependencies,
666})
667 .annotate(Tool.Title, "List pull requests")
668 .annotate(Tool.Readonly, true)
669 .annotate(Tool.Destructive, false)
670 .annotate(Tool.Idempotent, true)
671 .annotate(Tool.OpenWorld, true)
672 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
673
674const CreatePullRequestTool = Tool.make("pull_request_create", {
675 description: "Push this thread's branch, then open its PR and link it, or return its open PR.",
676 parameters: Schema.Struct({
677 environmentId: OrchestratorMcpEnvironmentTarget,
678 title: TrimmedNonEmptyString,
679 body: Schema.String,
680 base: Schema.optional(TrimmedNonEmptyString),
681 draft: Schema.optional(Schema.Boolean),
682 private: Schema.optional(Schema.Boolean).annotate({
683 description:
684 "Visible to writers only; the branch never reaches origin. Fails on a host without private PRs.",
685 }),
686 }),
687 success: CreatePullRequestResult,
688 failure: PullRequestToolError,
689 dependencies: [...forgeDependencies, GitManager.GitManager],
690})
691 .annotate(Tool.Title, "Create pull request")
692 .annotate(Tool.Readonly, false)
693 .annotate(Tool.Destructive, false)
694 .annotate(Tool.Idempotent, true)
695 .annotate(Tool.OpenWorld, true)
696 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
697
698const UpdateNames = Schema.Array(TrimmedNonEmptyString).check(
699 Schema.isMinLength(1),
700 Schema.isMaxLength(25),
701);
702
703const UpdatePullRequestTool = Tool.make("pull_request_update", {
704 description:
705 "Edit a PR's title, body, labels, or reviewers, close or reopen it, or set ready (false: back to draft).",
706 parameters: Schema.Struct({
707 ...ThreadPullRequestInput.fields,
708 title: Schema.optional(TrimmedNonEmptyString),
709 body: Schema.optional(Schema.String),
710 ready: Schema.optional(Schema.Boolean),
711 state: Schema.optional(Schema.Literals(["open", "closed"])),
712 addLabels: Schema.optional(UpdateNames),
713 removeLabels: Schema.optional(UpdateNames),
714 requestReviewers: Schema.optional(
715 UpdateNames.annotate({ description: "Logins, or org/team for a team." }),
716 ),
717 }),
718 success: Schema.Struct(PullRequestIdentity),
719 failure: PullRequestToolError,
720 dependencies: forgeDependencies,
721})
722 .annotate(Tool.Title, "Update pull request")
723 .annotate(Tool.Readonly, false)
724 .annotate(Tool.Destructive, false)
725 .annotate(Tool.Idempotent, true)
726 .annotate(Tool.OpenWorld, true)
727 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
728
729const RespondToPullRequestTool = Tool.make("pull_request_respond", {
730 description:
731 "Reply to, resolve, or reopen a review thread. Without reviewThreadId, comment on the PR.",
732 parameters: Schema.Struct({
733 ...ThreadPullRequestInput.fields,
734 reviewThreadId: Schema.optional(TrimmedNonEmptyString),
735 body: Schema.optional(TrimmedNonEmptyString),
736 resolved: Schema.optional(Schema.Boolean),
737 }),
738 success: RespondToPullRequestResult,
739 failure: PullRequestToolError,
740 dependencies: forgeDependencies,
741})
742 .annotate(Tool.Title, "Respond on pull request")
743 .annotate(Tool.Readonly, false)
744 .annotate(Tool.Destructive, false)
745 .annotate(Tool.Idempotent, false)
746 .annotate(Tool.OpenWorld, true)
747 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
748
749export const ReviewLineComment = Schema.Struct({
750 path: TrimmedNonEmptyString,
751 line: PositiveInt,
752 startLine: Schema.optional(PositiveInt),
753 side: Schema.optional(
754 Schema.Literals(["new", "old"]).annotate({
755 description: "Default new. old: line numbers in the base, for removed lines.",
756 }),
757 ),
758 body: TrimmedNonEmptyString,
759});
760export type ReviewLineComment = typeof ReviewLineComment.Type;
761
762const ReviewPullRequestTool = Tool.make("pull_request_review", {
763 description:
764 "Submit a review: approve, request changes, or comment, with optional line comments.",
765 parameters: Schema.Struct({
766 ...ThreadPullRequestInput.fields,
767 verdict: PullRequestReviewVerdict,
768 body: Schema.optional(Schema.String),
769 comments: Schema.optional(Schema.Array(ReviewLineComment).check(Schema.isMaxLength(50))),
770 }),
771 success: Schema.Struct(PullRequestIdentity),
772 failure: PullRequestToolError,
773 dependencies: forgeDependencies,
774})
775 .annotate(Tool.Title, "Review pull request")
776 .annotate(Tool.Readonly, false)
777 .annotate(Tool.Destructive, false)
778 .annotate(Tool.Idempotent, false)
779 .annotate(Tool.OpenWorld, true)
780 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
781
782const MergePullRequestTool = Tool.make("pull_request_merge", {
783 description:
784 "Merge a PR. Refuses if the head moved from expectedHeadSha (headSha in pull_request_read).",
785 parameters: Schema.Struct({
786 ...ThreadPullRequestInput.fields,
787 expectedHeadSha: TrimmedNonEmptyString,
788 method: Schema.optional(PullRequestMergeMethod),
789 auto: Schema.optional(
790 Schema.Boolean.annotate({
791 description: "Merge when required checks pass, including later pushes.",
792 }),
793 ),
794 stack: Schema.optional(
795 Schema.Boolean.annotate({
796 description: "Merge this PR and every open stack layer below it at once. Not with auto.",
797 }),
798 ),
799 }),
800 success: MergePullRequestResult,
801 failure: PullRequestToolError,
802 dependencies: forgeDependencies,
803})
804 .annotate(Tool.Title, "Merge pull request")
805 .annotate(Tool.Readonly, false)
806 .annotate(Tool.Destructive, true)
807 .annotate(Tool.Idempotent, false)
808 .annotate(Tool.OpenWorld, true)
809 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
810
811export const CheckLogsResult = Schema.Struct({
812 ...PullRequestIdentity,
813 checks: Schema.Array(
814 Schema.Struct({
815 name: Schema.String,
816 url: Schema.NullOr(Schema.String),
817 log: Schema.NullOr(Schema.String),
818 truncated: Schema.Boolean,
819 detail: Schema.NullOr(Schema.String),
820 }),
821 ),
822});
823export type CheckLogsResult = typeof CheckLogsResult.Type;
824
825export const RerunChecksResult = Schema.Struct({
826 ...PullRequestIdentity,
827 checks: Schema.Array(
828 Schema.Struct({
829 name: Schema.String,
830 rerun: Schema.Boolean,
831 detail: Schema.NullOr(Schema.String),
832 }),
833 ),
834});
835export type RerunChecksResult = typeof RerunChecksResult.Type;
836
837const CheckLogsTool = Tool.make("ci_logs", {
838 description: "Read the tail of a PR's failing check logs, or one named check's.",
839 parameters: Schema.Struct({
840 ...ThreadPullRequestInput.fields,
841 check: Schema.optional(TrimmedNonEmptyString),
842 tailLines: Schema.optional(
843 Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 2000 })).annotate({
844 description: "Default: 200.",
845 }),
846 ),
847 }),
848 success: CheckLogsResult,
849 failure: PullRequestToolError,
850 dependencies: forgeDependencies,
851})
852 .annotate(Tool.Title, "Read CI logs")
853 .annotate(Tool.Readonly, true)
854 .annotate(Tool.Destructive, false)
855 .annotate(Tool.Idempotent, true)
856 .annotate(Tool.OpenWorld, true)
857 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
858
859const RerunChecksTool = Tool.make("ci_rerun", {
860 description: "Re-run a PR's failing checks, or one named check.",
861 parameters: Schema.Struct({
862 ...ThreadPullRequestInput.fields,
863 check: Schema.optional(TrimmedNonEmptyString),
864 }),
865 success: RerunChecksResult,
866 failure: PullRequestToolError,
867 dependencies: forgeDependencies,
868})
869 .annotate(Tool.Title, "Re-run CI")
870 .annotate(Tool.Readonly, false)
871 .annotate(Tool.Destructive, false)
872 .annotate(Tool.Idempotent, false)
873 .annotate(Tool.OpenWorld, true)
874 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
875
876export const PullRequestsToolkit = Toolkit.make(
877 LinkPullRequestTool,
878 LinkPullRequestsTool,
879 UnlinkPullRequestTool,
880 UnlinkPullRequestsTool,
881 ListThreadPullRequestsTool,
882 WatchPullRequestTool,
883 UnwatchPullRequestTool,
884 WritePullRequestGuidesTool,
885 ReadPullRequestGuideTool,
886 ReadPullRequestTool,
887 ListPullRequestsTool,
888 CreatePullRequestTool,
889 UpdatePullRequestTool,
890 RespondToPullRequestTool,
891 ReviewPullRequestTool,
892 MergePullRequestTool,
893 CheckLogsTool,
894 RerunChecksTool,
895);