Skip to content

feat: Add CORS configuration for browser-based MCP clients #713

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

Merged
merged 4 commits into from
Jul 7, 2025

Conversation

jerome3o-anthropic
Copy link
Member

Summary

  • Add CORS middleware to example servers to expose the Mcp-Session-Id header
  • Add documentation for CORS configuration in the README
  • Use minimal CORS settings (only expose required headers and methods)

Problem

Browser-based MCP clients cannot access the Mcp-Session-Id header from initialization responses due to CORS restrictions. Without this header, they cannot establish sessions with MCP servers.

Solution

This PR adds the cors npm package to example servers and configures it to expose the Mcp-Session-Id header via Access-Control-Expose-Headers. The configuration is minimal, only exposing what's necessary for MCP protocol operation.

Changes

  • Add cors import and middleware to:
    • sseAndStreamableHttpCompatibleServer.ts
    • simpleStatelessStreamableHttp.ts
    • jsonResponseStreamableHttp.ts
  • Add CORS configuration section to README explaining when and how to configure CORS for browser clients

Test plan

  • Example servers start successfully with CORS configured
  • Browser-based clients can read the Mcp-Session-Id header from responses
  • CORS headers are properly set on responses

Reported-by: Jerome

- Add cors middleware to example servers with Mcp-Session-Id exposed - Add CORS documentation section to README - Configure minimal CORS settings (only expose required headers) This enables browser-based clients to connect to MCP servers by properly exposing the Mcp-Session-Id header required for session management. Reported-by: Jerome
@jerome3o-anthropic jerome3o-anthropic force-pushed the jerome/cors-browser-support branch from 6c23ed6 to bda811a Compare July 4, 2025 15:06
@jerome3o-anthropic jerome3o-anthropic requested a review from ihrpr July 4, 2025 17:11
@felixweinberger felixweinberger requested review from felixweinberger and removed request for felixweinberger July 6, 2025 14:38
Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

LGTM, just need to merge README as there is already a warning about CORS

@@ -584,6 +584,26 @@ app.listen(3000);
> );
> ```


#### CORS Configuration for Browser-Based Clients

Copy link
Contributor

Choose a reason for hiding this comment

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

this was added in #633

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah up - that's just above this, I've merged them

@@ -96,6 +97,12 @@ const getServer = () => {
const app = express();
app.use(express.json());

// Configure CORS to expose Mcp-Session-Id header for browser-based clients
app.use(cors({
origin: '*', // Allow all origins - adjust as needed for production
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably add a very big "WARNING" here not to include "*"

Copy link
Member Author

Choose a reason for hiding this comment

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

If your MCP server will be accessible by browsers, then you might actually want this

Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

LGTM

@jerome3o-anthropic jerome3o-anthropic merged commit 1bd56ee into main Jul 7, 2025
5 checks passed
@jerome3o-anthropic jerome3o-anthropic deleted the jerome/cors-browser-support branch July 7, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants