Prevent MCP transport reuse by creating a server per request#44
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses the MCP endpoint outage (#43) by avoiding reuse of MCP server connection state across requests, which can lead to transport/connect failures and HTTP 500s.
Changes:
- Removed the in-memory MCP server cache keyed by external-doc allowlist/blocklist.
- Updated
/mcpto instantiate a fresh MCP server for each incoming request before connecting the HTTP transport.
Comments suppressed due to low confidence (1)
src/index.ts:76
- Creating a new MCP server on every /mcp request re-registers all tools and Zod schemas each time, which adds per-request CPU/latency overhead and can increase cost under load. If the goal is only to avoid reusing a connected transport, consider keeping a cached, pre-initialized tool registry (or another lightweight factory) so you don’t rebuild the entire server for every request, while still ensuring each transport gets its own isolated connection state.
const mcpServer = createMcpServer({
EXTERNAL_DOC_HOST_ALLOWLIST: c.env.EXTERNAL_DOC_HOST_ALLOWLIST,
EXTERNAL_DOC_HOST_BLOCKLIST: c.env.EXTERNAL_DOC_HOST_BLOCKLIST,
})
const transport = new StreamableHTTPTransport()
await mcpServer.connect(transport)
return transport.handleRequest(c)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Closed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #43