You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: collapse context hierarchy into single RequestContext class
Merge HandlerContext, RequestHandlerContext, and NotificationHandlerContext
into a single RequestContext class. Request-specific fields (request_id,
meta, etc.) are now optional with None defaults, so the same class works
for both request and notification handlers.
Also:
- Make Server.add_handler/has_handler private (_add_handler/_has_handler)
- MCPServer._setup_handlers() -> _create_handlers() returning a list
passed to Server(handlers=...) constructor
- Update migration docs to reflect single context class
### Lowlevel `Server`: decorator-based handlers replaced with `RequestHandler`/`NotificationHandler`
410
410
411
-
The lowlevel `Server` class no longer uses decorator methods for handler registration. Instead, handlers are `RequestHandler` and `NotificationHandler` objects passed to the constructor or added via `add_handler()`.
411
+
The lowlevel `Server` class no longer uses decorator methods for handler registration. Instead, handlers are `RequestHandler` and `NotificationHandler` objects passed to the constructor.
- Handlers receive `(ctx, params)` instead of the full request object or unpacked arguments. `ctx` is a `RequestHandlerContext` (for requests) or `NotificationHandlerContext` (for notifications) with `session`, `lifespan_context`, and `experimental` fields. `params` is the typed request params object.
469
+
- Handlers receive `(ctx, params)` instead of the full request object or unpacked arguments. `ctx` is a `RequestContext`with `session`, `lifespan_context`, and `experimental` fields (plus `request_id`, `meta`, etc. for request handlers). `params` is the typed request params object.
470
470
- Handlers return the full result type (e.g. `ListToolsResult`) rather than unwrapped values (e.g. `list[Tool]`).
-`NotificationHandlerContext(HandlerContext)` — empty subclass for notifications
534
-
535
-
**Before (v1):**
527
+
The `RequestContext` class now uses optional fields for request-specific data (`request_id`, `meta`, etc.) so it can be used for both request and notification handlers. In notification handlers, these fields are `None`.
536
528
537
529
```python
538
530
from mcp.shared.context import RequestContext
539
-
```
540
-
541
-
**After (v2):**
542
531
543
-
```python
544
-
from mcp.shared.context import HandlerContext, RequestHandlerContext, NotificationHandlerContext
532
+
# request_id, meta, etc. are available in request handlers
533
+
# but None in notification handlers
545
534
```
546
535
547
536
## New Features
@@ -552,11 +541,11 @@ The `streamable_http_app()` method is now available directly on the lowlevel `Se
552
541
553
542
```python
554
543
from mcp.server.lowlevel import Server, RequestHandler
555
-
from mcp.shared.context importRequestHandlerContext
544
+
from mcp.shared.context importRequestContext
556
545
from mcp.types import ListToolsResult, PaginatedRequestParams
0 commit comments