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 headBranch: Schema.String,
615 created: Schema.Boolean.annotate({
616 description: "False when the branch already had an open PR.",
617 }),
618 note: Schema.optional(Schema.String),
619 next: Schema.optional(Schema.String),
620});
621export type CreatePullRequestResult = typeof CreatePullRequestResult.Type;
622
623export const RespondToPullRequestResult = Schema.Struct({
624 ...PullRequestIdentity,
625 replied: Schema.Boolean,
626 resolved: Schema.NullOr(Schema.Boolean),
627});
628export type RespondToPullRequestResult = typeof RespondToPullRequestResult.Type;
629
630export const MergePullRequestResult = Schema.Struct({
631 ...PullRequestIdentity,
632 state: Schema.NullOr(PullRequestState).annotate({
633 description:
634 "open while auto-merge or a merge queue waits for checks. null: done, but not read back.",
635 }),
636 note: Schema.optional(Schema.String),
637});
638export type MergePullRequestResult = typeof MergePullRequestResult.Type;
639
640const ReadPullRequestTool = Tool.make("pull_request_read", {
641 description: "Read a PR's state and check counts. `include` adds detail.",
642 parameters: Schema.Struct({
643 ...ThreadPullRequestInput.fields,
644 include: Schema.optional(
645 Schema.Array(PullRequestReadSection).annotate({
646 description: "threads: unresolved review threads.",
647 }),
648 ),
649 includeResolved: Schema.optional(Schema.Boolean),
650 cursor: Schema.optional(TrimmedNonEmptyString),
651 }),
652 success: ReadPullRequestResult,
653 failure: PullRequestToolError,
654 dependencies: forgeDependencies,
655})
656 .annotate(Tool.Title, "Read pull request")
657 .annotate(Tool.Readonly, true)
658 .annotate(Tool.Destructive, false)
659 .annotate(Tool.Idempotent, true)
660 .annotate(Tool.OpenWorld, true)
661 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
662
663const ListPullRequestsTool = Tool.make("pull_request_list", {
664 description: "List PRs in this thread's repository, recently updated first.",
665 parameters: Schema.Struct({
666 environmentId: OrchestratorMcpEnvironmentTarget,
667 state: Schema.optional(PullRequestListState.annotate({ description: "Default: open." })),
668 involvement: Schema.optional(PullRequestInvolvement),
669 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
670 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
671 cursor: Schema.optional(TrimmedNonEmptyString),
672 }),
673 success: ListPullRequestsResult,
674 failure: PullRequestToolError,
675 dependencies: forgeDependencies,
676})
677 .annotate(Tool.Title, "List pull requests")
678 .annotate(Tool.Readonly, true)
679 .annotate(Tool.Destructive, false)
680 .annotate(Tool.Idempotent, true)
681 .annotate(Tool.OpenWorld, true)
682 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
683
684const UpdateNames = Schema.Array(TrimmedNonEmptyString).check(
685 Schema.isMinLength(1),
686 Schema.isMaxLength(25),
687);
688
689const CreatePullRequestTool = Tool.make("pull_request_create", {
690 description:
691 "Push this thread's branch, or `head` when you work in another worktree, then open its PR and link it, or return its open PR.",
692 parameters: Schema.Struct({
693 environmentId: OrchestratorMcpEnvironmentTarget,
694 head: Schema.optional(TrimmedNonEmptyString),
695 title: TrimmedNonEmptyString,
696 body: Schema.String,
697 base: Schema.optional(TrimmedNonEmptyString),
698 draft: Schema.optional(Schema.Boolean),
699 private: Schema.optional(Schema.Boolean).annotate({
700 description:
701 "Visible to writers only; the branch never reaches origin. Fails on a host without private PRs.",
702 }),
703 labels: Schema.optional(UpdateNames),
704 }),
705 success: CreatePullRequestResult,
706 failure: PullRequestToolError,
707 dependencies: [...forgeDependencies, GitManager.GitManager],
708})
709 .annotate(Tool.Title, "Create pull request")
710 .annotate(Tool.Readonly, false)
711 .annotate(Tool.Destructive, false)
712 .annotate(Tool.Idempotent, true)
713 .annotate(Tool.OpenWorld, true)
714 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
715
716const UpdatePullRequestTool = Tool.make("pull_request_update", {
717 description:
718 "Edit a PR's title, body, labels, or reviewers, close or reopen it, or set ready (false: back to draft).",
719 parameters: Schema.Struct({
720 ...ThreadPullRequestInput.fields,
721 title: Schema.optional(TrimmedNonEmptyString),
722 body: Schema.optional(Schema.String),
723 ready: Schema.optional(Schema.Boolean),
724 state: Schema.optional(Schema.Literals(["open", "closed"])),
725 addLabels: Schema.optional(UpdateNames),
726 removeLabels: Schema.optional(UpdateNames),
727 requestReviewers: Schema.optional(
728 UpdateNames.annotate({ description: "Logins, or org/team for a team." }),
729 ),
730 }),
731 success: Schema.Struct(PullRequestIdentity),
732 failure: PullRequestToolError,
733 dependencies: forgeDependencies,
734})
735 .annotate(Tool.Title, "Update pull request")
736 .annotate(Tool.Readonly, false)
737 .annotate(Tool.Destructive, false)
738 .annotate(Tool.Idempotent, true)
739 .annotate(Tool.OpenWorld, true)
740 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
741
742const RespondToPullRequestTool = Tool.make("pull_request_respond", {
743 description:
744 "Reply to, resolve, or reopen a review thread. Without reviewThreadId, comment on the PR.",
745 parameters: Schema.Struct({
746 ...ThreadPullRequestInput.fields,
747 reviewThreadId: Schema.optional(TrimmedNonEmptyString),
748 body: Schema.optional(TrimmedNonEmptyString),
749 resolved: Schema.optional(Schema.Boolean),
750 }),
751 success: RespondToPullRequestResult,
752 failure: PullRequestToolError,
753 dependencies: forgeDependencies,
754})
755 .annotate(Tool.Title, "Respond on pull request")
756 .annotate(Tool.Readonly, false)
757 .annotate(Tool.Destructive, false)
758 .annotate(Tool.Idempotent, false)
759 .annotate(Tool.OpenWorld, true)
760 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
761
762export const ReviewLineComment = Schema.Struct({
763 path: TrimmedNonEmptyString,
764 line: PositiveInt,
765 startLine: Schema.optional(PositiveInt),
766 side: Schema.optional(
767 Schema.Literals(["new", "old"]).annotate({
768 description: "Default new. old: line numbers in the base, for removed lines.",
769 }),
770 ),
771 body: TrimmedNonEmptyString,
772});
773export type ReviewLineComment = typeof ReviewLineComment.Type;
774
775const ReviewPullRequestTool = Tool.make("pull_request_review", {
776 description:
777 "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.",
778 parameters: Schema.Struct({
779 ...ThreadPullRequestInput.fields,
780 verdict: PullRequestReviewVerdict,
781 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
782 body: Schema.optional(Schema.String),
783 comments: Schema.optional(Schema.Array(ReviewLineComment).check(Schema.isMaxLength(50))),
784 }),
785 success: Schema.Struct(PullRequestIdentity),
786 failure: PullRequestToolError,
787 dependencies: forgeDependencies,
788})
789 .annotate(Tool.Title, "Review pull request")
790 .annotate(Tool.Readonly, false)
791 .annotate(Tool.Destructive, false)
792 .annotate(Tool.Idempotent, false)
793 .annotate(Tool.OpenWorld, true)
794 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
795
796const MergePullRequestTool = Tool.make("pull_request_merge", {
797 description:
798 "Merge a PR. Refuses if the head moved from expectedHeadSha (headSha in pull_request_read).",
799 parameters: Schema.Struct({
800 ...ThreadPullRequestInput.fields,
801 expectedHeadSha: TrimmedNonEmptyString,
802 method: Schema.optional(PullRequestMergeMethod),
803 auto: Schema.optional(
804 Schema.Boolean.annotate({
805 description: "Merge when required checks pass, including later pushes.",
806 }),
807 ),
808 stack: Schema.optional(
809 Schema.Boolean.annotate({
810 description: "Merge this PR and every open stack layer below it at once.",
811 }),
812 ),
813 }),
814 success: MergePullRequestResult,
815 failure: PullRequestToolError,
816 dependencies: forgeDependencies,
817})
818 .annotate(Tool.Title, "Merge pull request")
819 .annotate(Tool.Readonly, false)
820 .annotate(Tool.Destructive, true)
821 .annotate(Tool.Idempotent, false)
822 .annotate(Tool.OpenWorld, true)
823 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
824
825export const CheckLogsResult = Schema.Struct({
826 ...PullRequestIdentity,
827 checks: Schema.Array(
828 Schema.Struct({
829 name: Schema.String,
830 url: Schema.NullOr(Schema.String),
831 log: Schema.NullOr(Schema.String),
832 truncated: Schema.Boolean,
833 detail: Schema.NullOr(Schema.String),
834 }),
835 ),
836});
837export type CheckLogsResult = typeof CheckLogsResult.Type;
838
839export const RerunChecksResult = Schema.Struct({
840 ...PullRequestIdentity,
841 checks: Schema.Array(
842 Schema.Struct({
843 name: Schema.String,
844 rerun: Schema.Boolean,
845 detail: Schema.NullOr(Schema.String),
846 }),
847 ),
848});
849export type RerunChecksResult = typeof RerunChecksResult.Type;
850
851const CheckLogsTool = Tool.make("ci_logs", {
852 description: "Read the tail of a PR's failing check logs, or one named check's.",
853 parameters: Schema.Struct({
854 ...ThreadPullRequestInput.fields,
855 check: Schema.optional(TrimmedNonEmptyString),
856 tailLines: Schema.optional(
857 Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 2000 })).annotate({
858 description: "Default: 200.",
859 }),
860 ),
861 }),
862 success: CheckLogsResult,
863 failure: PullRequestToolError,
864 dependencies: forgeDependencies,
865})
866 .annotate(Tool.Title, "Read CI logs")
867 .annotate(Tool.Readonly, true)
868 .annotate(Tool.Destructive, false)
869 .annotate(Tool.Idempotent, true)
870 .annotate(Tool.OpenWorld, true)
871 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
872
873const RerunChecksTool = Tool.make("ci_rerun", {
874 description: "Re-run a PR's failing checks, or one named check.",
875 parameters: Schema.Struct({
876 ...ThreadPullRequestInput.fields,
877 check: Schema.optional(TrimmedNonEmptyString),
878 }),
879 success: RerunChecksResult,
880 failure: PullRequestToolError,
881 dependencies: forgeDependencies,
882})
883 .annotate(Tool.Title, "Re-run CI")
884 .annotate(Tool.Readonly, false)
885 .annotate(Tool.Destructive, false)
886 .annotate(Tool.Idempotent, false)
887 .annotate(Tool.OpenWorld, true)
888 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
889
890export const PullRequestsToolkit = Toolkit.make(
891 LinkPullRequestTool,
892 LinkPullRequestsTool,
893 UnlinkPullRequestTool,
894 UnlinkPullRequestsTool,
895 ListThreadPullRequestsTool,
896 WatchPullRequestTool,
897 UnwatchPullRequestTool,
898 WritePullRequestGuidesTool,
899 ReadPullRequestGuideTool,
900 ReadPullRequestTool,
901 ListPullRequestsTool,
902 CreatePullRequestTool,
903 UpdatePullRequestTool,
904 RespondToPullRequestTool,
905 ReviewPullRequestTool,
906 MergePullRequestTool,
907 CheckLogsTool,
908 RerunChecksTool,
909);