Skip to Content

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.

MCP does not require the Respan gateway. This integration traces MCP client and server traffic directly, regardless of which model provider your host application uses. If the host also makes LLM calls, you can route those separate calls through the gateway using the relevant provider or framework guide.

Set up Respan

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

Paste this into your coding agent:

Help me set up Respan tracing with the MCP integration. Follow https://www.respan.ai/docs/integrations/mcp.md

Setup

Install packages

Terminal
pip install respan-ai openinference-instrumentation-mcp mcp

Set environment variables

Terminal
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

RESPAN_API_KEY is used to export traces to Respan.

Initialize and run

Python
import asyncio from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from respan import Respan from openinference.instrumentation.mcp import MCPInstrumentor respan = Respan( app_name="mcp-python-client", instrumentations=[MCPInstrumentor()], ) async def main(): server_params = StdioServerParameters( command="python", args=["your_mcp_server.py"], ) async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools = await session.list_tools() print(f"Available tools: {[tool.name for tool in tools.tools]}") result = await session.call_tool( "summarize_city", arguments={"city": "Paris"}, ) print(result) asyncio.run(main())

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.

Python
from respan import Respan from openinference.instrumentation.mcp import MCPInstrumentor respan = Respan( app_name="mcp-api", instrumentations=[MCPInstrumentor()], customer_identifier="user_123", metadata={"service": "mcp-api", "version": "1.0.0"}, )

With propagate_attributes

Override per request using a context scope.

Python
from respan import Respan, propagate_attributes from openinference.instrumentation.mcp import MCPInstrumentor respan = Respan(instrumentations=[MCPInstrumentor()]) async def handle_request(user_id: str, tool_name: str, args: dict): with propagate_attributes( customer_identifier=user_id, thread_identifier="conv_abc_123", metadata={"plan": "pro"}, ): result = await session.call_tool(tool_name, arguments=args) 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.