Skip to content

Commit a2681c4

Browse files
cliffhalldhshah13claude
committed
fix(fetch): emit inclusive minimum/maximum for max_length (modelcontextprotocol#1624)
The Fetch model bounded max_length with pydantic gt=0/lt=1000000, which emits exclusiveMinimum/exclusiveMaximum in the tool's inputSchema. Gemini (and other clients that accept only the OpenAPI 3.0 subset) reject those keywords, so the fetch tool could not be declared at all. Use ge=1/le=999999 instead: the same accepted range for an int, emitted as minimum/maximum. Ported from the max_length part of modelcontextprotocol#3812 by Dharit Shah. Its url-schema change (dropping format: uri and minLength) is not part of this fix. The protocol suite's tools/list assertion now expects minimum/maximum and its KNOWN BUG modelcontextprotocol#1624 marker is gone; a new test pins that the edges 1 and 999999 are accepted, beside the existing 0 and 1000000 rejections. Part of Wave 5 of modelcontextprotocol#5004. Co-authored-by: Dharit Shah <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 142bb84 commit a2681c4

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎src/mcp_server_fetch/server.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ class Fetch(BaseModel):
169169
Field(
170170
default=5000,
171171
description="Maximum number of characters to return.",
172-
gt=0,
173-
lt=1000000,
172+
# ge/le rather than gt/lt: the inclusive bounds emit minimum/maximum,
173+
# which every client accepts, while exclusiveMinimum/exclusiveMaximum
174+
# are rejected by some (Gemini, #1624). Same range for an int.
175+
ge=1,
176+
le=999999,
174177
),
175178
]
176179
start_index: Annotated[

‎tests/test_protocol.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ async def test_server_version_is_the_sdk_version(web: FakeWeb) -> None:
107107
# --------------------------------------------------------------------------
108108

109109

110-
# KNOWN BUG #1624: pins current (wrong) behavior; the fix changes this assertion.
111110
async def test_list_tools_wire_shape(web: FakeWeb) -> None:
112111
async with connect() as (session, _):
113112
result = await session.list_tools()
@@ -129,13 +128,13 @@ async def test_list_tools_wire_shape(web: FakeWeb) -> None:
129128
"title": "Url",
130129
"type": "string",
131130
},
132-
# #1624: pydantic's gt/lt emit the numeric (draft 6+) form of
133-
# exclusiveMinimum/exclusiveMaximum, which some clients reject.
131+
# #1624: inclusive minimum/maximum, never exclusiveMinimum/
132+
# exclusiveMaximum, which some clients (Gemini) reject.
134133
"max_length": {
135134
"default": 5000,
136135
"description": "Maximum number of characters to return.",
137-
"exclusiveMaximum": 1000000,
138-
"exclusiveMinimum": 0,
136+
"maximum": 999999,
137+
"minimum": 1,
139138
"title": "Max Length",
140139
"type": "integer",
141140
},
@@ -184,7 +183,8 @@ async def test_call_with_only_url_applies_defaults(web: FakeWeb) -> None:
184183
({}, "'url'"),
185184
({"url": ""}, "''"),
186185
({"url": 5}, "5"),
187-
# #1624: the bounds come from exclusiveMinimum/exclusiveMaximum.
186+
# #1624: the bounds are minimum 1 and maximum 999999, so the rejected
187+
# values are the same as under the old exclusive 0 and 1000000.
188188
({"url": PAGE, "max_length": 0}, "0"),
189189
({"url": PAGE, "max_length": 1000000}, "1000000"),
190190
({"url": PAGE, "start_index": -1}, "-1"),
@@ -205,6 +205,18 @@ async def test_schema_violations_are_rejected_by_the_sdk(
205205
assert web.requests == []
206206

207207

208+
@pytest.mark.parametrize("max_length", [1, 999999])
209+
async def test_max_length_bounds_are_inclusive(web: FakeWeb, max_length: int) -> None:
210+
# #1624: the edges of the inclusive range pass both the SDK's schema check
211+
# and Fetch's own validation, and the page is fetched.
212+
web.add(ROBOTS, plain("", status=404))
213+
web.add(PAGE, plain("hello"))
214+
async with connect() as (session, _):
215+
result = await call(session, {"url": PAGE, "max_length": max_length})
216+
assert wire(result)["isError"] is False
217+
assert web.urls() == [ROBOTS, PAGE]
218+
219+
208220
@pytest.mark.parametrize("url", ["not a url", "example.com/page", "http://"])
209221
async def test_invalid_url_is_rejected_by_pydantic(web: FakeWeb, url: str) -> None:
210222
# jsonschema does not check `format: uri`, so these reach Fetch(**arguments)

0 commit comments

Comments
 (0)