Skip to content

Commit b260a7c

Browse files
authored
fix(mcp): point a bare thread uuid at its mcp: thread in thread_not_found (pingdotgg#22452)
1 parent 1d9060b commit b260a7c

5 files changed

Lines changed: 158 additions & 7 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,3 +2315,79 @@ describe("OrchestratorMcpService provider resolution", () => {
23152315
);
23162316
});
23172317
});
2318+
2319+
describe("OrchestratorMcpService thread lookup", () => {
2320+
const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
2321+
const launched = liveThreadShell(ThreadId.make(`mcp:${bareId}`));
2322+
const deletedLaunched = { ...launched, deletedAt: DateTime.makeUnsafe("2026-10-05T00:00:00Z") };
2323+
const client: McpInvocationScope = {
2324+
environmentId: EnvironmentId.make("environment:thread-lookup"),
2325+
requestNamespace: "client:thread-lookup",
2326+
thread: undefined,
2327+
client: { sessionId: "thread-lookup", label: "Claude Code", access: "full-access" },
2328+
capabilities: new Set(["orchestration"]),
2329+
issuedAt: 1,
2330+
};
2331+
const service = (shells: ReadonlyArray<OrchestrationV2ThreadShell>, sent: Ref.Ref<number>) =>
2332+
layerOrchestratorService.pipe(
2333+
Layer.provide(
2334+
Layer.mergeAll(
2335+
NodeServices.layer,
2336+
Layer.mock(ThreadManagementService.ThreadManagementService)({
2337+
getThreadShell: (threadId) =>
2338+
Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null),
2339+
sendToThread: () => Ref.update(sent, (count) => count + 1).pipe(Effect.as({} as never)),
2340+
}),
2341+
ServerSettings.layerTest(),
2342+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
2343+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
2344+
list: () => Effect.succeed([]),
2345+
}),
2346+
Layer.mock(ProjectService.ProjectService)({}),
2347+
Layer.mock(SecretRequests.SecretRequests)({}),
2348+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
2349+
),
2350+
),
2351+
);
2352+
const failuresFor = (shells: ReadonlyArray<OrchestrationV2ThreadShell>) =>
2353+
Effect.gen(function* () {
2354+
const sent = yield* Ref.make(0);
2355+
return yield* Effect.gen(function* () {
2356+
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
2357+
const read = yield* mcp.readThread(client, { threadId: bareId }).pipe(Effect.flip);
2358+
const send = yield* mcp
2359+
.sendToThread(client, { threadId: bareId, message: "hello" })
2360+
.pipe(Effect.flip);
2361+
return { read, send, sends: yield* Ref.get(sent) };
2362+
}).pipe(Effect.provide(service(shells, sent)));
2363+
});
2364+
2365+
it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
2366+
Effect.gen(function* () {
2367+
const { read, send, sends } = yield* failuresFor([launched]);
2368+
for (const error of [read, send]) {
2369+
assert.equal(error.code, "thread_not_found");
2370+
assert.include(error.message, `mcp:${bareId}`);
2371+
}
2372+
assert.equal(sends, 0);
2373+
}),
2374+
);
2375+
2376+
it.effect("keeps the plain message for an id that matches nothing", () =>
2377+
Effect.gen(function* () {
2378+
const { read, send } = yield* failuresFor([]);
2379+
assert.equal(read.code, "thread_not_found");
2380+
assert.equal(read.message, `Thread ${bareId} is no longer available.`);
2381+
assert.equal(send.code, "thread_not_found");
2382+
assert.equal(send.message, `Thread ${bareId} was not found.`);
2383+
}),
2384+
);
2385+
2386+
it.effect("does not name an mcp: thread that was deleted", () =>
2387+
Effect.gen(function* () {
2388+
const { read, send } = yield* failuresFor([deletedLaunched]);
2389+
assert.equal(read.message, `Thread ${bareId} is no longer available.`);
2390+
assert.equal(send.message, `Thread ${bareId} was not found.`);
2391+
}),
2392+
);
2393+
});

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,26 @@ export const isLiveCallerShell = (
232232
) ??
233233
false));
234234

235+
/**
236+
* Threads an agent starts over MCP are `mcp:<uuid>`, and agents often paste the
237+
* bare uuid. The id is never rewritten, so permission checks keyed on it still
238+
* hold; the miss only names a live thread the caller could read by its full id.
239+
*/
240+
export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")(function* (
241+
threads: Pick<ThreadManagementService.ThreadManagementService["Service"], "getThreadShell">,
242+
threadId: ThreadId,
243+
message: string,
244+
) {
245+
const prefixed = ThreadId.make(`mcp:${threadId}`);
246+
const shell = yield* threads.getThreadShell(prefixed).pipe(Effect.orElseSucceed(() => null));
247+
return yield* failure(
248+
"thread_not_found",
249+
shell === null || shell.deletedAt !== null
250+
? message
251+
: `${message} Did you mean \`${prefixed}\`?`,
252+
);
253+
});
254+
235255
function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
236256
if (!isThreadManagementError(error)) return failure("orchestration_error", errorMessage(error));
237257
switch (error._tag) {
@@ -1124,7 +1144,7 @@ const make = Effect.gen(function* () {
11241144
.getThreadShell(threadId)
11251145
.pipe(Effect.mapError(threadManagementFailure));
11261146
if (shell === null || shell.deletedAt !== null) {
1127-
return yield* failure("thread_not_found", `Thread ${threadId} was not found.`);
1147+
return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
11281148
}
11291149
return yield* loadProjectThread(shell.projectId, threadId);
11301150
});
@@ -1199,7 +1219,11 @@ const make = Effect.gen(function* () {
11991219
.getThreadShell(threadId)
12001220
.pipe(Effect.mapError(threadManagementFailure));
12011221
if (shell === null || shell.deletedAt !== null) {
1202-
return yield* failure("thread_not_found", `Thread ${threadId} is no longer available.`);
1222+
return yield* threadNotFound(
1223+
threadManagement,
1224+
threadId,
1225+
`Thread ${threadId} is no longer available.`,
1226+
);
12031227
}
12041228
const target = yield* threadManagement
12051229
.getProjectThreadRecords({ projectId: shell.projectId, threadId }, [

‎apps/server/src/mcp/ThreadMetadataMcpService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as Layer from "effect/Layer";
1616

1717
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
1818
import type { McpInvocationScope } from "./McpInvocationContext.ts";
19+
import { threadNotFound } from "./OrchestratorMcpService.ts";
1920

2021
export class ThreadMetadataMcpService extends Context.Service<
2122
ThreadMetadataMcpService,
@@ -167,7 +168,7 @@ const make = Effect.gen(function* () {
167168
),
168169
);
169170
if (shell === null || shell.deletedAt !== null) {
170-
return yield* failure("thread_not_found", `Thread ${threadId} was not found.`);
171+
return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
171172
}
172173
const target = yield* threadManagement
173174
.getProjectThreadRecords({ projectId: shell.projectId, threadId }, [])
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expect, it } from "@effect/vitest";
2+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import * as Effect from "effect/Effect";
4+
import * as Layer from "effect/Layer";
5+
6+
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
7+
import * as McpInvocationContext from "./McpInvocationContext.ts";
8+
import { liveThreadShell } from "./McpToolAccess.testkit.ts";
9+
import { readThread } from "./threadAccess.ts";
10+
11+
const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
12+
const client: McpInvocationContext.McpInvocationScope = {
13+
environmentId: EnvironmentId.make("environment:thread-access"),
14+
requestNamespace: "client:thread-access",
15+
thread: undefined,
16+
client: { sessionId: "thread-access", label: "Claude Code", access: "full-access" },
17+
capabilities: new Set(["orchestration"]),
18+
issuedAt: 1,
19+
};
20+
21+
const readBare = (existing: ReadonlyArray<ThreadId>) =>
22+
readThread(bareId).pipe(
23+
Effect.flip,
24+
Effect.provideService(McpInvocationContext.McpInvocationContext, client),
25+
Effect.provide(
26+
Layer.mock(ThreadManagement.ThreadManagementService)({
27+
getThreadShell: (threadId) =>
28+
Effect.succeed(existing.includes(threadId) ? liveThreadShell(threadId) : null),
29+
}),
30+
),
31+
);
32+
33+
it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
34+
Effect.gen(function* () {
35+
const error = yield* readBare([ThreadId.make(`mcp:${bareId}`)]);
36+
37+
expect(error.code).toBe("thread_not_found");
38+
expect(error.message).toBe(`The thread was not found. Did you mean \`mcp:${bareId}\`?`);
39+
}),
40+
);
41+
42+
it.effect("keeps the plain message for an id that matches nothing", () =>
43+
Effect.gen(function* () {
44+
const error = yield* readBare([]);
45+
46+
expect(error.code).toBe("thread_not_found");
47+
expect(error.message).toBe("The thread was not found.");
48+
}),
49+
);

‎apps/server/src/mcp/threadAccess.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ export const readThread = Effect.fn("mcp.readThread")(function* <
156156
const targetId = yield* resolveThreadId(context, threadId);
157157
const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable));
158158
if (shell === null || shell.deletedAt !== null) {
159-
return yield* new OrchestratorMcpFailure({
160-
code: "thread_not_found",
161-
message: "The thread was not found.",
162-
});
159+
return yield* OrchestrationMcp.threadNotFound(
160+
context.threads,
161+
targetId,
162+
"The thread was not found.",
163+
);
163164
}
164165
const projection = yield* context.threads
165166
.getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, {

0 commit comments

Comments
 (0)