Skip to content

mcp: use rmcp for the stdio server - #164

Open
12458 wants to merge 3 commits into
facebookexperimental:mainfrom
12458:mcp-rmcp-stdio
Open

12458 wants to merge 3 commits into
facebookexperimental:mainfrom
12458:mcp-rmcp-stdio

Conversation

@12458

@12458 12458 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Replaces semcode-mcp's hand-written JSON-RPC loop with rmcp 3.12 (the official Rust MCP SDK). rmcp now handles framing, the handshake, version negotiation, and request routing, so semcode-mcp only owns its tools and concurrency limit.

This PR is so I can make improvements to the MCP tools without rewriting a bunch of things.

Changes

  • McpHandler implements rmcp::ServerHandler over McpServer. It keeps --max-concurrent-requests (-32001 with retryable: true when full) and runs tool futures on blocking workers.
  • CancelOnWriteError wraps stdout so the server exits on a broken pipe even when stdin stays open (rmcp logs write errors and keeps reading).
  • Clean shutdown: main ends with runtime.shutdown_background() so the process does not hang on a blocked stdin read (for example, Unix socket pairs used by Node clients) or a tool still running after the session ends.
  • Single tool table: merged the duplicate tool dispatch tables for tools/call and lazy-mode call_tool into McpServer::run_tool.

Behavior changes

  • Protocol version & lifecycle: initialize negotiates the client's version when rmcp supports it (instead of always 2024-11-05), and serverInfo.version now reports the crate version. The session lifecycle is rmcp's: before initialize, ping is answered, other requests are refused (unless they carry the 2026-07-28 per-request metadata) and a notification ends the server. Closing stdin before initialize exits cleanly.
  • Messages: lines that are not valid JSON-RPC are ignored instead of answered with -32700, a tools/call without a name or with non-object arguments gets -32601, and a line over 16 MiB is dropped without a reply.
  • Pending output is unbounded: a slot is freed when its worker returns, before rmcp writes the response, and rmcp keeps reading while replies queue. A client that stops reading stdout but keeps sending requests grows server memory; the old loop stalled its reads instead. Documented in README.md.
  • Cancellation: cancelled tool calls stop at their next .await (such as a database query) and free their slot once the worker stops. Work between awaits still runs to completion.
  • Stale-index fields: index_stale, written_by, expected, and command move under _meta on the tool result, since rmcp drops unknown top-level fields. The error text is unchanged (documented in README.md).
  • Background indexing notifications: removed notifications/message progress updates (deprecated in MCP and rmcp). Progress is still available via indexing_status and stderr.
  • Shutdown on EOF: when stdin closes, running calls have up to 5 seconds (rmcp's drain limit) to send responses before the server exits, instead of waiting without a limit.

Testing

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, and cargo test pass.
  • Unit tests in src/bin/semcode-mcp.rs cover concurrency limits, cancelled calls freeing slots once dropped and keeping them while synchronous work runs, _meta on error results, and tool schema conversion.
  • Integration tests in tests/mcp_stdio.rs cover version negotiation (2024-11-05 and 2025-06-18), tools/list + tools/call, clean exit on EOF before initialize, and a clean exit on broken stdout over pipes and socket pairs.

The tools/call handler and the lazy-mode call_tool meta-tool each
listed all nineteen tools, so adding a tool meant editing both lists
and nothing checked that they agreed.

run_tool now holds the only table and returns None for a name it does
not know. Each caller keeps its own answer for that case: tools/call
reports "Unknown tool" as an error result, and call_tool points at
list_categories. handle_tool_call takes the tool name and arguments
rather than the raw request parameters, so it no longer depends on the
shape of the JSON-RPC request.

The handler tests built McpServer by listing every field, seventeen
times over; they now start from a test_server() helper and override
only the fields they test.
A query against an index written by an older semcode is refused, and
the refusal carries what wrote the index, what this build expects and
the command that rebuilds it as fields, so a model acting on it does
not have to parse prose. Those fields sat at the top level of the
tool result, next to content and isError.

The MCP result type has no such fields. An SDK that parses the result
into its CallToolResult drops them, and clients are not expected to
look there. _meta is the result's slot for extra data, so the same
fields move under it: index_stale, written_by, expected and command.
The text, including the rebuild command, is unchanged.

The README documents the new location.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 29, 2026
@12458
12458 marked this pull request as ready for review November 29, 2026 16:06
semcode-mcp spoke MCP through its own JSON-RPC loop: it parsed lines
from a stdin thread, routed methods, answered initialize with a fixed
protocol version, and drained admitted requests at end of input. Every
protocol revision meant changing that loop by hand.

The server now runs on rmcp, the official Rust SDK. rmcp owns framing,
the handshake, version negotiation and request routing; semcode keeps
the parts that are its own:

- McpHandler maps rmcp's tools/list and tools/call onto McpServer. It
  converts the tool schemas once at startup and each tool's JSON
  result into a CallToolResult, so isError and _meta reach the client
  as before.
- Admission stays in call_tool. At most --max-concurrent-requests tool
  calls run at once; a call beyond that is refused at once with error
  -32001 and "retryable": true instead of queueing. Other requests
  never take a slot.
- McpServer hands over each tool call as a future; McpHandler polls it
  on a blocking worker, since tools mix synchronous work with futures
  that are not Send. The worker owns the call's slot, so the slot is
  free again only once the work has stopped. When the client cancels,
  the worker drops the call at its next await, as MCP asks a receiver
  to stop work on a cancelled request; work between awaits runs to the
  end.
- rmcp logs a failed response write and keeps reading, so a client
  that closed our stdout but not our stdin would leave the server
  running with nowhere to answer. CancelOnWriteError wraps stdout and
  cancels the service on the first failed write.
- main builds its runtime and ends with shutdown_background. When
  stdin is a socket, as Node clients spawn servers, rmcp reads it on a
  blocking thread that a closed session cannot interrupt, and dropping
  the runtime would wait for it forever, as it would for a tool still
  running after the session ended.

Other behaviour changes:

- initialize answers with the client's version where rmcp supports it,
  instead of always 2024-11-05. serverInfo reports the crate version.
- At end of input, responses to calls already running are written for
  up to five seconds, then the server exits without waiting for the
  rest. Input closing before initialize is a clean exit.
- Pending output is no longer bounded. The old loop wrote replies
  inline and held a tool's slot until its reply was written, so a
  client that stopped reading stalled the server's reads. rmcp keeps
  reading and queues every reply, and the slot is released when the
  worker returns, so a client that stops reading stdout while it keeps
  sending requests grows the server's memory. A stdio client is the
  parent process, and the README documents the limit.
- The session lifecycle and message handling are rmcp's: before
  initialize, ping is answered, a request carrying the 2026-07-28
  per-request metadata is served, other requests are refused with
  -32602, and a notification ends the server. Lines that are not
  valid JSON-RPC are ignored rather than answered with -32700, a
  tools/call without a name or with non-object arguments is refused
  with -32601, and a line over 16 MiB is dropped without a reply.

Background indexing no longer sends notifications/message progress
lines. MCP logging is deprecated (SEP-2577) and rmcp marks it so;
progress stays available from the indexing_status tool and on stderr.

McpHandler takes its tool runner as a function, so the protocol tests
drive a real rmcp session over an in-memory pipe with tools they can
hold, at an await or in synchronous work: the overload error, a
cancelled call freeing its slot once dropped, a cancelled call keeping
its slot while its synchronous work runs, and error results keeping
their _meta. The lazy and full tool lists go through McpHandler::new,
so their schemas are checked to convert. tests/mcp_stdio.rs runs the
binary: initialize with 2024-11-05 and 2025-06-18, initialize,
tools/list and a list_branches call against an indexed repository, a
clean exit when input closes before initialize, and a clean exit when
stdout breaks while stdin stays open, over pipes and over sockets.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant