Skip to content

Architecture

github-actions[bot] edited this page Oct 18, 2026 · 5 revisions

Architecture

            ┌──────────────────────── mcp-exporter ────────────────────────┐
client ───► │ transport (stdio pipes | HTTP reverse proxy + SSE observer)  │ ───► MCP server
            │        │ every frame (both directions)                       │
            │        ▼                                                     │
            │  jsonrpc.Parse ─► Tracker.Frame ─► spans · metrics · logs   ─┼──► OTLP / Prometheus
            └──────────────────────────────────────────────────────────────┘

Frames and correlation

  • Framing: stdio frames are newline-delimited, and lines of any length are supported. HTTP request bodies are read in full, up to 16 MiB. JSON responses are buffered as they stream to the client. SSE responses are parsed event by event as the bytes pass.
  • Parsing: each frame is parsed as a single message or a batch. A frame that isn't valid JSON-RPC is forwarded unchanged and counted.
  • Correlation key: a request opens a pending entry keyed by (scope, direction, id), and the response travelling the other way closes it.
    • The scope is the id space: the stdio pipe, the HTTP session (Mcp-Session-Id), or, before a session exists (initialize), the single HTTP exchange.
    • 2026-07-28 is stateless: there's no session, and each request is answered on its own response stream. Each exchange is therefore its own scope. When an exchange's response ends with a request still open, that request ends at once: cancelled if the stream closed, or the HTTP status if the upstream failed (502 if unreachable). With a session (up to 2025-11-25), a response may come on another stream, so those requests wait for it or for the idle sweep.
    • Legacy HTTP+SSE (2024-11-05) has no session header, and responses arrive on the GET /sse stream, not on the POST. The stream's endpoint event names the POST URL. Its query (sessionId, or session_id in the Python SDK) becomes the session and scope for that stream and for POSTs to exactly that URL, and the session ends when the stream closes. Query strings the exporter hasn't learned this way are ignored. Gateway mode doesn't support legacy servers: their endpoint path has no route prefix, and the exporter never rewrites forwarded bytes. Give each legacy server its own exporter.
    • This scoping is what lets responses arrive on a different stream than the request: a resumed GET stream, or server-initiated requests answered by a later POST.
  • Other endings: notifications/cancelled closes the entry as cancelled. Entries left open longer than -session-idle are closed as timeout, and idle sessions and tasks are dropped. That bounds memory when clients vanish.

What the proxy changes, and what it doesn't

The proxy observes and doesn't rewrite, with these documented exceptions:

Change Why Opt out
traceparent (+ tracestate/baggage) added to params._meta of client requests that have none so instrumented servers continue the trace -inject-context=false
upstream requests get Accept-Encoding: identity (http mode) compressed responses can't be observed; the sidecar hop is local
gzip request bodies are forwarded decompressed (http mode) so they can be observed and injected; capped at 16 MiB
  • Re-encoding: when injection re-encodes a request, ids and arguments are preserved byte for byte in meaning, with no HTML escaping. params that aren't an object (array or null) are never touched.
  • SSE: responses are forwarded byte for byte. That property is fuzz-tested under arbitrary read chunking.

stdio specifics

In stdio mode, stdout is the protocol channel. Two layers keep it clean:

  • All platforms: console exporters (OTEL_*_EXPORTER=console) are built with an explicit stderr writer.
  • Linux and macOS: the exporter also duplicates the real stdout for protocol frames, then points file descriptor 1 at stderr (dup2), so even a stray print from a library lands on stderr. Windows has no equivalent for already-open files.

The server's stderr is passed through and also exported as log records.

Tested with

Each combination below was run end to end through the exporter, checking spans for correct correlation, error classification, session, protocol version and server name. The table is updated when a combination is added.

Client Server Transports
TypeScript SDK (server-everything) / curl TypeScript SDK server-everything Streamable HTTP (CI smoke test), legacy HTTP+SSE
Python SDK 1.30 ClientSession Python SDK 1.30 FastMCP Streamable HTTP, legacy HTTP+SSE (session_id), stdio
curl, raw 2026-07-28 Python SDK 2.3 MCPServer (protocol 2026-07-28) Streamable HTTP (CI smoke test), stdio; includes server/discover and subscriptions/listen
Go cmd/demo-agent TypeScript SDK server-everything Streamable HTTP, with MCP Tasks (CI smoke test)
Claude Desktop config via mcp-remote mcp.deepwiki.com stdio client mode

Code map

Package Responsibility
internal/jsonrpc parsing and encoding; params._meta and related-task extraction; SetMeta
internal/observe OTel provider setup (autoexport), the error handler, and Tracker: correlation, spans, metrics, logs, redaction, tasks, sweeping
internal/proxy stdio relay; HTTP reverse proxy with JSON/SSE observers, gzip handling, /healthz, /readyz
cmd/mcp-exporter CLI, stdout reservation, pprof listener, TLS

Robustness: the parser, the SSE observer, bounded gunzip and Tracker.Frame all have Go fuzz targets that CI runs on every PR. Fuzzing found and fixed a crash on "_meta": null in v0.3.1.

Clone this wiki locally