Skip to content

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

Send {} for required OpenAPI object bodies with no fields (#6340)

rome-xi2fdd341b3fe3
diff --git a/fastmcp_slim/fastmcp/utilities/openapi/director.py b/fastmcp_slim/fastmcp/utilities/openapi/director.py
index 428147dc5..cf9b53681 100644
--- a/fastmcp_slim/fastmcp/utilities/openapi/director.py
+++ b/fastmcp_slim/fastmcp/utilities/openapi/director.py
@@ -48,6 +48,31 @@ def _uses_default_multipart_encoding(encoding: dict[str, Any]) -> bool:
)
+def _required_json_object_without_properties(route: HTTPRoute) -> bool:
+ """Return whether an omitted required JSON object body should be sent empty.
+
+ Flattened object properties are optional tool arguments. Leaving all of
+ them out is still an empty JSON object. Optional bodies stay absent.
+ """
+ request_body = route.request_body
+ if (
+ request_body is None
+ or not request_body.required
+ or not request_body.content_schema
+ ):
+ return False
+ content_type = next(iter(request_body.content_schema))
+ media_type = content_type.split(";")[0].strip().lower()
+ if media_type != "application/json" and not media_type.endswith("+json"):
+ return False
+ body_schema = request_body.content_schema[content_type]
+ return (
+ isinstance(body_schema, dict)
+ and bool(body_schema.get("properties"))
+ and _ref_is_mergeable_object(body_schema, route.request_schemas)
+ )
+
+
def _prepare_parameter_map(
route: HTTPRoute,
) -> tuple[HTTPRoute, dict[str, dict[str, str]]]:
@@ -365,6 +390,8 @@ class RequestDirector:
body = body_props
else:
body = body_props
+ elif _required_json_object_without_properties(route):
+ body = {}
return path_params, query_params, header_params, cookie_params, body
diff --git a/tests/server/providers/openapi/test_object_request_bodies.py b/tests/server/providers/openapi/test_object_request_bodies.py
index de2324040..e9740c145 100644
--- a/tests/server/providers/openapi/test_object_request_bodies.py
+++ b/tests/server/providers/openapi/test_object_request_bodies.py
@@ -170,3 +170,71 @@ async def test_fastapi_dictionary_body(labels: dict[str, str]) -> None:
result = await client.call_tool("set_labels", {"labels": labels})
assert result.structured_content == labels
+
+
[email protected]("openapi_version", ["3.0.3", "3.1.0"])
+ ("required", "arguments", "expected"),
+ [
+ (True, {}, {}),
+ (True, {"limit": 5}, {"limit": 5}),
+ (False, {}, None),
+ (False, {"limit": 5}, {"limit": 5}),
+ ],
+)
+async def test_required_json_object_with_omitted_properties_sends_empty_object(
+ openapi_version: str,
+ required: bool,
+ arguments: dict[str, Any],
+ expected: dict[str, Any] | None,
+) -> None:
+ """A required object body is {} when every property is left out.
+
+ Optional bodies stay absent. Supplying a property still sends that object.
+ """
+ spec = {
+ "openapi": openapi_version,
+ "info": {"title": "Search", "version": "1.0"},
+ "paths": {
+ "/search": {
+ "post": {
+ "operationId": "search",
+ "requestBody": {
+ "required": required,
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {"limit": {"type": "integer"}},
+ }
+ }
+ },
+ },
+ "responses": {"200": {"description": "OK"}},
+ }
+ }
+ },
+ }
+ requests: list[httpx2.Request] = []
+
+ def capture(request: httpx2.Request) -> httpx2.Response:
+ requests.append(request)
+ return httpx2.Response(200, json={"ok": True})
+
+ async with httpx2.AsyncClient(
+ base_url="https://offline.example.test",
+ transport=httpx2.MockTransport(capture),
+ ) as http_client:
+ server = FastMCP.from_openapi(openapi_spec=spec, client=http_client)
+ async with Client(server) as client:
+ await client.call_tool("search", arguments)
+
+ assert len(requests) == 1
+ if expected is None:
+ assert requests[0].content == b""
+ assert "content-type" not in requests[0].headers
+ return
+ assert requests[0].headers["content-type"] == "application/json"
+ assert json.loads(requests[0].content) == expected
+ if expected == {}:
+ assert requests[0].content == b"{}"