Skip to content

Commit e4a0343

Browse files
authored
Prevent MCP transport reuse by creating a server per request (#44)
1 parent 7797a81 commit e4a0343

1 file changed

Lines changed: 4 additions & 19 deletions

File tree

src/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,6 @@ interface Env {
3232
}
3333

3434
const app = new Hono<{ Bindings: Env }>()
35-
const mcpServerCache = new Map<string, ReturnType<typeof createMcpServer>>()
36-
37-
function getMcpServer(env: Env) {
38-
const allowlist = env.EXTERNAL_DOC_HOST_ALLOWLIST ?? ""
39-
const blocklist = env.EXTERNAL_DOC_HOST_BLOCKLIST ?? ""
40-
const cacheKey = `${allowlist}\n---\n${blocklist}`
41-
const cached = mcpServerCache.get(cacheKey)
42-
if (cached) {
43-
return cached
44-
}
45-
46-
const server = createMcpServer({
47-
EXTERNAL_DOC_HOST_ALLOWLIST: env.EXTERNAL_DOC_HOST_ALLOWLIST,
48-
EXTERNAL_DOC_HOST_BLOCKLIST: env.EXTERNAL_DOC_HOST_BLOCKLIST,
49-
})
50-
mcpServerCache.set(cacheKey, server)
51-
return server
52-
}
5335

5436
app.use("*", async (c, next) => {
5537
await next()
@@ -85,7 +67,10 @@ app.use("*", async (c, next) => {
8567
})
8668

8769
app.all("/mcp", async (c) => {
88-
const mcpServer = getMcpServer(c.env)
70+
const mcpServer = createMcpServer({
71+
EXTERNAL_DOC_HOST_ALLOWLIST: c.env.EXTERNAL_DOC_HOST_ALLOWLIST,
72+
EXTERNAL_DOC_HOST_BLOCKLIST: c.env.EXTERNAL_DOC_HOST_BLOCKLIST,
73+
})
8974
const transport = new StreamableHTTPTransport()
9075
await mcpServer.connect(transport)
9176
return transport.handleRequest(c)

0 commit comments

Comments
 (0)