Fix SSE/JSON race condition in session capability injection#4075
Merged
ChrisJBurns merged 1 commit intomainfrom Mar 10, 2026
Merged
Fix SSE/JSON race condition in session capability injection#4075ChrisJBurns merged 1 commit intomainfrom
ChrisJBurns merged 1 commit intomainfrom
Conversation
Bypass the mcp-go SDK notification mechanism during session registration to prevent stale notifications from corrupting HTTP responses. During OnRegisterSession, the notification-listening goroutine from the initialize request has already exited, so notifications sent by AddSessionTools/AddSessionResources accumulate in the channel and race with the next request's response writer. Replace AddSessionTools/AddSessionResources calls with direct SetSessionTools/SetSessionResources on the session object, which sets capabilities without triggering notifications. Also register resource capabilities explicitly at server creation since the implicit registration in AddSessionResources is now bypassed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
JAORMX
approved these changes
Mar 10, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4075 +/- ##
==========================================
- Coverage 68.67% 68.49% -0.19%
==========================================
Files 446 446
Lines 45433 45574 +141
==========================================
+ Hits 31203 31216 +13
- Misses 11818 11944 +126
- Partials 2412 2414 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
The mcp-go SDK's
AddSessionTools/AddSessionResourcesmethods send notifications through the session's notification channel. DuringOnRegisterSession, the notification-listening goroutine from the initialize request has already exited, so these notifications accumulate as stale messages and corrupt the next HTTP response — causing a race between the SSE upgrade and JSON response.This change bypasses the SDK notification mechanism during session registration by writing tools and resources directly to the session object via
SetSessionTools/SetSessionResources, which sets capabilities without triggering notifications.AddSessionTools/AddSessionResourcescalls during registration with direct session setterssetSessionToolsDirectandsetSessionResourcesDirecthelper functions that merge capabilities into the sessionAddSessionResourcesis now bypassed)Type of change
Test plan
task test)task lint-fix)Special notes for reviewers
This is a workaround for a limitation in the mcp-go SDK where session registration hooks cannot safely use
AddSessionTools/AddSessionResourcesbecause the notification goroutine is not active during that lifecycle phase. The direct session setter approach (SetSessionTools/SetSessionResources) achieves the same capability injection without side effects. A future SDK update may provide a cleaner API for this use case.🤖 Generated with Claude Code