mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

prevent SSE upgrade with HTTP Streaming servers

Open rubys opened this issue 6 months ago • 7 comments

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

  • [x] Bug fix (non-breaking change that fixes an issue)
  • [ ] New feature (non-breaking change that adds functionality)
  • [x] 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

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

MCP Spec Compliance

  • [x] 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.

rubys avatar Jun 13 '25 13:06 rubys

"""

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 815f9942d3268cc474a0516f27f21fcfb043718c and 38db0f65eb99803c12f7bd281415694ec5b2855a.

📒 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.

coderabbitai[bot] avatar Jun 13 '25 13:06 coderabbitai[bot]

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 avatar Jun 13 '25 14:06 rubys

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 avatar Jun 14 '25 14:06 rubys

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:

  • If you add _meta.progressToken to your request, the TypeScript SDK will overwrite it if you specify an onprogress handler: https://github.com/modelcontextprotocol/typescript-sdk/blob/6b3d326cf77cf212ea97d38572cf6e20ae047543/src/shared/protocol.ts#L540-L549 . A workaround would be to create a customProgressToken field.
  • Messages ids are assigned sequentially so they aren't unique across clients: https://github.com/modelcontextprotocol/typescript-sdk/blob/6b3d326cf77cf212ea97d38572cf6e20ae047543/src/shared/protocol.ts#L533 (not an issue with stdio)
  • The server will return back tokenForCancel in progress notifications, but that only works if a progress notification has been sent prior to the point where the client wants to cancel.
  • Claude couldn't figure out how to pass metadata on tool requests using the python SDK. At the moment, that is not a priority for me, so I didn't pursue it further.

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.

rubys avatar Jun 14 '25 19:06 rubys

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?

  • python-SDK: https://github.com/modelcontextprotocol/python-sdk/blob/d0443a18328a2fc9e87da6feee9130f95c0b37a7/src/mcp/server/streamable_http.py#L400
  • TypeScript MCP SDK: https://github.com/modelcontextprotocol/typescript-sdk/blob/a68dcdf4520fd17bc2cbe58939ca6ca385f0467b/src/server/streamableHttp.ts#L57C3-L57C21

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.

dugenkui03 avatar Jun 19 '25 04:06 dugenkui03

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 avatar Jun 19 '25 07:06 rubys

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?

rubys avatar Jun 19 '25 21:06 rubys