feat(core): add getRequestHandler to Protocol#1759
feat(core): add getRequestHandler to Protocol#1759tizmagik wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
🦋 Changeset detectedLatest commit: b86068f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
Add a public `getRequestHandler()` method to Protocol that returns the currently registered handler for a given request method. This enables composable handler wrapping without re-implementing SDK internals. Closes modelcontextprotocol#1757 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
c3ea456 to
b86068f
Compare
Alternative approach:
|
getRequestHandler (this PR) |
setResponseTransform |
|
|---|---|---|
| Generality | Any request method | Any request method |
| Double schema parse | Yes (documented, harmless) | No |
| Type safety | Has an internal as unknown cast |
Clean — transform receives/returns typed result |
| Caller complexity | get/set/wrap dance | Single call |
| Internal changes | Pure addition, no existing code modified | Small change to _onrequest (~3-4 lines + new map) |
| Snapshot semantics | Yes (needs documenting) | No |
Trade-offs vs Option A from the issue (onToolsList):
setResponseTransform is general-purpose (works for any method) while Option A would need separate hooks per list type (onToolsList, onPromptsList, onResourcesList, etc.).
Happy to implement either direction — just wanted to surface this option in case it's a better fit for the SDK's design philosophy.
Summary
Adds a public
getRequestHandler()method to theProtocolclass, enabling retrieval and composable wrapping of existing request handlers.Closes #1757
Motivation
Currently, servers that need to transform request responses (e.g.,
tools/list) must either:_registeredToolsviaas anycastssetRequestHandler, re-implementing all SDK serialization logic (~80 lines)Both approaches are fragile and break on SDK refactors. This change provides a clean public API for the common "wrap and transform" pattern.
API
Returns the currently registered handler for a method, or
undefinedif none exists. The returned function is properly typed for both input and output.Design notes:
Usage Example
Changes
packages/core/src/shared/protocol.ts— AddedgetRequestHandler()method with JSDoc documenting snapshot semantics and schema re-validation trade-offpackages/core/src/shared/protocol.examples.ts— Type-checked JSDoc example with proper undefined guardpackages/core/test/shared/protocol.test.ts— 7 tests: undefined for unregistered, returns handler, undefined after remove, reflects latest after replacement, snapshot works after replacement, wrapping with result verification, error propagation.changeset/add-get-request-handler.md— Minor changeset for@modelcontextprotocol/coreTest Plan
pnpm --filter @modelcontextprotocol/core test— all 447 tests passpnpm check:all— typecheck + lint + docs all pass🤖 Generated with Claude Code