Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Don't record operation duration for JSON-RPC responses - #2048

Open
sivaraman-krishnan wants to merge 3 commits into
modelcontextprotocol:mainfrom
sivaraman-krishnan:fix/2043-operation-duration-responses
Open

sivaraman-krishnan wants to merge 3 commits into
modelcontextprotocol:mainfrom
sivaraman-krishnan:fix/2043-operation-duration-responses

Conversation

@sivaraman-krishnan

@sivaraman-krishnan sivaraman-krishnan commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #2043

Problem

McpSessionHandler recorded mcp.server.operation.duration and mcp.client.operation.duration for JSON-RPC responses and errors, tagged mcp.method.name = unknownMethod. As a result, every request was counted twice.

The issue describes the server's send path, but the receive path had the same problem. HandleMessageAsync timed every incoming message, including responses routed to pending requests. So a client also recorded an unknownMethod data point for each response it received, on top of the real duration measured in SendRequestAsync.

Fix

Per the MCP semantic conventions, these metrics describe requests and notifications. A new IsOperation helper now gates the duration timer in both HandleMessageAsync and SendMessageAsync, so responses and errors are no longer recorded as operations.

  • Requests are still measured once per side: in SendRequestAsync on the sender and in HandleMessageAsync on the receiver. Notifications are measured on send and receive.
  • Traces are unchanged, because Diagnostics.ShouldInstrumentMessage already excludes responses.
  • This also brings the behavior in line with docs/concepts/stateless/stateless.md, which describes the metric as the duration of each request or notification.

Tests

Added DiagnosticTests.Session_OperationDuration_RecordsOnlyRequestsAndNotifications. It uses a MeterListener over an in-memory client/server pair. The test calls tools/list and tools/call, sends a request the server rejects with a JSON-RPC error, and sends a notification. It asserts that there are no unknownMethod measurements and exactly one client-side and one server-side measurement for each of those four.

The assertions run after RunConnected returns. By then the server's message processing has drained every in-flight handler on disposal, so no polling is needed.

  • The new test fails before the fix and passes after it on net10.0, net9.0, net8.0 and net472. It also passed 20 repeated runs.
  • The full ModelContextProtocol.Tests and ModelContextProtocol.AspNetCore.Tests suites pass locally in Release on every target. The Release build has no warnings, and the AOT compatibility app and dotnet pack succeed.

Note

This PR was prepared with the help of GitHub Copilot. I reviewed the change and the description.

sivaraman-krishnan and others added 3 commits November 25, 2026 21:26
Sent and received JSON-RPC responses and errors were recorded in
mcp.server.operation.duration and mcp.client.operation.duration with
mcp.method.name set to unknownMethod, so every request was counted twice.
Per the MCP semantic conventions, these metrics cover requests and
notifications, so only those are measured now, on both the send and
receive paths.

Fixes modelcontextprotocol#2043

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
RunConnected returns only after the server has finished handling every
in-flight message, so the measurements are complete without waiting.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
Assert that notifications are still measured once on each side, and that a
rejected request produces one measurement per side with no separate
measurement for the error response. Link the MCP semantic conventions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
@sivaraman-krishnan

Copy link
Copy Markdown
Author

Hi! Friendly ping on this one: the CI workflows are still waiting for maintainer approval to run, since this is my first contribution here. Could someone approve them when you have a moment? Happy to make any changes.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp.server.operation.duration records every sent response as an operation with mcp.method.name unknownMethod

1 participant