Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions src/adk_code_mode/mcp_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2025-present Agenta <hello@buildagenta.com>
#
# SPDX-License-Identifier: Apache-2.0
"""Return an MCP tool's ``structuredContent`` to code instead of the whole result.

ADK's ``McpTool`` returns the full ``CallToolResult`` (``content``,
``structuredContent``, ``isError``). When the tool declares an ``outputSchema``,
the MCP spec requires ``structuredContent`` to match it, and ``content`` is meant to
be a serialised copy for older clients. So code gets ``structuredContent`` alone,
and the stub's return type is built from the ``outputSchema``.
"""

from __future__ import annotations

from typing import Any

from google.adk.tools.base_tool import BaseTool
from google.adk.tools.mcp_tool.mcp_tool import McpTool


def output_schema(tool: BaseTool) -> dict[str, Any] | None:
"""Return the ``outputSchema`` of an MCP tool, or ``None`` for any other tool.

``tool`` is the tool itself, not a ``ToolResultArtifactTool`` wrapping it.
"""
return tool.raw_mcp_tool.outputSchema if isinstance(tool, McpTool) else None


def structured_result(tool: BaseTool, result: Any) -> Any:
"""Return ``structuredContent`` when ``tool`` is an MCP tool with an ``outputSchema``."""
if output_schema(tool) and isinstance(result, dict) and "structuredContent" in result:
return result["structuredContent"]
return result


__all__ = ["output_schema", "structured_result"]
4 changes: 3 additions & 1 deletion src/adk_code_mode/tool_result_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from google.adk.tools.tool_context import ToolContext
from google.genai import types

from adk_code_mode.mcp_results import structured_result
from adk_code_mode.tool_failures import raise_for_failed_result

# Transport-neutral metadata stamped on saved tool-result artifacts. Downstream
Expand Down Expand Up @@ -115,7 +116,8 @@ async def run_async(self, *, args: dict[str, Any], tool_context: ToolContext) ->
stem = artifact_name or _default_stem(self.name, tool_context)
filename = f"{stem}.json"

data, mime_type = _serialise_result(result)
# Save what code receives, so reloading the artifact gives the same value.
data, mime_type = _serialise_result(structured_result(self._wrapped, result))
metadata: dict[str, Any] = {
TOOL_RESULT_METADATA_KEY: "true",
TOOL_RESULT_FILENAME_KEY: filename,
Expand Down
6 changes: 3 additions & 3 deletions src/adk_code_mode/tools/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Literal

from adk_code_mode.tools.namespacing import NamespacedTool
from adk_code_mode.tools.stubs import RenderedTool, render_tool
from adk_code_mode.tools.stubs import RenderedTool, render_tools

Detail = Literal["full", "names"]

Expand Down Expand Up @@ -54,7 +54,7 @@ def render_catalog(namespaced: list[NamespacedTool], *, detail: Detail = "full")

for ns in sorted(n for n in grouped if n is not None):
tools = sorted(grouped[ns], key=lambda t: t.attribute)
rendered = [render_tool(t) for t in tools]
rendered = render_tools(tools)
names = ", ".join(rt.attribute for rt in rendered)
entries = [_render_entry(rt) for rt in rendered] if detail == "full" else []
sections.append(
Expand All @@ -67,7 +67,7 @@ def render_catalog(namespaced: list[NamespacedTool], *, detail: Detail = "full")

top_level = sorted(grouped.get(None, []), key=lambda t: t.attribute)
if top_level:
rendered = [render_tool(t) for t in top_level]
rendered = render_tools(top_level)
names = ", ".join(rt.attribute for rt in rendered)
entries = [_render_entry(rt) for rt in rendered] if detail == "full" else []
sections.append(
Expand Down
7 changes: 6 additions & 1 deletion src/adk_code_mode/tools/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
``agent.canonical_on_tool_error_callbacks``. If no replacement, re-raise.
5. ``plugin_manager.run_after_tool_callback``
6. ``agent.canonical_after_tool_callbacks`` (first non-``None`` replaces result)

Callbacks see the result in ADK's shape; only then does an MCP tool with an
``outputSchema`` hand code its ``structuredContent`` (see ``mcp_results``).
"""

from __future__ import annotations
Expand All @@ -33,6 +36,7 @@
from google.adk.tools.tool_context import ToolContext
from google.genai import types as genai_types

from adk_code_mode.mcp_results import structured_result
from adk_code_mode.tool_failures import HTTPStatusError, raise_for_failed_result
from adk_code_mode.tool_result_artifacts import ToolResultArtifactTool
from adk_code_mode.tools.namespacing import Registry
Expand Down Expand Up @@ -178,7 +182,8 @@ async def _run_and_merge(self, tool: BaseTool, args: dict[str, Any]) -> Any:
async with self._merge_lock:
_deep_merge_dicts(self._ctx.session.state, tool_context.actions.state_delta)
self._artifact_delta.update(tool_context.actions.artifact_delta)
return response
inner = tool._wrapped if isinstance(tool, ToolResultArtifactTool) else tool
return structured_result(inner, response)

async def _run_with_callbacks(
self,
Expand Down
Loading
Loading