fix(mcp): conditionally include body/upload in tool schemas, drop empty body on execution#213
Conversation
… and drop empty body on execution
Full-mode tool schemas now only include `body` when the Discovery Document
method defines a request body, and `upload` when `supportsMediaUpload` is
true. This prevents LLMs from hallucinating these fields on GET-only methods.
Additionally, empty body objects (`{}`) are filtered out before execution
in both compact and full modes, and empty upload strings are ignored. LLMs
commonly send "body": {} on read-only methods, which causes Google APIs to
return HTTP 400.
🦋 Changeset detectedLatest commit: a073e1d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to 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 ChangesHello, I'm Gemini Code Assist1! 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 significantly enhances the robustness and accuracy of the MCP (Multi-Cloud Platform) server's interaction with Google APIs. By intelligently refining the JSON schemas presented to Large Language Models (LLMs) and proactively filtering out empty or irrelevant request parameters, it aims to prevent common issues like LLM hallucinations and API errors, leading to more reliable tool execution and a smoother user experience. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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 👍 and 👎 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request improves the robustness of the MCP server. It conditionally includes body and upload in tool schemas, which helps guide LLMs to use the tools correctly. It also adds defensive filtering to ignore empty {} bodies and empty "" upload paths during execution, preventing errors from invalid inputs that are common with LLMs. The changes are well-implemented and address the issues described.
|
Ran |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request improves the MCP tool schemas by conditionally including body and upload properties, which helps prevent LLMs from generating them for methods where they aren't supported. It also adds defensive code to filter out empty body objects and upload strings during execution, which will prevent errors with some Google APIs. The changes are well-implemented and align with the goal of making the tool schemas more precise. I have one suggestion to further improve schema accuracy for paginated methods.
Only include the page_all property in full-mode tool schemas when the method has a pageToken parameter, preventing LLMs from attempting pagination on non-paginable methods.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces several improvements to the MCP tool schema generation and execution logic. The changes conditionally include body and upload properties in tool schemas based on method capabilities, which is a significant improvement for LLM-based tool use. Additionally, the defensive filtering of empty body objects and upload strings in execute_mcp_method enhances robustness by preventing invalid requests to the Google APIs. The implementation is clean and effectively addresses the described issues.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request modifies the Model Context Protocol (MCP) server to enhance tool schema generation and execution handling. Tool schemas are now generated conditionally, including body, upload, and page_all properties only when the corresponding API method supports them. During tool execution, empty {} body objects and empty "" upload path strings are filtered out to prevent downstream errors. These changes aim to improve schema accuracy for language models and increase the robustness of method execution.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #213 +/- ##
==========================================
- Coverage 57.51% 57.40% -0.11%
==========================================
Files 38 38
Lines 14188 14214 +26
==========================================
Hits 8160 8160
- Misses 6028 6054 +26 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
bodywhenmethod.requestis defined, anduploadwhenmethod.supports_media_uploadis true — prevents LLMs from hallucinating these fields on GET-only methods{}) inexecute_mcp_methodbefore forwarding to the HTTP executor — defensive fix for compact mode where body is always in the schema"") to prevent spurious file-read attemptsFixes #212
Related: #182, #183, #188
Test plan
gmail_users_labels_listno longer includesbody/uploadproperties, whilegmail_users_messages_sendstill includesbody