Skip to content

Adding Opentelemetry to MCP SDK #421

Description

@fali007

Is your feature request related to a problem? Please describe.

I would like to see Opentelemetry traces and Metrics baked into SDK.
E.g.

  1. Traces emitted on all Requests from client and server side of MCP SDK.
  2. Metrics for tool/prompt/resource calls

Describe the solution you'd like

I am trying to use Opentelemetry sdk to add traces and metrics features.

Steps

  1. Creating session in client and server.
  2. Initialise tracer (Opentelemetry tracer instances) in BaseSession (src/mcp/shared/session.py).
  3. Binding trace context (traceparent) to _meta of RequestParams.Meta before sending request from client (send_request in src/mcp/shared/session.py)
  4. In server use the traceparent from received request to create new span with incoming span as parent span.

Describe alternatives you've considered

I tried to implement the same via traceloop. I was not able to get correct parent - child relationship between spans while doing so.

Additional context

I am able to achieve distributed tracing across my Agentic application and MCP server.

Image

Activity

  1. hk-bmi commented on Apr 3, 2025

    @hk-bmi

    related Issue for feature request in traceloop/openllmetry to enable observability for MCP Server, traceloop/openllmetry#2662

  2. wenxuwan commented on Apr 7, 2025

    @wenxuwan

    I'm now trying to inject a tracer for each request by defining the SofaTracerMiddleware(which inherits BaseHTTPMiddleware) and accessing the above plugin for the mcp server application via add_middleware.

    class SofaTracerMiddleware(BaseHTTPMiddleware):
        def __init__(self, app):
            super().__init__(app)
    
        @property
        def tracer(self):
            return opentracing.tracer
    
        async def dispatch(self, request, call_next):
            headers = request.headers
            # Extract the OpenTracing context from the incoming request headers
            try:
                input_context = self.tracer.extract(format=Format.HTTP_HEADERS, carrier=headers)
            except opentracing.InvalidCarrierException:
                input_context = None
            # Start a new span for the incoming request
            with self.tracer.start_active_server_span(SPAN_CODE_HTTP_SERVER, context=input_context) as scope:
                span = scope.span
                span.url = str(request.url)
                span.method = request.method.upper()
                span.request_size = request.headers.get("content-length", 0)
    
                response = await call_next(request)
    
                span.result_code = str(response.status_code)
                span.response_size = response.headers.get("content-length", 0)
                current_context = _SCOPE.get()
                if current_context:
                    print(f"Current tracer in worker: {current_context}")
                else:
                    print("No active tracer in this context.")
            return response
    
    app = FastMCP()
    app.sse_app().add_middleware(SofaTracerMiddleware)

    But I found that the requests about message used the same tracer from the initialise method with url messages, and the subsequent initialize and tools methods didn't use the new tracer

  3. wenxuwan commented on Apr 8, 2025

    @wenxuwan

    Image

    Image

    It is also clear from the logs that the ContextVarsScope used for opentracing each time a request is processed is that of the first request, even though handle_post_message and SofaTracerMiddleware use the latest ContextVarsScope for each request.

    Image

    So now do I have a way to get my own async ContextVarsScope for each request?

  4. fali007 commented on Apr 8, 2025

    @fali007
    Author

    Hi @wenxuwan,
    I tried your approach and I don't think it will work because if you choose http header to propagate trace context, only the initialize request has http header. That's why you see the same parent for all subsequent requests as well.

    I was tinkering with the traceloop way of passing context by intercepting and wrapping over _handle_request function in mcp.server.lowlevel.server.py for server side and send_request function in mcp.shared.session.py for client side tracing. I am trying to pass traceparent in params.meta.traceparent of the RequestParams from types.py.
    I am able to map correct parent - child span relationship.

    Screenshot shows how the context is propagated from Agent application to MCP servers back and forth. Here parent - child span ID is correct.
    Image

  5. wenxuwan commented on Apr 8, 2025

    @wenxuwan

    Hi @wenxuwan, I tried your approach and I don't think it will work because if you choose http header to propagate trace context, only the initialize request has http header. That's why you see the same parent for all subsequent requests as well.

    I was tinkering with the traceloop way of passing context by intercepting and wrapping over _handle_request function in mcp.server.lowlevel.server.py for server side and send_request function in mcp.shared.session.py for client side tracing. I am trying to pass traceparent in params.meta.traceparent of the RequestParams from types.py. I am able to map correct parent - child span relationship.

    Screenshot shows how the context is propagated from Agent application to MCP servers back and forth. Here parent - child span ID is correct. Image

    You are right, but not quite the same as my problem. Right now I'm not passing the tracer inside my http request. every time I make a request

    with self.tracer.start_active_server_span(SPAN_CODE_HTTP_SERVER, context=input_context) as scope 
    

    The new contextVar is generated inside the current async, but now only one async has been processing the user's request, so the tracer is the Contextvar of the first request. That's why handle_post_message gets the newest tracer every time, but when processing the request is using the tracer of the first request.

  6. samsp-msft commented on Apr 28, 2025

    @samsp-msft

    Adding some links:
    modelcontextprotocol/modelcontextprotocol#246 - request to add tracing to MCP spec
    modelcontextprotocol/modelcontextprotocol#414 - fix for the spec to use params._meta for trace context propagation such as params._meta.traceparent
    open-telemetry/semantic-conventions#2083 - semantic convention proposal for MCP traces and metrics.

  7. codefromthecrypt commented on May 1, 2025

    @codefromthecrypt

    for the context propagation part, you can have a look at @anuraaga's code in openinference or if such a change is welcome maybe he can help raise it. I don't know the SDK specific policies about an otel dependency, but as python is flexible with imports maybe it is fine to just add it directly?

  8. self-assigned this
    on Aug 12, 2025
  9. alexmojaki commented on Oct 6, 2025

    @alexmojaki

    Workaround for the problem of ContextVars being lost in the async streams preventing other forms of context propagation from working: pydantic/logfire#1459

  10. added
    enhancementRequest for a new feature that's not currently supported
    ready for workEnough information for someone to start working on
    P3Nice to haves, rare edge cases
    and removed on Oct 6, 2025
  11. 5 remaining items

  12. anuraaga commented on Nov 28, 2025

    @anuraaga

    Noticed this since happen to be mentioned here a long time ago but not a maintainer. But as a general instrumenter, want to note that API can't work unless

    • instrument-start returns a value that is passed to instrument_end
    • it's one instrument function accepting a next type of function

    Generally I'd recommend the latter. Langchain was stuck for going with the former with no return value. Now I can't open this link for whatever reason.

    https://github.com/langchain-ai/langchain/discussions/27954

  13. added a commit that references this issue on Nov 28, 2025
    c6bcdb5
  14. DylanRussell commented on Dec 10, 2025

    @DylanRussell

    OTel python uses a ContextVar to propagate the trace/span context. A ContextVar is supposed to be a per-thread and per-task variable.. Is the issue that the MCP library just uses 1 task per-connection instead of per-request, so we get the ContextVars from when the connection started ?

    Or is it possibly due to where the task is getting it's context from ? According to this a anyio.Task gets it's context from the anyio.Task that call's it's start method..

    Withasyncio.Task you can explicitly pass a Context..

  15. added
    v2Affects the v2 line (2.x on main)
    on Dec 10, 2025
  16. aabmass commented on Jan 13, 2026

    @aabmass
    Contributor

    I have a pretty minimal fix to propagate contextvars through the anyio streams by attaching a contextvars.Context to SessionMessage and restoring it in the worker tasks main...aabmass:mcp-python-sdk:propagate-contextvars.

    I'm happy to add more tests send a full PR if maintainers are onboard with this approach. The change is completely independent of OTel and would also benefit other users of contextvars.

  17. dgenio commented on Feb 28, 2026

    @dgenio

    Hi — I opened PR #1693 a while back with a pluggable instrumentation interface (token-based API) to address this issue. That PR is now stale due to significant refactoring in the codebase (_handle_request, ServerRequestContext, task support, etc.).

    I noticed this issue is assigned to @Kludex — are you already planning or working on an approach for this? I'm happy to:

    1. Rewrite my PR against the current codebase if the contribution is welcome
    2. Step aside if you have a different design in mind
    3. Collaborate on a shared approach

    Just want to avoid duplicating work. Let me know how you'd like to proceed.

  18. verdie-g commented on Apr 5, 2026

    @verdie-g

    I'm proposing an implementation for the server metrics in #2394

  19. MukundaKatta commented on May 10, 2026

    @MukundaKatta

    Strong agree on baking OTel in. The minimum useful spans are server-side request_handler (with tool/prompt/resource type as an attribute), client-side outgoing tool_call, and a transport-layer parent that links sessions to spans across the SSE/streamable-http boundary.

    Worth using the gen_ai semantic conventions for tool spans (gen_ai.tool.name, gen_ai.tool.call.id) so existing OTel collectors can correlate MCP traffic with the LLM call that triggered it. OpenInference's semconv overlaps but isn't the standardized one.

    Happy to draft a proposal if there's appetite.

  20. Kakarottoooo commented on Jun 5, 2026

    @Kakarottoooo

    I’m interested in taking a narrow, non-overlapping slice of #421.

    I noticed #2093 is already working on trace-context propagation through _meta, so I would avoid duplicating that work.

    A smaller first PR I can take:

    • add or document a lightweight request/tool lifecycle instrumentation surface;
    • avoid adding OpenTelemetry as a hard SDK dependency;
    • include tests using a fake recorder rather than a real exporter;
    • add an example showing how users can map MCP request/tool lifecycle events to OpenTelemetry spans externally.

    Would maintainers prefer this as:

    1. a small code-level hook surface on main / v2, or
    2. a docs/examples-only PR first?

    I’m happy to keep the first PR minimal and aligned with the existing v2 direction.

  21. Necmttn commented on Jun 21, 2026

    @Necmttn

    The minimum stable API should make OTel mapping possible without making OTel the dependency.

    I would expose request/tool/resource lifecycle events with a fake-recorder test suite: send_request, handle_request, tool_call, resource_read, prompt_get, success, and error. Each event should carry session id, request id, method, tool/resource name, and traceparent metadata when present.

    Separately, context propagation through anyio/contextvars needs a regression test. If context disappears across the task boundary, every higher-level span design becomes hard to reason about.


    Generated with ax.

  22. dgenio commented on Jun 22, 2026

    @dgenio

    This is close to the direction I was exploring in #1693.

    One detail I’d keep explicit in the API shape: lifecycle hooks need either to return a token/state from start and receive it again on end/error, or be modeled as a wrapper around the handled operation. Otherwise OTel span lifetime has to be tracked through side storage, which gets fragile once async/task boundaries are involved.

    I also agree that a fake-recorder test suite is the right first layer. It lets the SDK define stable lifecycle semantics without committing to OpenTelemetry as a hard dependency, while a separate adapter can map those events to OTel spans/metrics.

    And +1 on the contextvars/anyio regression test being a prerequisite. If context disappears across the stream/task boundary, every higher-level instrumenter becomes harder to reason about.

  23. HarperZ9 commented on Jun 29, 2026

    @HarperZ9

    I added a public synthetic fixture for the lifecycle-hook shape discussed here, mainly to make the fake-recorder requirement concrete without forcing OpenTelemetry as a hard dependency.

    The case is mcp_lifecycle_fake_recorder: send_request -> handle_request -> tool_call -> tool_result -> span_end, all joined by the same request id. The tool result explicitly carries mcp_is_error: true and
    esult_state: failed, so an implementation can prove that a failed MCP result does not become a successful span or persisted result by accident. The fixture also asserts that span lifetime needs a token/state that can survive async boundaries.

    Fixture: https://github.com/HarperZ9/telos/blob/e12425b/demo/integrations/agent-boundary-fixtures.json
    Verifier: https://github.com/HarperZ9/telos/blob/e12425b/demo/agent-boundary-fixtures.test.mjs

    This is not a conformance claim against the SDK; it is just a small receipt-shaped test corpus that might help keep the first instrumentation API narrow and fake-recorder-testable.

  24. maxisbey commented on Jul 25, 2026

    @maxisbey
    Contributor

    Hey everyone! As this issue has gotten quite long and open telemetry has been added into the v2 beta version of mcp I think it'd be best to go ahead and close this issue for now.

    I'd super appreciate if people go ahead and test the open telemetry integration we currently have, and then make new issues for feedback/suggestions/issues you run into going forward.

    More info here: https://py.sdk.modelcontextprotocol.io/v2/run/opentelemetry/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3Nice to haves, rare edge casesenhancementRequest for a new feature that's not currently supportedready for workEnough information for someone to start working onv2Affects the v2 line (2.x on main)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions