Skip to content

Prevent MCP transport reuse by creating a server per request#44

Merged
mattt merged 1 commit intomainfrom
mattt/fix-500-error-cached-mcp
Mar 2, 2026
Merged

Prevent MCP transport reuse by creating a server per request#44
mattt merged 1 commit intomainfrom
mattt/fix-500-error-cached-mcp

Conversation

@mattt
Copy link
Copy Markdown
Member

@mattt mattt commented Mar 2, 2026

Resolves #43

@mattt mattt requested a review from Copilot March 2, 2026 18:28
@mattt mattt merged commit e4a0343 into main Mar 2, 2026
4 checks passed
@mattt mattt deleted the mattt/fix-500-error-cached-mcp branch March 2, 2026 18:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 /mcp to 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.

@mattt mattt mentioned this pull request Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

HTTP 500 on MCP endpoint

2 participants