-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): custom-method support (3-arg setRequestHandler + request schema overload) #1974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
690af0d
feat(core): 3-arg setRequestHandler/setNotificationHandler and reques…
felixweinberger 10a7878
fix: address review — setNotificationHandler 3-arg passes raw notific…
felixweinberger 34a4fd5
fix: address review — assertCapability* params as RequestMethod|strin…
felixweinberger dc09b1d
fix: address review — guard cancel() once response received (abort ra…
felixweinberger 614dbe8
fix: response result-schema mismatch throws SdkError(InvalidResult) i…
felixweinberger d49a02b
docs: add SdkErrorCode.InvalidResult to migration-guide enumerations …
felixweinberger 35a9628
fix: apply RequestMethod|string discoverability hint to public remove…
felixweinberger 9df35c2
Merge branch 'main' into fweinberger/custom-methods-minimal
felixweinberger a2f2f8a
fix: import stdio transports from /stdio subpath in customMethodExamp…
felixweinberger 5568303
Merge branch 'main' into fweinberger/custom-methods-minimal
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@modelcontextprotocol/core': minor | ||
| '@modelcontextprotocol/client': minor | ||
| '@modelcontextprotocol/server': minor | ||
| --- | ||
|
|
||
| Add custom (non-spec) method support: a 3-arg `setRequestHandler(method, schemas, handler)` / `setNotificationHandler(method, schemas, handler)` form for vendor-prefixed methods, and a `request(req, resultSchema)` overload (also on `ctx.mcpReq.send`) for typed custom-method results. Spec-method calls are unchanged. | ||
|
|
||
| Response result-schema validation failure now rejects with `SdkError(InvalidResult)` instead of a raw `ZodError`. Adds `SdkErrorCode.InvalidResult`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * Type-checked examples for `protocol.ts`. | ||
| * | ||
| * These examples are synced into JSDoc comments via the sync-snippets script. | ||
| * Each function's region markers define the code snippet that appears in the docs. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import * as z from 'zod/v4'; | ||
|
|
||
| import type { BaseContext, Protocol } from './protocol.js'; | ||
|
|
||
| /** | ||
| * Example: registering a handler for a custom (non-spec) request method. | ||
| */ | ||
| function Protocol_setRequestHandler_customMethod(protocol: Protocol<BaseContext>) { | ||
| //#region Protocol_setRequestHandler_customMethod | ||
| const SearchParams = z.object({ query: z.string(), limit: z.number().optional() }); | ||
| const SearchResult = z.object({ hits: z.array(z.string()) }); | ||
|
|
||
| protocol.setRequestHandler('acme/search', { params: SearchParams, result: SearchResult }, async (params, _ctx) => { | ||
| return { hits: [`result for ${params.query}`] }; | ||
| }); | ||
| //#endregion Protocol_setRequestHandler_customMethod | ||
| void protocol; | ||
| } | ||
|
|
||
| void Protocol_setRequestHandler_customMethod; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.