You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: implement mcp.server.operation.duration and mcp.server.session.duration metrics
Implements the two server-side OTel metrics defined in the MCP semantic
conventions (https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp), as
discussed in #421.
Changes:
- src/mcp/shared/_otel.py: add a meter and two histograms with the
spec-mandated explicit bucket boundaries
[0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300].
Expose record_server_operation_duration() and
record_server_session_duration() helpers.
- src/mcp/server/lowlevel/server.py: record mcp.server.operation.duration
in _handle_request() on all exit paths (success, MCPError, cancellation,
transport close, raise_exceptions=True). Attributes recorded:
mcp.method.name (required), error.type, rpc.response.status_code,
gen_ai.tool.name + gen_ai.operation.name (tools/call),
gen_ai.prompt.name (prompts/get), mcp.protocol.version.
- src/mcp/server/session.py: record mcp.server.session.duration in
__aexit__(). Session start time is captured in __aenter__() (not
__init__()) so the clock starts when the session is actually active.
Cancellation exceptions (transport close) are not treated as errors.
- tests/shared/test_otel.py: extend test_client_and_server_instrumentation
to assert both metrics are emitted with correct attributes and units.
Transport attributes (network.transport, network.protocol.name,
network.protocol.version) are not set. ServerSession does not currently
know the transport type — adding it would require a new constructor
parameter and a corresponding API change. This can be addressed in a
follow-up once the transport kind is plumbed through.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
0 commit comments