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