Skip to content
mentoor

Topic 2 of 4 · 5 min read

Tracing and logging MCP tool calls

Goal: Design a tool-call log record and find the missing correlation field or the leaked secret in a sample trace.

Record one structured line per tool call that ties it to the agent's trace, names the tool, and shows the outcome, while keeping secrets and personal data out of it. When an agent run goes wrong, this is the only way to answer "what exactly did it call, and what came back".

Check yourself · 1 of 3

Where does MCP carry OpenTelemetry trace context in a request?

Choose one answer

Why it matters

An agent run is a chain: a user message, a model step, three tool calls, another model step. If the MCP server logs only "request received", you cannot tell which run caused a slow query or a bad write. Production MCP servers are called by many agents at once, so uncorrelated logs are close to useless.

Pass the trace context in

MCP reserves the request _meta keys traceparent, tracestate and baggage for OpenTelemetry trace context. Their values follow the W3C Trace Context format. The client puts its current span's context there, and your server starts its span as a child of it. In your own code, read _meta.traceparent on each request and continue the trace. Do not generate a new trace id when one arrives.

OpenTelemetry also defines MCP attribute names, such as mcp.method.name. Use the standard names so traces from different servers can be queried together.

What to record per call

  1. Correlation: trace id, span id, request id.
  2. What was called: method (tools/call), tool name, server and tool version.
  3. Who: a stable user or client id, never a raw token.
  4. Result: status (ok, tool error, protocol error), error code, duration in milliseconds.
  5. Size: argument and result sizes in bytes, not the content.

Record the content of arguments only for tools where you have decided it is safe, and only after redaction.

Before and after

Before:

{"level":"info","msg":"tools/call get_invoice","args":{"invoice_id":4411,"api_key":"sk_live_...redacted"}}

No trace id, no duration, no outcome, and a credential in the log.

After:

{"ts":"2026-12-02T09:14:07Z","trace_id":"4bf92f3577b34da6a3ce929d0e0e4736","span_id":"00f067aa0ba902b7","mcp.method.name":"tools/call","tool":"get_invoice","tool_version":"3.2.0","client_id":"agent-billing-7","status":"ok","duration_ms":182,"result_bytes":1240}

Now you can open the whole agent trace from the trace id, see how long the call took, and nothing sensitive is stored.

What goes wrong

  • Starting a new trace on every request, so the agent run and the tool calls never connect.
  • Logging arguments and results in full. Tool results often hold customer data. Log sizes and hashes, and redact known secret fields before writing.
  • Counting only protocol errors. An MCP tool can fail with isError: true and still return a normal response. If you only count HTTP or JSON-RPC failures, tool failures stay invisible.
  • High-cardinality metric labels. A tool name is a good label. A user id or invoice id is not; put those in logs.

Examples

The trace that stops at the agent

The agent framework shows a 9-second run. The MCP server logs show a 7-second search_orders call, but with its own random trace id. Nothing links them. Fix: read _meta.traceparent, start the server span as its child, and put the same trace id in the log line.

A leaked key in a debug log

A developer logs params.arguments to debug a failing tool. One tool takes an api_key argument. The key is now in the log store, with the retention and access rules of logs. Fix: redact by field name before writing, and remove secrets from tool arguments where the server can read them from its own config instead.

Key points

  • Log one structured record per tool call with trace id, tool name, caller id, status and duration.
  • MCP reserves traceparent, tracestate and baggage in request _meta for W3C trace context, so continue the incoming trace instead of starting a new one.
  • Use OpenTelemetry's standard MCP attribute names so traces from different servers can be queried together.
  • Record sizes and outcomes by default; log argument or result content only after redaction and only where it is justified.
  • Tool failures can arrive as a normal response with isError set, so count them separately from transport errors.

Common mistakes

  • Creating a new trace id for every request instead of continuing the one in _meta.
  • Logging full arguments and results, which puts secrets and customer data in the logs.
  • Treating a 200 response as success when the tool result has isError set.
  • Using user ids or record ids as metric labels.

Check yourself · 2 of 3

A server log line reads: tools/call get_invoice, args {invoice_id: 4411, api_key: sk_live_...redacted}. What is the main fix?

Choose one answer

Check yourself · 3 of 3

Your error dashboard counts only HTTP 5xx and JSON-RPC errors. Users say a search tool fails often, but the dashboard is flat. Why?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic