feat: Add CORS configuration for browser-based MCP clients

- 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
This commit is contained in:
Jerome 2025-06-30 15:21:57 +01:00
commit bda811a950
No known key found for this signature in database

View file

@ -584,6 +584,26 @@ app.listen(3000);
> );
> ```
#### CORS Configuration for Browser-Based Clients
If you'd like your server to be accessible by browser-based MCP clients, you'll need to configure CORS headers. The `Mcp-Session-Id` header must be exposed for browser clients to access it:
```typescript
import cors from 'cors';
// Add CORS middleware before your MCP routes
app.use(cors({
origin: '*', // Configure appropriately for production
exposedHeaders: ['Mcp-Session-Id']
}));
```
This configuration is necessary because:
- The MCP streamable HTTP transport uses the `Mcp-Session-Id` header for session management
- Browsers restrict access to response headers unless explicitly exposed via CORS
- Without this configuration, browser-based clients won't be able to read the session ID from initialization responses
#### Without Session Management (Stateless)
For simpler use cases where session management isn't needed: