Skip to content

Commit 01bd0a2

Browse files
authored
feat(mcp): link several PRs to a thread with one call (#27994)
Co-authored-by: maria-rcks <[email protected]>
1 parent 78aa0d5 commit 01bd0a2

22 files changed

Lines changed: 239 additions & 71 deletions

File tree

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,28 @@ describe("pull request toolkit handlers", () => {
517517
}),
518518
);
519519

520+
it.effect("links several pull requests in one call", () =>
521+
Effect.gen(function* () {
522+
const harness = yield* makeHarness({
523+
thread: makeThread([makeLink(1), { ...makeLink(2), source: "stack-dismissed" }]),
524+
});
525+
const url = (number: number) => `https://github.com/t3tools/t3code/pull/${number}`;
526+
const result = yield* harness.call("link_pull_requests", {
527+
urls: [url(1), url(2), url(3), url(3), "https://example.com/not-a-pr"],
528+
});
529+
expect(result).toEqual({
530+
linked: [{ host: "github.com", repository: "t3tools/t3code", numbers: [2, 3] }],
531+
alreadyLinked: [{ host: "github.com", repository: "t3tools/t3code", numbers: [1] }],
532+
linkedCount: 3,
533+
invalidUrls: ["https://example.com/not-a-pr"],
534+
});
535+
expect(yield* Ref.get(harness.commands)).toMatchObject([
536+
{ type: "thread.pull-request.link", number: 2, source: "agent" },
537+
{ type: "thread.pull-request.link", number: 3, source: "agent" },
538+
]);
539+
}),
540+
);
541+
520542
it.effect("unlinks every visible pull request in the given states in one call", () =>
521543
Effect.gen(function* () {
522544
const withState = (number: number, state: "open" | "closed" | "merged") => {

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,55 @@ const make = Effect.gen(function* () {
392392
return { ...target, alreadyLinked, ...next };
393393
}),
394394
),
395+
link_pull_requests: writesThread((input) =>
396+
Effect.gen(function* () {
397+
const thread = yield* requireThread(PullRequestLinkFailedError, input.threadId);
398+
const targets: Array<ResolvedTarget> = [];
399+
const invalidUrls: Array<string> = [];
400+
for (const url of input.urls) {
401+
const target = yield* resolveTarget({ url }, undefined).pipe(Effect.option);
402+
if (Option.isNone(target)) invalidUrls.push(url);
403+
else if (!targets.some((seen) => threadPullRequestKeysEqual(seen, target.value)))
404+
targets.push(target.value);
405+
}
406+
const links = threadPullRequestsOf(thread);
407+
const isLinked = (target: ResolvedTarget) =>
408+
links.some(
409+
(link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target),
410+
);
411+
const fresh = targets.filter((target) => !isLinked(target));
412+
const alreadyLinked = targets.filter(isLinked);
413+
yield* Effect.forEach(fresh, (target) =>
414+
Effect.gen(function* () {
415+
yield* engine.dispatch({
416+
type: "thread.pull-request.link",
417+
commandId: yield* commandId("mcp-pr-link", thread.id),
418+
threadId: thread.id,
419+
host: target.host,
420+
repository: target.repository,
421+
number: target.number,
422+
url: target.url,
423+
source: "agent",
424+
});
425+
}).pipe(Effect.catchCause(dispatchFailure(PullRequestLinkFailedError))),
426+
);
427+
const byRepository = (prs: ReadonlyArray<ResolvedTarget>) =>
428+
[...Map.groupBy(prs, (pr) => `${pr.host}/${pr.repository}`).values()].map((group) => ({
429+
host: group[0]!.host,
430+
repository: group[0]!.repository,
431+
numbers: group.map((pr) => pr.number),
432+
}));
433+
return {
434+
linked: byRepository(fresh),
435+
alreadyLinked: byRepository(alreadyLinked),
436+
linkedCount: targets.length,
437+
...(invalidUrls.length > 0 ? { invalidUrls } : {}),
438+
...(thread.lineage.relationshipToParent === "subagent"
439+
? { next: CHILD_PULL_REQUEST_REPORT_NEXT }
440+
: {}),
441+
};
442+
}),
443+
),
395444
unlink_pull_request: writesThread((input) =>
396445
Effect.gen(function* () {
397446
const thread = yield* requireThread(PullRequestUnlinkFailedError, input.threadId);

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ export const LinkPullRequestResult = Schema.Struct({
223223
});
224224
export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
225225

226+
const PullRequestNumbersByRepository = Schema.Array(
227+
Schema.Struct({
228+
host: Schema.String,
229+
repository: Schema.String,
230+
numbers: Schema.Array(Schema.Int),
231+
}),
232+
);
233+
234+
export const LinkPullRequestsResult = Schema.Struct({
235+
linked: PullRequestNumbersByRepository,
236+
alreadyLinked: PullRequestNumbersByRepository,
237+
linkedCount: Schema.Int,
238+
invalidUrls: Schema.optional(Schema.Array(Schema.String)),
239+
next: Schema.optional(Schema.String),
240+
});
241+
export type LinkPullRequestsResult = typeof LinkPullRequestsResult.Type;
242+
226243
export const UnlinkPullRequestResult = Schema.Struct({
227244
host: Schema.String,
228245
repository: Schema.String,
@@ -285,7 +302,8 @@ export const ListThreadPullRequestsResult = Schema.Struct({
285302
export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
286303

287304
const LinkPullRequestTool = Tool.make("link_pull_request", {
288-
description: "Link a PR to this thread. T3 shows its status and settles the thread on merge.",
305+
description:
306+
"Link a PR to this thread. T3 shows its status and settles the thread on merge. For several PRs, use `link_pull_requests`.",
289307
parameters: PullRequestTargetInput,
290308
success: LinkPullRequestResult,
291309
failure: PullRequestToolError,
@@ -297,6 +315,24 @@ const LinkPullRequestTool = Tool.make("link_pull_request", {
297315
.annotate(Tool.Idempotent, true)
298316
.annotate(Tool.OpenWorld, false);
299317

318+
const LinkPullRequestsTool = Tool.make("link_pull_requests", {
319+
description:
320+
"Link several PRs to a thread in one call. Use it instead of one `link_pull_request` per PR. URLs that are not PRs come back in `invalidUrls`.",
321+
parameters: Schema.Struct({
322+
environmentId: OrchestratorMcpEnvironmentTarget,
323+
threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
324+
urls: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1)),
325+
}),
326+
success: LinkPullRequestsResult,
327+
failure: PullRequestToolError,
328+
dependencies,
329+
})
330+
.annotate(Tool.Title, "Link pull requests to thread")
331+
.annotate(Tool.Readonly, false)
332+
.annotate(Tool.Destructive, false)
333+
.annotate(Tool.Idempotent, true)
334+
.annotate(Tool.OpenWorld, false);
335+
300336
const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
301337
description:
302338
"Remove a PR link from this thread. Succeeds with `wasLinked=false` when it was not linked.",
@@ -442,6 +478,7 @@ const ReadPullRequestGuideTool = Tool.make("read_pull_request_guide", {
442478

443479
export const PullRequestsToolkit = Toolkit.make(
444480
LinkPullRequestTool,
481+
LinkPullRequestsTool,
445482
UnlinkPullRequestTool,
446483
UnlinkPullRequestsTool,
447484
ListThreadPullRequestsTool,

0 commit comments

Comments
 (0)