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