Open History to see the last commit
1import { beforeAll, describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3
4import { GPT_4O } from '../../packages/rate-card/src/fixtures/gpt-4o.ts'
5import { llmsTxt } from './llms-txt.ts'
6import { getDb } from '../db/index.ts'
7import type { Db } from '../db/index.ts'
8import { endpoints, models, providers, schemaVersions } from '../db/schema.ts'
9import { handleMcpRequest } from './mcp.ts'
10
11const NOW = 1_781_150_000
12const SCHEMA = {
13 type: 'object',
14 required: ['model'],
15 properties: { model: { type: 'string' } },
16}
17let db: Db
18
19function rpc(method: string, params?: Record<string, unknown>, id = 1) {
20 return new Request('http://mcp.test/mcp', {
21 method: 'POST',
22 headers: { 'Content-Type': 'application/json' },
23 body: JSON.stringify({ jsonrpc: '2.0', id, method, params }),
24 })
25}
26
27interface ToolCallResult {
28 result: { content: Array<{ text: string }>; isError: boolean }
29}
30
31async function callTool(name: string, args: Record<string, unknown>) {
32 const response = await handleMcpRequest(
33 db,
34 rpc('tools/call', { name, arguments: args }),
35 )
36 const body = (await response.json()) as ToolCallResult
37 return {
38 isError: body.result.isError,
39 data: JSON.parse(body.result.content[0]?.text ?? 'null') as unknown,
40 }
41}
42
43beforeAll(async () => {
44 db = getDb(env)
45 await db.insert(providers).values({
46 id: 'mcp-prov',
47 displayName: 'MCP Prov',
48 specSourceUrl: 'https://example.com/spec.json',
49 })
50 await db.insert(models).values({
51 id: 'mcp-prov-modelzilla',
52 providerId: 'mcp-prov',
53 rawId: 'modelzilla',
54 activity: 'chat',
55 displayName: 'Modelzilla',
56 reasoning: { mode: 'toggle', mandatory: true },
57 pricing: GPT_4O,
58 firstSeenAt: NOW,
59 lastSeenAt: NOW,
60 })
61 await db.insert(endpoints).values({
62 id: 'mcp-prov/v1/messages',
63 providerId: 'mcp-prov',
64 activity: 'chat',
65 method: 'POST',
66 path: '/v1/messages',
67 })
68 await db.insert(schemaVersions).values({
69 id: 'mcp-prov/v1/messages:input:1',
70 endpointId: 'mcp-prov/v1/messages',
71 kind: 'input',
72 contentHash: 'c'.repeat(64),
73 schema: JSON.stringify(SCHEMA),
74 createdAt: NOW,
75 })
76})
77
78describe('MCP endpoint', () => {
79 it('initializes and lists the tools', async () => {
80 const init = await handleMcpRequest(db, rpc('initialize'))
81 const initBody = (await init.json()) as {
82 result: {
83 protocolVersion: string
84 serverInfo: { name: string }
85 instructions: string
86 }
87 }
88 expect(initBody.result.serverInfo.name).toBe('modelschemas')
89 expect(initBody.result.protocolVersion).toBeTruthy()
90 // Task 11.2: initialize instructions share the llms.txt source.
91 expect(initBody.result.instructions).toBe(llmsTxt)
92
93 const list = await handleMcpRequest(db, rpc('tools/list'))
94 const listBody = (await list.json()) as {
95 result: { tools: Array<{ name: string }> }
96 }
97 expect(listBody.result.tools.map((t) => t.name).sort()).toEqual([
98 'estimate_cost',
99 'get_model',
100 'get_schema',
101 'list_models',
102 'list_schemas',
103 'recent_changes',
104 'validate_payload',
105 ])
106 })
107
108 it('round-trips get_schema', async () => {
109 const { isError, data } = await callTool('get_schema', {
110 provider: 'mcp-prov',
111 activity: 'chat',
112 endpointId: 'v1/messages',
113 })
114 expect(isError).toBe(false)
115 const schema = data as { contentHash: string; schema: unknown }
116 expect(schema.contentHash).toBe('c'.repeat(64))
117 expect(schema.schema).toEqual(SCHEMA)
118 })
119
120 it('serves list_models / get_model / validate_payload / recent_changes', async () => {
121 const listed = await callTool('list_models', { provider: 'mcp-prov' })
122 expect(
123 (listed.data as { models: Array<{ id: string }> }).models.map(
124 (m) => m.id,
125 ),
126 ).toContain('mcp-prov-modelzilla')
127
128 const model = await callTool('get_model', {
129 provider: 'mcp-prov',
130 modelId: 'modelzilla',
131 })
132 expect((model.data as { displayName: string }).displayName).toBe(
133 'Modelzilla',
134 )
135 expect((model.data as { reasoning: unknown }).reasoning).toEqual({
136 mode: 'toggle',
137 mandatory: true,
138 })
139
140 const valid = await callTool('validate_payload', {
141 provider: 'mcp-prov',
142 endpointId: 'v1/messages',
143 payload: { model: 'modelzilla' },
144 })
145 expect((valid.data as { valid: boolean }).valid).toBe(true)
146
147 const invalid = await callTool('validate_payload', {
148 provider: 'mcp-prov',
149 endpointId: 'v1/messages',
150 payload: {},
151 })
152 expect((invalid.data as { valid: boolean }).valid).toBe(false)
153
154 const changes = await callTool('recent_changes', { provider: 'mcp-prov' })
155 expect(changes.isError).toBe(false)
156
157 const estimate = await callTool('estimate_cost', {
158 provider: 'mcp-prov',
159 model: 'modelzilla',
160 usage: { input_tokens: 1200, output_tokens: 400 },
161 })
162 expect(estimate.isError).toBe(false)
163 expect((estimate.data as { usd: number }).usd).toBe(0.007)
164 })
165
166 it('handles errors per protocol', async () => {
167 const unknownTool = await callTool('frobnicate', {})
168 expect(unknownTool.isError).toBe(true)
169
170 const missingPrice = await callTool('estimate_cost', {
171 provider: 'mcp-prov',
172 model: 'nope',
173 })
174 expect(missingPrice.isError).toBe(true)
175 expect((missingPrice.data as { error: string }).error).toBe('unknown_model')
176
177 const missingSchema = await callTool('get_schema', {
178 provider: 'mcp-prov',
179 activity: 'image',
180 endpointId: 'nope',
181 })
182 expect(missingSchema.isError).toBe(true)
183
184 const badMethod = await handleMcpRequest(db, rpc('resources/list'))
185 const body = (await badMethod.json()) as { error: { code: number } }
186 expect(body.error.code).toBe(-32601)
187
188 const notification = await handleMcpRequest(
189 db,
190 rpc('notifications/initialized'),
191 )
192 expect(notification.status).toBe(202)
193
194 const get = await handleMcpRequest(
195 db,
196 new Request('http://mcp.test/mcp', { method: 'GET' }),
197 )
198 expect(get.status).toBe(405)
199 })
200})