- Update client initialization check to accept both 2025-06-18 and 2025-11-25 - Server echoes back client's version if valid (proper version negotiation) - Fixes compatibility with SDK 1.25.1+ which sends 2025-11-25 🦉 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
146 lines
3.7 KiB
TypeScript
146 lines
3.7 KiB
TypeScript
import http from 'http';
|
|
import { Scenario, ScenarioUrls, ConformanceCheck } from '../../types';
|
|
import { clientChecks } from '../../checks/index';
|
|
|
|
export class InitializeScenario implements Scenario {
|
|
name = 'initialize';
|
|
description = 'Tests MCP client initialization handshake';
|
|
|
|
private server: http.Server | null = null;
|
|
private checks: ConformanceCheck[] = [];
|
|
private port: number = 0;
|
|
|
|
async start(): Promise<ScenarioUrls> {
|
|
return new Promise((resolve, reject) => {
|
|
this.server = http.createServer((req, res) => {
|
|
this.handleRequest(req, res);
|
|
});
|
|
|
|
this.server.on('error', reject);
|
|
|
|
this.server.listen(0, () => {
|
|
const address = this.server!.address();
|
|
if (address && typeof address === 'object') {
|
|
this.port = address.port;
|
|
resolve({
|
|
serverUrl: `http://localhost:${this.port}`
|
|
});
|
|
} else {
|
|
reject(new Error('Failed to get server address'));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
async stop(): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
if (this.server) {
|
|
this.server.close((err) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
this.server = null;
|
|
resolve();
|
|
}
|
|
});
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
}
|
|
|
|
getChecks(): ConformanceCheck[] {
|
|
return this.checks;
|
|
}
|
|
|
|
private handleRequest(
|
|
req: http.IncomingMessage,
|
|
res: http.ServerResponse
|
|
): void {
|
|
let body = '';
|
|
|
|
req.on('data', (chunk) => {
|
|
body += chunk.toString();
|
|
});
|
|
|
|
req.on('end', () => {
|
|
try {
|
|
const request = JSON.parse(body);
|
|
|
|
if (request.method === 'initialize') {
|
|
this.handleInitialize(request, res);
|
|
} else if (request.method === 'tools/list') {
|
|
this.handleToolsList(request, res);
|
|
} else {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(
|
|
JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: request.id,
|
|
result: {}
|
|
})
|
|
);
|
|
}
|
|
} catch (error) {
|
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
res.end(
|
|
JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
error: {
|
|
code: -32700,
|
|
message: `Parse error ${error}`
|
|
}
|
|
})
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
private handleInitialize(request: any, res: http.ServerResponse): void {
|
|
const initializeRequest = request.params;
|
|
|
|
const check =
|
|
clientChecks.createClientInitializationCheck(initializeRequest);
|
|
this.checks.push(check);
|
|
|
|
const serverInfo = {
|
|
name: 'test-server',
|
|
version: '1.0.0'
|
|
};
|
|
|
|
this.checks.push(clientChecks.createServerInfoCheck(serverInfo));
|
|
|
|
// Echo back client's version if valid, otherwise use latest
|
|
const VALID_VERSIONS = ['2025-06-18', '2025-11-25'];
|
|
const clientVersion = initializeRequest?.protocolVersion;
|
|
const responseVersion = VALID_VERSIONS.includes(clientVersion)
|
|
? clientVersion
|
|
: '2025-11-25';
|
|
|
|
const response = {
|
|
jsonrpc: '2.0',
|
|
id: request.id,
|
|
result: {
|
|
protocolVersion: responseVersion,
|
|
serverInfo,
|
|
capabilities: {}
|
|
}
|
|
};
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify(response));
|
|
}
|
|
|
|
private handleToolsList(request: any, res: http.ServerResponse): void {
|
|
const response = {
|
|
jsonrpc: '2.0',
|
|
id: request.id,
|
|
result: {
|
|
tools: []
|
|
}
|
|
};
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify(response));
|
|
}
|
|
}
|