Files
SnowLuma/.pnpm-store/v11/files/d8/cad6d875f28d3054c6b9a9e7f071c11b1215166f78d21d0e4e2db2459b39a02efe5ed4ae0cd7b294ecbb985aa5f1d13f9270968ffb9984e50132e773102f92
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

47 lines
1.6 KiB
Plaintext

/**
* Example: Progress notifications over stdio.
*
* Demonstrates a tool that reports progress to the client while processing.
*
* Run:
* npx tsx src/examples/server/progressExample.ts
*
* Then connect a client with an `onprogress` callback (see docs/protocol.md).
*/
import { McpServer } from '../../server/mcp.js';
import { StdioServerTransport } from '../../server/stdio.js';
import { z } from 'zod';
const server = new McpServer({ name: 'progress-example', version: '1.0.0' }, { capabilities: { logging: {} } });
server.registerTool('count', {
description: 'Count to N with progress updates',
inputSchema: { n: z.number().int().min(1).max(100) }
}, async ({ n }, extra) => {
for (let i = 1; i <= n; i++) {
if (extra.signal.aborted) {
return { content: [{ type: 'text', text: `Cancelled at ${i}` }], isError: true };
}
if (extra._meta?.progressToken !== undefined) {
await extra.sendNotification({
method: 'notifications/progress',
params: {
progressToken: extra._meta.progressToken,
progress: i,
total: n,
message: `Counting: ${i}/${n}`
}
});
}
// Simulate work
await new Promise(resolve => setTimeout(resolve, 100));
}
return { content: [{ type: 'text', text: `Counted to ${n}` }] };
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(error => {
console.error('Server error:', error);
process.exit(1);
});
//# sourceMappingURL=progressExample.js.map