51 lines
2.2 KiB
TypeScript
51 lines
2.2 KiB
TypeScript
/**
|
|
* 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 { serve } from '@hono/node-server';
|
|
import { parseExampleArgs } from '@mcp-examples/shared';
|
|
import { createMcpHonoApp } from '@modelcontextprotocol/hono';
|
|
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);
|
|
// `createMcpHonoApp()` arms localhost host/origin validation by default.
|
|
const app = createMcpHonoApp();
|
|
app.all('/mcp', c => handler.fetch(c.req.raw));
|
|
serve({ fetch: app.fetch, port, hostname: '127.0.0.1' }, () => {
|
|
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
|
|
});
|
|
}
|