Skip to content

Commit 1a57d84

Browse files
committed
cheat with the cache
1 parent 56705bd commit 1a57d84

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

app/routes/mcp+/mcp.server.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { AsyncLocalStorage } from 'node:async_hooks'
22
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
33
import { z } from 'zod'
44
import { addSubscriberToForm } from '#app/kit/kit.server.js'
5+
import { cache, cachified } from '#app/utils/cache.server.js'
6+
import {
7+
ensureInstance,
8+
getInstanceInfo,
9+
} from '#app/utils/cjs/litefs-js.server.js'
510
import { downloadMdxFilesCached } from '#app/utils/mdx.server.js'
611
import { getDomainUrl, getErrorMessage } from '#app/utils/misc.js'
712
import { searchKCD } from '#app/utils/search.server.js'
@@ -228,15 +233,39 @@ function createServer() {
228233
const server = createServer()
229234

230235
export async function connect(sessionId?: string | null) {
236+
const { currentInstance } = await getInstanceInfo()
231237
const transport = new FetchSSEServerTransport('/mcp', sessionId)
232238
transport.onclose = () => {
233239
transports.delete(transport.sessionId)
234240
}
235241
await server.connect(transport)
236242
transports.set(transport.sessionId, transport)
243+
244+
// we're cheating to get this sessionId into the cache so it's accessible in
245+
// every instance.
246+
await cachified({
247+
key: `mcp-${sessionId}`,
248+
cache,
249+
ttl: 60 * 60 * 24 * 30,
250+
getFreshValue() {
251+
return {
252+
sessionId,
253+
instance: currentInstance,
254+
}
255+
},
256+
})
237257
return transport
238258
}
239259

240260
export async function getTransport(sessionId: string) {
261+
const { instance } = await cachified({
262+
key: `mcp-${sessionId}`,
263+
cache,
264+
getFreshValue() {
265+
throw new Error(`Instance for sessionId "${sessionId}" not found`)
266+
},
267+
})
268+
ensureInstance(instance)
269+
241270
return transports.get(sessionId)
242271
}

0 commit comments

Comments
 (0)