-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add resolver dependency injection for MCPServer tools #2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kludex
wants to merge
17
commits into
main
Choose a base branch
from
resolver-dependency-injection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e58ff02
Add resolver dependency injection for MCPServer tools
Kludex e110093
Cover Context.headers and resolver schema-only paths
Kludex cafe8f3
Resolve type hints for callable-object tools in resolver detection
Kludex 3f59ea3
Merge remote-tracking branch 'origin/main' into worktree-synthetic-si…
Kludex 9e9282a
Pin elicitation resolver tests to legacy mode for 2026-07-28 default
Kludex c3ea531
Address cubic review: by-name aliasing, return-annotation, callable-r…
Kludex aac86dc
Fix resolver edge cases: non-BaseModel returns, optional Context, bou…
Kludex 37c038c
Validate resolver tool args once; key resolvers by method identity
Kludex 58238b1
Memoize built-in bound-method resolvers; stop mutating pre_validated
Kludex b7b8967
Make ElicitationResult subscriptable so the documented Resolve union …
Kludex 163721f
Merge remote-tracking branch 'origin/main' into worktree-synthetic-si…
Kludex b0424da
Update test_resolve imports to mcp_types after the mcp-types package …
Kludex 8f677c9
Switch resolver docs/example to a delete-folder confirmation flow
Kludex d22ce97
Merge remote-tracking branch 'origin/main' into resolver-dependency-i…
Kludex 800d253
Reject union-wrapped Resolve; honor the bare ElicitationResult alias
Kludex 6b10702
Note the ElicitationResult isinstance behavior change in the migratio…
Kludex f2106f5
Document resolver dependency injection in the elicitation tutorial; c…
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from typing import Annotated | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from mcp.server import MCPServer | ||
| from mcp.server.mcpserver import ( | ||
| AcceptedElicitation, | ||
| CancelledElicitation, | ||
| DeclinedElicitation, | ||
| Elicit, | ||
| ElicitationResult, | ||
| Resolve, | ||
| ) | ||
|
|
||
| mcp = MCPServer("Files") | ||
|
|
||
| _FOLDERS: dict[str, list[str]] = {"/tmp/empty": [], "/tmp/project": ["main.py", "README.md"]} | ||
|
|
||
|
|
||
| class Confirm(BaseModel): | ||
| ok: bool | ||
|
|
||
|
|
||
| async def confirm_delete(path: str) -> Confirm | Elicit[Confirm]: | ||
| """Resolver: ask for confirmation only when the folder is not empty.""" | ||
| file_count = len(_FOLDERS.get(path, [])) | ||
| if file_count == 0: | ||
| return Confirm(ok=True) # nothing to confirm, no round-trip to the client | ||
| return Elicit(f"{path} has {file_count} file(s). Delete anyway?", Confirm) | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| async def delete_folder( | ||
| path: str, | ||
| confirm: Annotated[ElicitationResult[Confirm], Resolve(confirm_delete)], | ||
| ) -> str: | ||
| """Delete a folder, asking for confirmation when it is not empty.""" | ||
| match confirm: | ||
| case AcceptedElicitation(data=Confirm(ok=True)): | ||
| _FOLDERS.pop(path, None) | ||
| return f"deleted {path}" | ||
| case AcceptedElicitation(): | ||
| return "kept the folder" | ||
| case DeclinedElicitation(): | ||
| return "declined: folder not deleted" | ||
| case CancelledElicitation(): | ||
| return "cancelled: folder not deleted" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.