MCP (tracing)

Model Context Protocol (MCP) is an open standard for connecting AI applications to tools, prompts, and external context. Respan gives you full observability over MCP client requests, server tool handlers, resource reads, prompt reads, and tool outputs, with optional gateway routing for LLM calls made inside your MCP application.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See MCP (Model Context Protocol) gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai openinference-instrumentation-mcp mcp
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import asyncio
2from mcp import ClientSession, StdioServerParameters
3from mcp.client.stdio import stdio_client
4from respan import Respan
5from openinference.instrumentation.mcp import MCPInstrumentor
6
7respan = Respan(
8 app_name="mcp-python-client",
9 instrumentations=[MCPInstrumentor()],
10)
11
12
13async def main():
14 server_params = StdioServerParameters(
15 command="python",
16 args=["your_mcp_server.py"],
17 )
18
19 async with stdio_client(server_params) as (read, write):
20 async with ClientSession(read, write) as session:
21 await session.initialize()
22
23 tools = await session.list_tools()
24 print(f"Available tools: {[tool.name for tool in tools.tools]}")
25
26 result = await session.call_tool(
27 "summarize_city",
28 arguments={"city": "Paris"},
29 )
30 print(result)
31
32 respan.flush()
33
34
35asyncio.run(main())
4

View your trace

Open the Traces page to see your MCP workflow with client operations, server tool handlers, resource reads, prompt reads, inputs, outputs, and timing.

Configuration

ParameterTypeDefaultDescription
api_key / apiKeystr | None / string | undefinedRESPAN_API_KEYRespan API key used to export traces.
base_url / baseURLstr | None / string | undefinedRESPAN_BASE_URLOptional Respan API base URL.
app_name / appNamestr | None / string | undefinedNone / undefinedService name shown on exported spans.
instrumentationslist / array[]Plugin instrumentations to activate, such as MCPInstrumentor() or new MCPInstrumentor().
customer_identifierstr | NoneNoneDefault customer identifier for Python spans.
metadatadict | None / objectNone / undefinedDefault metadata attached to exported spans.
environmentstr | None / string | undefinedNone / undefinedEnvironment tag, such as "production" or "test".

MCPInstrumentor options

ParameterTypeDefaultDescription
captureClientOperationsbooleantrueCapture MCP client methods including connect, tools, resources, and prompts.
captureServerToolsbooleantrueCapture McpServer.registerTool() and McpServer.tool() handlers.
clientModuleobject | undefinedAuto-importedOptional explicit @modelcontextprotocol/sdk/client/index.js module reference.
serverModuleobject | undefinedAuto-importedOptional explicit @modelcontextprotocol/sdk/server/mcp.js module reference.

Attributes

In Respan()

Set defaults at initialization — these apply to exported spans.

1from respan import Respan
2from openinference.instrumentation.mcp import MCPInstrumentor
3
4respan = Respan(
5 app_name="mcp-api",
6 instrumentations=[MCPInstrumentor()],
7 customer_identifier="user_123",
8 metadata={"service": "mcp-api", "version": "1.0.0"},
9)

With propagate_attributes

Override per request using a context scope.

1from respan import Respan, propagate_attributes
2from openinference.instrumentation.mcp import MCPInstrumentor
3
4respan = Respan(instrumentations=[MCPInstrumentor()])
5
6
7async def handle_request(user_id: str, tool_name: str, args: dict):
8 with propagate_attributes(
9 customer_identifier=user_id,
10 thread_identifier="conv_abc_123",
11 metadata={"plan": "pro"},
12 ):
13 result = await session.call_tool(tool_name, arguments=args)
14 print(result)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.