Implement MCP 2026-07-28 (#2286)
Co-authored-by: Matt <77928207+mattzcarey@users.noreply.github.com> Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
e4227d135c
commit
1823aae891
4 changed files with 69131 additions and 9001 deletions
5
examples/parallel-calls/README.md
Normal file
5
examples/parallel-calls/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# parallel-calls
|
||||
|
||||
Multiple clients connecting to one endpoint in parallel, and one client making parallel `callTool()` calls — with per-call logging notifications attributed back to their caller.
|
||||
|
||||
Over HTTP every client connects to the one running endpoint; over stdio each client spawns its own server process (so the "one client / parallel calls" leg is the per-call attribution test on either transport).
|
||||
56
examples/parallel-calls/client.ts
Normal file
56
examples/parallel-calls/client.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* Two clients in parallel, each calling the notification-emitting tool, and
|
||||
* one client making two parallel tool calls — asserts every result returns
|
||||
* and that notifications were attributed back to the right caller.
|
||||
*
|
||||
* Over HTTP every client connects to the one running endpoint; over stdio
|
||||
* each `makeClient` spawns its own server process (so the
|
||||
* "multiple clients" leg is per-process, while the "one client / parallel
|
||||
* calls" leg exercises one server's per-call attribution either way).
|
||||
*/
|
||||
import { check, parseExampleArgs, siblingPath } from '@mcp-examples/shared';
|
||||
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
|
||||
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
|
||||
|
||||
async function makeClient(): Promise<{ client: Client; notifications: string[] }> {
|
||||
const { transport, url, era } = parseExampleArgs();
|
||||
|
||||
const client = new Client(
|
||||
{ name: 'parallel-calls-example-client', version: '1.0.0' },
|
||||
{ versionNegotiation: { mode: era === 'modern' ? 'auto' : 'legacy' } }
|
||||
);
|
||||
|
||||
await (transport === 'stdio'
|
||||
? client.connect(new StdioClientTransport({ command: 'npx', args: ['-y', 'tsx', siblingPath(import.meta.url, 'server.ts')] }))
|
||||
: client.connect(new StreamableHTTPClientTransport(new URL(url))));
|
||||
|
||||
const notifications: string[] = [];
|
||||
client.setNotificationHandler('notifications/message', n => {
|
||||
notifications.push(String(n.params.data));
|
||||
});
|
||||
return { client, notifications };
|
||||
}
|
||||
|
||||
// --- multiple clients, one call each ---
|
||||
const [a, b] = await Promise.all([makeClient(), makeClient()]);
|
||||
const [ra, rb] = await Promise.all([
|
||||
a.client.callTool({ name: 'start-notification-stream', arguments: { caller: 'A', count: 3 } }),
|
||||
b.client.callTool({ name: 'start-notification-stream', arguments: { caller: 'B', count: 3 } })
|
||||
]);
|
||||
check.match(ra.content?.[0]?.type === 'text' ? ra.content[0].text : '', /\[A\] done/);
|
||||
check.match(rb.content?.[0]?.type === 'text' ? rb.content[0].text : '', /\[B\] done/);
|
||||
check.ok(a.notifications.every(m => m.includes('[A]')));
|
||||
check.ok(b.notifications.every(m => m.includes('[B]')));
|
||||
check.ok(a.notifications.length >= 3 && b.notifications.length >= 3);
|
||||
await a.client.close();
|
||||
await b.client.close();
|
||||
|
||||
// --- one client, parallel tool calls ---
|
||||
const c = await makeClient();
|
||||
const results = await Promise.all([
|
||||
c.client.callTool({ name: 'start-notification-stream', arguments: { caller: 'C1', count: 2 } }),
|
||||
c.client.callTool({ name: 'start-notification-stream', arguments: { caller: 'C2', count: 2 } })
|
||||
]);
|
||||
check.equal(results.length, 2);
|
||||
check.ok(c.notifications.some(m => m.includes('[C1]')) && c.notifications.some(m => m.includes('[C2]')));
|
||||
await c.client.close();
|
||||
19
examples/parallel-calls/package.json
Normal file
19
examples/parallel-calls/package.json
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "@mcp-examples/parallel-calls",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"server": "tsx server.ts",
|
||||
"client": "tsx client.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mcp-examples/shared": "workspace:*",
|
||||
"@modelcontextprotocol/client": "workspace:*",
|
||||
"@modelcontextprotocol/node": "workspace:*",
|
||||
"@modelcontextprotocol/server": "workspace:*",
|
||||
"zod": "catalog:runtimeShared"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsx": "catalog:devTools"
|
||||
}
|
||||
}
|
||||
49
examples/parallel-calls/server.ts
Normal file
49
examples/parallel-calls/server.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* One notification-emitting tool that the parallel-calls client drives with
|
||||
* multiple concurrent clients (HTTP) or one client / multiple concurrent
|
||||
* calls (both transports), asserting in-flight notifications are attributed
|
||||
* back to the right caller. One binary, either transport.
|
||||
*/
|
||||
import { createServer } from 'node:http';
|
||||
|
||||
import { parseExampleArgs } from '@mcp-examples/shared';
|
||||
import { toNodeHandler } from '@modelcontextprotocol/node';
|
||||
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
|
||||
import { serveStdio } from '@modelcontextprotocol/server/stdio';
|
||||
import * as z from 'zod/v4';
|
||||
|
||||
function buildServer(): McpServer {
|
||||
const server = new McpServer({ name: 'parallel-calls-example', version: '1.0.0' }, { capabilities: { logging: {} } });
|
||||
server.registerTool(
|
||||
'start-notification-stream',
|
||||
{
|
||||
description: 'Sends a few periodic logging notifications tagged with the caller id',
|
||||
inputSchema: z.object({ caller: z.string(), count: z.number().int().min(1).max(20).default(3) })
|
||||
},
|
||||
async ({ caller, count }, ctx) => {
|
||||
for (let i = 1; i <= count; i++) {
|
||||
// Send as a request-tied notification so it rides the same SSE
|
||||
// stream as the eventual result.
|
||||
await ctx.mcpReq.notify({
|
||||
method: 'notifications/message',
|
||||
params: { level: 'info', data: `[${caller}] tick ${i}/${count}` }
|
||||
});
|
||||
await new Promise(r => setTimeout(r, 20));
|
||||
}
|
||||
return { content: [{ type: 'text', text: `[${caller}] done (${count})` }] };
|
||||
}
|
||||
);
|
||||
return server;
|
||||
}
|
||||
|
||||
const { transport, port } = parseExampleArgs();
|
||||
|
||||
if (transport === 'stdio') {
|
||||
void serveStdio(buildServer);
|
||||
console.error('[server] serving over stdio');
|
||||
} else {
|
||||
const handler = createMcpHandler(buildServer);
|
||||
createServer(toNodeHandler(handler)).listen(port, () => {
|
||||
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue