MCP (Model Context Protocol)

Model Context Protocol (MCP) is an open standard by Anthropic for connecting AI models with external data sources and tools. It provides a standardized way for LLM applications to access context from files, databases, APIs, and other services. Respan gives you full observability over every MCP session, tool invocation, resource access, and server-client message — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

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

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(instrumentations=[MCPInstrumentor()])
8
9async def main():
10 server_params = StdioServerParameters(
11 command="python",
12 args=["your_mcp_server.py"],
13 )
14
15 async with stdio_client(server_params) as (read, write):
16 async with ClientSession(read, write) as session:
17 await session.initialize()
18
19 tools = await session.list_tools()
20 print(f"Available tools: {[t.name for t in tools.tools]}")
21
22 result = await session.call_tool("your_tool", arguments={"query": "hello"})
23 print(result)
24
25 respan.flush()
26
27asyncio.run(main())
4

View your trace

Open the Traces page to see your MCP interactions with tool invocations, resource access, and server-client communication.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. MCPInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

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

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
6async def handle_request(user_id: str, tool_name: str, args: dict):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 result = await session.call_tool(tool_name, arguments=args)
13 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.