Uh oh!
There was an error while loading. Please reload this page.
feat: public API for runtime handler registration/deregistration - #2146
feat: public API for runtime handler registration/deregistration#2146BabyChrist666 wants to merge 3 commits into
Conversation
Add add_request_handler(), remove_request_handler(), add_notification_handler(), remove_notification_handler(), and has_handler() as public methods on the low-level Server class. This enables frameworks and advanced use cases to register handlers for protocol extensions or custom methods after server construction, and to remove or replace handlers dynamically. Refactors ExperimentalHandlers to use the new public API instead of receiving private method references, validating the API with its first internal consumer. Fixesmodelcontextprotocol#2135 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix Server type arg count in ExperimentalHandlers (1 param, not 2) - Add proper type annotations to all test functions and parameters Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use Server[Any] instead of Server[None] to match the default lifespan return type, and add pragma: no cover to dummy handler bodies that are only used for registration verification. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
403dd9e to
c413473CompareBabyChrist666
commented
Mar 21, 2026
The CI failure here ( The root cause was fixed in #2265 (merged Mar 12) which rewrote A rebase onto |
maxisbey
commented
Jul 29, 2026
Thanks for the PR. This has since landed via #2710. I'm closing this out as part of a backlog cleanup now that v2 is out. Feel free to reopen if this is still relevant. |
Summary
Adds public methods for registering and deregistering request/notification handlers at runtime on the low-level
Serverclass:add_request_handler(method, handler)— register a request handlerremove_request_handler(method)— deregister a request handleradd_notification_handler(method, handler)— register a notification handlerremove_notification_handler(method)— deregister a notification handlerhas_handler(method)— check if a handler exists for a given methodThis enables frameworks and advanced use cases to register handlers for protocol extensions or custom methods after server construction, and to remove or replace handlers dynamically (feature flags, hot-reloading, test fixtures).
Also refactors
ExperimentalHandlersto use the new public API instead of receiving private method references, validating the API with its first internal consumer.Design note
The issue proposed
method: stras the key — this aligns with the current internal architecture where_request_handlersis adict[str, ...]keyed by JSON-RPC method names. The private_add_request_handlerand_has_handlermethods remain for backward compatibility but the public API is the recommended path forward.Test plan
Fixes#2135
🤖 Generated with Claude Code