-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathdraft-result-fields.test.ts
More file actions
365 lines (343 loc) · 10.8 KB
/
Copy pathdraft-result-fields.test.ts
File metadata and controls
365 lines (343 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { describe, it, expect } from 'vitest';
import { testScenarioContext } from '../../mock-server/testing';
import { DRAFT_PROTOCOL_VERSION } from '../../types';
import { HttpStandardHeadersScenario } from './http-standard-headers';
import {
HttpCustomHeadersScenario,
HttpInvalidToolHeadersScenario
} from './http-custom-headers';
import { RequestMetadataScenario } from './request-metadata';
import { MRTRClientScenario } from './mrtr-client';
import { JsonSchemaRefDerefScenario } from './json-schema-ref-deref';
/**
* Pins that the hand-rolled mock servers used by client-direction scenarios
* at 2026-07-28 return spec-valid results: every result carries `resultType`,
* and the cacheable list/read/discover results also carry `ttlMs` and
* `cacheScope`. Without these, a strictly-conforming client is failed by the
* suite's own non-conformant mock. The shared stateless mock is covered in
* src/mock-server/mock-server.test.ts.
*/
const meta = {
'io.modelcontextprotocol/protocolVersion': DRAFT_PROTOCOL_VERSION,
'io.modelcontextprotocol/clientInfo': { name: 'test', version: '1.0' },
'io.modelcontextprotocol/clientCapabilities': {}
};
async function post(
url: string,
body: object,
headers: Record<string, string> = {}
) {
const r = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...headers },
body: JSON.stringify(body)
});
return { status: r.status, body: await r.json() };
}
const CACHEABLE_FIELDS = {
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private'
};
const DISCOVER_FIELDS = {
...CACHEABLE_FIELDS,
supportedVersions: [DRAFT_PROTOCOL_VERSION]
};
describe('hand-rolled mock servers serve server/discover (2026-07-28)', () => {
const cases = [
{
name: 'http-standard-headers',
make: () => new HttpStandardHeadersScenario(),
capabilities: { tools: {}, resources: {}, prompts: {} }
},
{
name: 'http-custom-headers',
make: () => new HttpCustomHeadersScenario(),
capabilities: { tools: {} }
},
{
name: 'http-invalid-tool-headers',
make: () => new HttpInvalidToolHeadersScenario(),
capabilities: { tools: {} }
},
{
name: 'sep-2322-client-request-state',
make: () => new MRTRClientScenario(),
capabilities: { tools: {} }
},
{
name: 'json-schema-ref-no-deref',
make: () => new JsonSchemaRefDerefScenario(),
capabilities: { tools: {} }
}
];
for (const c of cases) {
it(`${c.name} returns a valid DiscoverResult`, async () => {
const scenario = c.make();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
try {
const { status, body } = await post(
serverUrl,
{
jsonrpc: '2.0',
id: 1,
method: 'server/discover',
params: { _meta: meta }
},
{ 'mcp-protocol-version': DRAFT_PROTOCOL_VERSION }
);
expect(status).toBe(200);
expect(body.result).toMatchObject({
...DISCOVER_FIELDS,
capabilities: c.capabilities
});
expect(body.result.serverInfo?.name).toBeTypeOf('string');
} finally {
await scenario.stop();
}
});
}
});
describe('http-standard-headers mock results (2026-07-28)', () => {
it('carries the draft-required result members on every handled method', async () => {
const scenario = new HttpStandardHeadersScenario();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
try {
const cases: Array<{
method: string;
params?: object;
cacheable: boolean;
}> = [
{ method: 'initialize', cacheable: false },
{ method: 'tools/list', cacheable: true },
{
method: 'tools/call',
params: { name: 'test_headers', arguments: {} },
cacheable: false
},
{ method: 'resources/list', cacheable: true },
{
method: 'resources/read',
params: { uri: 'file:///path/to/file%20name.txt' },
cacheable: true
},
// Not explicitly handled by the scenario — exercises the method-aware
// generic fallback, which must still add the caching hints.
{ method: 'resources/templates/list', cacheable: true },
{ method: 'prompts/list', cacheable: true },
{
method: 'prompts/get',
params: { name: 'test_prompt' },
cacheable: false
},
{ method: 'unknown/method', cacheable: false }
];
let id = 1;
for (const c of cases) {
const { status, body } = await post(
serverUrl,
{
jsonrpc: '2.0',
id: id++,
method: c.method,
params: c.params ?? {}
},
{ 'Mcp-Method': c.method }
);
expect(status, c.method).toBe(200);
expect(body.result.resultType, c.method).toBe('complete');
if (c.cacheable) {
expect(body.result, c.method).toMatchObject({
ttlMs: 0,
cacheScope: 'private'
});
}
}
} finally {
await scenario.stop();
}
});
});
describe('http-custom-headers mock results (2026-07-28)', () => {
it('carries the draft-required result members', async () => {
const scenario = new HttpCustomHeadersScenario();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
try {
const list = await post(serverUrl, {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
});
expect(list.body.result).toMatchObject({
resultType: 'complete',
cacheScope: 'private'
});
expect(list.body.result.ttlMs).toBeGreaterThan(0);
const call = await post(serverUrl, {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: {
name: 'test_custom_headers',
arguments: { region: 'us-east', priority: 1, query: 'q' }
}
});
expect(call.body.result.resultType).toBe('complete');
} finally {
await scenario.stop();
}
});
});
describe('http-invalid-tool-headers mock results (2026-07-28)', () => {
it('carries the draft-required result members', async () => {
const scenario = new HttpInvalidToolHeadersScenario();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
try {
const list = await post(serverUrl, {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
});
expect(list.body.result).toMatchObject(CACHEABLE_FIELDS);
const call = await post(serverUrl, {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: { name: 'valid_tool', arguments: { region: 'us-east' } }
});
expect(call.body.result.resultType).toBe('complete');
} finally {
await scenario.stop();
}
});
});
describe('request-metadata mock results (2026-07-28)', () => {
it('carries the draft-required result members after the simulated rejection', async () => {
const scenario = new RequestMetadataScenario();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
const headers = { 'MCP-Protocol-Version': DRAFT_PROTOCOL_VERSION };
try {
// The first request is always answered with the simulated -32022
// rejection (retry probe); results are served from the second on.
const first = await post(
serverUrl,
{
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: { _meta: meta }
},
headers
);
expect(first.status).toBe(400);
const discover = await post(
serverUrl,
{
jsonrpc: '2.0',
id: 2,
method: 'server/discover',
params: { _meta: meta }
},
headers
);
expect(discover.body.result).toMatchObject(CACHEABLE_FIELDS);
const list = await post(
serverUrl,
{
jsonrpc: '2.0',
id: 3,
method: 'tools/list',
params: { _meta: meta }
},
headers
);
expect(list.body.result).toMatchObject(CACHEABLE_FIELDS);
const call = await post(
serverUrl,
{
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: { _meta: meta, name: 'x' }
},
headers
);
expect(call.body.result.resultType).toBe('complete');
const other = await post(
serverUrl,
{ jsonrpc: '2.0', id: 5, method: 'ping', params: { _meta: meta } },
headers
);
expect(other.body.result.resultType).toBe('complete');
} finally {
await scenario.stop();
}
});
});
describe('sep-2322-client-request-state mock results (2026-07-28)', () => {
it('carries the draft-required result members on conformant results and keeps the deliberate omission', async () => {
const scenario = new MRTRClientScenario();
const { serverUrl } = await scenario.start(
testScenarioContext(DRAFT_PROTOCOL_VERSION)
);
try {
const list = await post(serverUrl, {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
});
expect(list.body.result).toMatchObject(CACHEABLE_FIELDS);
const initial = await post(serverUrl, {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: { name: 'test_mrtr_echo_state', arguments: {} }
});
expect(initial.body.result.resultType).toBe('input_required');
const retry = await post(serverUrl, {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'test_mrtr_echo_state',
arguments: {},
inputResponses: { confirm: { action: 'accept' } },
requestState: initial.body.result.requestState
}
});
expect(retry.body.result.resultType).toBe('complete');
const unrelated = await post(serverUrl, {
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: { name: 'test_mrtr_unrelated', arguments: {} }
});
expect(unrelated.body.result.resultType).toBe('complete');
// The default-resultType probe deliberately omits resultType — that is
// the check's stimulus and must not be "fixed".
const noResultType = await post(serverUrl, {
jsonrpc: '2.0',
id: 5,
method: 'tools/call',
params: { name: 'test_mrtr_no_result_type', arguments: {} }
});
expect(noResultType.body.result.content).toBeDefined();
expect(noResultType.body.result).not.toHaveProperty('resultType');
} finally {
await scenario.stop();
}
});
});