|
| 1 | +import { createMcpHandler, McpServer } from '@modelcontextprotocol/server' |
| 2 | +import { McpRegistrar } from '@orpc/mcp' |
| 3 | +import { os } from '@orpc/server' |
| 4 | +import { promiseWithResolvers, sleep } from '@orpc/shared' |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +import { fetchText, setFetchHandler, withTimeout } from './__shared__/fetch-handler' |
| 8 | + |
| 9 | +/** |
| 10 | + * A lazy router whose first load waits on a timer created in the calling request, |
| 11 | + * so it never settles once that request ends, while later loads resolve at once. |
| 12 | + */ |
| 13 | +function createLazyRouter() { |
| 14 | + let loads = 0 |
| 15 | + const firstLoadStarted = promiseWithResolvers<void>() |
| 16 | + |
| 17 | + const router = { |
| 18 | + planet: os.lazy(async () => { |
| 19 | + if (++loads === 1) { |
| 20 | + firstLoadStarted.resolve() |
| 21 | + await sleep(10) |
| 22 | + } |
| 23 | + |
| 24 | + return { default: { find: os.handler(() => ({ name: 'Earth' })) } } |
| 25 | + }), |
| 26 | + } |
| 27 | + |
| 28 | + return { router, firstLoadStarted: firstLoadStarted.promise, loads: () => loads } |
| 29 | +} |
| 30 | + |
| 31 | +function createServer() { |
| 32 | + return new McpServer({ name: 'test-server', version: '1.0.0' }) |
| 33 | +} |
| 34 | + |
| 35 | +describe('mcpRegistrar with a lazy router on Workers', () => { |
| 36 | + it('registers tools in a later request after the request that started the load ends early', async () => { |
| 37 | + const { router, firstLoadStarted, loads } = createLazyRouter() |
| 38 | + const registrar = new McpRegistrar(router) |
| 39 | + |
| 40 | + setFetchHandler(async (request) => { |
| 41 | + if (new URL(request.url).pathname === '/a') { |
| 42 | + // ends once the load starts, without awaiting it, like a disconnected client |
| 43 | + registrar.register(createServer()).catch(() => {}) |
| 44 | + return new Response(await withTimeout(firstLoadStarted.then(() => 'started'))) |
| 45 | + } |
| 46 | + |
| 47 | + const result = await withTimeout(registrar.register(createServer())) |
| 48 | + return new Response(result === 'timeout' ? result : Object.keys(result.tools).join(',')) |
| 49 | + }) |
| 50 | + |
| 51 | + expect(await fetchText('/a')).toBe('started') |
| 52 | + expect(await fetchText('/b')).toBe('planet_find') |
| 53 | + expect(loads()).toBe(2) |
| 54 | + expect(await fetchText('/b')).toBe('planet_find') |
| 55 | + expect(loads()).toBe(2) |
| 56 | + }) |
| 57 | + |
| 58 | + it('calls a tool through createMcpHandler in a later request after the request that started the load ends early', async () => { |
| 59 | + const { router, firstLoadStarted } = createLazyRouter() |
| 60 | + const registrar = new McpRegistrar(router) |
| 61 | + const handler = createMcpHandler(async () => { |
| 62 | + const server = createServer() |
| 63 | + await registrar.register(server) |
| 64 | + return server |
| 65 | + }) |
| 66 | + |
| 67 | + const toolCall = () => |
| 68 | + new Request('http://x/mcp', { |
| 69 | + method: 'POST', |
| 70 | + headers: { |
| 71 | + 'content-type': 'application/json', |
| 72 | + 'accept': 'application/json, text/event-stream', |
| 73 | + }, |
| 74 | + body: JSON.stringify({ |
| 75 | + jsonrpc: '2.0', |
| 76 | + id: 1, |
| 77 | + method: 'tools/call', |
| 78 | + params: { name: 'planet_find', arguments: {} }, |
| 79 | + }), |
| 80 | + }) |
| 81 | + |
| 82 | + setFetchHandler(async (request) => { |
| 83 | + if (new URL(request.url).pathname === '/a') { |
| 84 | + handler.fetch(toolCall()).catch(() => {}) |
| 85 | + return new Response(await withTimeout(firstLoadStarted.then(() => 'started'))) |
| 86 | + } |
| 87 | + |
| 88 | + const result = await withTimeout(handler.fetch(toolCall()).then((r) => r.text())) |
| 89 | + return new Response(result) |
| 90 | + }) |
| 91 | + |
| 92 | + try { |
| 93 | + expect(await fetchText('/a')).toBe('started') |
| 94 | + |
| 95 | + const text = await fetchText('/b') |
| 96 | + // a stateless legacy request is answered over SSE |
| 97 | + expect(JSON.parse(text.match(/^data: (.*)$/m)![1]!)).toEqual({ |
| 98 | + jsonrpc: '2.0', |
| 99 | + id: 1, |
| 100 | + result: { |
| 101 | + content: [{ type: 'text', text: '{"name":"Earth"}' }], |
| 102 | + structuredContent: { name: 'Earth' }, |
| 103 | + }, |
| 104 | + }) |
| 105 | + } finally { |
| 106 | + await handler.close() |
| 107 | + } |
| 108 | + }) |
| 109 | +}) |
0 commit comments