A session on `engine`, driven the way a transport drives it.
(engine: MCPEngine)
| 84 | |
| 85 | /** A session on `engine`, driven the way a transport drives it. */ |
| 86 | function connect(engine: MCPEngine) { |
| 87 | let handle: ((m: JsonRpcRequest | JsonRpcNotification) => Promise<void>) | null = null; |
| 88 | const replies = new Map<unknown, any>(); |
| 89 | const transport: JsonRpcTransport = { |
| 90 | start(h) { handle = h as typeof handle; }, |
| 91 | stop() { /* nothing to tear down */ }, |
| 92 | send() { /* unused */ }, |
| 93 | notify() { /* unused */ }, |
| 94 | async request() { return {}; }, |
| 95 | sendResult(id, result) { replies.set(id, result); }, |
| 96 | sendError(id, code, message) { replies.set(id, { error: { code, message } }); }, |
| 97 | }; |
| 98 | new MCPSession(transport, engine).start(); |
| 99 | let nextId = 1; |
| 100 | return async (method: string, params?: unknown): Promise<any> => { |
| 101 | const id = nextId++; |
| 102 | await handle!({ jsonrpc: '2.0', id, method, params } as JsonRpcRequest); |
| 103 | return replies.get(id); |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | function initializeParams(clientInfo: object | undefined, root: string): object { |
| 108 | return { |
no test coverage detected