Skip to content

prevent SSE upgrade with HTTP Streaming servers #393

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rubys
Copy link

@rubys rubys commented Jun 13, 2025

Description

It appears that the current code attempts to "upgrade" to SSE if progress notifications are used. This behavior is incompatible with the TypeScript MCP SDK. This change prevents the upgrade

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • MCP spec compatibility implementation
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Tests only (no functional changes)
  • Other (please describe):

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly

MCP Spec Compliance

  • This PR implements a feature defined in the MCP specification
  • Link to relevant spec section: Link text
  • Implementation follows the specification exactly

Additional Information

Summary by CodeRabbit

  • New Features

    • Added an option to disable automatic upgrade from JSON HTTP responses to Server-Sent Events (SSE) when notifications are sent. SSE upgrades remain enabled by default for backward compatibility.
  • Tests

    • Introduced a new test to verify correct server behavior when SSE upgrades are disabled, ensuring notifications are delivered as standard JSON responses.

It appears that the current code attempts to "upgrade" to SSE if
progress notifications are used. This behavior is incompatible with
the TypeScript MCP SDK.
Copy link
Contributor

coderabbitai bot commented Jun 13, 2025

"""

Walkthrough

A configuration option was added to the streamable HTTP server to allow disabling automatic upgrades from JSON HTTP responses to Server-Sent Events (SSE) when notifications are sent. The server and session structures were updated to support this flag, and a test was introduced to verify the new behavior.

Changes

Files Change Summary
server/streamable_http.go Added WithDisableSSEUpgrade option, updated server/session structs and constructors, modified SSE upgrade logic to honor the new flag.
server/streamable_http_test.go Added test to verify that disabling SSE upgrade results in standard JSON responses on notifications.

Possibly related PRs

  • mark3labs/mcp-go#348: Introduces SSE upgrade logic and fixes related to notifications, directly modifying the same SSE upgrade mechanism.
  • mark3labs/mcp-go#273: Implements and introduces streamable HTTP server and session code, which is extended by the current changes.

Suggested labels

type: enhancement, area: mcp spec

Suggested reviewers

  • pottekkat
    """

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 815f994 and 38db0f6.

📒 Files selected for processing (1)
  • server/streamable_http.go (8 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/streamable_http.go
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
server/streamable_http.go (2)

93-101: WithDisableSSEUpgrade could be a flag-less option

Having the caller pass an argument that is always true in practice (WithDisableSSEUpgrade(true)) is slightly awkward and error-prone. A common Go patterns is to expose a flag-less option that enables the behaviour and keep the default field value (false) for the regular case.

-func WithDisableSSEUpgrade(disable bool) StreamableHTTPOption {
-	return func(s *StreamableHTTPServer) {
-		s.disableSSEUpgrade = disable
-	}
-}
+
+// WithDisableSSEUpgrade disables automatic SSE upgrades.
+func WithDisableSSEUpgrade() StreamableHTTPOption {
+	return func(s *StreamableHTTPServer) {
+		s.disableSSEUpgrade = true
+	}
+}

This eliminates the possibility of somebody writing WithDisableSSEUpgrade(false) and thinking the flag is off while silently doing nothing.


267-268: Embedding server pointer inside session introduces a hidden cycle

streamableHttpSession now keeps a pointer to StreamableHTTPServer, and the server keeps references to sessions in sessionToolsStore / maps.
This is fine but creates an object graph cycle that prevents either side from becoming GC-eligible until both are released. Make sure sessionTools.delete(sessionID) (and UnregisterSession) actually removes all references to the session to avoid leaks in long-running servers.

server/streamable_http_test.go (2)

780-895: Great coverage – but assert header keys case-insensitively

The test validates Content-Type using:

contentType := resp.Header.Get("Content-Type")

Earlier assertions use the lower-case key "content-type". HTTP headers are case-insensitive, but mixing cases makes the test brittle and slightly confusing. Prefer the canonical form consistently:

-contentType := resp.Header.Get("Content-Type")
+contentType := resp.Header.Get("content-type") // or always CanonicalMIMEHeaderKey

Very minor, but helps readability.


780-895: Missing parallelisation guard

The new test spins up an HTTP server and performs network operations. Marking the sub-test (t.Run) or the whole function with t.Parallel() improves overall suite speed and prevents accidental shared-state coupling with other tests.

func TestStreamableHTTPServer_DisableSSEUpgrade(t *testing.T) {
    t.Parallel()
    ...
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5c49f63 and 815f994.

📒 Files selected for processing (2)
  • server/streamable_http.go (6 hunks)
  • server/streamable_http_test.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
server/streamable_http.go (1)
mcp/types.go (1)
  • JSONRPCNotification (318-321)
🔇 Additional comments (1)
server/streamable_http.go (1)

140-141: Potential data race on disableSSEUpgrade

disableSSEUpgrade is read from goroutines that out-live request handling (UpgradeToSSEWhenReceiveNotification).
Although the field is written only at construction time (before other goroutines start) and plain reads of aligned booleans are race-free on current architectures, the Go race detector still flags unsynchronised concurrent access as a race.

If you ever plan to make this option mutable at runtime, guard the field with atomic.Bool or move the value into the session at construction.

@rubys
Copy link
Author

rubys commented Jun 13, 2025

Let me know about the nitpicks. I'll be going on vacation next week at which point I'll be of little help; I'd like to see the problem fixed whether or not my pull request is the basis for that fix.

@rubys
Copy link
Author

rubys commented Jun 14, 2025

I asked Claude Code to build a simple MCP server with progress indicators that supports all three transports, and then python and typescript clients, and produce a report of what works. You can see the results here:

https://github.com/rubys/mcp-time-progress-server

@rubys
Copy link
Author

rubys commented Jun 14, 2025

Update: I added a branch https://github.com/rubys/mcp-time-progress-server/tree/cancelable which adds:

  • A TypeScript MCP Server (that can run all three transports)
  • A client notification request that cancels the long running tool

This allows me to demonstrate the original problem I was seeing that caused me to explore HTTP streaming. If you run npm run stdio you will see that a cancel request is sent to the server but it isn't processed until the long running tool request completes (which defeats the purpose of the cancel request). It actually is worse than this, with stdio, no requests are accepted while a request with progress notifications enabled is active. This effectively means that sse is the only fully functional transport if bidirectional communications are desired.

Running npm run ts:stdio will run the same client with the typescript MCP server, and that processes the cancel request when it is received.

A few other things I noted which are protocol or client SDK related, but not directly related to mcp-go:

For context, I work at fly.io. and we have a fly mcp server: code and docs. I encountered these problems while attempting to build a fly explorer which essentially is a custom "inspector" tool for this MCP server.

@dugenkui03
Copy link
Collaborator

hi @rubys If we disableSSEUpgrade, client will not receive notification related to concurrently-running request, I think that is a problem. And the official SDK way is to use enable_json_xx to restrict the server to only return JSON-type responses, will the server not send notification in the JSON-type mode?

It is different way from mcp-go, but it seems that client should support both stream and json. could you please explain more why auto-upgrade is not incompatible with the TypeScript MCP SDK

from spec: https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#sending-messages-to-the-server
If the input is a JSON-RPC request, the server MUST either return Content-Type: text/event-stream, to initiate an SSE stream, or Content-Type: application/json, to return one JSON object. The client MUST support both these cases.

@rubys
Copy link
Author

rubys commented Jun 19, 2025

Thanks for responding! If you run the tests you can see the problem. While a request that sends progress updates is active, no other request will be accepted from any client using stdio or Streaming HTTP, only with SSE. Disabling the upgrade was a Claude Code suggestion and it very well might be a red herring. I asked Claude Code to build a small MCP server from scratch with no mcp library using go routines and channels so that no i/o is blocked, and it worked for this small test case. I can push the results if they are of interest.

@rubys
Copy link
Author

rubys commented Jun 19, 2025

While on vacation I vibe coded an entire MCP SDK with an emphasis on concurrency and compatibility to prove that it could be done: https://github.com/rubys/mcp-go-sdk

I didn't do it because I believe that the world needs yet another SDK; I did it because I need concurrency. Perhaps there are some ideas in that implementation that can be adapted?

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.

2 participants