Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/green-dryers-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server": patch
---

fix(server): reject requests before initialization
20 changes: 16 additions & 4 deletions packages/server/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class Server extends Protocol<ServerContext> {
private _capabilities: ServerCapabilities;
private _instructions?: string;
private _jsonSchemaValidator: jsonSchemaValidator;
private _receivedInitialize = false;
private _cacheHints?: ServerOptions['cacheHints'];
private _requestStateVerify?: (state: string, ctx: ServerContext) => unknown | Promise<unknown>;
private _inputRequiredServing: { maxRounds: number; roundTimeoutMs: number; legacyShim: boolean };
Expand Down Expand Up @@ -473,6 +474,17 @@ export class Server extends Protocol<ServerContext> {
method: string,
handler: (request: JSONRPCRequest, ctx: ServerContext) => Promise<Result>
): (request: JSONRPCRequest, ctx: ServerContext) => Promise<Result> {
const lifecycleHandler: (request: JSONRPCRequest, ctx: ServerContext) => Promise<Result> = async (request, ctx) => {
if (!ctx.http && ctx.sessionId === undefined && !this._receivedInitialize && method !== 'initialize' && method !== 'ping') {
throw new ProtocolError(ProtocolErrorCode.InvalidRequest, 'Server not initialized');
}
const result = await handler(request, ctx);
if (method === 'initialize') {
this._receivedInitialize = true;
}
return result;
};

if (method !== 'tools/call') {
const cacheHint = (this._cacheHints as Record<string, CacheHint | undefined> | undefined)?.[method];
const isInputRequiredCapable = INPUT_REQUIRED_CAPABLE_METHODS.has(method);
Expand All @@ -481,7 +493,7 @@ export class Server extends Protocol<ServerContext> {
// whose result vocabulary does not include it is never
// mis-typed onto the wire.
return async (request, ctx) => {
const result = await handler(request, ctx);
const result = await lifecycleHandler(request, ctx);
if (isInputRequiredResult(result)) {
throw new ProtocolError(
ProtocolErrorCode.InternalError,
Expand All @@ -494,8 +506,8 @@ export class Server extends Protocol<ServerContext> {
}
return async (request, ctx) => {
const result = isInputRequiredCapable
? await this._invokeInputRequiredCapableHandler(method, handler, request, ctx)
: await handler(request, ctx);
? await this._invokeInputRequiredCapableHandler(method, lifecycleHandler, request, ctx)
: await lifecycleHandler(request, ctx);
if (isInputRequiredResult(result)) {
if (!isInputRequiredCapable) {
throw new ProtocolError(
Expand Down Expand Up @@ -530,7 +542,7 @@ export class Server extends Protocol<ServerContext> {
);
}

const result = await this._invokeInputRequiredCapableHandler('tools/call', handler, request, ctx);
const result = await this._invokeInputRequiredCapableHandler('tools/call', lifecycleHandler, request, ctx);
if (isInputRequiredResult(result)) {
// Already checked by the seam; the CallToolResult schema does
// not apply to it (no widening — InputRequiredResult travels
Expand Down
68 changes: 68 additions & 0 deletions packages/server/test/server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InMemoryTransport,
isJSONRPCResultResponse,
LATEST_PROTOCOL_VERSION,
ProtocolErrorCode,
SUPPORTED_PROTOCOL_VERSIONS
} from '@modelcontextprotocol/core-internal';
import { Server } from '../../src/server/server';
Expand Down Expand Up @@ -84,6 +85,73 @@ describe('Server', () => {

await server.close();
});

it('rejects requests before initialize', async () => {
const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { tools: {} } });

server.setRequestHandler('tools/list', async () => ({ tools: [] }));

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);

const responses: JSONRPCMessage[] = [];
clientTransport.onmessage = message => responses.push(message);
await clientTransport.start();

await clientTransport.send({
jsonrpc: '2.0',
method: 'notifications/initialized'
} as JSONRPCMessage);

await clientTransport.send({
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
} as JSONRPCMessage);

await vi.waitFor(() => expect(responses.some(message => 'id' in message && message.id === 1)).toBe(true));

const rejected = responses.find(message => 'id' in message && message.id === 1);
expect(rejected).toMatchObject({
error: {
code: ProtocolErrorCode.InvalidRequest,
message: 'Server not initialized'
}
});

await clientTransport.send({
jsonrpc: '2.0',
id: 2,
method: 'initialize',
params: {
protocolVersion: LATEST_PROTOCOL_VERSION,
capabilities: {},
clientInfo: { name: 'test-client', version: '1.0.0' }
}
} as JSONRPCMessage);
await vi.waitFor(() => expect(responses.some(message => 'id' in message && message.id === 2)).toBe(true));

await clientTransport.send({
jsonrpc: '2.0',
method: 'notifications/initialized'
} as JSONRPCMessage);

await clientTransport.send({
jsonrpc: '2.0',
id: 3,
method: 'tools/list',
params: {}
} as JSONRPCMessage);

await vi.waitFor(() => expect(responses.some(message => 'id' in message && message.id === 3)).toBe(true));

expect(responses.find(message => 'id' in message && message.id === 3)).toMatchObject({
result: { tools: [] }
});

await server.close();
});
});

describe('getNegotiatedProtocolVersion', () => {
Expand Down
Loading