Skip to main content

Module mcp_client

Module mcp_client 

Source
Expand description

MCP Streamable HTTP client for backends configured with transport = "mcp" (the default for [[backends]]).

The gateway speaks to each backend the way a conforming MCP client does (Streamable HTTP transport, 2025-11-25):

  • every POST carries Accept: application/json, text/event-stream;
  • the first request of a backend session is initialize, followed by notifications/initialized; a returned MCP-Session-Id is sent on every later request together with MCP-Protocol-Version;
  • a 404 on a request that carried a session id means the backend ended the session: the client starts a new one and retries once (the backend did not process the request, so the retry cannot run a tool twice);
  • a request may be answered with one JSON object or with an SSE stream. The stream is decoded incrementally and reading stops at the JSON-RPC response whose id matches the request. Server-to-client requests that arrive first are answered (ping with an empty result, anything else with “method not found”), because the gateway declares no client capabilities and a waiting backend would otherwise hang until the deadline. A stream that ends early after carrying an event id is resumed with GET and Last-Event-ID.

Backend sessions are kept per (agent session, backend) pair so state a backend keeps per session never leaks between agents. The table is bounded; an evicted entry only costs a fresh initialize.

Structs§

BackendEndpoint
Everything needed to talk to one backend endpoint.
BackendSession
A negotiated backend session.
McpBackendClient
Client for MCP backends, holding the bounded backend-session table.
SseDecoder
Incremental decoder for text/event-stream bodies, following the WHATWG event-stream interpretation rules that MCP relies on.

Enums§

McpError
Why a backend exchange failed. The distinction matters to the caller: only McpError::NotDelivered proves the backend never processed the request, so only that failure may release a claimed tool execution.

Constants§

LATEST_PROTOCOL_VERSION
The version offered in initialize and preferred in negotiation.
PROTOCOL_VERSION_HEADER
Header carrying the negotiated protocol version on later requests.
SESSION_ID_HEADER
Header carrying the backend-assigned (or gateway-assigned) session id.
SUPPORTED_PROTOCOL_VERSIONS
Protocol versions this gateway implements, newest first.

Functions§

is_supported_version
True for a protocol version string this gateway implements.