Add MCP shutdown notification, in-flight cap, cancellation plumbing (#1567) - #2210
Merged
Conversation
…1567) The MCP JSON-RPC loop now accepts `notifications/shutdown` (and the legacy LSP-style `notifications/exit` alias) that cancels an internal shutdown token, unblocks any pending `IMcpTransport.ReadFrameAsync`, and exits the loop after the current tool call drains; frames queued behind the shutdown notification are not served. `ProcessFrame` is now wrapped in a `SemaphoreSlim` (default cap `8`, exposed as `McpServer.MaxConcurrency` and validated by the constructor) so a future parallel-dispatch transport cannot exceed the bound. The per-request `CancellationToken` is plumbed through `WithDbReader` into `DbReader` via a new `(connection, isReadOnly, cancellation)` ctor plus `DbReader.Cancellation` / `ThrowIfCancellationRequested`, so SQLite work started by a tool observes shutdown / client-disconnect cancellation instead of running to completion after the client is gone. The legacy two-argument `DbReader` ctor is preserved as a thin wrapper that passes `CancellationToken.None`, keeping existing CLI and test call sites source-compatible. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Summary
notifications/shutdownhandler (with legacy LSP-stylenotifications/exitalias) that cancels an internal shutdown token, unblocks pendingIMcpTransport.ReadFrameAsync, drains the current tool call, and exits the loop without serving frames queued behind the shutdown notification.SemaphoreSlim(default cap8, exposed asMcpServer.MaxConcurrencyand validated by the constructor) before everyProcessFrameso any future parallel-dispatch transport cannot exceed the bound.CancellationTokenintoDbReader(new DbReader(connection, isReadOnly, cancellation)+DbReader.Cancellation/ThrowIfCancellationRequested) so SQLite work observes shutdown / client-disconnect cancellation. The legacy 2-argDbReaderctor is preserved as a thin wrapper that passesCancellationToken.None.Fixes #1567
Codex adversarial review (
codex exec review --base origin/main) reported no blocking or actionable correctness issues against the focus areas (race on_currentRequestToken, dispose order vs in-flight requests,notifications/exitalias semantics, JSON-RPC error semantics forOperationCanceledException, callers on removed ctor signatures, loop break semantics on!_running,RateLimiterenv read at construction).Test plan
dotnet test— 4894 passed, 0 failed, 3 skipped (post-rebase)McpServerTests.Notification_Shutdown_ReturnsNullAndLogsToStderrMcpServerTests.Notification_Exit_ReturnsNullAndLogsToStderrMcpServerTests.RunAsync_ShutdownNotification_DrainsAndExitsMcpServerTests.RunAsync_ShutdownNotification_PreemptsRemainingFramesMcpServerTests.MaxConcurrency_DefaultExposesIssueBoundMcpServerTests.MaxConcurrency_ExplicitOverride_TakesEffectMcpServerTests.MaxConcurrency_NonPositive_ThrowsDbReaderTests.Constructor_DefaultOverload_LeavesCancellationNoneDbReaderTests.Constructor_ExplicitToken_PropagatedThroughHelperscodex exec review --base origin/main): no blocking issuesGenerated with Claude Code