move scenarios to be client based
This commit is contained in:
parent
40f16c265d
commit
2644074125
3 changed files with 6 additions and 6 deletions
136
src/scenarios/client/initialize.ts
Normal file
136
src/scenarios/client/initialize.ts
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
import http from 'http';
|
||||||
|
import { Scenario, ScenarioUrls, ConformanceCheck } from '../../types.js';
|
||||||
|
import { createClientInitializationCheck, createServerInfoCheck } from '../../checks.js';
|
||||||
|
|
||||||
|
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 = createClientInitializationCheck(initializeRequest);
|
||||||
|
this.checks.push(check);
|
||||||
|
|
||||||
|
const serverInfo = {
|
||||||
|
name: 'test-server',
|
||||||
|
version: '1.0.0'
|
||||||
|
};
|
||||||
|
|
||||||
|
const serverInfoCheck = createServerInfoCheck(serverInfo);
|
||||||
|
this.checks.push(serverInfoCheck);
|
||||||
|
|
||||||
|
const response = {
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
id: request.id,
|
||||||
|
result: {
|
||||||
|
protocolVersion: '2025-06-18',
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
172
src/scenarios/client/tools_call.ts
Normal file
172
src/scenarios/client/tools_call.ts
Normal file
|
|
@ -0,0 +1,172 @@
|
||||||
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||||
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||||
|
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
||||||
|
import type { Scenario, ConformanceCheck } from '../../types.js';
|
||||||
|
import express from 'express';
|
||||||
|
import { ScenarioUrls } from '../../types.js';
|
||||||
|
|
||||||
|
function createServer(checks: ConformanceCheck[]): express.Application {
|
||||||
|
const server = new Server(
|
||||||
|
{
|
||||||
|
name: 'add-numbers-server',
|
||||||
|
version: '1.0.0'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
capabilities: {
|
||||||
|
tools: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||||
|
return {
|
||||||
|
tools: [
|
||||||
|
{
|
||||||
|
name: 'add_numbers',
|
||||||
|
description: 'Add two numbers together',
|
||||||
|
inputSchema: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
a: {
|
||||||
|
type: 'number',
|
||||||
|
description: 'First number'
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: 'number',
|
||||||
|
description: 'Second number'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
required: ['a', 'b']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
server.setRequestHandler(CallToolRequestSchema, async request => {
|
||||||
|
if (request.params.name === 'add_numbers') {
|
||||||
|
const { a, b } = request.params.arguments as { a: number; b: number };
|
||||||
|
const result = a + b;
|
||||||
|
|
||||||
|
checks.push({
|
||||||
|
id: 'tool-add-numbers',
|
||||||
|
name: 'ToolAddNumbers',
|
||||||
|
description: 'Validates that the add_numbers tool works correctly',
|
||||||
|
status: 'SUCCESS',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
specReferences: [
|
||||||
|
{
|
||||||
|
id: 'MCP-Tools',
|
||||||
|
url: 'https://modelcontextprotocol.io/specification/2025-06-18/server/tools#calling-tools'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
details: {
|
||||||
|
a,
|
||||||
|
b,
|
||||||
|
result
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
text: `The sum of ${a} and ${b} is ${result}`
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unknown tool: ${request.params.name}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
// Log incoming requests for debugging
|
||||||
|
// console.log(`Incoming request: ${req.method} ${req.url}`);
|
||||||
|
checks.push({
|
||||||
|
id: 'incoming-request',
|
||||||
|
name: 'IncomingRequest',
|
||||||
|
description: `Received ${req.method} request for ${req.url}`,
|
||||||
|
status: 'INFO',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
details: {
|
||||||
|
body: JSON.stringify(req.body)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
next();
|
||||||
|
checks.push({
|
||||||
|
id: 'outgoing-response',
|
||||||
|
name: 'OutgoingResponse',
|
||||||
|
// TODO: include MCP method?
|
||||||
|
description: `Sent ${res.statusCode} response`,
|
||||||
|
status: 'INFO',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
details: {
|
||||||
|
// TODO: this isn't working
|
||||||
|
body: JSON.stringify(res.body)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/mcp', async (req, res) => {
|
||||||
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
sessionIdGenerator: undefined
|
||||||
|
});
|
||||||
|
await server.connect(transport);
|
||||||
|
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ToolsCallScenario implements Scenario {
|
||||||
|
name = 'tools_call';
|
||||||
|
private app: express.Application | null = null;
|
||||||
|
private httpServer: any = null;
|
||||||
|
private checks: ConformanceCheck[] = [];
|
||||||
|
|
||||||
|
async start(): Promise<ScenarioUrls> {
|
||||||
|
this.checks = [];
|
||||||
|
this.app = createServer(this.checks);
|
||||||
|
this.httpServer = this.app.listen(0);
|
||||||
|
const port = this.httpServer.address().port;
|
||||||
|
return { serverUrl: `http://localhost:${port}/mcp` };
|
||||||
|
}
|
||||||
|
|
||||||
|
async stop() {
|
||||||
|
if (this.httpServer) {
|
||||||
|
await new Promise(resolve => this.httpServer.close(resolve));
|
||||||
|
this.httpServer = null;
|
||||||
|
}
|
||||||
|
this.app = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getChecks(): ConformanceCheck[] {
|
||||||
|
const expectedSlugs = ['tool-add-numbers'];
|
||||||
|
// add a failure if not in there already
|
||||||
|
for (const slug of expectedSlugs) {
|
||||||
|
if (!this.checks.find(c => c.id === slug)) {
|
||||||
|
// TODO: this is duplicated from above, refactor
|
||||||
|
this.checks.push({
|
||||||
|
id: slug,
|
||||||
|
name: `ToolAddNumbers`,
|
||||||
|
description: `Validates that the add_numbers tool works correctly`,
|
||||||
|
status: 'FAILURE',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
details: { message: 'Tool was not called by client' },
|
||||||
|
specReferences: [
|
||||||
|
{
|
||||||
|
id: 'MCP-Tools',
|
||||||
|
url: 'https://modelcontextprotocol.io/specification/2025-06-18/server/tools#calling-tools'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.checks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { Scenario } from '../types.js';
|
import { Scenario } from '../types.js';
|
||||||
import { InitializeScenario } from './initialize.js';
|
import { InitializeScenario } from './client/initialize.js';
|
||||||
import { ToolsCallScenario } from './tools_call.js';
|
import { ToolsCallScenario } from './client/tools_call.js';
|
||||||
|
|
||||||
export const scenarios = new Map<string, Scenario>([
|
export const scenarios = new Map<string, Scenario>([
|
||||||
['initialize', new InitializeScenario()],
|
['initialize', new InitializeScenario()],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue