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: make context required at internal layers, optional at MCPServer
Addresses review feedback asking when Context could be None. The answer:
only via direct calls (tests, programmatic use). Rather than guard at
the leaf layers, make the internal layers type-honest.
- MCPServer.call_tool/read_resource/get_prompt: keep context optional,
auto-construct Context(mcp_server=self) when None (restores the
behavior get_context() used to provide)
- ToolManager/Tool, PromptManager/Prompt, ResourceManager/ResourceTemplate:
context is now required — type signature matches production reality
where _handle_* always provides it
- Guards removed from Tool.run/Prompt.render/ResourceTemplate.create_resource;
no longer reachable since context is required
- Prompt.render and PromptManager.render_prompt: arguments parameter no
longer has a default (both arguments and context are now required positional)
- Added TODO noting Context constructor nullability is vestigial (follow-up)
### `MCPServer.call_tool()`, `read_resource()`, `get_prompt()` now accept a `context` parameter
317
317
318
-
`MCPServer.call_tool()`, `MCPServer.read_resource()`, and `MCPServer.get_prompt()` now accept an optional `context: Context | None = None` parameter. The framework passes this automatically during normal request handling — you only need to supply it when calling these methods directly.
318
+
`MCPServer.call_tool()`, `MCPServer.read_resource()`, and `MCPServer.get_prompt()` now accept an optional `context: Context | None = None` parameter. The framework passes this automatically during normal request handling. If you call these methods directly and omit `context`, a Context with no active request is constructed for you — tools that don't use `ctx` work normally, but any attempt to use `ctx.session`, `ctx.request_id`, etc. will raise.
319
319
320
-
If the tool, resource template, or prompt you're invoking declares a `ctx: Context` parameter, you must pass a `Context`. Calling without one raises `ToolError` for tools or `ValueError` for prompts and resource templates.
320
+
The internal layers (`ToolManager.call_tool`, `Tool.run`, `Prompt.render`, `ResourceTemplate.create_resource`, etc.) now require `context` as a positional argument.
321
321
322
322
### Replace `RootModel` by union types with `TypeAdapter` validation
0 commit comments