Skip to content

fix(fetch): make fetch tool schema Gemini/OpenAPI 3.0 compatible#3812

Open
Dharit13 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Dharit13:fix/fetch-schema-inclusive-bounds
Open

fix(fetch): make fetch tool schema Gemini/OpenAPI 3.0 compatible#3812
Dharit13 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Dharit13:fix/fetch-schema-inclusive-bounds

Conversation

@Dharit13
Copy link
Copy Markdown

@Dharit13 Dharit13 commented Apr 4, 2026

Summary

Fixes the fetch server's tool schema to be compatible with LLM providers that only support OpenAPI 3.0 schema keywords (e.g. Google Gemini 2.5 Pro), which currently reject the schema with a 400 INVALID_ARGUMENT error.

Two incompatibilities are addressed:

  1. max_length field used gt=0 / lt=1000000 Pydantic constraints, which generate exclusiveMinimum / exclusiveMaximum in the JSON Schema. Gemini does not recognize these keywords. Changed to ge=1 / le=999999 — identical semantics for integers — which emits the universally supported minimum / maximum keywords instead.

  2. url field used AnyUrl, which generates format: \"uri\" and minLength: 1. Gemini only supports \"enum\" and \"date-time\" for string format, and does not support minLength. Added a WithJsonSchema override to emit a plain {"type": "string"} schema while preserving AnyUrl runtime validation — so invalid URLs are still rejected with a proper error, but the schema no longer contains unsupported keywords.

Fixes #1624

Changes

src/fetch/src/mcp_server_fetch/server.py

  • max_length field: gt=0ge=1, lt=1000000le=999999
  • url field: added WithJsonSchema({"type": "string", "description": "URL to fetch"}) to override schema output while keeping AnyUrl runtime validation

src/fetch/tests/test_server.py

  • Added TestFetchToolSchema.test_schema_uses_inclusive_bounds — asserts minimum/maximum present, exclusiveMinimum/exclusiveMaximum absent
  • Added TestFetchToolSchema.test_url_schema_omits_unsupported_keywords — asserts format and minLength absent from url schema

Evidence

Live Gemini validation was run against gemini-2.5-pro using the exact generated tool schema sent to generateContent.

Pre-fix schema (reproduced locally):

  • HTTP 400
  • INVALID_ARGUMENT
  • Error excerpt:
    • Unknown name "exclusiveMaximum"
    • Unknown name "exclusiveMinimum"

Current PR schema:

  • HTTP 200
  • Gemini accepted the tool declaration payload
  • Response text: Acknowledged.

Test plan

  • All 22 tests pass (uv run python -m pytest tests/test_server.py -v)
  • Schema verified: no exclusiveMinimum, exclusiveMaximum, format, or minLength in output
  • AnyUrl runtime validation confirmed: valid URLs accepted, invalid URLs rejected with ValidationError
  • Live Gemini validation: sent the exact generated schema to gemini-2.5-pro via generateContent; request was accepted with HTTP 200 and returned Acknowledged.
  • No behavioral change — same validation semantics, only schema representation changed

…port

The Fetch model's JSON schema used keywords incompatible with LLM
providers that only support OpenAPI 3.0 (e.g. Google Gemini 2.5 Pro),
causing 400 INVALID_ARGUMENT errors:

1. max_length field used gt=0/lt=1000000 (Pydantic Field constraints),
   which generated exclusiveMinimum/exclusiveMaximum — not recognized
   by Gemini. Changed to ge=1/le=999999 (identical semantics for
   integers), which emits the supported minimum/maximum keywords.

2. url field used AnyUrl, which generated format: "uri" and minLength: 1
   — Gemini only supports "enum" and "date-time" for string format.
   Added WithJsonSchema override to emit a plain string schema while
   preserving AnyUrl runtime validation.

Fixes modelcontextprotocol#1624

Made-with: Cursor
@Dharit13 Dharit13 force-pushed the fix/fetch-schema-inclusive-bounds branch from 26d0503 to 067766b Compare April 4, 2026 03:20
@Dharit13 Dharit13 changed the title fix(fetch): use inclusive minimum/maximum instead of exclusive bounds in schema fix(fetch): make fetch tool schema Gemini/OpenAPI 3.0 compatible Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] mcp-server-fetch tool json schema can‘t use in Google Gemini 2.5 pro

2 participants