Skip to content
GitHubLog inStart free
cetra is not released yet. These docs describe the development version.

MCP

cetra reads the spans that the MCP Python SDK writes for each request, with a server’s tool call as a tool span that joins the client’s trace.

Install MCP 2.x, the OpenTelemetry SDK and its OTLP exporter. The fixture was captured with MCP 2.3.0.

Terminal window
pip install "mcp>=2" opentelemetry-sdk opentelemetry-exporter-otlp-proto-http

The MCP SDK creates spans through the global tracer provider. Each process that runs a client or a server sets its own provider, so each one exports its own spans. The client and the server in the fixture ran in separate processes.

Set the exporter variables for each process:

Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8080
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer <SK_CETRA_KEY>"

Replace <SK_CETRA_KEY> with your sk-cetra-… API key. See Send OpenTelemetry.

from mcp.server.mcpserver import MCPServer
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
server = MCPServer("weather")
@server.tool()
def get_weather(city: str) -> str:
"""Look up the weather for a city."""
return f'{{"temperature_c": 18, "conditions": "sunny", "city": "{city}"}}'
if __name__ == "__main__":
server.run("stdio")
import asyncio
import os
import sys
from mcp import Client
from mcp.client.stdio import StdioServerParameters
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
params = StdioServerParameters(
command=sys.executable,
args=["server.py"],
# The stdio transport starts the server with only the environment given here.
env={k: v for k, v in os.environ.items() if k in ("PATH", "HOME") or k.startswith("OTEL_")},
)
async def main() -> None:
async with Client(params) as client:
await client.call_tool("get_weather", {"city": "Paris"})
provider.force_flush()
asyncio.run(main())

The server exports each span as it ends, so its spans arrive even when the client closes it first. Use a span processor that exports at once, as the samples do.

import cetra
cetra.init(instrument=["mcp"])

The mcp instrument in cetra-sdk installs with pip install "cetra-sdk[mcp]". It uses OpenInference’s MCP instrumentation, openinference-instrumentation-mcp 2.1.5. It creates no spans. It carries the trace context to the server, so spans that the server process makes, with its own instrumentation or with spans you write, join the client’s trace. On mcp<2 the MCP SDK makes no spans itself. On mcp>=2 the SDK passes the context itself, and the instrument is not needed. In a spike with mcp>=2 the instrument logged a harmless ValueError and had no effect. No fixture backs the instrument: the fixture above was captured without it.

One call_tool makes a trace with two spans that share it:

  • a span for the client’s send, MCP send tools/call get_weather;
  • a tool span for the server’s tools/call get_weather, a child of the client’s span, with the tool’s name and the operation execute_tool.

The scenario also lists the tools, so the fixture has more spans. Each request is a trace of its own. The SDK sends more requests than the scenario makes: server/discover and a second tools/list. Each of them is a span on both sides.

  • The SDK records no arguments and no results. The spans have no input and no output, so the tool’s content is not in cetra.
  • The mcp instrument creates no spans, and it is not needed on MCP 2.x. On MCP 1.x there are no MCP spans at all.
  • Each process exports its own spans, so a server process needs its own provider.
  • The fixture was captured with MCP 2.3.0 over stdio. Streamable HTTP and the other transports were not captured.
  • If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.