|
| 1 | +import type { StandardSchemaV1 } from '@standard-schema/spec' |
| 2 | +import type { WebMcpModelContext, WebMcpToolDescriptor } from './webmcp' |
| 3 | +import { RpcFunctionsCollectorBase } from 'devframe/rpc' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { registerWebMcpTools } from './webmcp' |
| 6 | + |
| 7 | +/** A Standard Schema that also implements the Standard JSON Schema converter (like zod 4). */ |
| 8 | +function withJsonSchema(json: Record<string, unknown>): StandardSchemaV1 { |
| 9 | + return { |
| 10 | + '~standard': { |
| 11 | + version: 1, |
| 12 | + vendor: 'test', |
| 13 | + validate: (value: unknown) => ({ value }), |
| 14 | + jsonSchema: { |
| 15 | + input: () => json, |
| 16 | + output: () => json, |
| 17 | + }, |
| 18 | + } as StandardSchemaV1['~standard'], |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** Spec-shaped model context: unregisters by aborting the passed signal. */ |
| 23 | +function createFakeModelContext() { |
| 24 | + const tools = new Map<string, WebMcpToolDescriptor>() |
| 25 | + const modelContext: WebMcpModelContext = { |
| 26 | + registerTool(tool, options) { |
| 27 | + tools.set(tool.name, tool) |
| 28 | + options?.signal?.addEventListener('abort', () => tools.delete(tool.name)) |
| 29 | + return Promise.resolve() |
| 30 | + }, |
| 31 | + } |
| 32 | + return { modelContext, tools } |
| 33 | +} |
| 34 | + |
| 35 | +function createCollector() { |
| 36 | + return new RpcFunctionsCollectorBase<Record<string, any>, undefined>(undefined) |
| 37 | +} |
| 38 | + |
| 39 | +describe('registerWebMcpTools', () => { |
| 40 | + it('registers only agent-flagged functions, under their wire names', () => { |
| 41 | + const collector = createCollector() |
| 42 | + collector.register({ |
| 43 | + name: 'my-plugin:greet', |
| 44 | + jsonSerializable: true, |
| 45 | + agent: { description: 'Greet someone by name.' }, |
| 46 | + args: [withJsonSchema({ type: 'string' })], |
| 47 | + returns: withJsonSchema({ type: 'string' }), |
| 48 | + handler: (name: string) => `Hello ${name}`, |
| 49 | + }) |
| 50 | + collector.register({ |
| 51 | + name: 'my-plugin:internal', |
| 52 | + handler: () => 'hidden', |
| 53 | + }) |
| 54 | + |
| 55 | + const { modelContext, tools } = createFakeModelContext() |
| 56 | + const dispose = registerWebMcpTools(collector, { modelContext }) |
| 57 | + |
| 58 | + expect([...tools.keys()]).toEqual(['my-plugin_greet']) |
| 59 | + const tool = tools.get('my-plugin_greet')! |
| 60 | + expect(tool.description).toBe('Greet someone by name.') |
| 61 | + expect(tool.inputSchema).toEqual({ |
| 62 | + type: 'object', |
| 63 | + properties: { arg0: { type: 'string' } }, |
| 64 | + required: ['arg0'], |
| 65 | + additionalProperties: false, |
| 66 | + }) |
| 67 | + // `query` (default type) infers read-only. |
| 68 | + expect(tool.annotations).toMatchObject({ readOnlyHint: true, destructiveHint: false }) |
| 69 | + |
| 70 | + dispose() |
| 71 | + expect(tools.size).toBe(0) |
| 72 | + }) |
| 73 | + |
| 74 | + it('executes with arg0/argN coercion and returns a text result', async () => { |
| 75 | + const collector = createCollector() |
| 76 | + collector.register({ |
| 77 | + name: 'add', |
| 78 | + jsonSerializable: true, |
| 79 | + agent: { description: 'Add two numbers.' }, |
| 80 | + args: [withJsonSchema({ type: 'number' }), withJsonSchema({ type: 'number' })], |
| 81 | + returns: withJsonSchema({ type: 'object' }), |
| 82 | + handler: (a: number, b: number) => ({ sum: a + b }), |
| 83 | + }) |
| 84 | + |
| 85 | + const { modelContext, tools } = createFakeModelContext() |
| 86 | + registerWebMcpTools(collector, { modelContext }) |
| 87 | + |
| 88 | + const result = await tools.get('add')!.execute({ arg0: 2, arg1: 3 }) |
| 89 | + expect(result.isError).toBeUndefined() |
| 90 | + expect(JSON.parse(result.content[0]!.text)).toEqual({ sum: 5 }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('surfaces a thrown error as an isError text result', async () => { |
| 94 | + const collector = createCollector() |
| 95 | + collector.register({ |
| 96 | + name: 'boom', |
| 97 | + type: 'action', |
| 98 | + jsonSerializable: true, |
| 99 | + agent: { description: 'Always fails.' }, |
| 100 | + handler: () => { |
| 101 | + throw new Error('nope') |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + const { modelContext, tools } = createFakeModelContext() |
| 106 | + registerWebMcpTools(collector, { modelContext }) |
| 107 | + |
| 108 | + const result = await tools.get('boom')!.execute({}) |
| 109 | + expect(result.isError).toBe(true) |
| 110 | + expect(result.content[0]!.text).toBe('Error: nope') |
| 111 | + }) |
| 112 | + |
| 113 | + it('follows later register/update calls until disposed', async () => { |
| 114 | + const collector = createCollector() |
| 115 | + const { modelContext, tools } = createFakeModelContext() |
| 116 | + const dispose = registerWebMcpTools(collector, { modelContext }) |
| 117 | + expect(tools.size).toBe(0) |
| 118 | + |
| 119 | + collector.register({ |
| 120 | + name: 'greet', |
| 121 | + jsonSerializable: true, |
| 122 | + agent: { description: 'Greet.' }, |
| 123 | + handler: () => 'hi', |
| 124 | + }) |
| 125 | + expect(tools.has('greet')).toBe(true) |
| 126 | + |
| 127 | + collector.update({ |
| 128 | + name: 'greet', |
| 129 | + jsonSerializable: true, |
| 130 | + agent: { description: 'Greet politely.' }, |
| 131 | + handler: () => 'good day', |
| 132 | + }) |
| 133 | + expect(tools.get('greet')!.description).toBe('Greet politely.') |
| 134 | + const updated = await tools.get('greet')!.execute({}) |
| 135 | + expect(updated.content[0]!.text).toBe('good day') |
| 136 | + |
| 137 | + dispose() |
| 138 | + expect(tools.size).toBe(0) |
| 139 | + |
| 140 | + // Post-dispose registrations no longer reach the model context. |
| 141 | + collector.register({ |
| 142 | + name: 'late', |
| 143 | + jsonSerializable: true, |
| 144 | + agent: { description: 'Too late.' }, |
| 145 | + handler: () => 'late', |
| 146 | + }) |
| 147 | + expect(tools.size).toBe(0) |
| 148 | + }) |
| 149 | + |
| 150 | + it('unregisters through a legacy handle when registerTool returns one', () => { |
| 151 | + const unregister = vi.fn() |
| 152 | + const modelContext: WebMcpModelContext = { |
| 153 | + registerTool: () => ({ unregister }), |
| 154 | + } |
| 155 | + const collector = createCollector() |
| 156 | + collector.register({ |
| 157 | + name: 'legacy', |
| 158 | + jsonSerializable: true, |
| 159 | + agent: { description: 'Legacy handle.' }, |
| 160 | + handler: () => 'ok', |
| 161 | + }) |
| 162 | + |
| 163 | + const dispose = registerWebMcpTools(collector, { modelContext }) |
| 164 | + dispose() |
| 165 | + expect(unregister).toHaveBeenCalledTimes(1) |
| 166 | + }) |
| 167 | + |
| 168 | + it('is a no-op without a model context', () => { |
| 169 | + const collector = createCollector() |
| 170 | + collector.register({ |
| 171 | + name: 'greet', |
| 172 | + jsonSerializable: true, |
| 173 | + agent: { description: 'Greet.' }, |
| 174 | + handler: () => 'hi', |
| 175 | + }) |
| 176 | + expect(() => registerWebMcpTools(collector)()).not.toThrow() |
| 177 | + }) |
| 178 | +}) |
0 commit comments