generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 461
mcp elicitation #1094
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
Merged
Merged
mcp elicitation #1094
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """MCP server for testing elicitation. | ||
|
|
||
| - Docs: https://modelcontextprotocol.io/specification/draft/client/elicitation | ||
| """ | ||
|
|
||
| from mcp.server import FastMCP | ||
| from mcp.types import ElicitRequest, ElicitRequestParams, ElicitResult | ||
|
|
||
|
|
||
| def server() -> None: | ||
| """Simulate approval through MCP elicitation.""" | ||
| server_ = FastMCP() | ||
|
|
||
| @server_.tool(description="Tool to request approval") | ||
| async def approval_tool() -> str: | ||
| """Simulated approval tool. | ||
|
|
||
| Returns: | ||
| The elicitation result from the user. | ||
| """ | ||
| request = ElicitRequest( | ||
| params=ElicitRequestParams( | ||
| message="Do you approve", | ||
| requestedSchema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "message": {"type": "string", "description": "request message"}, | ||
| }, | ||
| "required": ["message"], | ||
| }, | ||
| ), | ||
| ) | ||
| result = await server_.get_context().session.send_request(request, ElicitResult) | ||
|
|
||
| return result.model_dump_json() | ||
|
|
||
| server_.run(transport="stdio") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| server() |
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,40 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
| from mcp import StdioServerParameters, stdio_client | ||
| from mcp.types import ElicitResult | ||
|
|
||
| from strands import Agent | ||
| from strands.tools.mcp import MCPClient | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def callback(): | ||
| async def callback_(_, params): | ||
| return ElicitResult(action="accept", content={"message": params.message}) | ||
|
|
||
| return callback_ | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(callback): | ||
| return MCPClient( | ||
| lambda: stdio_client( | ||
| StdioServerParameters(command="python", args=["tests_integ/mcp/elicitation_server.py"]), | ||
| ), | ||
| elicitation_callback=callback, | ||
| ) | ||
|
|
||
|
|
||
| def test_mcp_elicitation(client): | ||
| with client: | ||
| tools = client.list_tools_sync() | ||
| agent = Agent(tools=tools) | ||
|
|
||
| agent("Can you get approval") | ||
|
|
||
| tool_result = agent.messages[-2] | ||
|
|
||
| tru_result = json.loads(tool_result["content"][0]["toolResult"]["content"][0]["text"]) | ||
| exp_result = {"meta": None, "action": "accept", "content": {"message": "Do you approve"}} | ||
| assert tru_result == exp_result |
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.