Skip to content

Commit 7a702f5

Browse files
fix(http-base): return valid empty results for unhandled list methods (#476)
The BaseHttpScenario generic fallback answered any unrouted request with only the required draft result fields, so a standard list method a scenario does not route came back without its required list member. http-standard-headers advertises resources but has no resources/templates/list route; the reply's missing resourceTemplates fails schema validation (draft and 2025-11-25 both require it), and strictly-validating clients drop the connection before the scenario's header checks run. Merge schema-valid empty results for the six list-shaped methods (tools, resources, resource templates, prompts, roots, tasks) into the fallback, keyed by a ReadonlyMap so method names that collide with Object.prototype ("constructor", "toString", ...) miss instead of poisoning the reply with a prototype function. tasks/list has a typed result only at 2025-11-25; the empty member is harmless on the draft wire. Non-list results (tools/call, resources/read, prompts/get, ...) have no meaningful empty default and keep the bare stamped fallback, so a route a scenario forgot surfaces instead of being masked. Tests pin the fixed behaviour on all three http scenario classes, the prototype-name lookups, and the spec schema's rejection of the pre-fix payload. Fixes #474
1 parent 4ff474c commit 7a702f5

2 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎src/scenarios/client/draft-result-fields.test.ts‎

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { describe, it, expect } from 'vitest';
22
import { testScenarioContext } from '../../mock-server/testing';
3+
import { withRequiredDraftResultFields } from '../../mock-server';
4+
import { wireSchemaErrors } from '../../validation/wire-schema';
35
import { DRAFT_PROTOCOL_VERSION } from '../../types';
46
import { HttpStandardHeadersScenario } from './http-standard-headers';
57
import {
68
HttpCustomHeadersScenario,
79
HttpInvalidToolHeadersScenario
810
} from './http-custom-headers';
11+
import type { BaseHttpScenario } from './http-base';
912
import { RequestMetadataScenario } from './request-metadata';
1013
import { MRTRClientScenario } from './mrtr-client';
1114
import { JsonSchemaRefDerefScenario } from './json-schema-ref-deref';
@@ -134,7 +137,8 @@ describe('http-standard-headers mock results (2026-07-28)', () => {
134137
cacheable: true
135138
},
136139
// 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.
138142
{ method: 'resources/templates/list', cacheable: true },
139143
{ method: 'prompts/list', cacheable: true },
140144
{
@@ -171,6 +175,167 @@ describe('http-standard-headers mock results (2026-07-28)', () => {
171175
});
172176
});
173177

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+
174339
describe('http-custom-headers mock results (2026-07-28)', () => {
175340
it('carries the draft-required result members', async () => {
176341
const scenario = new HttpCustomHeadersScenario();

‎src/scenarios/client/http-base.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ import {
2020
DRAFT_PROTOCOL_VERSION
2121
} from '../../types.js';
2222

23+
/**
24+
* Schema-valid empty results for the standard list-shaped methods, keyed by
25+
* method. A Map, not an object literal, so a method name that collides with
26+
* Object.prototype ("constructor", "toString", ...) misses instead of
27+
* returning a function. Merged into the generic fallback so a list method a
28+
* scenario does not route still carries its required list member — a bare
29+
* `{}` fails schema validation and strict clients drop the connection before
30+
* the scenario's real checks run (#474). `tasks/list` exists only at
31+
* 2025-11-25 (the draft schema has no ListTasksResult); the empty member is
32+
* harmless on the draft wire. Non-list results (tools/call, resources/read,
33+
* prompts/get, ...) have no meaningful empty default and keep the bare
34+
* stamped fallback, so a route a scenario forgot surfaces instead of being
35+
* masked.
36+
*/
37+
const EMPTY_LIST_RESULTS: ReadonlyMap<string, object> = new Map([
38+
['tools/list', { tools: [] }],
39+
['resources/list', { resources: [] }],
40+
['resources/templates/list', { resourceTemplates: [] }],
41+
['prompts/list', { prompts: [] }],
42+
['roots/list', { roots: [] }],
43+
['tasks/list', { tasks: [] }]
44+
]);
45+
2346
export abstract class BaseHttpScenario implements Scenario {
2447
abstract name: string;
2548
abstract description: string;
@@ -181,8 +204,12 @@ export abstract class BaseHttpScenario implements Scenario {
181204
jsonrpc: '2.0',
182205
id: request.id,
183206
// Method-aware so cacheable methods that fall through to the generic
184-
// reply still carry the ttlMs/cacheScope the draft revision requires.
185-
result: withRequiredDraftResultFields(request.method, {})
207+
// reply still carry the ttlMs/cacheScope the draft revision requires,
208+
// and unrouted standard list methods carry their required list member.
209+
result: withRequiredDraftResultFields(
210+
request.method,
211+
EMPTY_LIST_RESULTS.get(request.method) ?? {}
212+
)
186213
});
187214
}
188215
}

0 commit comments

Comments
 (0)