Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2
3import { TOOLS } from '../server/mcp.ts'
4import { WEBMCP_TOOLS, registerWebMcp } from './webmcp.ts'
5
6afterEach(() => {
7 vi.unstubAllGlobals()
8})
9
10describe('WebMCP (task 10.6)', () => {
11 it('tool definitions are exact copies of the server MCP tools', () => {
12 for (const webTool of WEBMCP_TOOLS) {
13 const serverTool = TOOLS.find((t) => t.name === webTool.name)
14 expect(serverTool, webTool.name).toBeDefined()
15 expect(webTool.description).toBe(serverTool?.description)
16 expect(webTool.inputSchema).toEqual(serverTool?.inputSchema)
17 }
18 })
19
20 it('no-ops when navigator.modelContext is absent', () => {
21 vi.stubGlobal('navigator', {})
22 expect(registerWebMcp()).toBe(false)
23 })
24
25 it('registers tools and proxies execute through /mcp JSON-RPC', async () => {
26 const provideContext = vi.fn()
27 vi.stubGlobal('navigator', { modelContext: { provideContext } })
28 const fetchMock = vi.fn().mockResolvedValue(
29 new Response(
30 JSON.stringify({
31 jsonrpc: '2.0',
32 id: 1,
33 result: { content: [{ type: 'text', text: '{"models":[]}' }] },
34 }),
35 ),
36 )
37 vi.stubGlobal('fetch', fetchMock)
38
39 expect(registerWebMcp()).toBe(true)
40 const { tools } = provideContext.mock.calls[0]?.[0] as {
41 tools: Array<{
42 name: string
43 execute: (args: Record<string, unknown>) => Promise<unknown>
44 }>
45 }
46 expect(tools.map((t) => t.name)).toEqual([
47 'list_models',
48 'list_schemas',
49 'get_schema',
50 'validate_payload',
51 ])
52
53 const result = await tools[0]?.execute({ activity: 'chat' })
54 expect(result).toEqual({
55 content: [{ type: 'text', text: '{"models":[]}' }],
56 })
57 const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
58 expect(url).toBe('/mcp')
59 expect(JSON.parse(init.body as string)).toMatchObject({
60 method: 'tools/call',
61 params: { name: 'list_models', arguments: { activity: 'chat' } },
62 })
63 })
64})