|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | 2 | import { testScenarioContext } from '../../mock-server/testing'; |
| 3 | +import { withRequiredDraftResultFields } from '../../mock-server'; |
| 4 | +import { wireSchemaErrors } from '../../validation/wire-schema'; |
3 | 5 | import { DRAFT_PROTOCOL_VERSION } from '../../types'; |
4 | 6 | import { HttpStandardHeadersScenario } from './http-standard-headers'; |
5 | 7 | import { |
6 | 8 | HttpCustomHeadersScenario, |
7 | 9 | HttpInvalidToolHeadersScenario |
8 | 10 | } from './http-custom-headers'; |
| 11 | +import type { BaseHttpScenario } from './http-base'; |
9 | 12 | import { RequestMetadataScenario } from './request-metadata'; |
10 | 13 | import { MRTRClientScenario } from './mrtr-client'; |
11 | 14 | import { JsonSchemaRefDerefScenario } from './json-schema-ref-deref'; |
@@ -134,7 +137,8 @@ describe('http-standard-headers mock results (2026-07-28)', () => { |
134 | 137 | cacheable: true |
135 | 138 | }, |
136 | 139 | // Not explicitly handled by the scenario — exercises the method-aware |
137 | | - // generic fallback, which must still add the caching hints. |
| 140 | + // generic fallback, which must still add the caching hints and the |
| 141 | + // required (empty) list member. |
138 | 142 | { method: 'resources/templates/list', cacheable: true }, |
139 | 143 | { method: 'prompts/list', cacheable: true }, |
140 | 144 | { |
@@ -171,6 +175,167 @@ describe('http-standard-headers mock results (2026-07-28)', () => { |
171 | 175 | }); |
172 | 176 | }); |
173 | 177 |
|
| 178 | +describe('generic fallback answers unrouted list methods schema-valid (#474)', () => { |
| 179 | + // http-standard-headers advertises `resources` but has no |
| 180 | + // resources/templates/list route; the request falls through to the generic |
| 181 | + // fallback in BaseHttpScenario. Before the fix the reply had no |
| 182 | + // `resourceTemplates` member, so strictly-validating clients (e.g. |
| 183 | + // cloudflare/agents MCPClientManager) dropped the connection before the |
| 184 | + // scenario's header checks ran. |
| 185 | + it('http-standard-headers answers resources/templates/list with a valid empty result', async () => { |
| 186 | + const scenario = new HttpStandardHeadersScenario(); |
| 187 | + const { serverUrl } = await scenario.start( |
| 188 | + testScenarioContext(DRAFT_PROTOCOL_VERSION) |
| 189 | + ); |
| 190 | + try { |
| 191 | + const { status, body } = await post( |
| 192 | + serverUrl, |
| 193 | + { |
| 194 | + jsonrpc: '2.0', |
| 195 | + id: 1, |
| 196 | + method: 'resources/templates/list', |
| 197 | + params: {} |
| 198 | + }, |
| 199 | + { 'Mcp-Method': 'resources/templates/list' } |
| 200 | + ); |
| 201 | + expect(status).toBe(200); |
| 202 | + expect(body.result).toEqual({ |
| 203 | + ...CACHEABLE_FIELDS, |
| 204 | + resourceTemplates: [] |
| 205 | + }); |
| 206 | + expect( |
| 207 | + wireSchemaErrors( |
| 208 | + DRAFT_PROTOCOL_VERSION, |
| 209 | + body, |
| 210 | + 'resources/templates/list' |
| 211 | + ) |
| 212 | + ).toEqual([]); |
| 213 | + } finally { |
| 214 | + await scenario.stop(); |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + // Every list-shaped method a scenario does not route must reach the |
| 219 | + // fallback and come back with its required (empty) list member, on all |
| 220 | + // three BaseHttpScenario subclasses. http-standard-headers routes the |
| 221 | + // tools/resources/prompts lists itself; the custom-headers scenarios route |
| 222 | + // only initialize and tools/*. tasks/list has no typed result in the draft |
| 223 | + // schema (2025-11-25 only), so it is additionally validated at 2025-11-25. |
| 224 | + const listMember = new Map([ |
| 225 | + ['resources/list', 'resources'], |
| 226 | + ['resources/templates/list', 'resourceTemplates'], |
| 227 | + ['prompts/list', 'prompts'], |
| 228 | + ['roots/list', 'roots'], |
| 229 | + ['tasks/list', 'tasks'] |
| 230 | + ]); |
| 231 | + const subclasses: Array<{ |
| 232 | + name: string; |
| 233 | + make: () => BaseHttpScenario; |
| 234 | + unrouted: string[]; |
| 235 | + }> = [ |
| 236 | + { |
| 237 | + name: 'http-standard-headers', |
| 238 | + make: () => new HttpStandardHeadersScenario(), |
| 239 | + unrouted: ['resources/templates/list', 'roots/list', 'tasks/list'] |
| 240 | + }, |
| 241 | + { |
| 242 | + name: 'http-custom-headers', |
| 243 | + make: () => new HttpCustomHeadersScenario(), |
| 244 | + unrouted: [...listMember.keys()] |
| 245 | + }, |
| 246 | + { |
| 247 | + name: 'http-invalid-tool-headers', |
| 248 | + make: () => new HttpInvalidToolHeadersScenario(), |
| 249 | + unrouted: [...listMember.keys()] |
| 250 | + } |
| 251 | + ]; |
| 252 | + |
| 253 | + for (const s of subclasses) { |
| 254 | + it(`${s.name} answers unrouted list methods with valid empty results`, async () => { |
| 255 | + const scenario = s.make(); |
| 256 | + const { serverUrl } = await scenario.start( |
| 257 | + testScenarioContext(DRAFT_PROTOCOL_VERSION) |
| 258 | + ); |
| 259 | + try { |
| 260 | + let id = 1; |
| 261 | + for (const method of s.unrouted) { |
| 262 | + const { status, body } = await post( |
| 263 | + serverUrl, |
| 264 | + { jsonrpc: '2.0', id: id++, method, params: {} }, |
| 265 | + { 'Mcp-Method': method } |
| 266 | + ); |
| 267 | + expect(status, method).toBe(200); |
| 268 | + expect(body.result[listMember.get(method)!], method).toEqual([]); |
| 269 | + expect( |
| 270 | + wireSchemaErrors(DRAFT_PROTOCOL_VERSION, body, method), |
| 271 | + method |
| 272 | + ).toEqual([]); |
| 273 | + if (method === 'tasks/list') { |
| 274 | + expect(wireSchemaErrors('2025-11-25', body, method)).toEqual([]); |
| 275 | + } |
| 276 | + } |
| 277 | + } finally { |
| 278 | + await scenario.stop(); |
| 279 | + } |
| 280 | + }); |
| 281 | + } |
| 282 | + |
| 283 | + // Method names that collide with Object.prototype must miss the empty-list |
| 284 | + // lookup and get the plain generic result — an object-literal map here once |
| 285 | + // returned Object.prototype functions, which JSON.stringify drops, leaving |
| 286 | + // a reply with neither result nor error. |
| 287 | + for (const s of subclasses) { |
| 288 | + it(`${s.name} answers Object.prototype-colliding method names with a valid generic result`, async () => { |
| 289 | + const scenario = s.make(); |
| 290 | + const { serverUrl } = await scenario.start( |
| 291 | + testScenarioContext(DRAFT_PROTOCOL_VERSION) |
| 292 | + ); |
| 293 | + try { |
| 294 | + let id = 1; |
| 295 | + for (const method of [ |
| 296 | + 'constructor', |
| 297 | + 'toString', |
| 298 | + 'valueOf', |
| 299 | + 'hasOwnProperty' |
| 300 | + ]) { |
| 301 | + const { status, body } = await post( |
| 302 | + serverUrl, |
| 303 | + { jsonrpc: '2.0', id: id++, method, params: {} }, |
| 304 | + { 'Mcp-Method': method } |
| 305 | + ); |
| 306 | + expect(status, method).toBe(200); |
| 307 | + expect(body.result, method).toEqual({ resultType: 'complete' }); |
| 308 | + expect( |
| 309 | + wireSchemaErrors(DRAFT_PROTOCOL_VERSION, body, method), |
| 310 | + method |
| 311 | + ).toEqual([]); |
| 312 | + } |
| 313 | + } finally { |
| 314 | + await scenario.stop(); |
| 315 | + } |
| 316 | + }); |
| 317 | + } |
| 318 | + |
| 319 | + // Regression pin: the pre-fix fallback payload (required draft fields only, |
| 320 | + // no list member) is rejected by the spec schema — proves the assertions |
| 321 | + // above have teeth. |
| 322 | + it('rejects the pre-fix bare fallback result for resources/templates/list', () => { |
| 323 | + const preFix = { |
| 324 | + jsonrpc: '2.0', |
| 325 | + id: 1, |
| 326 | + result: withRequiredDraftResultFields('resources/templates/list', {}) |
| 327 | + }; |
| 328 | + const errors = wireSchemaErrors( |
| 329 | + DRAFT_PROTOCOL_VERSION, |
| 330 | + preFix, |
| 331 | + 'resources/templates/list' |
| 332 | + ); |
| 333 | + expect(errors.join('\n')).toContain( |
| 334 | + "must have required property 'resourceTemplates'" |
| 335 | + ); |
| 336 | + }); |
| 337 | +}); |
| 338 | + |
174 | 339 | describe('http-custom-headers mock results (2026-07-28)', () => { |
175 | 340 | it('carries the draft-required result members', async () => { |
176 | 341 | const scenario = new HttpCustomHeadersScenario(); |
|
0 commit comments