diff --git a/litellm/proxy/_experimental/mcp_server/openapi_to_mcp_generator.py b/litellm/proxy/_experimental/mcp_server/openapi_to_mcp_generator.py index f0353f309a9..c79bbd939dd 100644 --- a/litellm/proxy/_experimental/mcp_server/openapi_to_mcp_generator.py +++ b/litellm/proxy/_experimental/mcp_server/openapi_to_mcp_generator.py @@ -361,13 +361,21 @@ def build_input_schema(operation: _OpenAPIOperation) -> dict[str, object]: if "name" not in param: continue param_name = param["name"] - param_schema = param.get("schema", {}) - param_type = param_schema.get("type", "string") - - properties[param_name] = { - "type": param_type, - "description": param.get("description", ""), - } + raw_schema = param.get("schema") + # Preserve the full resolved JSON Schema for the parameter + # (#29715). Previously only `type` was copied, dropping `items`, + # `enum`, `format`, `default`, `properties`, `required`, etc. + # Downstream consumers (CrewAI etc.) then converted the + # truncated schema to Pydantic and produced `items: {}` for + # arrays, which OpenAI rejects as an invalid tool schema. + schema_copy = dict(raw_schema) if isinstance(raw_schema, dict) else {} + schema_copy.setdefault("type", "string") + # OpenAPI puts the human description on the parameter object, + # not on the schema; let it win when present, else fall back to + # any schema-level description, else empty string. + description = param.get("description") or schema_copy.get("description") or "" + schema_copy["description"] = description + properties[param_name] = schema_copy if param.get("required", False): required.append(param_name) @@ -380,11 +388,14 @@ def build_input_schema(operation: _OpenAPIOperation) -> dict[str, object]: # Try to get JSON schema if "application/json" in content: schema: Final[_OpenAPIJSONSchema] = content["application/json"].get("schema", {}) - properties["body"] = { - "type": "object", - "description": request_body.get("description", "Request body"), - "properties": schema.get("properties", {}), - } + # Preserve the full body schema (#29715) instead of only + # `properties` — required, additionalProperties, $defs, + # nested items/enum, etc. are all load-bearing for downstream + # tool-call validation. + body_copy: Final = dict(schema) if schema else {} + body_copy.setdefault("type", "object") + body_copy["description"] = request_body.get("description", "Request body") + properties["body"] = body_copy if request_body.get("required", False): required.append("body") diff --git a/tests/unit/proxy/_experimental/mcp_server/test_openapi_to_mcp_generator.py b/tests/unit/proxy/_experimental/mcp_server/test_openapi_to_mcp_generator.py index 9f3db632db1..b18c2f44aa6 100644 --- a/tests/unit/proxy/_experimental/mcp_server/test_openapi_to_mcp_generator.py +++ b/tests/unit/proxy/_experimental/mcp_server/test_openapi_to_mcp_generator.py @@ -598,6 +598,167 @@ class TestBuildInputSchema: # Required should include original names assert "repository-id" in schema["required"] + def test_preserves_array_items_for_repeatable_query_params(self): + """#29715: array-of-string query parameters must carry their + `items` keyword through to the MCP schema. Without it, downstream + consumers (CrewAI etc.) emit `items: {}` and OpenAI rejects the + tool schema.""" + operation = { + "parameters": [ + { + "name": "domain", + "in": "query", + "required": True, + "description": "Repeatable query parameter", + "schema": { + "type": "array", + "items": {"type": "string"}, + }, + } + ] + } + + schema = build_input_schema(operation) + + domain = schema["properties"]["domain"] + assert domain["type"] == "array" + assert domain["items"] == {"type": "string"} + assert domain["description"] == "Repeatable query parameter" + + def test_preserves_enum_format_default_on_parameters(self): + """#29715: enum/format/default and similar JSON Schema keywords + must round-trip into the MCP schema.""" + operation = { + "parameters": [ + { + "name": "status", + "in": "query", + "schema": { + "type": "string", + "enum": ["open", "closed"], + "default": "open", + }, + }, + { + "name": "created_at", + "in": "query", + "schema": { + "type": "string", + "format": "date-time", + }, + }, + ] + } + + schema = build_input_schema(operation) + + status = schema["properties"]["status"] + assert status["enum"] == ["open", "closed"] + assert status["default"] == "open" + created = schema["properties"]["created_at"] + assert created["format"] == "date-time" + + def test_preserves_nested_object_properties_and_required(self): + """#29715: an inline object parameter must keep its `properties` + and `required` so downstream validators see the full shape.""" + operation = { + "parameters": [ + { + "name": "filter", + "in": "query", + "schema": { + "type": "object", + "properties": { + "min": {"type": "integer"}, + "max": {"type": "integer"}, + }, + "required": ["min"], + }, + } + ] + } + + schema = build_input_schema(operation) + + f = schema["properties"]["filter"] + assert f["properties"]["min"] == {"type": "integer"} + assert f["properties"]["max"] == {"type": "integer"} + assert f["required"] == ["min"] + + def test_parameter_description_overrides_schema_description(self): + """OpenAPI 3 places the description on the parameter object; let it + win over a schema-level description when both are present.""" + operation = { + "parameters": [ + { + "name": "q", + "in": "query", + "description": "from parameter", + "schema": { + "type": "string", + "description": "from schema", + }, + } + ] + } + + schema = build_input_schema(operation) + assert schema["properties"]["q"]["description"] == "from parameter" + + def test_parameter_falls_back_to_schema_description(self): + """If only the schema has a description, surface that instead of an + empty string.""" + operation = { + "parameters": [ + { + "name": "q", + "in": "query", + "schema": { + "type": "string", + "description": "from schema only", + }, + } + ] + } + + schema = build_input_schema(operation) + assert schema["properties"]["q"]["description"] == "from schema only" + + def test_preserves_request_body_required_and_additional_props(self): + """#29715 (requestBody half): the body schema must keep `required`, + `additionalProperties`, and other top-level JSON Schema keywords — + not just `properties`.""" + operation = { + "requestBody": { + "required": True, + "description": "Create user", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "email": {"type": "string"}, + "tags": { + "type": "array", + "items": {"type": "string"}, + }, + }, + "required": ["email"], + "additionalProperties": False, + } + } + }, + } + } + + schema = build_input_schema(operation) + body = schema["properties"]["body"] + assert body["properties"]["email"] == {"type": "string"} + assert body["properties"]["tags"]["items"] == {"type": "string"} + assert body["required"] == ["email"] + assert body["additionalProperties"] is False + assert body["description"] == "Create user" + class TestExtractParameters: """Test parameter extraction from OpenAPI operations."""