From bd72a1e00379f828318eb94b47676b5a0728cd9e Mon Sep 17 00:00:00 2001 From: Rodrigo Brechard Date: Thu, 16 Jul 2026 14:17:59 +0200 Subject: [PATCH] Accept complete thread reference inputs Co-authored-by: codex --- .../toolkits/threadReference/handlers.test.ts | 46 ++++++++++++---- .../mcp/toolkits/threadReference/handlers.ts | 53 +++++++++++++------ .../src/mcp/toolkits/threadReference/tools.ts | 2 +- .../provider/CodexDeveloperInstructions.ts | 2 +- packages/contracts/src/threadReference.ts | 3 +- 5 files changed, 76 insertions(+), 30 deletions(-) diff --git a/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts b/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts index c70dfab2d1..f5d056b1bd 100644 --- a/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/threadReference/handlers.test.ts @@ -19,6 +19,7 @@ import { ProjectionSnapshotQuery } from "../../../orchestration/Services/Project import { buildThreadReferencePage, hasUserThreadReference, + normalizeThreadReferenceThreadId, threadRead, ThreadReferenceToolkitHandlersLive, } from "./handlers.ts"; @@ -208,19 +209,44 @@ it("only authorizes user-supplied references from the invoking environment", () ).toBe(false); }); +it("normalizes model-facing thread reference inputs", () => { + expect(normalizeThreadReferenceThreadId(thread.id, environmentId)).toBe(thread.id); + expect( + normalizeThreadReferenceThreadId(ThreadId.make(`${environmentId}/${thread.id}`), environmentId), + ).toBe(thread.id); + expect( + normalizeThreadReferenceThreadId( + ThreadId.make(`t3-thread:///${environmentId}/${thread.id}`), + environmentId, + ), + ).toBe(thread.id); + expect( + normalizeThreadReferenceThreadId( + ThreadId.make(`t3-thread:///environment-2/${thread.id}`), + environmentId, + ), + ).toBe(`t3-thread:///environment-2/${thread.id}`); +}); + it.effect("reads a referenced thread through the MCP toolkit", () => Effect.gen(function* () { const server = yield* McpServer.McpServer; - const result = yield* server.callTool({ - name: "thread_read", - arguments: { threadId: thread.id }, - }); - expect(result.isError).toBe(false); - expect(result.structuredContent).toMatchObject({ - threadId: thread.id, - title: thread.title, - totalMessages: 2, - }); + for (const threadId of [ + thread.id, + `${environmentId}/${thread.id}`, + `t3-thread:///${environmentId}/${thread.id}`, + ]) { + const result = yield* server.callTool({ + name: "thread_read", + arguments: { threadId }, + }); + expect(result.isError).toBe(false); + expect(result.structuredContent).toMatchObject({ + threadId: thread.id, + title: thread.title, + totalMessages: 2, + }); + } }).pipe( Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), Effect.provideService(McpSchema.McpServerClient, mcpServerClient), diff --git a/apps/server/src/mcp/toolkits/threadReference/handlers.ts b/apps/server/src/mcp/toolkits/threadReference/handlers.ts index 6a6f92d0f4..1226620de4 100644 --- a/apps/server/src/mcp/toolkits/threadReference/handlers.ts +++ b/apps/server/src/mcp/toolkits/threadReference/handlers.ts @@ -8,9 +8,10 @@ import { ThreadReferenceUnavailableError, type EnvironmentId, type OrchestrationThread, - type ThreadId, + ThreadId, } from "@t3tools/contracts"; import { collectComposerThreadReferences } from "@t3tools/shared/composerInlineTokens"; +import { parseComposerThreadLink } from "@t3tools/shared/composerTrigger"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; @@ -116,38 +117,56 @@ export function hasUserThreadReference( ); } +export function normalizeThreadReferenceThreadId( + input: ThreadId, + environmentId: EnvironmentId, +): ThreadId { + const parsedLink = parseComposerThreadLink(input); + if (parsedLink?.environmentId === environmentId) { + return ThreadId.make(parsedLink.threadId); + } + + const environmentPrefix = `${environmentId}/`; + if (input.startsWith(environmentPrefix)) { + const threadId = input.slice(environmentPrefix.length); + if (threadId && !threadId.includes("/")) { + try { + return ThreadId.make(decodeURIComponent(threadId)); + } catch { + return input; + } + } + } + + return input; +} + export const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* ( input: ThreadReferenceReadInput, ) { const invocation = yield* McpInvocationContext.McpInvocationContext; + const threadId = normalizeThreadReferenceThreadId(input.threadId, invocation.environmentId); + const normalizedInput = { ...input, threadId }; if (!invocation.capabilities.has("thread-reference")) { - return yield* new ThreadReferenceUnavailableError({ threadId: input.threadId }); + return yield* new ThreadReferenceUnavailableError({ threadId }); } const snapshotQuery = yield* ProjectionSnapshotQuery; const sourceThreadOption = yield* snapshotQuery .getThreadDetailById(invocation.threadId) - .pipe( - Effect.mapError( - (cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }), - ), - ); + .pipe(Effect.mapError((cause) => new ThreadReferenceUnavailableError({ threadId, cause }))); if ( Option.isNone(sourceThreadOption) || - !hasUserThreadReference(sourceThreadOption.value, invocation.environmentId, input.threadId) + !hasUserThreadReference(sourceThreadOption.value, invocation.environmentId, threadId) ) { - return yield* new ThreadReferenceUnavailableError({ threadId: input.threadId }); + return yield* new ThreadReferenceUnavailableError({ threadId }); } const threadOption = yield* snapshotQuery - .getThreadDetailById(input.threadId) - .pipe( - Effect.mapError( - (cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }), - ), - ); + .getThreadDetailById(threadId) + .pipe(Effect.mapError((cause) => new ThreadReferenceUnavailableError({ threadId, cause }))); if (Option.isNone(threadOption)) { - return yield* new ThreadReferenceNotFoundError({ threadId: input.threadId }); + return yield* new ThreadReferenceNotFoundError({ threadId }); } - const page = buildThreadReferencePage(threadOption.value, input); + const page = buildThreadReferencePage(threadOption.value, normalizedInput); if ("_tag" in page) { return yield* page; } diff --git a/apps/server/src/mcp/toolkits/threadReference/tools.ts b/apps/server/src/mcp/toolkits/threadReference/tools.ts index 75d60338f3..a3dc91e343 100644 --- a/apps/server/src/mcp/toolkits/threadReference/tools.ts +++ b/apps/server/src/mcp/toolkits/threadReference/tools.ts @@ -10,7 +10,7 @@ import { ProjectionSnapshotQuery } from "../../../orchestration/Services/Project export const ThreadReadTool = Tool.make("thread_read", { description: - "Read a T3 Code chat thread referenced by a t3-thread link in the user's message. Pass the threadId from that link. The transcript is paginated; when nextCursor is non-null, call thread_read again with that cursor to continue. Do not call this for unrelated threads that the user did not reference.", + "Read a T3 Code chat thread referenced by a t3-thread link in the user's message. For t3-thread:///ENVIRONMENT_ID/THREAD_ID, pass the final THREAD_ID as threadId. The full t3-thread link and ENVIRONMENT_ID/THREAD_ID are also accepted. The transcript is paginated; when nextCursor is non-null, call thread_read again with that cursor to continue. Do not call this for unrelated threads that the user did not reference.", parameters: ThreadReferenceReadInput, success: ThreadReferenceReadResult, failure: ThreadReferenceReadError, diff --git a/apps/server/src/provider/CodexDeveloperInstructions.ts b/apps/server/src/provider/CodexDeveloperInstructions.ts index 1c54f4f541..abdc5be21f 100644 --- a/apps/server/src/provider/CodexDeveloperInstructions.ts +++ b/apps/server/src/provider/CodexDeveloperInstructions.ts @@ -15,7 +15,7 @@ const T3_CODE_THREAD_REFERENCE_INSTRUCTIONS = ` ## T3 Code thread references -The user may reference another chat with a \`t3-thread\` link. Do not treat the link as a web URL and do not assume its contents. Use the read-only \`thread_read\` tool from the \`t3-code\` MCP server with the referenced thread id. Follow \`nextCursor\` until you have read as much of the transcript as the task requires. +The user may reference another chat with a \`t3-thread\` link. Do not treat the link as a web URL and do not assume its contents. Use the read-only \`thread_read\` tool from the \`t3-code\` MCP server. For \`t3-thread:///ENVIRONMENT_ID/THREAD_ID\`, pass the final \`THREAD_ID\` path segment as \`threadId\`; the tool also accepts the full link. Follow \`nextCursor\` until you have read as much of the transcript as the task requires. `; export const CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS = `# Plan Mode (Conversational) diff --git a/packages/contracts/src/threadReference.ts b/packages/contracts/src/threadReference.ts index 2207c2395c..2e6efd66fb 100644 --- a/packages/contracts/src/threadReference.ts +++ b/packages/contracts/src/threadReference.ts @@ -22,7 +22,8 @@ export type ThreadReferenceCursor = typeof ThreadReferenceCursor.Type; export const ThreadReferenceReadInput = Schema.Struct({ threadId: ThreadId.annotate({ - description: "Thread id from a t3-thread reference in the user's message.", + description: + "Referenced thread id. Prefer the final THREAD_ID path segment; full t3-thread:///ENVIRONMENT_ID/THREAD_ID links and ENVIRONMENT_ID/THREAD_ID are also accepted.", }), cursor: Schema.optional(ThreadReferenceCursor).annotate({ description: "nextCursor from the previous page. Omit for the first page.",