Skip to content

Commit fdc1c3b

Browse files
committed
add migration
1 parent 3585b22 commit fdc1c3b

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

docs/migration.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,50 @@ async def handle_tool(name: str, arguments: dict) -> list[TextContent]:
371371
await ctx.session.send_progress_notification(ctx.meta["progress_token"], 0.5, 100)
372372
```
373373

374+
### `RequestContext` and `ProgressContext` type parameters simplified
375+
376+
The `RequestContext` class has been split to separate shared fields from server-specific fields. The shared `RequestContext` now only takes 1 type parameter (the session type) instead of 3.
377+
378+
**`RequestContext` changes:**
379+
380+
- Type parameters reduced from `RequestContext[SessionT, LifespanContextT, RequestT]` to `RequestContext[SessionT]`
381+
- Server-specific fields (`lifespan_context`, `experimental`, `request`, `close_sse_stream`, `close_standalone_sse_stream`) moved to new `ServerRequestContext` class in `mcp.server.context`
382+
383+
**`ProgressContext` changes:**
384+
385+
- Type parameters reduced from `ProgressContext[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT]` to `ProgressContext[SessionT]`
386+
387+
**Before (v1):**
388+
389+
```python
390+
from mcp.shared.context import RequestContext, LifespanContextT, RequestT
391+
from mcp.shared.progress import ProgressContext
392+
393+
# RequestContext with 3 type parameters
394+
ctx: RequestContext[ClientSession, LifespanContextT, RequestT]
395+
396+
# ProgressContext with 5 type parameters
397+
progress_ctx: ProgressContext[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT]
398+
```
399+
400+
**After (v2):**
401+
402+
```python
403+
from mcp.shared.context import RequestContext
404+
from mcp.shared.progress import ProgressContext
405+
406+
# RequestContext with 1 type parameter
407+
ctx: RequestContext[ClientSession]
408+
409+
# ProgressContext with 1 type parameter
410+
progress_ctx: ProgressContext[ClientSession]
411+
412+
# For server-specific context with lifespan and request types
413+
from mcp.server.context import ServerRequestContext, LifespanContextT, RequestT
414+
415+
server_ctx: ServerRequestContext[LifespanContextT, RequestT]
416+
```
417+
374418
### Resource URI type changed from `AnyUrl` to `str`
375419

376420
The `uri` field on resource-related types now uses `str` instead of Pydantic's `AnyUrl`. This aligns with the [MCP specification schema](https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/draft/schema.ts) which defines URIs as plain strings (`uri: string`) without strict URL validation. This change allows relative paths like `users/me` that were previously rejected.

tests/client/test_list_roots_callback.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from mcp.client.session import ClientSession
66
from mcp.server.mcpserver import MCPServer
77
from mcp.server.mcpserver.server import Context
8-
from mcp.server.session import ServerSession
98
from mcp.shared.context import RequestContext
109
from mcp.types import ListRootsResult, Root, TextContent
1110

@@ -16,14 +15,8 @@ async def test_list_roots_callback():
1615

1716
callback_return = ListRootsResult(
1817
roots=[
19-
Root(
20-
uri=FileUrl("file://users/fake/test"),
21-
name="Test Root 1",
22-
),
23-
Root(
24-
uri=FileUrl("file://users/fake/test/2"),
25-
name="Test Root 2",
26-
),
18+
Root(uri=FileUrl("file://users/fake/test"), name="Test Root 1"),
19+
Root(uri=FileUrl("file://users/fake/test/2"), name="Test Root 2"),
2720
]
2821
)
2922

@@ -33,7 +26,7 @@ async def list_roots_callback(
3326
return callback_return
3427

3528
@server.tool("test_list_roots")
36-
async def test_list_roots(context: Context[ServerSession], message: str):
29+
async def test_list_roots(context: Context[None], message: str):
3730
roots = await context.session.list_roots()
3831
assert roots == callback_return
3932
return True

0 commit comments

Comments
 (0)