Repository navigation
fix(mcp): answer malformed request params as -32602 Invalid params naming the param - #29205
Conversation
…th a readable sentence Both MCP transports now build their server through createPlatformMcpServer, which maps a request-schema parse failure to JSON-RPC -32602 naming the param (`params.name is required`) instead of the SDK's -32603 Internal error carrying the serialised Zod issue list. Claude-Session: https://claude.ai/code/session_01ADHkmT3dPW9V7vSk6VKtuN Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ADHkmT3dPW9V7vSk6VKtuN Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift CheckThis PR changes 1 package(s): 10 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:
⛔ 3 release-owned page(s) also name something this change touched. These are read-only:
What this run could not see
Coarse fallback — 12 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 68befdb32b6d26fe7980765ac355536afa85e5ae && git checkout 68befdb32b6d26fe7980765ac355536afa85e5ae
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 692e0dc8b936ad37311352868083d78745c76a21 7bff29ad7389d1c21f14868901f85b9364d20c0e && git checkout -B drift-repro 692e0dc8b936ad37311352868083d78745c76a21 && git merge --no-ff 7bff29ad7389d1c21f14868901f85b9364d20c0e
node scripts/docs-audit/affected-docs.mjs --json 692e0dc8b936ad37311352868083d78745c76a21
|
|
domain:server#1 · os-steve · session_01ADHkmQjPiXgNoSaEpqeGvY 席位核对 · ACCEPT ·
|
| item | reading |
|---|---|
| Shape | Draft, base main, Fixes #29135. 4 files (+300 / −2): new packages/mcp/src/request-params.ts (createPlatformMcpServer), both new McpServer(...) sites in mcp-server-runtime.ts (long-lived stdio server and the per-request HTTP server) moved onto it, request-params.test.ts, a @objectstack/mcp patch changeset. Metadata-protocol: none. No packages/spec/**, no new code beyond JSON-RPC's own -32602, no lockfile bump, no gate. Not governed. |
| Rule | One seam: every request dispatcher in the SDK protocol's handler map (constructor-installed and later-registered) is wrapped so a SYNCHRONOUS $ZodError (the request-schema parse, which the dispatcher runs before calling the async handler) is rethrown as McpError(InvalidParams) with a plain sentence (params.name is required / must be a string, else the shared Zod issue formatter). A ZodError from inside a handler arrives as a rejection and keeps its own answer, so the control (validate_expression bad site → -32602) is unchanged. |
| Accepted deviation | The seam reads the SDK's private _requestHandlers map: the SDK offers no public hook between the parse and the error answer (measured 1.32.1 installed, 1.32.2 latest). Its absence throws at server construction with the function's name, so an SDK rename fails loudly instead of returning to -32603. Accepted as the narrowest fix; the long-term fix is upstream. |
| Evidence | Base 0d6d4061: -32603 + Zod dump on the three card requests over HTTP and on tools/call {} over stdio. Head: mcp 54 files / 651 green, runtime MCP domain 4 files / 162 green, typecheck 0, eslint 0; two ablations red, restore clean. Gates 59 derived, 58 run, check:dual-build-cjs-loads NOT MEASURED (full build), left to CI. CI on this head is running; auto-merge waits for every check green or an expected skip. |
| Open question | Report the gap to the SDK upstream: not this PR; noted on the card for the maintainer, no card filed (an upstream issue is an outward act). |
| Out of scope | none. |
| Landing | 「按照 pm-dispatch 规则复核过的都授权」 (Leehom, 2026-10-25T11:35Z): ready and auto-merge through the relay. |
Requested by Steve · project thread
Fixes #29135
Clause-②: no
Metadata-protocol: none
维护者速读
prompts/get不带 name、tools/call传{})时,服务器现在回 JSON-RPC -32602「参数无效」,并用一句英文点名哪个参数错了(params.name is required),不再回 -32603「内部错误」加一段 Zod 原始 JSON。validate_expression的 site 枚举)和处理函数内部抛出的错误都不受影响。回滚即回退本 PR。packages/mcp/src/request-params.ts(唯一的接缝,读 SDK 未公开的请求处理表,缺失时构造即报错)、packages/mcp/src/mcp-server-runtime.ts两处建服务器的调用点、packages/mcp/src/request-params.test.ts。pnpm --filter @objectstack/mcp exec vitest run src/request-params.test.ts,预期 7 个测试全绿。What changes
packages/mcp/src/request-params.tswithcreatePlatformMcpServer(...), now the only place the platform builds an MCPMcpServer. Both transports use it: the long-lived (stdio) server in theMCPServerRuntimeconstructor and the per-request Streamable HTTP server inhandleHttpRequest.Serverholds, the ones the SDK installs in its own constructor (initialize,ping) and every later one (tools/call,prompts/get,resources/read, …). A ZodError thrown synchronously by a dispatcher is the method's request-schema parse (the SDK parses before it calls the async handler), and it is rethrown asMcpError(ErrorCode.InvalidParams, sentence). A ZodError raised inside a handler arrives as a rejected promise and keeps its own answer.;:params.name is required(the value is absent),params.name must be a string(wrong type),must be one of …(enum or literal),has unrecognized key(s): …. Any other issue falls back to the sharedformatZodIssuefrom@objectstack/spec/shared.McpErroradds its ownMCP error -32602:prefix.packages/spec/**edit, no lockfile change, no new gate.Why
The SDK's
Protocol.setRequestHandler(measured on the installed@modelcontextprotocol/sdk1.32.1, and unchanged in the latest 1.32.2) installs a closure that parses the request with the method's schema and rethrows the ZodError unchanged. A ZodError has no numericcode, so the protocol answers-32603 Internal errorwith the error's message, which is the serialised issue array. The SDK offers no public hook between that parse and the error answer, so the seam reads the protocol's_requestHandlersmap. That is an SDK-internal field, so the seam checks it is there: if a later SDK renames it, server construction throws an error that names this function, and the -32603 answer does not come back silently.Measured on base
0d6d4061(in-process, same calls as the card):prompts/get{}(HTTP)params.name is requiredtools/call{}(HTTP)params.name is requiredprompts/get{"name":42}(HTTP)params.name must be a stringtools/call{}(stdio, long-lived server)params.name is requiredvalidate_expressionwithsite: "nope"isError,MCP error -32602: Input validation error: …Clause-② measured: the same requests are refused on base and head (no request accepted on one side is refused on the other), so
no. Only the error class and the message change.Tests
packages/mcp/src/request-params.test.ts(new, 7 cases):prompts/getwith no name,tools/callwith{},prompts/getwithname: 42, andinitializewith noprotocolVersion(a handler the SDK installs in its own constructor). Each assertserror.code === -32602and the full message.tools/callwith{}on the long-lived server over a realStdioServerTransport.validate_expressionwith an unknownsitekeeps its tool-result answer.-32603.No existing test was edited.
Acceptance notes
resources/readwith malformed params: neither server in these tests registers resources, so it answers -32601 Method not found there. The seam is method-agnostic (it wraps the map, not a method list), and theinitializepin proves that a handler installed outside the platform's own registration is covered too.Verification
All readings at head
7bff29adunless noted.Ablations (both restored, `git diff HEAD` empty, blob == HEAD)
answerMalformedParamsAsInvalidParams(server);deleted throughscripts/ablation-replace.mjs, anchor 1 → 0, at 1d7f90e): 5 failed, 2 passed. Each failure isexpected -32603 to be -32602. The control and the inner-ZodError pin stay green.a ZodError thrown inside a handler keeps the -32603 answerfails,expected -32602 to be -32603. 6 passed.Suites
pnpm --filter @objectstack/mcp test: Test Files 54 passed (54), Tests 651 passed (651).pnpm --filter @objectstack/mcp typecheck(tsc + check:test-typecheck): exit 0.@objectstack/runtimeMCP domain tests (src/domains/mcp-{federation-bridge,merged-skill-read,option-fields-read,package-object-discovery}.test.ts, after building@objectstack/runtime^...): Test Files 4 passed (4), Tests 162 passed (162).packages/mcp(--no-inline-config): exit 0.Gates (dispatch-gates --commands, 59 derived)
node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstackat7bff29ad: 59 commands derived; 4 changed paths, 302 changed lines.node scripts/check-plugin-teardown-shape.mjs --self-test: exit 3 on the first run (its positive-control commit was outside the shallow clone). Exit 0 aftergit fetch --unshallow origin main(48 cases pass).pnpm check:dual-build-cjs-loads: exit 3, PREREQUISITE NOT MET (it needs every package'sdist/; only the@objectstack/runtimeclosure was built here). NOT MEASURED locally; CI's Lint & Repo Gates runs it.--ranreconciliation:59 derived, 58 run, 1 NOT-MEASURED, 0 UNRUN.🤖 Generated with Claude Code · https://claude.ai/code/session_01ADHkmT3dPW9V7vSk6VKtuN
Generated by Claude Code