Skip to content

MCP servers and clients

When your agent calls tools on an MCP server, ctxflow can show the call on both sides as one call, with the same conversation and user. This page covers everything MCP: setup, what each MCP version records, and stdio servers. It applies to the MCP Python SDK.

  1. Install the extra in the agent and in every MCP server you own:

    Terminal window
    pip install "ctxflow-otel[mcp]==0.2.2"

    It works with mcp 1.24 or later, including 2.x. See Install sources.

  2. Start ctxflow in the agent and in each server, the same way as in the Python SDK page: ctxflow-instrument python server.py, or ctxflow_otel.init() at the top of the program. With attach(provider), call ctxflow_otel.mcp.instrument_mcp() yourself.

  3. In the agent, wrap each user turn in conversation(). Keep baggage in OTEL_PROPAGATORS (the default), because the identity travels in it.

  4. Point every server at ctxflow with its own OTEL_SERVICE_NAME and exporter variables. For a stdio server, see Stdio servers.

ctxflow then puts the trace context and the baggage in the MCP request (params._meta, for stdio and streamable HTTP), so the server’s spans join the agent’s trace and carry the same conversation and user.

mcp 2.x mcp 1.x
Spans The MCP SDK makes them: MCP send tools/call <tool> on the client and tools/call <tool> on the server. None of its own: your framework’s instrumentation (OpenInference, OpenLLMetry) or your own code makes the tool span.
Tool input and result ctxflow records them on the server span of tools/call: gen_ai.tool.call.arguments (the arguments as JSON) and gen_ai.tool.call.result (the text blocks of the result). From the instrumentation of the framework that calls the tool.

For mcp 2.x:

  • Tool text is recorded on the server side only. The client span (MCP send tools/call) carries no text. A server that does not run the ctxflow SDK sends no tool text, whatever the client does.
  • The server must keep the default span middleware of the MCP SDK (server.middleware).
  • Binary results (image, audio, blob) become a placeholder such as [image image/png 3000004 bytes]. A result that is still waiting for input records no result. Each value is cut at 64 KiB.
  • The text leaves as it is, and ctxflow keeps it or not according to the project’s Store text setting.

The agent side (MCP send tools/call) and the server side count as one tool call in ctxflow, so a call is not counted twice. See Tools.

The instrumentation replaces functions of the MCP SDK. With init() (not with ctxflow-instrument), call init() before you import the MCP client transports, or use them as module attributes (from mcp.client import stdio; stdio.stdio_client(...)). On a server, import mcp.server.mcpserver (mcp 2.x) or mcp.server.fastmcp (mcp 1.x) after init().

The MCP SDK starts a stdio server with a minimal environment, so the server does not know where to send its spans. ctxflow_otel.mcp.child_env("my-server") builds an environment that forwards every OTEL_* and CTXFLOW_* variable (the API key included: only use it for servers you trust), sets the server’s OTEL_SERVICE_NAME, and defaults OTEL_BSP_SCHEDULE_DELAY=200 and OTEL_EXPORTER_OTLP_TIMEOUT=1500. The MCP SDK closes the server’s stdin, waits 2 s and then stops it, so without short delays the last spans are lost.

from mcp import StdioServerParameters
from ctxflow_otel.mcp import child_env
params = StdioServerParameters(command="python", args=["server.py"], env=child_env("kb-server"))

The server itself starts with ctxflow-instrument python server.py (put it in command and args) or calls ctxflow_otel.init().

  • The baggage reaches every server you call, third-party ones too, user.id included. Do not put in conversation() what those servers must not see.
  • LlamaIndex agent. A complete setup with FunctionAgent and McpToolSpec: LlamaIndex agent with MCP tools.
  • Retrieval served by a tool. If a tool returns retrieved chunks, run the SDK in the server too, with a retrieval span around the search, so the chunks and their lineage marks reach ctxflow; see Retrieval in another process.
  • Check it. Run ctxflow-otel check (Verify your setup) on the agent host and on the server host, then open the turn: the tool call shows “Remote call: client and server side.” (Turn detail).
  • A skipped instrumentor. With mcp 1.x, openinference-instrumentation-mcp can be skipped because of a dependency conflict. ctxflow-instrument then logs a WARNING that names the instrumentor and the fix (Python SDK).