fix(ai-mcp): run a tool's own input schema before it executes #2309
main fromrefs/pull/2309/head·opened View on GitHubFixes #2198
🎯 Changes#
createMCPServer advertised each tool's inputSchema as JSON Schema, and the SDK validated against that view. JSON Schema cannot carry a transform or a default, so runTool received the raw arguments: .trim() kept its spaces, .min(1) accepted a blank string, and a .default(false) field stayed missing. chat() gave the same tool the parsed value, because executeServerTool calls validateToolInput first.
The tools/call handler now parses the input with the tool's own schema before the tool runs, through the same validateWithStandardSchema that parseToolOutput already uses on the output side:
- a transform and a default apply to the input the tool receives;
- an input the schema refuses answers as a tool error (
isError) naming the failed paths, so the model can correct its arguments instead of the tool acting on a default; - a tool whose
inputSchemais plain JSON Schema keeps the previous behaviour: the SDK check is the only one and the input passes through unchanged.
The task path (execution: 'task') parses before it starts the task, so a refused input never becomes a task.
Files#
packages/ai-mcp/src/server/input.ts:parseToolInput, the input-side twin ofparseToolOutput, andToolInputError.packages/ai-mcp/src/server/create-server.ts: thetools/callhandler parses first, and aToolInputErroranswers asisError: true.packages/ai-mcp/tests/server/input-schema.test.ts: three regression tests — the issue's repro (transform + default), a refused input, and a JSON Schema input that must not change.docs/mcp/server.md: one bullet on what the tool receives..changeset/ai-mcp-input-schema.md: patch changeset.
Tests#
The repro from the issue, through server.fetch, before and after the change:
before: the tool got {"title":" padded "} with no flag, and {"title":" "} ran the tool
after: the tool got {"title":"padded","flag":false}, and {"title":" "} answered isError
The package suite, run with a local vitest config that aliases the workspace packages to their src (this checkout has no built dist, and I did not run pnpm install at the root):
vitest run --config vitest.local.config.ts
→ Test Files 8 passed (8), Tests 146 passed (146)
That run covers tests/server/{create-server,auth,context,definitions,stores,tasks,input-schema}.test.ts and tests/tools.test.ts. stdio.test.ts was left out because the child process it spawns needs the built workspace. tsc on the changed files and the new test is clean. Without the fix, the two new behaviour tests fail and the JSON Schema test passes.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- New Features
- MCP tools now validate incoming arguments against their input schemas before execution. Schema transforms and defaults are applied, so tools receive parsed input.
- Invalid input returns a tool error identifying the failing paths, and the tool is not run with rejected arguments. Inputs that pass validation continue to work with existing JSON Schema definitions.
- Documentation
- Updated the MCP server guide to explain input parsing, schema transforms and defaults, and validation errors.
E2E#
testing/e2e/tests/mcp-input-schema.spec.ts covers the same input on the real server over HTTP. api.mcp-input-server advertises echo_trimmed (a .trim() transform and a .default(false) field) and strict_title (.trim().min(1)), and api.mcp-input-test?schema calls both with createMCPClient. The spec asserts the tool received padded|false, and that the blank title came back as isError naming title.
This checkout has no node_modules, so Playwright did not run locally; the E2E workflow is the check for it.
- 1
- 2
Pushedba4d089
- 2 bot comments
Sign in to join this conversation.