Open History to see the last commit
1import type { ClientOptions } from '@modelcontextprotocol/client'
2import type { TransportConfig } from '../transport'
3import type { MCPClientOptions } from '../types'
4
5export interface McpServerDescriptor {
6 /**
7 * The serializable connection config the call handler reconnects from.
8 * `undefined` for a client built from a ready-made `Transport` instance
9 * (single-use, not reconnectable) — the handler rejects such a call with a
10 * clear error.
11 */
12 transport: TransportConfig | undefined
13 prefix?: string
14 /**
15 * Options to rebuild the client with. Carried so a reconnect keeps a custom
16 * `jsonSchemaValidator` — an edge runtime cannot use the SDK's AJV default.
17 */
18 clientOptions?: ClientOptions
19 /** Carried so a reconnect hides the same tools from widgets as from the model. */
20 toolFilter?: MCPClientOptions['toolFilter']
21 /** Carried so a widget cannot run a tool that the model can run only after approval. */
22 needsApproval?: MCPClientOptions['needsApproval']
23}
24
25export interface McpSessionStore {
26 /**
27 * Resolve the server descriptor for a thread+serverId, or null if unknown.
28 *
29 * `serverId` may be undefined when the widget omits it (single-server setups);
30 * implementations should default to the sole recorded server in that case.
31 */
32 get: (
33 threadId: string,
34 serverId: string | undefined,
35 ) => Promise<McpServerDescriptor | null>
36 /** Record the servers a thread may interact with (called from the chat route). */
37 set: (
38 threadId: string,
39 servers: Record<string, McpServerDescriptor>,
40 ) => Promise<void>
41}
42
43/** Call-handler request shape; imported by call-handler.ts from this module. */
44export interface McpAppCallRequest {
45 threadId: string
46 /**
47 * The server the widget targets. May be undefined when the widget omits it
48 * (single-server setups) — the handler defaults to the sole configured server.
49 */
50 serverId?: string
51 toolName: string
52 args?: unknown
53 /**
54 * Reserved — forwarded by the bridge for correlation purposes but not
55 * consumed by the call handler. Mirrors the `meta` convention: accepted
56 * on the wire, carried through, but the handler does not read it.
57 */
58 messageId?: string
59}
60
61/**
62 * Creates a simple in-memory McpSessionStore.
63 *
64 * TTL is enforced on read (prune-on-read) and slides on each successful hit —
65 * this is single-instance only. The `McpSessionStore` interface is the
66 * extension point for persistent/SQL backends, which can drop in later with no
67 * API change.
68 *
69 * Growth is bounded by an opportunistic sweep on `set()`: prune-on-read alone
70 * never reclaims a thread that is recorded but never has a widget interaction
71 * (the common case — most threads never touch a `ui://` widget), so without the
72 * sweep the map would grow by one entry per chat thread for the process
73 * lifetime. The sweep drops every entry older than the TTL whenever a new one is
74 * written, keeping the map bounded to threads active within the TTL window.
75 */
76export function inMemoryMcpSessionStore(
77 opts: { ttlMs?: number } = {},
78): McpSessionStore {
79 const map = new Map<
80 string,
81 { at: number; servers: Record<string, McpServerDescriptor> }
82 >()
83 const ttl = opts.ttlMs ?? 30 * 60_000
84
85 return {
86 async set(threadId, servers) {
87 // Opportunistic sweep: reclaim every expired entry, not just this thread,
88 // so set-but-never-read threads can't accumulate unbounded.
89 const now = Date.now()
90 for (const [id, e] of map) {
91 if (now - e.at > ttl) map.delete(id)
92 }
93 map.set(threadId, { at: now, servers })
94 },
95 async get(threadId, serverId) {
96 const e = map.get(threadId)
97 if (!e || Date.now() - e.at > ttl) {
98 map.delete(threadId)
99 return null
100 }
101 // Sliding TTL: refresh on a successful hit so an actively-used thread
102 // doesn't expire by absolute time mid-session.
103 e.at = Date.now()
104 // serverId omitted (single-server setups): default to the sole server.
105 if (serverId === undefined) {
106 const entries = Object.entries(e.servers)
107 return entries.length === 1 ? (entries[0]?.[1] ?? null) : null
108 }
109 return e.servers[serverId] ?? null
110 },
111 }
112}