Skip to content

Commit 34816ec

Browse files
committed
Strengthen typed context regression coverage
1 parent 543c733 commit 34816ec

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

docs/handlers/context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Add a parameter annotated with `Context` to any tool:
1414

1515
* The SDK builds a fresh `Context` for every request and passes it in.
1616
* The parameter **name doesn't matter**. `ctx`, `context`, `c`: the SDK finds it by its annotation.
17-
* Resources and prompts can declare one too, the same way.
17+
* URI-template resource handlers and prompt handlers can declare one too, the same way.
1818
* `ctx.request_id` is the id of the request your function is serving right now.
1919

2020
!!! info
@@ -119,7 +119,7 @@ On a 2026-07-28 connection, clients receive change notifications only on a `subs
119119

120120
## Recap
121121

122-
* Annotate a parameter with `Context` (in a tool, a resource, or a prompt) and the SDK injects it. The name is yours.
122+
* Annotate a parameter with `Context` in a tool, resource-template, or prompt handler and the SDK injects it. Static resource handlers cannot accept `Context`. The name is yours.
123123
* It is invisible to the model: the input schema only ever contains your real arguments.
124124
* `ctx.request_id` identifies the request; `ctx.request_context.lifespan_context` is what your startup yielded.
125125
* `await ctx.read_resource(uri)` lets a tool read the server's own resources.

tests/server/mcpserver/resources/test_resource_template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ async def test_create_resource_preserves_context_with_variadic_kwargs(self):
226226
"""Injected context bypasses validation without changing **kwargs handling."""
227227
context = Context()
228228

229-
def my_func(ctx, **kwargs: str): # type: ignore
229+
def my_func(ctx: Context[None], **kwargs: str) -> str:
230230
assert ctx is context
231231
return kwargs["key"]
232232

233233
template = ResourceTemplate.from_function(
234-
fn=my_func, # pyright: ignore[reportUnknownArgumentType]
234+
fn=my_func,
235235
uri_template="test://{key}",
236236
context_kwarg="ctx",
237237
)

0 commit comments

Comments
 (0)