Repository navigation
Don't record operation duration for JSON-RPC responses - #2048
Open
sivaraman-krishnan wants to merge 3 commits into
Open
sivaraman-krishnan wants to merge 3 commits into
sivaraman-krishnan wants to merge 3 commits into
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2043
Problem
McpSessionHandlerrecordedmcp.server.operation.durationandmcp.client.operation.durationfor JSON-RPC responses and errors, taggedmcp.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.
HandleMessageAsynctimed every incoming message, including responses routed to pending requests. So a client also recorded anunknownMethoddata point for each response it received, on top of the real duration measured inSendRequestAsync.Fix
Per the MCP semantic conventions, these metrics describe requests and notifications. A new
IsOperationhelper now gates the duration timer in bothHandleMessageAsyncandSendMessageAsync, so responses and errors are no longer recorded as operations.SendRequestAsyncon the sender and inHandleMessageAsyncon the receiver. Notifications are measured on send and receive.Diagnostics.ShouldInstrumentMessagealready excludes responses.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 aMeterListenerover an in-memory client/server pair. The test callstools/listandtools/call, sends a request the server rejects with a JSON-RPC error, and sends a notification. It asserts that there are nounknownMethodmeasurements and exactly one client-side and one server-side measurement for each of those four.The assertions run after
RunConnectedreturns. By then the server's message processing has drained every in-flight handler on disposal, so no polling is needed.ModelContextProtocol.TestsandModelContextProtocol.AspNetCore.Testssuites pass locally in Release on every target. The Release build has no warnings, and the AOT compatibility app anddotnet packsucceed.Note
This PR was prepared with the help of GitHub Copilot. I reviewed the change and the description.