Skip to content
39 changes: 27 additions & 12 deletions apps/server/src/mcp/McpHttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,11 @@ 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(rejected.isError).toBe(true);
expect(rejected.content).toEqual([
{ type: "text", text: expect.stringContaining('at ["target"]') },
]);
}
expect(calls).toHaveLength(2);
expect(calls[0]).toEqual({
Expand All @@ -705,16 +708,25 @@ 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 }),
);
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(launch.isError).toBe(true);
expect(launch.content).toEqual([
{ type: "text", text: expect.stringContaining('Unknown argument "prompt"') },
]);
expect(wait.isError).toBe(true);
expect(wait.content).toEqual([
{ type: "text", text: expect.stringContaining('Unknown argument "timeout"') },
]);
}).pipe(Effect.provide(layerRecordingTools(calls)));
});

Expand Down Expand Up @@ -773,9 +785,12 @@ 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(rejected.isError).toBe(true);
expect(rejected.content).toEqual([
{ type: "text", text: expect.stringContaining("greater than or equal to 1") },
]);
expect(calls).toEqual([]);
}).pipe(Effect.provide(layerRecordingTools(calls)));
},
Expand Down
37 changes: 36 additions & 1 deletion apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,38 @@ const withAdvertisedSchemas = Effect.fn("McpHttpServer.withAdvertisedSchemas")(f
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, advertisingServer));
});

/**
* `registerToolkit` raises `InvalidParams` only for invalid arguments. MCP before 2025-11-25 sends
* that as a JSON-RPC error, which Cursor shows models as "Tool execution error", so it goes back
* as a tool result the model can read and correct.
*/
const withReadableInvalidArguments = Effect.fn("McpHttpServer.withReadableInvalidArguments")(
function* <A, E, R>(registration: Effect.Effect<A, E, R>) {
const server = yield* McpServer.McpServer;
const readableServer = 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.succeed(
new McpSchema.CallToolResult({
isError: true,
content: [{ type: "text", text: error.message }],
}),
),
}),
),
});
}),
});
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, readableServer));
},
);

/** Hands each call the services its registration was built with; the request's own still win. */
const provideRegistrationServices = <Services>() =>
McpServer.middleware<{ provides: Services }>()(
Expand All @@ -1301,7 +1333,10 @@ export const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX,
Layer.effectDiscard(
withToolAnalytics(
withToolSpans(
withAdvertisedSchemas(toolkit, McpServer.registerToolkit(decodeAsAdvertised(toolkit))),
withAdvertisedSchemas(
toolkit,
withReadableInvalidArguments(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