Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/mock-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ export interface ScenarioContext {
}

export { createServerStateful } from './stateful';
export { createServerStateless, validateStatelessRequest } from './stateless';
export {
createServerStateless,
validateStatelessRequest,
withRequiredDraftResultFields,
CACHEABLE_RESULT_METHODS
} from './stateless';
export { createServerFor } from './select';
138 changes: 137 additions & 1 deletion src/mock-server/mock-server.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { describe, it, expect } from 'vitest';
import { createServerFor } from './select';
import { createServerStateful } from './stateful';
import { createServerStateless, validateStatelessRequest } from './stateless';
import {
createServerStateless,
validateStatelessRequest,
withRequiredDraftResultFields,
CACHEABLE_RESULT_METHODS
} from './stateless';
import { STATELESS_SPEC_VERSIONS } from '../connection/select';
import { DRAFT_PROTOCOL_VERSION } from '../types';

Expand Down Expand Up @@ -73,6 +78,28 @@ describe('validateStatelessRequest', () => {
expect(v).toMatchObject({ kind: 'route', id: 1, method: 'tools/list' });
});

it('includes the draft-required result members on the server/discover result', () => {
const v = validateStatelessRequest(
{
headers,
body: {
jsonrpc: '2.0',
id: 1,
method: 'server/discover',
params: { _meta: meta }
}
},
{},
[DRAFT_PROTOCOL_VERSION]
);
expect(v).toMatchObject({
kind: 'handled',
body: {
result: { resultType: 'complete', ttlMs: 0, cacheScope: 'private' }
}
});
});

it('rejects versions outside the supported list with -32004 and echoes it', () => {
const v = validateStatelessRequest(
{
Expand Down Expand Up @@ -105,6 +132,53 @@ describe('validateStatelessRequest', () => {
});
});

describe('withRequiredDraftResultFields', () => {
it('stamps resultType "complete" when the handler omitted it', () => {
expect(
withRequiredDraftResultFields('tools/call', { content: [] })
).toEqual({ resultType: 'complete', content: [] });
});

it('adds ttlMs and cacheScope for every cacheable method', () => {
for (const method of CACHEABLE_RESULT_METHODS) {
expect(withRequiredDraftResultFields(method, {})).toEqual({
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private'
});
}
});

it('does not add caching hints to non-cacheable results', () => {
const result = withRequiredDraftResultFields('tools/call', {
content: []
});
expect(result).not.toHaveProperty('ttlMs');
expect(result).not.toHaveProperty('cacheScope');
});

it('preserves members the handler set itself', () => {
expect(
withRequiredDraftResultFields('tools/call', {
resultType: 'input_required',
inputRequests: {}
})
).toMatchObject({ resultType: 'input_required' });
expect(
withRequiredDraftResultFields('tools/list', {
ttlMs: 5000,
cacheScope: 'public',
tools: []
})
).toMatchObject({ ttlMs: 5000, cacheScope: 'public' });
});

it('passes non-object results through untouched', () => {
expect(withRequiredDraftResultFields('tools/call', null)).toBeNull();
expect(withRequiredDraftResultFields('tools/call', [1])).toEqual([1]);
});
});

describe('createServerFor', () => {
it('returns stateful for dated 2025-x versions', () => {
expect(createServerFor('2025-06-18')).toBe(createServerStateful);
Expand Down Expand Up @@ -286,6 +360,68 @@ describe('createServerStateless', () => {
}
});

it('stamps the draft-required result members onto handler results', async () => {
const srv = await createServerStateless({
'tools/list': () => ({ tools: [{ name: 'x' }] }),
'tools/call': () => ({ content: [{ type: 'text', text: 'ok' }] })
});
try {
const list = await post(
srv.url,
{
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: { _meta: meta }
},
headers
);
expect(list.body.result).toMatchObject({
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private',
tools: [{ name: 'x' }]
});

const call = await post(
srv.url,
{
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: { _meta: meta, name: 'x' }
},
headers
);
expect(call.body.result).toMatchObject({ resultType: 'complete' });
expect(call.body.result).not.toHaveProperty('ttlMs');
expect(call.body.result).not.toHaveProperty('cacheScope');
} finally {
await srv.close();
}
});

it('preserves a resultType the handler set itself', async () => {
const srv = await createServerStateless({
'tools/call': () => ({ resultType: 'input_required', inputRequests: {} })
});
try {
const { body } = await post(
srv.url,
{
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: { _meta: meta }
},
headers
);
expect(body.result.resultType).toBe('input_required');
} finally {
await srv.close();
}
});

it('returns -32601 for unknown methods', async () => {
const srv = await createServerStateless({});
try {
Expand Down
49 changes: 46 additions & 3 deletions src/mock-server/stateless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,45 @@ const META_KEYS = [
'io.modelcontextprotocol/clientCapabilities'
] as const;

/**
* Operations whose results the 2026-07-28 revision marks cacheable: servers
* MUST include the caching hints `ttlMs` and `cacheScope` on these results
* (draft `CacheableResult`, server/utilities/caching).
*/
export const CACHEABLE_RESULT_METHODS: ReadonlySet<string> = new Set([
'server/discover',
'tools/list',
'prompts/list',
'resources/list',
'resources/templates/list',
'resources/read'
]);

/**
* Fill in the result members the 2026-07-28 revision requires of servers when
* the handler did not set them itself: every result MUST carry `resultType`,
* and results of the cacheable operations MUST also carry `ttlMs` and
* `cacheScope`. Members the handler set are preserved (e.g. a handler may
* return `resultType: 'input_required'`); only absent (or undefined) ones are
* filled. A scenario that needs to send a deliberately non-conformant result
* must build its own server instead of routing through this mock.
*/
export function withRequiredDraftResultFields(
method: string,
result: unknown
): unknown {
if (result === null || typeof result !== 'object' || Array.isArray(result)) {
return result;
}
const stamped: Record<string, unknown> = { ...result };
stamped.resultType ??= 'complete';
if (CACHEABLE_RESULT_METHODS.has(method)) {
stamped.ttlMs ??= 0;
stamped.cacheScope ??= 'private';
}
return stamped;
}

type IncomingHeaders = Record<string, string | string[] | undefined>;

export type StatelessValidation =
Expand Down Expand Up @@ -114,11 +153,11 @@ export function validateStatelessRequest(
body: {
jsonrpc: '2.0',
id,
result: {
result: withRequiredDraftResultFields(method, {
supportedVersions,
capabilities,
serverInfo: { name: 'conformance-mock-server', version: '1.0.0' }
}
})
}
};
}
Expand Down Expand Up @@ -166,7 +205,11 @@ export async function createServerStateless(
}
try {
const result = await handler(params, req.body as JSONRPCRequest);
return res.json({ jsonrpc: '2.0', id, result });
return res.json({
jsonrpc: '2.0',
id,
result: withRequiredDraftResultFields(method, result)
});
} catch (e) {
return error(500, -32603, e instanceof Error ? e.message : String(e));
}
Expand Down
9 changes: 6 additions & 3 deletions src/scenarios/client/auth/helpers/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import express, { Request, Response, NextFunction } from 'express';
import type { ConformanceCheck } from '../../../../types';
import {
validateStatelessRequest,
withRequiredDraftResultFields,
type ScenarioContext
} from '../../../../mock-server';
import { isStatefulVersion } from '../../../../connection/select';
Expand Down Expand Up @@ -210,16 +211,18 @@ export function createServer(
return res.json({
jsonrpc: '2.0',
id,
result: {
result: withRequiredDraftResultFields(method, {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only happening in the stateless branch of the auth helper

tools: [{ name: 'test-tool', inputSchema: { type: 'object' } }]
}
})
});
}
if (method === 'tools/call') {
return res.json({
jsonrpc: '2.0',
id,
result: { content: [{ type: 'text', text: 'test' }] }
result: withRequiredDraftResultFields(method, {
content: [{ type: 'text', text: 'test' }]
})
});
}
return res.status(404).json({
Expand Down
Loading
Loading