# How to monitor MCP agents with OpenTelemetry: tool call spans, attributes, latency and errors

> Source: <https://dev.to/spanobserver/how-to-monitor-mcp-agents-with-opentelemetry-tool-call-spans-attributes-latency-and-errors-26bp>
> Published: 2026-11-21 07:30:16+00:00

**Recommendation:** Instrument MCP servers with OpenTelemetry spans per tool call and propagate traceparent in params._meta, then alert on latency and error.type rather than process health.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

You need to monitor MCP agents with OpenTelemetry as soon as an AI assistant calls your tools in production. An agent that answers slowly, picks the wrong tool or chains ten calls where two would do raises no classic alert: the process is running, the port responds, and yet the experience degrades and the bill climbs.

The good news is that an MCP tool call is nothing more than a distributed operation: a client sends a request, a server processes it, possibly by calling other services. Distributed traces, which SRE teams already know well, apply directly, provided you choose the right span names and the right attributes.

This article shows how to trace an MCP server and agent, which attributes to track, and how to turn them into actionable latency and error indicators.

In an MCP architecture (described in our [guide to the Model Context Protocol](https://span.observer/en/model-context-protocol-explained/)), a single user question can trigger a cascade: a call to the model, the choice of a tool, a request to the MCP server, then calls from the server to a database, an API or an observability platform. Without traces, each link is a black box.

OpenTelemetry brings three things: a standard, vendor-neutral format; shared semantic conventions for naming generative AI operations; and context propagation, which links the agent, the MCP server and downstream services in a single trace. It is the same logic as the one described for [end-to-end OpenTelemetry](https://span.observer/en/opentelemetry-end-to-end-dynatrace/), applied to a new type of call.

A span describes an operation: a name, a type (*kind*), a start and an end, a status, attributes and possibly events. For MCP, the OpenTelemetry GenAI semantic conventions, still at « development » status, offer a precise vocabulary.

The recommended name follows the form `{MCP method} {target}`, for example `tools/call chercher_runbooks`. It contains the tool name, never its arguments or an identifier, so that calls group cleanly in analysis views.

`mcp.method.name`: the JSON-RPC method (` tools/call`, `resources/read`…);` gen_ai.operation.name` = `execute_tool` and `gen_ai.tool.name`: these let you treat an MCP call like any other agent tool call;`mcp.protocol.version`, `mcp.session.id` and `jsonrpc.request.id` for diagnosis;`mcp.resource.uri` for resource reads;`error.type` on failure.
Tool arguments and results also exist as optional (*opt-in*) attributes. They are invaluable for debugging, but may contain personal data or secrets: only enable them knowingly.

HTTP trace context is not enough: a single HTTP request can carry several MCP messages, and stdio has no headers. The conventions therefore inject `traceparent` (and optionally `baggage`) into the `params._meta` field of each MCP request. The server extracts it and attaches its span to the client’s span.

Good news if your server uses the official Python SDK version 2: it already emits a SERVER span for each message received, with the attributes above. It depends only on the OpenTelemetry API; as long as no SDK is installed, these spans cost almost nothing. To collect them, simply add the SDK and an OTLP exporter:

```
pip install opentelemetry-sdk opentelemetry-exporter-otlp
```

For other SDKs and frameworks, check their documentation to see whether instrumentation exists or whether you need to create these spans yourself.

Standard configuration uses environment variables. To send directly to Dynatrace, you need the environment’s OTLP endpoint and an API token allowed to ingest traces; check the exact URL and scope in the documentation:

```
export OTEL_SERVICE_NAME=runbooks-mcp
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=https://YOUR-ENV.live.dynatrace.com/api/v2/otlp
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token%20<TOKEN>"
```

In the code, initialise the tracer provider at startup, before launching the server. Be careful with the stdio transport: never use a console exporter that would write to standard output, which is reserved for the protocol.

``` python
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 BatchSpanProcessor

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
```

On the agent side, wrap each reasoning turn in a parent span. Tool calls made inside it attach to it, and the SDK propagates the context to the server when both sides use it:

``` python
tracer = trace.get_tracer("agent-astreinte")

async def traiter_question(client, question: str):
    with tracer.start_as_current_span("invoke_agent agent-astreinte") as span:
        span.set_attribute("gen_ai.operation.name", "invoke_agent")
        resultat = await client.call_tool(
            "chercher_runbooks", {"mots_cles": question}
        )
        span.set_attribute("agent.outils_appeles", 1)
        return resultat
```

The `agent.outils_appeles` attribute is an example of a custom attribute: prefix your custom attributes so they are not confused with the official conventions.

A tool call can fail in two ways: an execution error returned to the model (the result carries an error flag and the model can retry) or a JSON-RPC protocol error. In both cases, the server span must switch to error status with `error.type` set. Test this deliberately with an invalid argument and check what arrives in your backend.

Once the spans are in Grail, a single DQL query is enough to track latency and errors per tool. Field names depend on ingestion; adapt them to what you see in your traces:

```
fetch spans, from: now() - 24h
| filter gen_ai.operation.name == "execute_tool"
| summarize appels = count(),
            erreurs = countIf(isNotNull(error.type)),
            p95 = percentile(duration, 95),
            by: { gen_ai.tool.name }
| sort appels desc
```

From there, set simple objectives: 95th percentile latency per tool, error rate, average number of tool calls per question. A sudden rise in the last one often signals an agent going round in circles.

No, they are at « development » status and have been moved to a repository dedicated to the GenAI conventions. Attribute names may still change.

Ideally both: the client shows the agent’s path, the server shows the real cost of each tool. Context propagation links them.

With the official SDK version 2, spans are created automatically; you still need to install an OpenTelemetry SDK and an exporter. See also our tutorial on [building an MCP server in Python](https://span.observer/en/build-mcp-server-python/).

An MCP agent does not escape the rules of observability: every tool call deserves a span, with a stable name, standard attributes and a reliable status. By relying on the OpenTelemetry conventions and on the [telemetry built into the Python SDK](https://py.sdk.modelcontextprotocol.io/run/opentelemetry/), you quickly get latency, errors and agent behaviour in the same views as the rest of production.

Further reading: [OpenTelemetry series: Anatomy of an OTel span](https://www.dynatrace.com/news/blog/opentelemetry-series-anatomy-of-an-otel-span/) (Dynatrace blog).

*span.observer is an independent community blog, not affiliated with Dynatrace LLC or the projects mentioned. Trademarks belong to their respective owners. Tools change quickly: check commands and options in their official documentation.*

*Originally published on [span.observer](https://span.observer/en/monitor-mcp-agents-opentelemetry/). French version: [https://span.observer/fr/observer-agents-mcp-opentelemetry/](https://span.observer/fr/observer-agents-mcp-opentelemetry/).*
