Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e58ff02
Add resolver dependency injection for MCPServer tools
Kludex Jun 25, 2026
e110093
Cover Context.headers and resolver schema-only paths
Kludex Jun 25, 2026
cafe8f3
Resolve type hints for callable-object tools in resolver detection
Kludex Jun 25, 2026
3f59ea3
Merge remote-tracking branch 'origin/main' into worktree-synthetic-si…
Kludex Jun 25, 2026
9e9282a
Pin elicitation resolver tests to legacy mode for 2026-07-28 default
Kludex Jun 25, 2026
c3ea531
Address cubic review: by-name aliasing, return-annotation, callable-r…
Kludex Jun 25, 2026
aac86dc
Fix resolver edge cases: non-BaseModel returns, optional Context, bou…
Kludex Jun 25, 2026
37c038c
Validate resolver tool args once; key resolvers by method identity
Kludex Jun 25, 2026
58238b1
Memoize built-in bound-method resolvers; stop mutating pre_validated
Kludex Jun 25, 2026
b7b8967
Make ElicitationResult subscriptable so the documented Resolve union …
Kludex Jun 26, 2026
163721f
Merge remote-tracking branch 'origin/main' into worktree-synthetic-si…
Kludex Jun 26, 2026
b0424da
Update test_resolve imports to mcp_types after the mcp-types package …
Kludex Jun 26, 2026
8f677c9
Switch resolver docs/example to a delete-folder confirmation flow
Kludex Jun 26, 2026
d22ce97
Merge remote-tracking branch 'origin/main' into resolver-dependency-i…
Kludex Jun 26, 2026
800d253
Reject union-wrapped Resolve; honor the bare ElicitationResult alias
Kludex Jun 26, 2026
6b10702
Note the ElicitationResult isinstance behavior change in the migratio…
Kludex Jun 26, 2026
f2106f5
Document resolver dependency injection in the elicitation tutorial; c…
Kludex Jun 26, 2026
2f8b657
Merge remote-tracking branch 'origin/main' into resolver-dependency-i…
maxisbey Jun 29, 2026
b671eaa
Return None from Context.headers when the request object has no headers
maxisbey Jun 29, 2026
b2e0ba3
Add a Dependencies tutorial page for resolver injection
maxisbey Jun 29, 2026
1795a2d
Add refund_desk story: resolver-injected parameters hidden from the s…
maxisbey Jun 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,72 @@ app = server.streamable_http_app(

The lowlevel `Server` also now exposes a `session_manager` property to access the `StreamableHTTPSessionManager` after calling `streamable_http_app()`.

### Resolver dependency injection for tools (`Resolve` / `Elicit`)

A tool parameter annotated `Annotated[T, Resolve(fn)]` is filled by running the resolver `fn` before the tool body, instead of by the calling LLM. Resolvers form a dependency graph: a resolver may declare its own `Resolve(...)` dependencies, read the `Context` (including `ctx.headers`), and receive the tool's own arguments by name. A resolver may return `Elicit[T]` to ask the client; the SDK runs the elicitation and injects the answer. Each resolver runs at most once per `tools/call`.

```python
from typing import Annotated

from pydantic import BaseModel

from mcp.server.mcpserver import (
AcceptedElicitation,
CancelledElicitation,
Context,
DeclinedElicitation,
Elicit,
MCPServer,
Resolve,
)

mcp = MCPServer(name="github")


class Login(BaseModel):
username: str


class Confirm(BaseModel):
ok: bool


async def login(ctx: Context) -> Login | Elicit[Login]:
if username := (ctx.headers or {}).get("x-github-user"):
return Login(username=username) # resolved from context, no question
return Elicit("GitHub username?", Login) # must ask


async def confirm(repo: str, login: Annotated[Login, Resolve(login)]) -> Elicit[Confirm]:
return Elicit(f"Star {repo} as {login.username}?", Confirm)


@mcp.tool()
async def star_repo(
repo: str,
login: Annotated[Login, Resolve(login)],
confirm: Annotated[Confirm, Resolve(confirm)],
) -> str:
"""Star a GitHub repo."""
return f"starred {repo} as {login.username}" if confirm.ok else "cancelled"
```

The injected type follows the consumer's annotation. Annotating the unwrapped model (`Annotated[Login, Resolve(login)]`) injects the model on accept and aborts the call with an error result on decline or cancel. To branch on the outcome instead, annotate the elicitation result union:

```python
@mcp.tool()
async def whoami(
login: Annotated[AcceptedElicitation[Login] | DeclinedElicitation | CancelledElicitation, Resolve(login)],
) -> str:
match login:
case AcceptedElicitation(data=data):
return f"hi {data.username}"
case _:
return "no username provided"
```

Resolved parameters are omitted from the tool's input schema, so the client never supplies them. Resolver parameters that cannot be classified, and cyclic resolver dependencies, raise at registration time.

## Need Help?

If you encounter issues during migration:
Expand Down
22 changes: 21 additions & 1 deletion src/mcp/server/mcpserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@
from mcp.types import Icon

from .context import Context
from .resolve import (
AcceptedElicitation,
CancelledElicitation,
DeclinedElicitation,
Elicit,
ElicitationResult,
Resolve,
)
from .server import MCPServer
from .utilities.types import Audio, Image

__all__ = ["MCPServer", "Context", "Image", "Audio", "Icon"]
__all__ = [
"MCPServer",
"Context",
"Image",
"Audio",
"Icon",
"Resolve",
"Elicit",
"ElicitationResult",
"AcceptedElicitation",
"DeclinedElicitation",
"CancelledElicitation",
]
20 changes: 18 additions & 2 deletions src/mcp/server/mcpserver/context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Generic
from collections.abc import Iterable, Mapping
from typing import TYPE_CHECKING, Any, Generic, Protocol, cast

from pydantic import AnyUrl, BaseModel
from typing_extensions import deprecated
Expand All @@ -22,6 +22,11 @@
from mcp.server.mcpserver.server import MCPServer


class _HasHeaders(Protocol):
@property
def headers(self) -> Mapping[str, str]: ...


class Context(BaseModel, Generic[LifespanContextT, RequestT]):
"""Context object providing access to MCP capabilities.

Expand Down Expand Up @@ -214,6 +219,17 @@ def client_id(self) -> str | None:
"""
return self.request_context.meta.get("client_id") if self.request_context.meta else None # pragma: no cover

@property
def headers(self) -> Mapping[str, str] | None:
"""Request headers carried by this message, when the transport has them.

Populated by HTTP-based transports; `None` on stdio.
"""
request = self.request_context.request
if request is None:
return None
return cast("_HasHeaders", request).headers

@property
def request_id(self) -> str:
"""Get the unique ID for this request."""
Expand Down
Loading
Loading