fix: Returns dict as result from McpTool

The `BaseTool` expects the run_async to return a json-serializable object. By model_dump the McpTool result explicitly can allow what ADK runtime sees is identical to what is persisted in the session event list.

Before the change, runtime sees CallToolResult instance and Session persists its serialized dict.

https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/types.py#L916-L922

PiperOrigin-RevId: 822465432
This commit is contained in:
Wei Sun (Jack)
2025-10-22 00:58:04 -07:00
committed by Copybara-Service
parent d4dc645478
commit 4df926388b
2 changed files with 42 additions and 25 deletions
+4 -4
View File
@@ -80,7 +80,7 @@ class McpTool(BaseAuthenticatedTool):
Callable[[ReadonlyContext], Dict[str, str]]
] = None,
):
"""Initializes an MCPTool.
"""Initializes an McpTool.
This tool wraps an MCP Tool interface and uses a session manager to
communicate with the MCP server.
@@ -186,7 +186,7 @@ class McpTool(BaseAuthenticatedTool):
@override
async def _run_async_impl(
self, *, args, tool_context: ToolContext, credential: AuthCredential
):
) -> Dict[str, Any]:
"""Runs the tool asynchronously.
Args:
@@ -217,7 +217,7 @@ class McpTool(BaseAuthenticatedTool):
)
response = await session.call_tool(self._mcp_tool.name, arguments=args)
return response
return response.model_dump(exclude_none=True, mode="json")
async def _get_headers(
self, tool_context: ToolContext, credential: AuthCredential
@@ -282,7 +282,7 @@ class McpTool(BaseAuthenticatedTool):
!= APIKeyIn.header
):
error_msg = (
"MCPTool only supports header-based API key authentication."
"McpTool only supports header-based API key authentication."
" Configured location:"
f" {self._credentials_manager._auth_config.auth_scheme.in_}"
)