Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions apps/server/src/mcp/McpHttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ const callTool = (name: string, args: Record<string, unknown>) =>
);
});

const invalidArgumentText = (result: McpSchema.CallToolResult) => {
expect(result.isError).toBe(true);
return result.content.map((part) => (part.type === "text" ? part.text : "")).join("");
};

const callSnapshot = (args: Record<string, unknown>) => callTool("preview_snapshot", args);

it.effect.each([
Expand Down Expand Up @@ -687,8 +692,10 @@ it.effect("decodes a JSON-stringified delegate target as the same object", () =>
yield* callTool("delegate_task", { task, target: JSON.stringify(target) });

for (const invalidTarget of ["{model:", '{"model":42}']) {
const error = yield* Effect.flip(callTool("delegate_task", { task, target: invalidTarget }));
expect(error).toBeInstanceOf(McpSchema.InvalidParams);
const rejected = yield* callTool("delegate_task", { task, target: invalidTarget });
expect(invalidArgumentText(rejected)).toContain(
"Invalid parameters for tool 'delegate_task'",
);
}
expect(calls).toHaveLength(2);
expect(calls[0]).toEqual({
Expand All @@ -705,16 +712,20 @@ it.effect("decodes a JSON-stringified delegate target as the same object", () =>
it.effect("rejects arguments a tool does not declare instead of dropping them", () => {
const calls: Array<unknown> = [];
return Effect.gen(function* () {
const launch = yield* Effect.flip(
callTool("t3_thread_launch", { title: "Repro", scratch: true, prompt: "say hi" }),
);
const wait = yield* Effect.flip(
callTool("t3_thread_wait", { threadId: "thread-excess-arguments", timeout: 5_000 }),
);
// A protocol error here reaches claude.ai only as "the tool failed (unavailable)".
const launch = yield* callTool("t3_thread_launch", {
title: "Repro",
scratch: true,
prompt: "say hi",
});
const wait = yield* callTool("t3_thread_wait", {
threadId: "thread-excess-arguments",
timeout: 5_000,
});

expect(calls).toEqual([]);
expect(launch.message).toContain('Unknown argument "prompt"');
expect(wait.message).toContain('Unknown argument "timeout"');
expect(invalidArgumentText(launch)).toContain('Unknown argument "prompt"');
expect(invalidArgumentText(wait)).toContain('Unknown argument "timeout"');
}).pipe(Effect.provide(layerRecordingTools(calls)));
});

Expand Down Expand Up @@ -773,9 +784,9 @@ it.effect.each(["limit", "runLimit", "maxCharsPerItem"])(
const rejected = yield* callTool("t3_thread_read", {
threadId: "thread-below-minimum",
[field]: 0,
}).pipe(Effect.flip);
});

expect(String(rejected)).toContain("greater than or equal to 1");
expect(invalidArgumentText(rejected)).toContain("greater than or equal to 1");
expect(calls).toEqual([]);
}).pipe(Effect.provide(layerRecordingTools(calls)));
},
Expand Down
44 changes: 41 additions & 3 deletions apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,42 @@ const withToolSpans = Effect.fn("McpHttpServer.withToolSpans")(function* <A, E,
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, tracingServer));
});

/** Before MCP 2025-11-25 a bad argument is a -32602 that claude.ai shows only as "unavailable"; a tool error shows the model why. */
const withInvalidArgumentsAsToolErrors = Effect.fn(
"McpHttpServer.withInvalidArgumentsAsToolErrors",
)(function* <A, E, R>(registration: Effect.Effect<A, E, R>) {
const server = yield* McpServer.McpServer;
const reportingServer = McpServer.McpServer.of({
...server,
addTool: Effect.fn("McpServer.addTool")(function (options) {
return server.addTool({
...options,
handle: (payload) =>
options.handle(payload).pipe(
Effect.catchTags({
InvalidParams: (error) =>
Effect.serviceOption(McpSchema.McpRequestContext).pipe(
Effect.flatMap((request) =>
Option.isSome(request) &&
(request.value.requestState !== undefined ||
request.value.inputResponses !== undefined)
? Effect.fail(error)
: Effect.succeed(
new McpSchema.CallToolResult({
isError: true,
content: [{ type: "text", text: error.message }],
}),
),
),
),
}),
),
});
}),
});
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, reportingServer));
});

/** `McpServer.registerToolkit` advertises the full codec schema; agents get the compact one. */
const withAdvertisedSchemas = Effect.fn("McpHttpServer.withAdvertisedSchemas")(function* <
Tools extends Record<string, Tool.Any>,
Expand Down Expand Up @@ -1299,9 +1335,11 @@ export const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX,
handlers: McpToolAccess.HandlersLayer<Tools, EX, RX>,
) =>
Layer.effectDiscard(
withToolAnalytics(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
withInvalidArgumentsAsToolErrors(
withToolAnalytics(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
),
),
),
).pipe(
Expand Down
5 changes: 2 additions & 3 deletions apps/server/src/mcp/toolkits/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,11 @@ it.effect("rejects an unknown organize target instead of archiving the calling t
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
Effect.result,
);
expect(dispatched).toEqual([]);
expect(outcome).toMatchObject({
_tag: "Failure",
failure: { message: expect.stringContaining('Unknown argument "threadIds"') },
isError: true,
content: [{ type: "text", text: expect.stringContaining('Unknown argument "threadIds"') }],
});
}).pipe(
Effect.provide(
Expand Down
Loading