Skip to content

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

@maxisbey

Description

@maxisbey

Summary

The low-level Server class has a private _add_request_handler() method but no public API for adding or removing request/notification handlers at runtime. The only supported way to register handlers is via on_* constructor kwargs, which means all handlers must be known at construction time.

This is limiting for frameworks and advanced use cases that need to:

  • Register handlers for protocol extensions or custom methods after server construction
  • Remove or replace handlers dynamically (e.g., feature flags, hot-reloading, test fixtures)
  • Register notification handlers post-construction (currently no mutation API at all for _notification_handlers)

Current state

  • _add_request_handler() exists but is private. It silently replaces existing handlers.
  • _has_handler() is also private.
  • There is no remove/deregister method for either request or notification handlers.
  • MCPServer.completion() already uses _add_request_handler() with a pyright: ignore[reportPrivateUsage] suppression and a TODO acknowledging this needs a better pattern.

Proposal

Make handler registration/deregistration a first-class public API on the low-level Server:

server.add_request_handler(method: str, handler: Callable) -> None
server.remove_request_handler(method: str) -> None
server.add_notification_handler(method: str, handler: Callable) -> None
server.remove_notification_handler(method: str) -> None
server.has_handler(method: str) -> bool

Related issues

AI Disclaimer

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRequest for a new feature that's not currently supportedv2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions