Skip to content

Commit ddb6b61

Browse files
committed
fix(fetch): accept null for optional parameters
Closes modelcontextprotocol#2035. Clients like LibreChat send explicit `null` for optional params instead of omitting them. The Fetch model declared `max_length: int`, `start_index: int`, and `raw: bool`, so Pydantic rejected `null` at the type-coercion step before field defaults could apply. Adds a `mode="before"` field_validator that maps `None` back to the field's declared default. Keeps field types as `int`/`bool`, preserves the `gt=0`/`lt=1_000_000`/`ge=0` validators on resolved values, and does not rely on falsy-coalescing (which would have replaced valid `start_index=0` or explicit `raw=false`). Adds 9 tests covering null handling, default preservation, explicit zero/false preservation, and validator behavior.
1 parent 4503e2d commit ddb6b61

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
INTERNAL_ERROR,
1919
)
2020
from protego import Protego
21-
from pydantic import BaseModel, Field, AnyUrl
21+
from pydantic import BaseModel, Field, AnyUrl, field_validator
2222

2323
DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
2424
DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)"
@@ -177,6 +177,13 @@ class Fetch(BaseModel):
177177
),
178178
]
179179

180+
@field_validator("max_length", "start_index", "raw", mode="before")
181+
@classmethod
182+
def _null_to_default(cls, value, info):
183+
if value is None:
184+
return cls.model_fields[info.field_name].default
185+
return value
186+
180187

181188
async def serve(
182189
custom_user_agent: str | None = None,

‎src/fetch/tests/test_server.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,72 @@
99
get_robots_txt_url,
1010
check_may_autonomously_fetch_url,
1111
fetch_url,
12+
Fetch,
1213
DEFAULT_USER_AGENT_AUTONOMOUS,
1314
)
1415

1516

17+
class TestFetchParamsNullHandling:
18+
"""Issue #2035: clients like LibreChat pass explicit null for optional params."""
19+
20+
def test_all_optional_params_omitted(self):
21+
args = Fetch(url="https://example.com")
22+
assert args.max_length == 5000
23+
assert args.start_index == 0
24+
assert args.raw is False
25+
26+
def test_explicit_null_max_length(self):
27+
args = Fetch(url="https://example.com", max_length=None)
28+
assert args.max_length == 5000
29+
30+
def test_explicit_null_start_index(self):
31+
args = Fetch(url="https://example.com", start_index=None)
32+
assert args.start_index == 0
33+
34+
def test_explicit_null_raw(self):
35+
args = Fetch(url="https://example.com", raw=None)
36+
assert args.raw is False
37+
38+
def test_all_nulls_together(self):
39+
args = Fetch(
40+
url="https://example.com",
41+
max_length=None,
42+
start_index=None,
43+
raw=None,
44+
)
45+
assert args.max_length == 5000
46+
assert args.start_index == 0
47+
assert args.raw is False
48+
49+
def test_explicit_values_are_preserved(self):
50+
args = Fetch(
51+
url="https://example.com",
52+
max_length=100,
53+
start_index=50,
54+
raw=True,
55+
)
56+
assert args.max_length == 100
57+
assert args.start_index == 50
58+
assert args.raw is True
59+
60+
def test_start_index_zero_preserved(self):
61+
"""Regression: `or`-coalescing would incorrectly fall through 0."""
62+
args = Fetch(url="https://example.com", start_index=0)
63+
assert args.start_index == 0
64+
65+
def test_raw_false_preserved(self):
66+
"""Regression: explicit False must stay False."""
67+
args = Fetch(url="https://example.com", raw=False)
68+
assert args.raw is False
69+
70+
def test_validators_still_apply(self):
71+
"""gt=0 on max_length must still reject invalid values after null handling."""
72+
with pytest.raises(ValueError):
73+
Fetch(url="https://example.com", max_length=0)
74+
with pytest.raises(ValueError):
75+
Fetch(url="https://example.com", start_index=-1)
76+
77+
1678
class TestGetRobotsTxtUrl:
1779
"""Tests for get_robots_txt_url function."""
1880

0 commit comments

Comments
 (0)