Skip to content

Commit c217c91

Browse files
fix(threads): bound surface errors and verify MCP server identity
- ThreadsSurfaceError is now a Schema.TaggedError with an operation tag and optional retained cause, matching the preview toolkit's failure shape; arbitrary upstream error messages no longer reach the model as detail. - matchThreadsSurfaceToolName accepts the item's server field and rejects explicit foreign servers, so another MCP server exposing a bare threads_list/threads_create tool cannot produce thread cards. - threadsSurface.ts uses effect/* namespace imports like the rest of contracts. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 2fcb052 commit c217c91

5 files changed

Lines changed: 73 additions & 22 deletions

File tree

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type ThreadsCreateResult,
77
type ThreadsListInput,
88
type ThreadsListResult,
9-
type ThreadsSurfaceError,
9+
ThreadsSurfaceError,
1010
type ThreadsListItem,
1111
THREADS_SURFACE_LIST_DEFAULT_LIMIT,
1212
THREADS_SURFACE_LIST_MAX_LIMIT,
@@ -20,10 +20,11 @@ import * as OrchestrationEngine from "../../../orchestration/Services/Orchestrat
2020
import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";
2121
import { ThreadsToolkit } from "./tools.ts";
2222

23-
const fail = (detail: string) =>
24-
Effect.fail<ThreadsSurfaceError>({ _tag: "ThreadsSurfaceError", detail });
23+
const fail = (operation: ThreadsSurfaceError["operation"], detail: string) =>
24+
Effect.fail(new ThreadsSurfaceError({ operation, detail }));
2525

26-
const failFrom = (error: { readonly message: string }) => fail(error.message);
26+
const failFrom = (operation: ThreadsSurfaceError["operation"], detail: string) =>
27+
Effect.mapError((cause: unknown) => new ThreadsSurfaceError({ operation, detail, cause }));
2728

2829
const isThreadSettled = (thread: OrchestrationReadModel["threads"][number]): boolean =>
2930
thread.settledOverride === "settled" ||
@@ -35,7 +36,9 @@ const liveThreads = (readModel: OrchestrationReadModel) =>
3536
const threadsList = (input: ThreadsListInput) =>
3637
Effect.gen(function* () {
3738
const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
38-
const readModel = yield* query.getCommandReadModel().pipe(Effect.catch(failFrom));
39+
const readModel = yield* query
40+
.getCommandReadModel()
41+
.pipe(failFrom("threads_list", "Failed to load the environment's threads."));
3942

4043
const threads = liveThreads(readModel)
4144
.filter((thread) => input.projectId === undefined || thread.projectId === input.projectId)
@@ -64,15 +67,23 @@ const threadsCreate = (input: ThreadsCreateInput) =>
6467
Effect.gen(function* () {
6568
const scope = yield* McpInvocationContext.McpInvocationContext;
6669
const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
67-
const readModel = yield* query.getCommandReadModel().pipe(Effect.catch(failFrom));
70+
const readModel = yield* query
71+
.getCommandReadModel()
72+
.pipe(failFrom("threads_create", "Failed to load the environment's threads."));
6873

6974
const callingThread = readModel.threads.find((thread) => thread.id === scope.threadId);
7075
if (!callingThread) {
71-
return yield* fail("Calling thread no longer exists; cannot derive the target project.");
76+
return yield* fail(
77+
"threads_create",
78+
"Calling thread no longer exists; cannot derive the target project.",
79+
);
7280
}
7381
const projectId = input.projectId ?? callingThread.projectId;
7482
if (!readModel.projects.some((project) => project.id === projectId)) {
75-
return yield* fail(`Project ${projectId} does not exist in this environment.`);
83+
return yield* fail(
84+
"threads_create",
85+
`Project ${projectId} does not exist in this environment.`,
86+
);
7687
}
7788

7889
const crypto = yield* Crypto.Crypto;
@@ -94,7 +105,7 @@ const threadsCreate = (input: ThreadsCreateInput) =>
94105
source: "agent",
95106
createdAt,
96107
})
97-
.pipe(Effect.catch(failFrom));
108+
.pipe(failFrom("threads_create", "Failed to create the thread."));
98109

99110
return { threadId: ThreadId.make(uuid), title: input.title } satisfies ThreadsCreateResult;
100111
});

‎apps/server/src/orchestration/ActivityPayloadProjection.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,26 @@ describe("projectActivityPayload", () => {
399399
expect(data.structuredResult).toEqual(result);
400400
});
401401

402+
it("does not treat a bare threads_list from a foreign MCP server as threads-surface", () => {
403+
const projected = projectActivityPayload(
404+
activity({
405+
itemType: "mcp_tool_call",
406+
data: {
407+
item: {
408+
type: "mcpToolCall",
409+
id: "item-3",
410+
tool: "threads_list",
411+
server: "github",
412+
status: "completed",
413+
result: { content: [{ type: "text", text: '{"threads":[]}' }] },
414+
},
415+
},
416+
}),
417+
);
418+
const data = (projected.payload as Record<string, unknown>).data as Record<string, unknown>;
419+
expect(data.structuredResult).toBeUndefined();
420+
});
421+
402422
it("keeps structuredResult when an already-projected activity is projected again", () => {
403423
const result = { threadId: "thrd_4", title: "Reprojected" };
404424
const once = projectActivityPayload(

‎apps/server/src/orchestration/ActivityPayloadProjection.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ function projectPreviewToolMetadata(data: Record<string, unknown>, status: unkno
309309

310310
/** True when the tool belongs to the server's own threads surface toolkit. */
311311
function isThreadsSurfaceTool(data: Record<string, unknown>, item: Record<string, unknown> | null) {
312-
return [data.toolName, item?.tool].some(
313-
(candidate) => matchThreadsSurfaceToolName(candidate) !== undefined,
312+
return (
313+
matchThreadsSurfaceToolName(data.toolName, data.server) !== undefined ||
314+
matchThreadsSurfaceToolName(item?.tool, item?.server) !== undefined
314315
);
315316
}
316317

‎apps/web/src/session-logic.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,10 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
722722
// `mcp__t3-code__*` at `data.toolName`, Codex keeps `item.tool`, and some
723723
// adapters classify the create call as a generic change item.
724724
const threadsToolData = asRecord(payload?.data);
725+
const threadsToolItem = asRecord(threadsToolData?.item);
725726
const threadsToolName =
726-
matchThreadsSurfaceToolName(threadsToolData?.toolName) ??
727-
matchThreadsSurfaceToolName(asRecord(threadsToolData?.item)?.tool);
727+
matchThreadsSurfaceToolName(threadsToolData?.toolName, threadsToolData?.server) ??
728+
matchThreadsSurfaceToolName(threadsToolItem?.tool, threadsToolItem?.server);
728729
if (threadsToolName === "threads_list") {
729730
const threadsList = extractThreadsListResult(threadsToolData?.structuredResult);
730731
if (threadsList) {

‎packages/contracts/src/threadsSurface.ts‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*
99
* @module ThreadsSurface
1010
*/
11-
import { Effect, Schema } from "effect";
11+
import * as Effect from "effect/Effect";
12+
import * as Schema from "effect/Schema";
1213
import {
1314
IsoDateTime,
1415
PositiveInt,
@@ -17,8 +18,7 @@ import {
1718
TrimmedNonEmptyString,
1819
} from "./baseSchemas.ts";
1920

20-
export const THREADS_SURFACE_TOOL_NAMES = ["threads_list", "threads_create"] as const;
21-
export type ThreadsSurfaceToolName = (typeof THREADS_SURFACE_TOOL_NAMES)[number];
21+
export type ThreadsSurfaceToolName = "threads_list" | "threads_create";
2222

2323
/**
2424
* Adapters surface toolkit tools under different names: Codex keeps the bare
@@ -28,11 +28,22 @@ export type ThreadsSurfaceToolName = (typeof THREADS_SURFACE_TOOL_NAMES)[number]
2828
*/
2929
const THREADS_SURFACE_QUALIFIED_NAME =
3030
/^(?:(?:mcp__)?t3[-_]?code_{1,2})?(threads_list|threads_create)$/;
31+
const THREADS_SURFACE_SERVER_NAME = /^t3[-_]?code$/;
3132

32-
export function matchThreadsSurfaceToolName(name: unknown): ThreadsSurfaceToolName | undefined {
33+
export function matchThreadsSurfaceToolName(
34+
name: unknown,
35+
server?: unknown,
36+
): ThreadsSurfaceToolName | undefined {
3337
if (typeof name !== "string") {
3438
return undefined;
3539
}
40+
// Adapters that split server and tool into separate fields (Codex
41+
// `item.server` + `item.tool`) carry bare tool names; an explicit foreign
42+
// server must not match, while a missing server stays permissive for
43+
// adapters that only report the tool name.
44+
if (typeof server === "string" && !THREADS_SURFACE_SERVER_NAME.test(server)) {
45+
return undefined;
46+
}
3647
return THREADS_SURFACE_QUALIFIED_NAME.exec(name)?.[1] as ThreadsSurfaceToolName | undefined;
3748
}
3849

@@ -77,8 +88,15 @@ export const ThreadsCreateResult = Schema.Struct({
7788
});
7889
export type ThreadsCreateResult = typeof ThreadsCreateResult.Type;
7990

80-
export const ThreadsSurfaceError = Schema.Struct({
81-
_tag: Schema.Literals(["ThreadsSurfaceError"]),
82-
detail: Schema.String,
83-
});
84-
export type ThreadsSurfaceError = typeof ThreadsSurfaceError.Type;
91+
export class ThreadsSurfaceError extends Schema.TaggedError<ThreadsSurfaceError>()(
92+
"ThreadsSurfaceError",
93+
{
94+
operation: Schema.Literals(["threads_list", "threads_create"]),
95+
detail: Schema.String,
96+
cause: Schema.optional(Schema.Defect()),
97+
},
98+
) {
99+
override get message(): string {
100+
return this.detail;
101+
}
102+
}

0 commit comments

Comments
 (0)