|
| 1 | +import type { StartedServer } from 'devframe/internal' |
| 2 | +import type { DevframeDefinition, DevframeRpcClientFunctions, DevframeRpcServerFunctions } from 'devframe/types' |
| 3 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { join } from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client' |
| 8 | +import { getDefaultEnvironment, StdioClientTransport } from '@modelcontextprotocol/client/stdio' |
| 9 | +import { createDevServer } from 'devframe/adapters/dev' |
| 10 | +import { createRpcClient } from 'devframe/rpc/client' |
| 11 | +import { createWsRpcChannel } from 'devframe/rpc/transports/ws-client' |
| 12 | +import { afterEach, describe, expect, it } from 'vitest' |
| 13 | + |
| 14 | +const definition: DevframeDefinition = { |
| 15 | + id: 'client-tools-test', |
| 16 | + name: 'Client Tools Test', |
| 17 | + version: '0.0.0', |
| 18 | + packageName: '@devframe/client-tools-test', |
| 19 | + homepage: 'https://example.com', |
| 20 | + description: 'Fixture: a devframe whose only agent tools live in browser tabs.', |
| 21 | + setup() {}, |
| 22 | +} |
| 23 | + |
| 24 | +/** A browser tab: one RPC connection exposing `page:selection` and answering with its own id. */ |
| 25 | +function connectTab(origin: string, client: { id: string, focused: boolean, visible?: boolean }) { |
| 26 | + const clientFunctions = { |
| 27 | + 'devframe:agent:invoke-client-tool': async (id: string, args: Record<string, unknown>) => ({ tab: client.id, tool: id, args }), |
| 28 | + } |
| 29 | + const rpc = createRpcClient<DevframeRpcServerFunctions, DevframeRpcClientFunctions>( |
| 30 | + clientFunctions as any, |
| 31 | + { channel: createWsRpcChannel({ url: `${origin.replace('http', 'ws')}/__ws` }) }, |
| 32 | + ) |
| 33 | + const sync = (focused = client.focused) => rpc.$call( |
| 34 | + 'devframe:agent:sync-client-tools', |
| 35 | + client.id, |
| 36 | + [{ id: 'page:selection', description: 'Read the selection.', safety: 'read', inputSchema: { type: 'object', properties: {} } }], |
| 37 | + { url: `http://app.local/${client.id}`, title: client.id, visible: client.visible ?? true, focused }, |
| 38 | + ) |
| 39 | + return { rpc, sync } |
| 40 | +} |
| 41 | + |
| 42 | +describe('client tools over the MCP route', () => { |
| 43 | + let server: StartedServer | undefined |
| 44 | + afterEach(async () => { |
| 45 | + await server?.close() |
| 46 | + server = undefined |
| 47 | + }) |
| 48 | + |
| 49 | + it('lists connected tabs and routes calls per tab', async () => { |
| 50 | + server = await createDevServer(definition, { host: '127.0.0.1', port: 0, auth: false, mcp: true }) |
| 51 | + const a = connectTab(server.origin, { id: 'tab-a', focused: false }) |
| 52 | + const b = connectTab(server.origin, { id: 'tab-b', focused: true }) |
| 53 | + await a.sync() |
| 54 | + await b.sync() |
| 55 | + |
| 56 | + const mcp = new Client({ name: 'test', version: '0.0.0' }, { versionNegotiation: { mode: 'auto' } }) |
| 57 | + await mcp.connect(new StreamableHTTPClientTransport(new URL(`${server.origin}/__mcp`), { |
| 58 | + requestInit: { headers: { origin: server.origin } }, |
| 59 | + })) |
| 60 | + try { |
| 61 | + const { tools } = await mcp.listTools() |
| 62 | + const names = tools.map(t => t.name) |
| 63 | + expect(names).toContain('devframe_agent_list-clients') |
| 64 | + expect(names.filter(n => n === 'page_selection')).toHaveLength(1) |
| 65 | + expect((tools.find(t => t.name === 'page_selection')!.inputSchema as any).properties.client_id).toMatchObject({ type: 'string' }) |
| 66 | + |
| 67 | + const listed = await mcp.callTool({ name: 'devframe_agent_list-clients', arguments: {} }) |
| 68 | + expect(listed.structuredContent).toEqual({ |
| 69 | + clients: [ |
| 70 | + expect.objectContaining({ id: 'tab-a', url: 'http://app.local/tab-a', focused: false, tools: ['page:selection'] }), |
| 71 | + expect.objectContaining({ id: 'tab-b', focused: true, tools: ['page:selection'] }), |
| 72 | + ], |
| 73 | + }) |
| 74 | + |
| 75 | + const unaddressed = await mcp.callTool({ name: 'page_selection', arguments: {} }) |
| 76 | + expect(JSON.parse((unaddressed.content as any)[0].text)).toMatchObject({ tab: 'tab-b', args: {} }) |
| 77 | + |
| 78 | + const addressed = await mcp.callTool({ name: 'page_selection', arguments: { client_id: 'tab-a' } }) |
| 79 | + expect(JSON.parse((addressed.content as any)[0].text)).toMatchObject({ tab: 'tab-a', args: {} }) |
| 80 | + |
| 81 | + const missing = await mcp.callTool({ name: 'page_selection', arguments: { client_id: 'gone' } }) |
| 82 | + expect(missing.isError).toBe(true) |
| 83 | + expect((missing.content as any)[0].text).toMatch(/DF0082.*tab-a, tab-b/s) |
| 84 | + |
| 85 | + // Focus moves to a: unaddressed calls follow it. |
| 86 | + await a.sync(true) |
| 87 | + const refocused = await mcp.callTool({ name: 'page_selection', arguments: {} }) |
| 88 | + expect(JSON.parse((refocused.content as any)[0].text)).toMatchObject({ tab: 'tab-a' }) |
| 89 | + } |
| 90 | + finally { |
| 91 | + await mcp.close() |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + it('devframe connect nests the connected tabs under each instance', async () => { |
| 96 | + const instancesDir = mkdtempSync(join(tmpdir(), 'devframe-instances-')) |
| 97 | + const env = { ...process.env } |
| 98 | + process.env.DEVFRAME_INSTANCES_DIR = instancesDir |
| 99 | + process.env.DEVFRAME_DISABLE_INSTANCE_REGISTRY = '0' |
| 100 | + const gateway = new Client({ name: 'test', version: '0.0.0' }) |
| 101 | + try { |
| 102 | + server = await createDevServer(definition, { host: '127.0.0.1', port: 0, auth: false, mcp: true }) |
| 103 | + await connectTab(server.origin, { id: 'tab-a', focused: true }).sync() |
| 104 | + |
| 105 | + await gateway.connect(new StdioClientTransport({ |
| 106 | + command: process.execPath, |
| 107 | + args: [fileURLToPath(new URL('../../../../devframe/bin/devframe.mjs', import.meta.url)), 'connect'], |
| 108 | + env: { ...getDefaultEnvironment(), DEVFRAME_INSTANCES_DIR: instancesDir }, |
| 109 | + })) |
| 110 | + const listed = await gateway.callTool({ name: 'devframe_connect_list-instances', arguments: {} }) |
| 111 | + const { instances } = JSON.parse((listed.content as any)[0].text) |
| 112 | + expect(instances).toHaveLength(1) |
| 113 | + expect(instances[0].mcp.clients).toEqual([ |
| 114 | + expect.objectContaining({ id: 'tab-a', url: 'http://app.local/tab-a', focused: true, tools: ['page:selection'] }), |
| 115 | + ]) |
| 116 | + } |
| 117 | + finally { |
| 118 | + await gateway.close().catch(() => {}) |
| 119 | + process.env = env |
| 120 | + rmSync(instancesDir, { recursive: true, force: true }) |
| 121 | + } |
| 122 | + }) |
| 123 | +}) |
0 commit comments