fix(server): propagate negotiated protocol version to transport (#1660)

Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com>
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
Luis Reche 2026-03-31 18:27:40 +02:00 • committed by GitHub
commit 689148dc72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@modelcontextprotocol/server': patch
---
fix(server): propagate negotiated protocol version to transport in _oninitialize

View file

@ -433,6 +433,8 @@ export class Server extends Protocol<ServerContext> {
? requestedVersion
: (this._supportedProtocolVersions[0] ?? LATEST_PROTOCOL_VERSION);
this.transport?.setProtocolVersion?.(protocolVersion);
return {
protocolVersion,
capabilities: this.getCapabilities(),

View file

@ -0,0 +1,42 @@
import type { JSONRPCMessage } from '@modelcontextprotocol/core';
import { InMemoryTransport, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core';
import { Server } from '../../src/server/server.js';
describe('Server', () => {
describe('_oninitialize', () => {
it('should propagate negotiated protocol version to transport', async () => {
const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: {} });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const setProtocolVersion = vi.fn();
(serverTransport as { setProtocolVersion?: (version: string) => void }).setProtocolVersion = setProtocolVersion;
await server.connect(serverTransport);
// Collect response from the server
const responsePromise = new Promise<JSONRPCMessage>(resolve => {
clientTransport.onmessage = msg => resolve(msg);
});
await clientTransport.start();
// Send initialize request directly
await clientTransport.send({
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: LATEST_PROTOCOL_VERSION,
capabilities: {},
clientInfo: { name: 'test-client', version: '1.0.0' }
}
} as JSONRPCMessage);
await responsePromise;
expect(setProtocolVersion).toHaveBeenCalledWith(LATEST_PROTOCOL_VERSION);
await server.close();
});
});
});