adk-python
adk-python copied to clipboard
fix(mcp): Transform tool arguments to match schema
Link to Issue or Description of Change
Problem:
MCP tools fail when model simplifies [{"type": "web"}] to ["web"].
Error:
MCP error -32602: sources.0: Invalid input: expected object, received string
Solution:
Added input transformation in McpTool._run_async_impl() to convert ["web"] → [{"type": "web"}] when schema expects array of single-property objects.
Testing Plan
Unit Tests:
- [x] Added 6 tests to
test_mcp_tool.py - [x] All tests pass
pytest tests/unittests/tools/mcp_tool/test_mcp_tool.py -k "transform" -v
# 6 passed
Tests:
- Simple types unchanged
- Array primitives → objects transformation
- Already-correct format unchanged
- Empty arrays
- Multi-property objects unchanged
- Integration test: transformation applied during tool execution
Manual E2E Test:
Tested with Firecrawl MCP firecrawl_search tool.
Before:
MCP error -32602: sources.0: Invalid input: expected object, received string
After:
[agent]: I found some recent AI news articles for you:
* AWS and OpenAI announce multi-year strategic partnership
* OpenAI and Amazon sign $38 billion deal for AI computing power
...
✅ Tool executes successfully
Checklist
- [x] I have read the CONTRIBUTING.md document.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [x] I have manually tested my changes end-to-end.
- [x] Any dependent changes have been merged and published in downstream modules.
Additional context
Implementation:
- Added 2 methods:
_transform_args_to_mcp_format()and_transform_value_to_schema() - ~70 lines of code
- No breaking changes
Test Code Used
The following agent code was used to test the fix with the query: "can you fetch recent ai news?"
from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset
from google.adk.tools.google_api_tool import CalendarToolset
from mcp import StdioServerParameters
from dotenv import load_dotenv
import os
from pathlib import Path
# Load .env file from the same directory as this file
env_path = Path(__file__).parent / '.env'
load_dotenv(env_path)
# Load configuration from environment variables
FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY")
OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET")
MODEL = "gemini-2.5-pro"
tools = [
# Firecrawl MCP Tool
MCPToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx",
args=["-y", "firecrawl-mcp"],
env={"FIRECRAWL_API_KEY": FIRECRAWL_API_KEY}
),
timeout=30,
),
),
# Google Calendar Toolset with OAuth
CalendarToolset(
client_id=OAUTH_CLIENT_ID,
client_secret=OAUTH_CLIENT_SECRET
),
]
# Create the agent
root_agent = LlmAgent(
model=MODEL,
name="firecrawl_calendar_agent",
description="A helpful AI assistant that scrapes websites with Firecrawl and manages your Google Calendar using natural language.",
instruction="""
You are an AI assistant with two main capabilities:
1. **Web Scraping with Firecrawl**: You can fetch and analyze content from websites.
2. **Google Calendar Management**: You can help users manage their calendar by:
- Listing upcoming events
- Creating new calendar events
- Getting event details
- Updating existing events
- Deleting events
- Searching for specific events
""",
tools=tools,
)
Current MCP tool usage error:
Fix after this PR:
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of the pull request.
Summary of Changes
Hello @leonardogrig, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses a critical bug where the output from language models, when simplified, caused MCP tools to fail due to a mismatch with their expected input schemas. Specifically, it resolves cases where an array of single-property objects was incorrectly simplified to an array of primitives. The solution introduces an intelligent argument transformation mechanism that automatically re-formats these simplified inputs to conform to the tool's schema, ensuring successful tool execution and improving the robustness of agent-tool interactions.
Highlights
- Problem Resolution: Fixed an issue where MCP tools failed due to model outputs simplifying array-of-object arguments (e.g., "[{"type": "web"}]" becoming "["web"]").
- Argument Transformation: Implemented a new argument transformation layer within
McpTool._run_async_impl()to automatically convert simplified array arguments back to the expected object format when the schema requires it. - New Methods: Introduced
_transform_args_to_mcp_format()and_transform_value_to_schema()to handle the argument restructuring logic. - Comprehensive Testing: Added 6 new unit tests covering various transformation scenarios and an end-to-end integration test to validate the fix.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
Response from ADK Triaging Agent
Hello @leonardogrig, thank you for creating this PR!
It looks like you have not signed the Contributor License Agreement (CLA) yet. Please make sure to sign the CLA so we can proceed with reviewing your contribution.
Thank you for providing a detailed testing plan, it is very helpful for the reviewers!