Skip to content
TypeScript

fix(mcp): point a bare thread uuid at its mcp: thread in thread_not_found (#22452)

fixfonb260a7cf69d0
diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts
index 015cf1f5769..5dbbd24b54c 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.test.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts
@@ -2315,3 +2315,79 @@ describe("OrchestratorMcpService provider resolution", () => {
);
});
});
+
+describe("OrchestratorMcpService thread lookup", () => {
+ const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
+ const launched = liveThreadShell(ThreadId.make(`mcp:${bareId}`));
+ const deletedLaunched = { ...launched, deletedAt: DateTime.makeUnsafe("2026-10-05T00:00:00Z") };
+ const client: McpInvocationScope = {
+ environmentId: EnvironmentId.make("environment:thread-lookup"),
+ requestNamespace: "client:thread-lookup",
+ thread: undefined,
+ client: { sessionId: "thread-lookup", label: "Claude Code", access: "full-access" },
+ capabilities: new Set(["orchestration"]),
+ issuedAt: 1,
+ };
+ const service = (shells: ReadonlyArray<OrchestrationV2ThreadShell>, sent: Ref.Ref<number>) =>
+ layerOrchestratorService.pipe(
+ Layer.provide(
+ Layer.mergeAll(
+ NodeServices.layer,
+ Layer.mock(ThreadManagementService.ThreadManagementService)({
+ getThreadShell: (threadId) =>
+ Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null),
+ sendToThread: () => Ref.update(sent, (count) => count + 1).pipe(Effect.as({} as never)),
+ }),
+ ServerSettings.layerTest(),
+ Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
+ Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
+ list: () => Effect.succeed([]),
+ }),
+ Layer.mock(ProjectService.ProjectService)({}),
+ Layer.mock(SecretRequests.SecretRequests)({}),
+ Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
+ ),
+ ),
+ );
+ const failuresFor = (shells: ReadonlyArray<OrchestrationV2ThreadShell>) =>
+ Effect.gen(function* () {
+ const sent = yield* Ref.make(0);
+ return yield* Effect.gen(function* () {
+ const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
+ const read = yield* mcp.readThread(client, { threadId: bareId }).pipe(Effect.flip);
+ const send = yield* mcp
+ .sendToThread(client, { threadId: bareId, message: "hello" })
+ .pipe(Effect.flip);
+ return { read, send, sends: yield* Ref.get(sent) };
+ }).pipe(Effect.provide(service(shells, sent)));
+ });
+
+ it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
+ Effect.gen(function* () {
+ const { read, send, sends } = yield* failuresFor([launched]);
+ for (const error of [read, send]) {
+ assert.equal(error.code, "thread_not_found");
+ assert.include(error.message, `mcp:${bareId}`);
+ }
+ assert.equal(sends, 0);
+ }),
+ );
+
+ it.effect("keeps the plain message for an id that matches nothing", () =>
+ Effect.gen(function* () {
+ const { read, send } = yield* failuresFor([]);
+ assert.equal(read.code, "thread_not_found");
+ assert.equal(read.message, `Thread ${bareId} is no longer available.`);
+ assert.equal(send.code, "thread_not_found");
+ assert.equal(send.message, `Thread ${bareId} was not found.`);
+ }),
+ );
+
+ it.effect("does not name an mcp: thread that was deleted", () =>
+ Effect.gen(function* () {
+ const { read, send } = yield* failuresFor([deletedLaunched]);
+ assert.equal(read.message, `Thread ${bareId} is no longer available.`);
+ assert.equal(send.message, `Thread ${bareId} was not found.`);
+ }),
+ );
+});
diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts
index f1ac8c57038..eca639ed713 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.ts
@@ -232,6 +232,26 @@ export const isLiveCallerShell = (
) ??
false));
+/**
+ * Threads an agent starts over MCP are `mcp:<uuid>`, and agents often paste the
+ * bare uuid. The id is never rewritten, so permission checks keyed on it still
+ * hold; the miss only names a live thread the caller could read by its full id.
+ */
+export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")(function* (
+ threads: Pick<ThreadManagementService.ThreadManagementService["Service"], "getThreadShell">,
+ threadId: ThreadId,
+ message: string,
+) {
+ const prefixed = ThreadId.make(`mcp:${threadId}`);
+ const shell = yield* threads.getThreadShell(prefixed).pipe(Effect.orElseSucceed(() => null));
+ return yield* failure(
+ "thread_not_found",
+ shell === null || shell.deletedAt !== null
+ ? message
+ : `${message} Did you mean \`${prefixed}\`?`,
+ );
+});
+
function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
if (!isThreadManagementError(error)) return failure("orchestration_error", errorMessage(error));
switch (error._tag) {
@@ -1124,7 +1144,7 @@ const make = Effect.gen(function* () {
.getThreadShell(threadId)
.pipe(Effect.mapError(threadManagementFailure));
if (shell === null || shell.deletedAt !== null) {
- return yield* failure("thread_not_found", `Thread ${threadId} was not found.`);
+ return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
}
return yield* loadProjectThread(shell.projectId, threadId);
});
@@ -1199,7 +1219,11 @@ const make = Effect.gen(function* () {
.getThreadShell(threadId)
.pipe(Effect.mapError(threadManagementFailure));
if (shell === null || shell.deletedAt !== null) {
- return yield* failure("thread_not_found", `Thread ${threadId} is no longer available.`);
+ return yield* threadNotFound(
+ threadManagement,
+ threadId,
+ `Thread ${threadId} is no longer available.`,
+ );
}
const target = yield* threadManagement
.getProjectThreadRecords({ projectId: shell.projectId, threadId }, [
diff --git a/apps/server/src/mcp/ThreadMetadataMcpService.ts b/apps/server/src/mcp/ThreadMetadataMcpService.ts
index 74176ec90c7..e05200fd8ed 100644
--- a/apps/server/src/mcp/ThreadMetadataMcpService.ts
+++ b/apps/server/src/mcp/ThreadMetadataMcpService.ts
@@ -16,6 +16,7 @@ import * as Layer from "effect/Layer";
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
import type { McpInvocationScope } from "./McpInvocationContext.ts";
+import { threadNotFound } from "./OrchestratorMcpService.ts";
export class ThreadMetadataMcpService extends Context.Service<
ThreadMetadataMcpService,
@@ -167,7 +168,7 @@ const make = Effect.gen(function* () {
),
);
if (shell === null || shell.deletedAt !== null) {
- return yield* failure("thread_not_found", `Thread ${threadId} was not found.`);
+ return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
}
const target = yield* threadManagement
.getProjectThreadRecords({ projectId: shell.projectId, threadId }, [])
diff --git a/apps/server/src/mcp/threadAccess.test.ts b/apps/server/src/mcp/threadAccess.test.ts
new file mode 100644
index 00000000000..a40f63baa6c
--- /dev/null
+++ b/apps/server/src/mcp/threadAccess.test.ts
@@ -0,0 +1,49 @@
+import { expect, it } from "@effect/vitest";
+import { EnvironmentId, ThreadId } from "@t3tools/contracts";
+import * as Effect from "effect/Effect";
+import * as Layer from "effect/Layer";
+
+import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
+import * as McpInvocationContext from "./McpInvocationContext.ts";
+import { liveThreadShell } from "./McpToolAccess.testkit.ts";
+import { readThread } from "./threadAccess.ts";
+
+const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
+const client: McpInvocationContext.McpInvocationScope = {
+ environmentId: EnvironmentId.make("environment:thread-access"),
+ requestNamespace: "client:thread-access",
+ thread: undefined,
+ client: { sessionId: "thread-access", label: "Claude Code", access: "full-access" },
+ capabilities: new Set(["orchestration"]),
+ issuedAt: 1,
+};
+
+const readBare = (existing: ReadonlyArray<ThreadId>) =>
+ readThread(bareId).pipe(
+ Effect.flip,
+ Effect.provideService(McpInvocationContext.McpInvocationContext, client),
+ Effect.provide(
+ Layer.mock(ThreadManagement.ThreadManagementService)({
+ getThreadShell: (threadId) =>
+ Effect.succeed(existing.includes(threadId) ? liveThreadShell(threadId) : null),
+ }),
+ ),
+ );
+
+it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
+ Effect.gen(function* () {
+ const error = yield* readBare([ThreadId.make(`mcp:${bareId}`)]);
+
+ expect(error.code).toBe("thread_not_found");
+ expect(error.message).toBe(`The thread was not found. Did you mean \`mcp:${bareId}\`?`);
+ }),
+);
+
+it.effect("keeps the plain message for an id that matches nothing", () =>
+ Effect.gen(function* () {
+ const error = yield* readBare([]);
+
+ expect(error.code).toBe("thread_not_found");
+ expect(error.message).toBe("The thread was not found.");
+ }),
+);
diff --git a/apps/server/src/mcp/threadAccess.ts b/apps/server/src/mcp/threadAccess.ts
index be70ecf1807..ba8dc4c3e57 100644
--- a/apps/server/src/mcp/threadAccess.ts
+++ b/apps/server/src/mcp/threadAccess.ts
@@ -156,10 +156,11 @@ export const readThread = Effect.fn("mcp.readThread")(function* <
const targetId = yield* resolveThreadId(context, threadId);
const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable));
if (shell === null || shell.deletedAt !== null) {
- return yield* new OrchestratorMcpFailure({
- code: "thread_not_found",
- message: "The thread was not found.",
- });
+ return yield* OrchestrationMcp.threadNotFound(
+ context.threads,
+ targetId,
+ "The thread was not found.",
+ );
}
const projection = yield* context.threads
.getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, {