diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index 3b2cb665b3548..aed212fe30395 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -192,6 +192,11 @@ const callTool = (name: string, args: Record) => ); }); +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) => callTool("preview_snapshot", args); it.effect.each([ @@ -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({ @@ -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 = []; 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))); }); @@ -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))); }, diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index be41f8630b7dd..cb79e59021bd4 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -1248,6 +1248,42 @@ const withToolSpans = Effect.fn("McpHttpServer.withToolSpans")(function* (registration: Effect.Effect) { + 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, @@ -1299,9 +1335,11 @@ export const toolkitRegistration = , EX, handlers: McpToolAccess.HandlersLayer, ) => Layer.effectDiscard( - withToolAnalytics( - withToolSpans( - withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))), + withInvalidArgumentsAsToolErrors( + withToolAnalytics( + withToolSpans( + withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))), + ), ), ), ).pipe( diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index 33c356af4e7e1..3ba2f971c73f4 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -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(