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
1 changed files with 69131 additions and 9001 deletions
71
examples/streaming/server.ts
Normal file
71
examples/streaming/server.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* In-flight channels: progress, logging, cancellation.
|
||||
*
|
||||
* The `countdown` tool emits a `notifications/progress` per step (when the
|
||||
* call carried a `_meta.progressToken`), a logging notification per step
|
||||
* (when the server has the `logging` capability), and stops promptly when the
|
||||
* client cancels (`ctx.mcpReq.signal.aborted`). 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: 'streaming-example', version: '1.0.0' }, { capabilities: { logging: {} } });
|
||||
|
||||
server.registerTool(
|
||||
'countdown',
|
||||
{
|
||||
description: 'Counts down from N, emitting progress + log per step; stops on cancellation',
|
||||
inputSchema: z.object({ n: z.number().int().min(1).max(50), delayMs: z.number().int().min(0).default(50) }),
|
||||
outputSchema: z.object({ completed: z.number(), total: z.number(), cancelled: z.boolean() })
|
||||
},
|
||||
async ({ n, delayMs }, ctx) => {
|
||||
const progressToken = ctx.mcpReq._meta?.progressToken;
|
||||
let completed = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (ctx.mcpReq.signal.aborted) break;
|
||||
await new Promise(r => setTimeout(r, delayMs));
|
||||
completed++;
|
||||
if (progressToken !== undefined) {
|
||||
await ctx.mcpReq.notify({
|
||||
method: 'notifications/progress',
|
||||
params: { progressToken, progress: completed, total: n, message: `step ${completed}/${n}` }
|
||||
});
|
||||
}
|
||||
// Send the log message as a request-tied notification so it
|
||||
// rides the same response stream as the progress notification
|
||||
// (the connection-level `ctx.mcpReq.log` shorthand sends an
|
||||
// unrelated notification, which a per-request HTTP entry
|
||||
// cannot deliver mid-call).
|
||||
await ctx.mcpReq.notify({
|
||||
method: 'notifications/message',
|
||||
params: { level: 'info', logger: 'countdown', data: `countdown step ${completed}/${n}` }
|
||||
});
|
||||
}
|
||||
const structuredContent = { completed, total: n, cancelled: ctx.mcpReq.signal.aborted };
|
||||
return {
|
||||
content: [{ type: 'text', text: `completed ${completed}/${n}${structuredContent.cancelled ? ' (cancelled)' : ''}` }],
|
||||
structuredContent
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
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