feat(core): custom-method support (3-arg setRequestHandler + request schema overload) (#1974)
This commit is contained in:
parent
e15a8ef3be
commit
db83829c5b
2 changed files with 600 additions and 70 deletions
25
examples/client/src/customMethodExample.ts
Normal file
25
examples/client/src/customMethodExample.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* Custom (non-spec) method example: a client that sends `acme/search` and
|
||||
* listens for `acme/searchProgress` notifications.
|
||||
*
|
||||
* Build `examples/server` first; this client spawns the server via stdio.
|
||||
*/
|
||||
import { Client } from '@modelcontextprotocol/client';
|
||||
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const SearchResult = z.object({ items: z.array(z.string()) });
|
||||
const SearchProgressParams = z.object({ stage: z.string(), pct: z.number() });
|
||||
|
||||
const client = new Client({ name: 'acme-search-client', version: '0.0.0' });
|
||||
|
||||
client.setNotificationHandler('acme/searchProgress', { params: SearchProgressParams }, params => {
|
||||
console.log(`[progress] ${params.stage} ${Math.round(params.pct * 100)}%`);
|
||||
});
|
||||
|
||||
await client.connect(new StdioClientTransport({ command: 'node', args: ['../server/dist/customMethodExample.js'] }));
|
||||
|
||||
const result = await client.request({ method: 'acme/search', params: { query: 'mcp', limit: 3 } }, SearchResult);
|
||||
console.log('items:', result.items);
|
||||
|
||||
await client.close();
|
||||
23
examples/server/src/customMethodExample.ts
Normal file
23
examples/server/src/customMethodExample.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/**
|
||||
* Custom (non-spec) method example: a server that handles a vendor-prefixed
|
||||
* `acme/search` request and emits `acme/searchProgress` notifications.
|
||||
*
|
||||
* Spawned via stdio by `examples/client/src/customMethodExample.ts`; do not run standalone.
|
||||
*/
|
||||
import { McpServer } from '@modelcontextprotocol/server';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const SearchParams = z.object({ query: z.string(), limit: z.number().int().default(10) });
|
||||
const SearchResult = z.object({ items: z.array(z.string()) });
|
||||
|
||||
const mcp = new McpServer({ name: 'acme-search', version: '0.0.0' });
|
||||
|
||||
mcp.server.setRequestHandler('acme/search', { params: SearchParams, result: SearchResult }, async (params, ctx) => {
|
||||
await ctx.mcpReq.notify({ method: 'acme/searchProgress', params: { stage: 'start', pct: 0 } });
|
||||
const items = Array.from({ length: params.limit }, (_, i) => `${params.query}-${i}`);
|
||||
await ctx.mcpReq.notify({ method: 'acme/searchProgress', params: { stage: 'done', pct: 1 } });
|
||||
return { items };
|
||||
});
|
||||
|
||||
await mcp.connect(new StdioServerTransport());
|
||||
Loading…
Reference in a new issue