-
Notifications
You must be signed in to change notification settings - Fork 7.5k
add streamableHttp server support for everything server #1496
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07af159
add streamableHttp server support for everything server
shivdeepak ace5c2a
update docs
shivdeepak 03e9a7b
ref: cleanup
shivdeepak d1d7944
fix: passing body to handleRequest, and optionally adding a response …
shivdeepak 7e602b0
update package.lock
shivdeepak e70bcd3
remove json middleware from everything streamable http server
shivdeepak aff2243
Merge branch 'main' into main
cliffhall 3020ae5
Merge branch 'main' into main
cliffhall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; | ||
import { InMemoryEventStore } from '@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js'; | ||
import express, { Request, Response } from "express"; | ||
import { createServer } from "./everything.js"; | ||
import { randomUUID } from 'node:crypto'; | ||
|
||
const app = express(); | ||
|
||
const { server, cleanup } = createServer(); | ||
|
||
const transports: { [sessionId: string]: StreamableHTTPServerTransport } = {}; | ||
|
||
app.post('/mcp', async (req: Request, res: Response) => { | ||
console.log('Received MCP POST request'); | ||
try { | ||
// Check for existing session ID | ||
const sessionId = req.headers['mcp-session-id'] as string | undefined; | ||
let transport: StreamableHTTPServerTransport; | ||
|
||
if (sessionId && transports[sessionId]) { | ||
// Reuse existing transport | ||
transport = transports[sessionId]; | ||
} else if (!sessionId) { | ||
// New initialization request | ||
const eventStore = new InMemoryEventStore(); | ||
transport = new StreamableHTTPServerTransport({ | ||
sessionIdGenerator: () => randomUUID(), | ||
eventStore, // Enable resumability | ||
onsessioninitialized: (sessionId) => { | ||
// Store the transport by session ID when session is initialized | ||
// This avoids race conditions where requests might come in before the session is stored | ||
console.log(`Session initialized with ID: ${sessionId}`); | ||
transports[sessionId] = transport; | ||
} | ||
}); | ||
|
||
// Set up onclose handler to clean up transport when closed | ||
transport.onclose = () => { | ||
const sid = transport.sessionId; | ||
if (sid && transports[sid]) { | ||
console.log(`Transport closed for session ${sid}, removing from transports map`); | ||
delete transports[sid]; | ||
} | ||
}; | ||
|
||
// Connect the transport to the MCP server BEFORE handling the request | ||
// so responses can flow back through the same transport | ||
await server.connect(transport); | ||
|
||
await transport.handleRequest(req, res); | ||
return; // Already handled | ||
} else { | ||
// Invalid request - no session ID or not initialization request | ||
res.status(400).json({ | ||
jsonrpc: '2.0', | ||
error: { | ||
code: -32000, | ||
message: 'Bad Request: No valid session ID provided', | ||
}, | ||
id: req?.body?.id, | ||
}); | ||
return; | ||
} | ||
|
||
// Handle the request with existing transport - no need to reconnect | ||
// The existing transport is already connected to the server | ||
await transport.handleRequest(req, res); | ||
} catch (error) { | ||
console.error('Error handling MCP request:', error); | ||
if (!res.headersSent) { | ||
res.status(500).json({ | ||
jsonrpc: '2.0', | ||
error: { | ||
code: -32603, | ||
message: 'Internal server error', | ||
}, | ||
id: req?.body?.id, | ||
}); | ||
return; | ||
} | ||
} | ||
}); | ||
|
||
// Handle GET requests for SSE streams (using built-in support from StreamableHTTP) | ||
app.get('/mcp', async (req: Request, res: Response) => { | ||
console.log('Received MCP GET request'); | ||
const sessionId = req.headers['mcp-session-id'] as string | undefined; | ||
if (!sessionId || !transports[sessionId]) { | ||
res.status(400).json({ | ||
jsonrpc: '2.0', | ||
error: { | ||
code: -32000, | ||
message: 'Bad Request: No valid session ID provided', | ||
}, | ||
id: req?.body?.id, | ||
}); | ||
return; | ||
} | ||
|
||
// Check for Last-Event-ID header for resumability | ||
const lastEventId = req.headers['last-event-id'] as string | undefined; | ||
if (lastEventId) { | ||
console.log(`Client reconnecting with Last-Event-ID: ${lastEventId}`); | ||
} else { | ||
console.log(`Establishing new SSE stream for session ${sessionId}`); | ||
} | ||
|
||
const transport = transports[sessionId]; | ||
await transport.handleRequest(req, res); | ||
}); | ||
|
||
// Handle DELETE requests for session termination (according to MCP spec) | ||
app.delete('/mcp', async (req: Request, res: Response) => { | ||
const sessionId = req.headers['mcp-session-id'] as string | undefined; | ||
if (!sessionId || !transports[sessionId]) { | ||
res.status(400).json({ | ||
jsonrpc: '2.0', | ||
error: { | ||
code: -32000, | ||
message: 'Bad Request: No valid session ID provided', | ||
}, | ||
id: req?.body?.id, | ||
}); | ||
return; | ||
} | ||
|
||
console.log(`Received session termination request for session ${sessionId}`); | ||
|
||
try { | ||
const transport = transports[sessionId]; | ||
await transport.handleRequest(req, res); | ||
} catch (error) { | ||
console.error('Error handling session termination:', error); | ||
if (!res.headersSent) { | ||
res.status(500).json({ | ||
jsonrpc: '2.0', | ||
error: { | ||
code: -32603, | ||
message: 'Error handling session termination', | ||
}, | ||
id: req?.body?.id, | ||
}); | ||
return; | ||
} | ||
} | ||
}); | ||
|
||
// Start the server | ||
const PORT = process.env.PORT || 3001; | ||
app.listen(PORT, () => { | ||
console.log(`MCP Streamable HTTP Server listening on port ${PORT}`); | ||
}); | ||
|
||
// Handle server shutdown | ||
process.on('SIGINT', async () => { | ||
console.log('Shutting down server...'); | ||
|
||
// Close all active transports to properly clean up resources | ||
for (const sessionId in transports) { | ||
try { | ||
console.log(`Closing transport for session ${sessionId}`); | ||
await transports[sessionId].close(); | ||
delete transports[sessionId]; | ||
} catch (error) { | ||
console.error(`Error closing transport for session ${sessionId}:`, error); | ||
} | ||
} | ||
await cleanup(); | ||
await server.close(); | ||
console.log('Server shutdown complete'); | ||
process.exit(0); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% certain how to test this part. I think we need to beef up the logic in the Inspector to make the Reconnect button send the
last-event-id
header.But this endpoint is working correctly for initiating and maintaining the SSE stream, and the only thing we're doing here is logging the fact that this proxy saw the header. The SDK deals with recognizing that header and doing a replay from it, so I think this server is just fine.