The best way to control Claude Code, Codex and more from any computer or phone
fix(mcp): optional tool parameters accept the null their schema advertises (#20411)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Victor Vogel committed
Oct 18, 2026 at 14:33 UTC
93ae22a4d59018cc858b9f66dae1b05584cc0df1
3 files changed
+119
-10
apps/server/src/mcp/McpHttpServer.test.ts
+96
-3
@@ -34,7 +34,10 @@ import * as McpToolAccess from "./McpToolAccess.ts";
34
import * as McpToolAccessTestkit from "./McpToolAccess.testkit.ts";
35
import * as McpInvocationContext from "./McpInvocationContext.ts";
36
import * as McpSessionRegistry from "./McpSessionRegistry.ts";
37
+import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
38
import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
39
+import { OrchestratorToolkit } from "./toolkits/orchestrator/tools.ts";
40
+import { ProjectToolkit } from "./toolkits/project/tools.ts";
41
42
const environmentId = EnvironmentId.make("environment-mcp-test");
43
const threadId = ThreadId.make("thread-mcp-test");
@@ -125,17 +128,55 @@ const serveSnapshots = (clientId: string, result: unknown) =>
128
return inputs;
129
});
130
128
-const callSnapshot = (args: Record<string, unknown>) =>
131
+const callTool = (name: string, args: Record<string, unknown>) =>
132
Effect.gen(function* () {
133
const server = yield* McpServer.McpServer;
134
return yield* server
132
- .callTool({ name: "preview_snapshot", arguments: args })
135
+ .callTool({ name, arguments: args })
136
.pipe(
137
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
138
Effect.provideService(McpSchema.McpServerClient, client),
139
);
140
});
141
142
+const callSnapshot = (args: Record<string, unknown>) => callTool("preview_snapshot", args);
143
+
144
+// Real tool definitions with handlers that record what they decoded.
145
+const layerRecordingTools = (calls: Array<unknown>) => {
146
+ const RecordingToolkit = Toolkit.make(
147
+ OrchestratorToolkit.tools.delegate_task,
148
+ OrchestratorToolkit.tools.t3_thread_read,
149
+ OrchestratorToolkit.tools.t3_thread_wait,
150
+ ProjectToolkit.tools.t3_project_update,
151
+ );
152
+ const record = McpToolAccess.reads((params: unknown) =>
153
+ Effect.sync(() => calls.push(params)).pipe(
154
+ Effect.andThen(
155
+ Effect.fail(new OrchestratorMcpFailure({ code: "thread_not_found", message: "Recorded." })),
156
+ ),
157
+ ),
158
+ );
159
+ return McpHttpServer.toolkitRegistration(
160
+ RecordingToolkit,
161
+ McpToolAccess.toLayer(RecordingToolkit, {
162
+ delegate_task: record,
163
+ t3_thread_read: record,
164
+ t3_thread_wait: record,
165
+ t3_project_update: record,
166
+ }),
167
+ ).pipe(
168
+ Layer.provideMerge(McpServer.McpServer.layer),
169
+ Layer.provide(
170
+ Layer.mergeAll(
171
+ McpToolAccessTestkit.liveThreadsLayer,
172
+ Layer.mock(OrchestratorMcpService.OrchestratorMcpService)({}),
173
+ Layer.mock(ProjectService.ProjectService)({}),
174
+ NodeServices.layer,
175
+ ),
176
+ ),
177
+ );
178
+};
179
+
180
it("normalizes empty successful notification responses to accepted", () => {
181
const notificationResponse = McpHttpServer.normalizeMcpHttpResponse(
182
HttpServerResponse.text("", { status: 200, contentType: "application/json" }),
@@ -390,7 +431,7 @@ it.effect("refuses preview tools to a client outside a thread before they run",
431
it.effect("rejects non-boolean snapshot image options before selecting a browser host", () =>
432
Effect.gen(function* () {
433
const server = yield* McpServer.McpServer;
393
- for (const includeImage of ["false", 0, null]) {
434
+ for (const includeImage of ["false", 0]) {
435
const result = yield* server
436
.callTool({
437
name: "preview_snapshot",
@@ -447,6 +488,58 @@ it.effect("saves the snapshot PNG on request and reports its path", () =>
488
).pipe(Effect.provide(layerTest)),
489
);
490
491
+it.effect("treats null snapshot options like omitted ones", () =>
492
+ Effect.scoped(
493
+ Effect.gen(function* () {
494
+ const inputs = yield* serveSnapshots("mcp-null-options-client", snapshotResult);
495
+
496
+ const omitted = yield* callSnapshot({});
497
+ const nulls = yield* callSnapshot({ tabId: null, includeImage: null, save: null });
498
+
499
+ expect(nulls).toEqual(omitted);
500
+ expect(inputs).toEqual([{}, {}]);
501
+ }),
502
+ ).pipe(Effect.provide(layerTest)),
503
+);
504
+
505
+it.effect("decodes null optional arguments the way it decodes omitted ones", () => {
506
+ const calls: Array<unknown> = [];
507
+ return Effect.gen(function* () {
508
+ const thread = { threadId: "thread-null-arguments" };
509
+ yield* callTool("t3_thread_read", thread);
510
+ yield* callTool("t3_thread_read", { ...thread, afterPosition: null, view: null });
511
+ yield* callTool("delegate_task", { task: "Inspect", target: {} });
512
+ yield* callTool("delegate_task", { task: "Inspect", title: null, target: { model: null } });
513
+ // A null the schema declares keeps its meaning: here it clears the project icon.
514
+ yield* callTool("t3_project_update", { projectId: "project-1", projectIcon: null });
515
+
516
+ expect(calls).toHaveLength(5);
517
+ expect(calls[1]).toEqual(calls[0]);
518
+ expect(calls[3]).toEqual(calls[2]);
519
+ expect(calls[4]).toStrictEqual({ projectId: "project-1", projectIcon: null });
520
+ }).pipe(Effect.provide(layerRecordingTools(calls)));
521
+});
522
+
523
+it.effect("accepts finite wait budgets and refuses non-finite ones", () => {
524
+ const calls: Array<unknown> = [];
525
+ return Effect.gen(function* () {
526
+ yield* callTool("delegate_task", { task: "Inspect", timeoutMs: 1500.5 });
527
+ yield* callTool("t3_thread_wait", { threadId: "thread-wait", timeoutMs: 5000 });
528
+ expect(calls).toMatchObject([{ timeoutMs: 1500.5 }, { timeoutMs: 5000 }]);
529
+
530
+ for (const [name, args] of [
531
+ ["delegate_task", { task: "Inspect", timeoutMs: "Infinity" }],
532
+ ["t3_thread_wait", { threadId: "thread-wait", timeoutMs: "NaN" }],
533
+ ["t3_thread_wait", { threadId: "thread-wait", timeoutMs: "-Infinity" }],
534
+ ["t3_thread_read", { threadId: "thread-wait", afterPosition: "2" }],
535
+ ] as const) {
536
+ const refused = yield* callTool(name, args).pipe(Effect.flip);
537
+ expect(refused._tag, `${name} ${JSON.stringify(args)}`).toBe("InvalidParams");
538
+ }
539
+ expect(calls).toHaveLength(2);
540
+ }).pipe(Effect.provide(layerRecordingTools(calls)));
541
+});
542
+
543
it.effect("reports a tagged error when the screenshot cannot be saved", () =>
544
Effect.scoped(
545
Effect.gen(function* () {
apps/server/src/mcp/McpHttpServer.ts
+21
-5
@@ -11,7 +11,7 @@ import * as Schema from "effect/Schema";
11
import * as Sink from "effect/Sink";
12
import * as Stream from "effect/Stream";
13
import type * as Types from "effect/Types";
14
-import { AiError, McpProtocol, McpSchema, McpServer, Tool, type Toolkit } from "effect/ai";
14
+import { AiError, McpProtocol, McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
15
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
16
import { OrchestratorMcpFailure, PreviewAutomationError } from "@t3tools/contracts";
17
@@ -386,6 +386,22 @@ const saveScreenshot = Effect.fn("McpHttpServer.saveScreenshot")(function* (
386
return screenshotPath;
387
});
388
389
+/**
390
+ * Tool input schemas are advertised through each schema's JSON codec, which
391
+ * offers `null` for every `Schema.optional` field, but Toolkit decodes with the
392
+ * schema itself and rejects that `null`. Decoding through the same codec makes
393
+ * a tool accept what it advertises.
394
+ */
395
+const decodeAsAdvertised = <Tools extends Record<string, Tool.Any>>(
396
+ toolkit: Toolkit.Toolkit<Tools>,
397
+): Toolkit.Toolkit<Tools> =>
398
+ // The clones keep each tool's id, name and decoded parameter type, so handlers still match.
399
+ Toolkit.make(
400
+ ...Object.values(toolkit.tools).map((tool) =>
401
+ tool.setParameters(Schema.toCodecJson(tool.parametersSchema)),
402
+ ),
403
+ ) as unknown as Toolkit.Toolkit<Tools>;
404
+
405
const isPreviewAutomationError = Schema.is(PreviewAutomationError);
406
407
const previewSnapshotFailure = <E>(cause: Cause.Cause<E>) => {
@@ -431,7 +447,7 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot
447
const saveServices = yield* Effect.context<
448
ServerConfig.ServerConfig | FileSystem.FileSystem | Path.Path | Crypto.Crypto
449
>();
434
- const built = yield* PreviewSnapshotToolkit;
450
+ const built = yield* decodeAsAdvertised(PreviewSnapshotToolkit);
451
const tool = PreviewSnapshotTool;
452
yield* server.addTool({
453
tool: new McpSchema.Tool({
@@ -687,7 +703,7 @@ const registerImageTool = <T extends Tool.Any, E, R>(
703
const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreenshot")(function* () {
704
const devices = yield* DeviceService.DeviceService;
705
const threads = yield* ThreadManagementService.ThreadManagementService;
690
- const built = yield* DeviceScreenshotToolkit;
706
+ const built = yield* decodeAsAdvertised(DeviceScreenshotToolkit);
707
yield* registerImageTool(
708
DeviceScreenshotTool,
709
(payload) =>
@@ -709,7 +725,7 @@ const isOrchestratorMcpFailure = Schema.is(OrchestratorMcpFailure);
725
const registerHtmlPreview = Effect.fn("McpHttpServer.registerHtmlPreview")(function* () {
726
const htmlRender = yield* HtmlRender.HtmlRender;
727
const threads = yield* ThreadManagementService.ThreadManagementService;
712
- const built = yield* HtmlPreviewToolkit;
728
+ const built = yield* decodeAsAdvertised(HtmlPreviewToolkit);
729
yield* registerImageTool(
730
HtmlPreviewTool,
731
(payload) =>
@@ -740,7 +756,7 @@ export const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX,
756
toolkit: Toolkit.Toolkit<Tools>,
757
handlers: McpToolAccess.HandlersLayer<Tools, EX, RX>,
758
) => {
743
- const registration = McpServer.toolkit(toolkit);
759
+ const registration = McpServer.toolkit(decodeAsAdvertised(toolkit));
760
// @effect-diagnostics-next-line unsafeEffectTypeAssertion:off - the auth middleware provides it per request.
761
const registered = registration as Layer.Layer<
762
never,
packages/contracts/src/orchestratorMcp.ts
+2
-2
@@ -173,7 +173,7 @@ export const OrchestratorMcpDelegateTaskInput = Schema.Struct({
173
"Defaults to async. Use wait only when this turn needs the child's result before you can continue.",
174
}),
175
),
176
- timeoutMs: Schema.optional(Schema.Number).annotate({
176
+ timeoutMs: Schema.optional(Schema.Finite).annotate({
177
description:
178
"Wait budget for mode=wait only. Default 10 minutes. Elapsing it returns waitTimedOut=true on that call and does not cancel the child.",
179
}),
@@ -443,7 +443,7 @@ export type OrchestratorMcpThreadSendResult = typeof OrchestratorMcpThreadSendRe
443
export const OrchestratorMcpThreadWaitInput = Schema.Struct({
444
threadId: ThreadId,
445
runId: Schema.optional(RunId),
446
- timeoutMs: Schema.optional(Schema.Number),
446
+ timeoutMs: Schema.optional(Schema.Finite),
447
});
448
export type OrchestratorMcpThreadWaitInput = typeof OrchestratorMcpThreadWaitInput.Type;
449