generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 556
feat: add meta field support to MCP tool results #1237
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
dbschmigelski
merged 3 commits into
strands-agents:main
from
vamgan:feat/mcp-meta-field-support
Dec 17, 2025
+138
−69
Merged
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
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
95 changes: 95 additions & 0 deletions
95
tests_integ/mcp/test_mcp_client_structured_content_and_metadata.py
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,95 @@ | ||
| """Integration test for MCP client structured content and metadata support. | ||
|
|
||
| This test verifies that MCP tools can return structured content and metadata, | ||
| and that the MCP client properly handles and exposes these fields in tool results. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| from mcp import StdioServerParameters, stdio_client | ||
|
|
||
| from strands import Agent | ||
| from strands.hooks import AfterToolCallEvent, HookProvider, HookRegistry | ||
| from strands.tools.mcp.mcp_client import MCPClient | ||
|
|
||
|
|
||
| class ToolResultCapture(HookProvider): | ||
| """Captures tool results for inspection.""" | ||
|
|
||
| def __init__(self): | ||
| self.captured_results = {} | ||
|
|
||
| def register_hooks(self, registry: HookRegistry) -> None: | ||
| """Register callback for after tool invocation events.""" | ||
| registry.add_callback(AfterToolCallEvent, self.on_after_tool_invocation) | ||
|
|
||
| def on_after_tool_invocation(self, event: AfterToolCallEvent) -> None: | ||
| """Capture tool results by tool name.""" | ||
| tool_name = event.tool_use["name"] | ||
| self.captured_results[tool_name] = event.result | ||
|
|
||
|
|
||
| def test_structured_content(): | ||
| """Test that MCP tools can return structured content.""" | ||
| # Set up result capture | ||
| result_capture = ToolResultCapture() | ||
|
|
||
| # Set up MCP client for echo server | ||
| stdio_mcp_client = MCPClient( | ||
| lambda: stdio_client(StdioServerParameters(command="python", args=["tests_integ/mcp/echo_server.py"])) | ||
| ) | ||
|
|
||
| with stdio_mcp_client: | ||
| # Create agent with MCP tools and result capture | ||
| agent = Agent(tools=stdio_mcp_client.list_tools_sync(), hooks=[result_capture]) | ||
|
|
||
| # Test structured content functionality | ||
| test_data = "STRUCTURED_TEST" | ||
| agent(f"Use the echo_with_structured_content tool to echo: {test_data}") | ||
|
|
||
| # Verify result was captured | ||
| assert "echo_with_structured_content" in result_capture.captured_results | ||
| result = result_capture.captured_results["echo_with_structured_content"] | ||
|
|
||
| # Verify basic result structure | ||
| assert result["status"] == "success" | ||
| assert len(result["content"]) == 1 | ||
|
|
||
| # Verify structured content is present and correct | ||
| assert "structuredContent" in result | ||
| assert result["structuredContent"] == {"echoed": test_data, "message_length": 15} | ||
|
|
||
| # Verify text content matches structured content | ||
| text_content = json.loads(result["content"][0]["text"]) | ||
| assert text_content == {"echoed": test_data, "message_length": 15} | ||
|
|
||
|
|
||
| def test_metadata(): | ||
| """Test that MCP tools can return metadata.""" | ||
| # Set up result capture | ||
| result_capture = ToolResultCapture() | ||
|
|
||
| # Set up MCP client for echo server | ||
| stdio_mcp_client = MCPClient( | ||
| lambda: stdio_client(StdioServerParameters(command="python", args=["tests_integ/mcp/echo_server.py"])) | ||
| ) | ||
|
|
||
| with stdio_mcp_client: | ||
| # Create agent with MCP tools and result capture | ||
| agent = Agent(tools=stdio_mcp_client.list_tools_sync(), hooks=[result_capture]) | ||
|
|
||
| # Test metadata functionality | ||
| test_data = "METADATA_TEST" | ||
| agent(f"Use the echo_with_metadata tool to echo: {test_data}") | ||
|
|
||
| # Verify result was captured | ||
| assert "echo_with_metadata" in result_capture.captured_results | ||
| result = result_capture.captured_results["echo_with_metadata"] | ||
|
|
||
| # Verify basic result structure | ||
| assert result["status"] == "success" | ||
|
|
||
| # Verify metadata is present and correct | ||
| assert "metadata" in result | ||
| expected_metadata = {"metadata": {"nested": 1}, "shallow": "val"} | ||
| assert result["metadata"] == expected_metadata |
64 changes: 0 additions & 64 deletions
64
tests_integ/mcp/test_mcp_client_structured_content_with_hooks.py
This file was deleted.
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.