Skip to content

feat(agent): tool-mcp runner (stdio + streamable HTTP) (#4291) - #4314

Merged
mstomar125 merged 2 commits into
mainfrom
issue-4291-mcp-runner
Nov 19, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-4291-mcp-runner

Conversation

@mstomar125

@mstomar125 mstomar125 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Part of #4291: the tool-mcp runner. It runs after the executor/host (#4310), the tool-cli runner (#4312) and the tool-rest runner (#4313).

Agent lane. This touches packages/agent, which is Michael's lane. The user has explicitly approved this build, and the user's sign-off stands in for Michael's.

What it does

McpRunner calls ONE tool, named in the node (remote_tool_name), on an MCP server over stdio or streamable HTTP.

  • The node is the contract. tools/list is never requested. The description and schema the model sees come from the node and are covered by the approval tag. The arguments are checked against the node's closed parameter_schema before anything is started or sent.
  • No client capabilities. initialize sends capabilities: {}. A server-initiated request (sampling/createMessage, roots/list, elicitation/create, anything) is answered with JSON-RPC error -32601. ping is answered with {}.
  • Protocol. JSON-RPC 2.0. stdio is newline-delimited. HTTP is POST with an application/json or text/event-stream reply, Mcp-Session-Id and MCP-Protocol-Version carried after initialize, and a best-effort DELETE of the session at the end. Versions accepted: 2025-06-18 and 2025-03-26 on both transports, 2024-11-05 on stdio only (its HTTP form was a different transport and is not implemented).
  • Bounds. One message and the total read are capped (derived from the 1 MiB result cap, clamped to 64 KiB..4 MiB per message). Unrelated messages (notifications, other ids, server requests) are limited to 64 per call. Non-JSON stdout lines are limited to 64 lines / 64 KiB. A batch is refused. The serde_json default recursion limit is kept: a 100-level result parses and is bounded data, a 200-level line is ignored as noise. Total timeout is the binding's timeout_seconds.
  • Results are data. Only text content is returned (the host fences it as external). image/audio/resource content is replaced by a size note. isError is a failure. structuredContent is used only when there is no text. Credential values are removed from results AND from error text.
  • Cancellation. On timeout, a best-effort notifications/cancelled, then the server is killed (stdio: whole process group; HTTP: session deleted).
  • stdio stderr is read (so the server can't block) and discarded: never shown to the user or the model.
  • Confirmation text. transport, program (with where it resolves) or origin (with the resolved address), credential references (always shown, before anything long), tool name, then the arguments; only the arguments are cut.

Reuse (factored, not copied)

Moved out of the two runners without changing behaviour (all their existing tests pass unchanged apart from import paths):

  • process.rs: the process group guard, private scratch dir, stop/reap, base environment, spawn_isolated, env credential resolution, confirmation notes. Used by tool-cli and the stdio transport.
  • net_policy.rs: address blocklist, resolver, destination rules, pinned client, send_following (per-hop checks, same-origin redirects, no proxy/cookies/compression), HTTP credential resolution/header, credential note. Used by tool-rest and the HTTP transport.
  • test_http.rs: the loopback test server shared by the REST and MCP tests.
  • The scratch dir prefix is now nodespace-run- (was nodespace-cli-); the cli test was updated.

Offering

Every MCP call needs confirmation (no read-only exemption), so like tool-cli, nothing is offered to the model until confirmation wiring lands. The runner is installed in the daemon's host; a test pins needs_confirmation() for MCP, and the host's existing "dispatched or not offered, never inert" test covers the Mcp kind.

Tests

38+ new tests with a scripted stdio fake server (python3, unix only) and a loopback HTTP fake. Hostile cases: server-initiated sampling/roots/elicitation request, huge unsolicited tools/list-style message, 10 MB tool result, never-ending notifications, id mismatch (bounded and ignored), non-JSON noise (bounded), 100- and 200-level nested JSON, credential echoed in a result / an error / stderr, server dying mid-call, server ignoring initialize, batch answer, unsupported version, bad session id, redirect to another origin, private address, non-text content type, never-answering HTTP stream.

Mutation batch (26 mutants, each failing at least one test): capabilities advertised, server request answered with a result, ping, unrelated-message bound, id match, version check, noise bound, per-message cap (stdio and HTTP), total cap, isError, result redaction, error scrubbing, cancel, session header, version header, session delete, HTTP version list, SSE event type, auth header, argument cut, bound tool name, argument validation, media note, batch refusal, structured-content rule, session id validation. One survived at first (session id validation) and got its own test; it is killed now.

Review round 1 fixes

  • F1 Server error.message is no longer cut before the credential is removed; scrub sanitizes the whole text and then shortens it. Test: the token at every offset across the cut.
  • F2 The credential lookup is now inside the call's time limit for stdio, HTTP and (same fix) the tool-cli runner; what is left of the limit bounds the run. Tests with a slow backend for all three, and one that the lookup time counts against the same limit.
  • F3 SSE events that carry nothing (comments, retries, other event types, empty data) count against the noise bound (64). Test: a 4 MiB blank-line flood ends promptly.
  • F4 structuredContent is redacted on its string leaves and keys before it is serialised. Test: a credential containing " and \.
  • F5 validate_arguments is top-level only: the closed schema is checked at the top level, nested shape is NOT validated, and the MCP server is not trusted with it (stated in the runner header).
  • F6 The unit test is renamed to what it proves (needs_confirmation() is what gates offering); the host's existing dispatched-or-not-offered test covers the Mcp kind.
  • F7 The stdio confirmation puts credentials first and caps the program/argv/tool-name parts so the arguments always keep room (test with 8 credentials and long argv). The stderr reader is detached: a grandchild outside the killed process group that holds stderr keeps it alive, same class as the cli runner (documented in the header).
  • Tests added: chatty stderr (1.2 MiB before answering), declared Content-Type does not duplicate the JSON one (the REST credential/Content-Type headers are inserted, so a same-named declared header gives one line; a declared header named like the credential header is refused by the binding).
  • A bare-CR SSE terminator is never split into events and fails closed at the message limit (documented).
  • 307/308 same-origin redirects of the MCP POST are intended (as for tool-rest); the credential goes only to that origin.

Mutation checks for the fixes: each killed (error cut, scrub, three credential-timeout moves, flood count, structured redaction, key redaction, argv room, stderr drain, Content-Type append). One timeout-budget mutant hangs the test (it was stopped by the harness timeout rather than failing an assertion).

Known limits

  • The stdio fake needs python3, so those tests are cfg(unix).
  • A result larger than the message cap is an error, not a truncated result (a cut JSON-RPC message cannot be parsed).
  • The HTTP transport does not resume a dropped event stream (no Last-Event-ID) and does not open the optional server-to-client GET stream.
  • Result size cap is the default 1 MiB (the MCP binding has no max_output_bytes field).

Remaining for #4291: the confirmation wiring, the enable-and-approve command, the tool_whitelist decision, agent fixtures.

🤖 Generated with Claude Code

@mstomar125

Copy link
Copy Markdown
Contributor Author

Merge queue: #4314 was taken out of the queue — the merge gate failed on it, on top of main. Reproduce by merging or rebasing origin/main and running bun run test:changed, fix, push, and re-run bun run merge 4314.

cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin
  CFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin
  CFLAGS_aarch64-apple-darwin = None
  cargo:warning=sccache: error: Server startup failed: File exists (os error 17)
  cargo:warning=Run with SCCACHE_LOG=debug SCCACHE_NO_DAEMON=1 to get more information
  exit status: 2
  cargo:warning=ToolExecError: command did not execute successfully (status code exit status: 2): env -u IPHONEOS_DEPLOYMENT_TARGET LC_ALL="C" "/Users/mayanktomar/nodespace/core/.tools/bin/sccache" "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=arm64-apple-macosx" "-mmacosx-version-min=27.2" "-I" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/include" "-I" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/pregenerated" "-Wall" "-Wextra" "-fvisibility=hidden" "-std=c1x" "-Wall" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-gfull" "-DNDEBUG" "-o" "/Users/mayanktomar/nodespace/core/.claude/worktrees/_gate/target/debug/build/ring-02464c93aa49b344/out/a0330e891e733f4e-ecp_nistz.o" "-c" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/crypto/fipsmodule/ec/ecp_nistz.c"
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 0
  --- stderr
  error occurred in cc-rs: command did not execute successfully (status code exit status: 2): env -u IPHONEOS_DEPLOYMENT_TARGET LC_ALL="C" "/Users/mayanktomar/nodespace/core/.tools/bin/sccache" "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=arm64-apple-macosx" "-mmacosx-version-min=27.2" "-I" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/include" "-I" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/pregenerated" "-Wall" "-Wextra" "-fvisibility=hidden" "-std=c1x" "-Wall" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-gfull" "-DNDEBUG" "-o" "/Users/mayanktomar/nodespace/core/.claude/worktrees/_gate/target/debug/build/ring-02464c93aa49b344/out/a0330e891e733f4e-ecp_nistz.o" "-c" "/Users/mayanktomar/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/crypto/fipsmodule/ec/ecp_nistz.c"
error: script "shape:check" exited with code 101
[stage exited with code 101]

  full output: /var/folders/3w/y9kj522n1_j9wqmt6vl_sb6r0000gp/T/nodespace-gate-logs/_gate-merge-2026-11-19T19-00-37-963Z/shape-check-built-in-shape-snapshot-and-change-log.log

✗ shape:check (built-in shape snapshot and change log) failed — merge blocked.
  Fix the failure, push, and re-run: bun run merge <PR#>

mstomar125 and others added 2 commits November 19, 2026 20:13
Calls the one tool bound in the node; tools/list is never used. No client
capabilities, server requests refused, messages bounded, results as data.
Process and network rules are shared with the cli and rest runners
(process.rs, net_policy.rs).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ntial lookup, count empty events, redact structured content (#4291)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant