-
Notifications
You must be signed in to change notification settings - Fork 567
Add server tool timeouts (default & per-tool) and honor JSON-RPC $/cancelRequest (#922) #951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add server tool timeouts (default & per-tool) and honor JSON-RPC $/cancelRequest (#922) #951
Conversation
I'm not understanding the purpose of this. Cancellation is already supported with |
I see how my original wording made this look like a cancellation feature PR. The actual primary goal here is server-side tool timeout support. The I’ve updated the PR title/description to reflect the real scope (tool timeout) much clearer. Thanks for pointing it out! |
|
I think this is conflating cancellation and server / client time-outs. A server-side timeout is, as I see it, an implementation detail of the server, and not really a protocol level concern. I can see how convenience to implement this is nice, but if it is added, I think it needs to be much more clear on this being a purely server side interaction, and the client also needs a better error message. This isn't a cancellation - it is the server deciding that long-running tool calls are an error and aborting the client call. |
…lation This introduces support for parsing and honoring JSON-RPC \$/cancelRequest\ notifications, making cancellation semantics compliant with MCP protocol expectations. Includes a new \JsonRpcCancelRequest\ constant in \NotificationMethods\ and integrates proper cancellation token linkage inside the server message loop. Additionally: - Added SlowTool test fixture to simulate long-running tools - Implemented full timeout vs external cancellation tests - Normalized English summaries / comments in related areas Fixes modelcontextprotocol#922
…ction, machine-readable Meta.TimeoutMs)
…er review feedback)
…ersion; ensure silent $/cancelRequest path
…path; no protocol error emitted)
fdb2157 to
48ad945
Compare
Thanks, that makes sense. I see now that this behavior blurred the line between user cancellation vs server-side timeouts. I’ve updated the PR so that:
This keeps the semantics clearly separated and avoids protocol confusion. Thanks again for the guidance, waiting for your review. |
…emit standard cancel; keep server timeouts in RunWithTimeoutAsync
The XML documentation for `McpErrorCode.RequestCancelled` has been updated to clearly indicate that this error code is reserved for user-initiated cancellation (e.g., a client sending a JSON-RPC `$/cancelRequest`). The C# server implementation does not emit this code for server-side timeouts. Timeouts are surfaced as regular CallToolResult errors with timeout metadata (`Meta["IsTimeout"]`, `Meta["TimeoutMs"]`). This aligns with reviewer feedback and ensures a clean separation between client cancellation semantics and server timeout behavior.
|
Thanks for your contribution, but I'm going close this. For one, this PR still has logic handling As for the part that addresses #922, I think it's better to use MCP Server Handler Filters. The logic in the A tool call timeout filter might be a good sample to add to the documentation, but I don't think it needs to be a first-class feature. If someone were to submit a PR adding this to filters.md, I think we'd accept it. Long term, it might be interesting to ship built-in handler timeout filters similar to ASP.NET Core's Request Timeout Middleware, but it's not currently priority. |
This PR introduces server-side tool timeout support and ensures proper JSON-RPC
$/cancelRequesthandling.Summary of Changes
DefaultToolTimeouttoMcpServerOptionsIMcpToolWithTimeoutinterface for per-tool overrideNotificationMethods.JsonRpcCancelRequestconstantSlowToolfixture for timeout scenarios$/cancelRequestcancellation pathFixes #922
Motivation and Context
Timeout control at the server/tool level is required for predictable behavior when dealing with long-running tool executions.
$/cancelRequesthandling complements this by ensuring JSON-RPC cancellation semantics correctly surface to the caller.How Has This Been Tested?
All new behavior is covered via unit tests under
McpServerToolTimeoutTestsusingTestServerTransport.Breaking Changes
No breaking changes. Existing server implementations continue to work without modification.
Types of Changes
Checklist
Additional Context
Server-side timeout is surfaced as a normal tool error with explicit
Meta.IsTimeoutmetadata - this is not modeled as cancellation and is distinct from$/cancelRequest.This PR also ensures the server does not respond on user-initiated
$/cancelRequestper JSON-RPC expectations.