Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Send numeric and boolean OpenAPI raw bodies as text (#5624)

Asem ahmed281a8575e452
diff --git a/fastmcp_slim/fastmcp/utilities/openapi/director.py b/fastmcp_slim/fastmcp/utilities/openapi/director.py
index 68568734e..cab9c71ba 100644
--- a/fastmcp_slim/fastmcp/utilities/openapi/director.py
+++ b/fastmcp_slim/fastmcp/utilities/openapi/director.py
@@ -203,14 +203,19 @@ class RequestDirector:
else:
json_body = body
else:
- content = body
+ # httpx rejects numbers and booleans as raw content
+ content = (
+ _query_scalar_to_str(body)
+ if isinstance(body, bool | int | float)
+ else body
+ )
if raw_content_type is not None:
headers = dict(headers) if headers else {}
headers["Content-Type"] = raw_content_type
- if isinstance(body, str):
+ if isinstance(content, str):
media_type = Message()
media_type["Content-Type"] = raw_content_type
- content = body.encode(
+ content = content.encode(
media_type.get_content_charset() or "utf-8"
)
diff --git a/tests/utilities/openapi/test_director.py b/tests/utilities/openapi/test_director.py
index 33e1373ef..4d71f521d 100644
--- a/tests/utilities/openapi/test_director.py
+++ b/tests/utilities/openapi/test_director.py
@@ -363,6 +363,38 @@ class TestRequestDirector:
assert request.content == expected
assert request.headers["content-type"] == media_type
+ @pytest.mark.parametrize(
+ ("schema_type", "media_type", "body", "expected"),
+ [
+ ("integer", "text/plain", 42, b"42"),
+ ("number", "text/plain", 1.5, b"1.5"),
+ ("boolean", "text/plain", True, b"true"),
+ ("boolean", "text/plain", False, b"false"),
+ ("integer", "text/plain; charset=utf-8", 7, b"7"),
+ ],
+ )
+ def test_body_construction_scalar_raw_body(
+ self, director, schema_type, media_type, body, expected
+ ):
+ """A numeric or boolean body for a non-JSON media type is sent as text."""
+ route = HTTPRoute(
+ path="/value",
+ method="POST",
+ operation_id="set_value",
+ request_body=RequestBodyInfo(
+ required=True,
+ content_schema={media_type: {"type": schema_type}},
+ ),
+ parameter_map={
+ "content": {"location": "body", "openapi_name": "content"},
+ },
+ )
+
+ request = director.build(route, {"content": body}, "https://api.example.com")
+
+ assert request.content == expected
+ assert request.headers["content-type"] == media_type
+
def test_body_construction_multiple_properties_non_object_schema(self, director):
"""Test body construction with multiple properties but non-object schema."""
route = HTTPRoute(