Skip to content

fix(mcp): conditionally include body/upload in tool schemas, drop empty body on execution#213

Merged
jpoehnelt merged 5 commits intogoogleworkspace:mainfrom
shreyaskarnik:fix/mcp-tool-schema-body-upload
Mar 5, 2026
Merged

fix(mcp): conditionally include body/upload in tool schemas, drop empty body on execution#213
jpoehnelt merged 5 commits intogoogleworkspace:mainfrom
shreyaskarnik:fix/mcp-tool-schema-body-upload

Conversation

@shreyaskarnik
Copy link
Contributor

Summary

  • Full-mode tool schemas now only include body when method.request is defined, and upload when method.supports_media_upload is true — prevents LLMs from hallucinating these fields on GET-only methods
  • Filters out empty body objects ({}) in execute_mcp_method before forwarding to the HTTP executor — defensive fix for compact mode where body is always in the schema
  • Filters out empty upload strings ("") to prevent spurious file-read attempts

Fixes #212

Related: #182, #183, #188

Test plan

  • All 443 tests pass (2 pre-existing auth test failures unrelated to this change)
  • All 13 MCP-specific tests pass
  • Clippy clean
  • Verified full-mode schema output: gmail_users_labels_list no longer includes body/upload properties, while gmail_users_messages_send still includes body

… 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-bot
Copy link

changeset-bot bot commented Mar 5, 2026

🦋 Changeset detected

Latest commit: a073e1d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

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

@google-cla
Copy link

google-cla bot commented Mar 5, 2026

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • Conditional Tool Schema Generation: Tool schemas for MCP methods now dynamically include body and upload properties only when the underlying Google API method explicitly supports them. This prevents Large Language Models (LLMs) from generating irrelevant fields for methods that do not require them.
  • Empty Body Object Filtering: Implemented a filter to automatically drop empty JSON objects ({}) provided for the body argument before forwarding to the HTTP executor. This addresses issues where LLMs might send empty bodies even for GET methods, which can cause API errors.
  • Empty Upload String Filtering: Added a filter to ignore empty strings ("") provided for the upload argument. This prevents spurious file-read attempts when an LLM might generate an empty upload path.
Changelog
  • src/mcp_server.rs
    • Refactored tool schema generation to conditionally include body and upload properties based on the Google API method's capabilities.
    • Introduced logic to filter out empty body objects ({}) before method execution to prevent API errors.
    • Added a filter to discard empty upload strings ("") to avoid unnecessary file processing.
Activity
  • All 443 tests passed (excluding 2 pre-existing auth test failures unrelated to this change).
  • All 13 MCP-specific tests passed.
  • Clippy checks were clean.
  • Verified full-mode schema output: gmail_users_labels_list no longer includes body/upload properties, while gmail_users_messages_send still includes body.
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 👍 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

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@shreyaskarnik shreyaskarnik requested a review from jpoehnelt as a code owner March 5, 2026 23:06
@shreyaskarnik
Copy link
Contributor Author

Ran cargo fmt --all — it touched files changed in this PR (src/mcp_server.rs, src/auth.rs) as well as formatting in other areas. Also added the required changeset file.

@googleworkspace-bot
Copy link
Contributor

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@googleworkspace-bot
Copy link
Contributor

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@googleworkspace-bot
Copy link
Contributor

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jpoehnelt jpoehnelt merged commit 6daf90d into googleworkspace:main Mar 5, 2026
32 of 34 checks passed
@shreyaskarnik shreyaskarnik deleted the fix/mcp-tool-schema-body-upload branch March 5, 2026 23:56
@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

❌ Patch coverage is 0% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.40%. Comparing base (bc84ba7) to head (a073e1d).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/mcp_server.rs 0.00% 37 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP full-mode tool schemas include body/upload on methods that don't support them

3 participants