Skip to content

feat: public API for runtime handler registration/deregistration#2146

Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:feat/public-handler-registration-2135
Open

feat: public API for runtime handler registration/deregistration#2146
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:feat/public-handler-registration-2135

Conversation

@BabyChrist666
Copy link
Contributor

Summary

Adds public methods for registering and deregistering request/notification handlers at runtime on the low-level Server class:

  • add_request_handler(method, handler) — register a request handler
  • remove_request_handler(method) — deregister a request handler
  • add_notification_handler(method, handler) — register a notification handler
  • remove_notification_handler(method) — deregister a notification handler
  • has_handler(method) — check if a handler exists for a given method

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 (feature flags, hot-reloading, test fixtures).

Also refactors ExperimentalHandlers to 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: str as the key — this aligns with the current internal architecture where _request_handlers is a dict[str, ...] keyed by JSON-RPC method names. The private _add_request_handler and _has_handler methods remain for backward compatibility but the public API is the recommended path forward.

Test plan

  • 12 new tests covering add/remove/has for both request and notification handlers
  • Existing lowlevel server tests pass (24/24)
  • Experimental handler tests pass (217 passed, 96 skipped)
  • CI passes

Fixes #2135

🤖 Generated with Claude Code

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.

Fixes modelcontextprotocol#2135

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Public API for runtime handler registration/deregistration on low-level Server

1 participant