Skip to content
TypeScript

fix(server): MCP thread list and read report every linked pull request (#17454)

Scott Nortemand6841e9098a2
diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts
index 4925bad6c9a..004f577dc1b 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.ts
@@ -36,6 +36,7 @@ import {
type OrchestratorMcpListScheduledTasksInput,
type ProjectId,
type OrchestratorMcpThreadDetail,
+ type OrchestratorMcpThreadPullRequest,
type OrchestratorMcpThreadInterruptInput,
type OrchestratorMcpThreadInterruptResult,
type OrchestratorMcpThreadListInput,
@@ -60,6 +61,10 @@ import {
ThreadId,
} from "@t3tools/contracts";
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
+import {
+ normalizeThreadPullRequestKey,
+ visibleThreadPullRequests,
+} from "@t3tools/shared/threadPullRequests";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
@@ -709,6 +714,20 @@ function threadSnooze(
};
}
+/** The same links `list_thread_pull_requests` reports, without the snapshot details. */
+function threadPullRequests(
+ links: OrchestrationV2ThreadShell["pullRequests"],
+): OrchestratorMcpThreadPullRequest[] {
+ return visibleThreadPullRequests(links ?? []).map((link) => ({
+ host: normalizeThreadPullRequestKey(link).host,
+ repository: link.repository,
+ number: link.number,
+ url: link.url,
+ source: link.source,
+ state: link.snapshot?.state ?? null,
+ }));
+}
+
function listItemFromShell(
shell: OrchestrationV2ThreadShell,
nowMs: number,
@@ -725,6 +744,7 @@ function listItemFromShell(
runtimeMode: shell.runtimeMode,
interactionMode: shell.interactionMode,
linkedPullRequest: shell.linkedPullRequest ?? null,
+ pullRequests: threadPullRequests(shell.pullRequests),
...threadSettlement(shell),
...threadSnooze(shell, nowMs),
parentThreadId: shell.lineage.parentThreadId,
@@ -757,6 +777,7 @@ function threadDetail(
runtimeMode: projection.thread.runtimeMode,
interactionMode: projection.thread.interactionMode,
linkedPullRequest: projection.thread.linkedPullRequest ?? null,
+ pullRequests: threadPullRequests(projection.thread.pullRequests),
titleRegeneration:
projection.thread.titleRegeneration === undefined ||
projection.thread.titleRegeneration === null
diff --git a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
index 44d53fdbc01..7db16a6a954 100644
--- a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+++ b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
@@ -2388,6 +2388,58 @@ describe("orchestrator MCP toolkit", () => {
);
expect(unlinked.linkedPullRequest).toBeNull();
+ // Links added through thread.pull-request.link leave linkedPullRequest unchanged.
+ yield* orchestrator.dispatch({
+ type: "thread.pull-request.link",
+ commandId: CommandId.make("command:mcp-empty:link-other-repository"),
+ threadId: emptyThread.threadId,
+ host: "github.com",
+ repository: "octo/other-repo",
+ number: 42,
+ url: "https://github.com/octo/other-repo/pull/42",
+ source: "agent",
+ });
+ yield* orchestrator.dispatch({
+ type: "thread.pull-request.link",
+ commandId: CommandId.make("command:mcp-empty:link-dismissed"),
+ threadId: emptyThread.threadId,
+ host: "github.com",
+ repository: "octo/other-repo",
+ number: 43,
+ url: "https://github.com/octo/other-repo/pull/43",
+ source: "stack-dismissed",
+ });
+ const otherRepositoryLink = {
+ host: "github.com",
+ repository: "octo/other-repo",
+ number: 42,
+ url: "https://github.com/octo/other-repo/pull/42",
+ source: "agent",
+ state: null,
+ };
+ const otherRepositoryReadCall = yield* invoke("t3_thread_read", {
+ threadId: emptyThread.threadId,
+ });
+ const otherRepositoryRead = yield* decodeThreadReadResult(
+ otherRepositoryReadCall.structuredContent,
+ ).pipe(Effect.orDie);
+ expect(otherRepositoryRead.thread).toMatchObject({
+ linkedPullRequest: null,
+ pullRequests: [otherRepositoryLink],
+ });
+ const otherRepositoryListCall = yield* invoke("t3_thread_list", { limit: 100 });
+ const otherRepositoryList = yield* decodeThreadListResult(
+ otherRepositoryListCall.structuredContent,
+ ).pipe(Effect.orDie);
+ expect(
+ otherRepositoryList.threads.find(
+ (thread) => thread.threadId === emptyThread.threadId,
+ ),
+ ).toMatchObject({ linkedPullRequest: null, pullRequests: [otherRepositoryLink] });
+ expect(
+ otherRepositoryList.threads.find((thread) => thread.threadId === parentThreadId),
+ ).toMatchObject({ pullRequests: [] });
+
const regenerateCall = yield* invoke("t3_thread_update", {
threadId: emptyThread.threadId,
action: "regenerate_title",
diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.ts
index 05299626c67..f96c070fbac 100644
--- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts
+++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts
@@ -192,7 +192,7 @@ export const CreateThreadsTool = Tool.make("create_threads", {
const ThreadListTool = Tool.make("t3_thread_list", {
description:
- "List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early when it asks for something, fails, or completes. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
+ "List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early when it asks for something, fails, or completes. pullRequests lists every pull request linked to each thread; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
parameters: OrchestratorMcpThreadListInput,
success: OrchestratorMcpThreadListResult,
failure: OrchestratorMcpFailure,
@@ -206,7 +206,7 @@ const ThreadListTool = Tool.make("t3_thread_list", {
const ThreadReadTool = Tool.make("t3_thread_read", {
description:
- "Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units. The thread also reports its snooze state. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
+ "Read durable state and a paginated timeline from any T3 thread in this environment. The default messages view returns user messages, assistant messages, and proposed plans; activity returns all summarized timeline items. Reading an untruncated terminal assistant result from this parent thread's direct app-owned child acknowledges that child's automatic completion delivery. Continue with afterPosition=nextPosition. Recover long item text with itemId and textOffset=nextTextOffset until nextTextOffset is null; offsets count UTF-16 code units. The thread also reports its snooze state and, in pullRequests, every linked pull request; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
parameters: OrchestratorMcpThreadReadInput,
success: OrchestratorMcpThreadReadResult,
failure: OrchestratorMcpFailure,
diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts
index 5e178c21551..778712ad239 100644
--- a/packages/contracts/src/orchestratorMcp.ts
+++ b/packages/contracts/src/orchestratorMcp.ts
@@ -23,7 +23,12 @@ import {
ScheduledTaskUpsertSchedule,
} from "./scheduledTask.ts";
import { ProviderInteractionMode, RuntimeMode } from "./providerPolicy.ts";
-import { ThreadLinkedPullRequest } from "./threadPullRequest.ts";
+import { PullRequestState } from "./pullRequest.ts";
+import {
+ ThreadLinkedPullRequest,
+ ThreadPullRequestKey,
+ ThreadPullRequestLinkSource,
+} from "./threadPullRequest.ts";
import { ThreadTitleRegeneration } from "./threadTitle.ts";
import {
OrchestrationV2Actor,
@@ -304,6 +309,15 @@ export const OrchestratorMcpThreadListInput = Schema.Struct({
});
export type OrchestratorMcpThreadListInput = typeof OrchestratorMcpThreadListInput.Type;
+/** A pull request linked to a thread; `state` is null until the host has been read. */
+export const OrchestratorMcpThreadPullRequest = Schema.Struct({
+ ...ThreadPullRequestKey.fields,
+ url: TrimmedNonEmptyString,
+ source: ThreadPullRequestLinkSource,
+ state: Schema.NullOr(PullRequestState),
+});
+export type OrchestratorMcpThreadPullRequest = typeof OrchestratorMcpThreadPullRequest.Type;
+
export const OrchestratorMcpThreadListItem = Schema.Struct({
threadId: ThreadId,
title: Schema.String,
@@ -316,6 +330,8 @@ export const OrchestratorMcpThreadListItem = Schema.Struct({
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
+ /** Missing on older servers. */
+ pullRequests: Schema.optional(Schema.Array(OrchestratorMcpThreadPullRequest)),
settled: Schema.Boolean,
settledAt: Schema.NullOr(IsoDateTime),
snoozed: Schema.Boolean,
@@ -363,6 +379,8 @@ export const OrchestratorMcpThreadDetail = Schema.Struct({
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
+ /** Missing on older servers. */
+ pullRequests: Schema.optional(Schema.Array(OrchestratorMcpThreadPullRequest)),
titleRegeneration: Schema.NullOr(ThreadTitleRegeneration),
branch: Schema.NullOr(Schema.String),
worktreePath: Schema.NullOr(Schema.String),