Skip to content

Commit bbb7528

Browse files
authored
Merge pull request #86 from MagnusS0/fix/convert
fix: make sure items etc. is passed to mcp tools
2 parents 39a8733 + dae8035 commit bbb7528

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

fastapi_mcp/openapi/convert.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ def convert_openapi_to_mcp_tools(
202202
param_desc = param.get("description", "")
203203
param_required = param.get("required", True) # Path params are usually required
204204

205-
properties[param_name] = {
206-
"type": param_schema.get("type", "string"),
207-
"title": param_name,
208-
"description": param_desc,
209-
}
205+
properties[param_name] = param_schema.copy()
206+
properties[param_name]["title"] = param_name
207+
if param_desc:
208+
properties[param_name]["description"] = param_desc
209+
210+
if "type" not in properties[param_name]:
211+
properties[param_name]["type"] = param_schema.get("type", "string")
210212

211213
if param_required:
212214
required_props.append(param_name)
@@ -217,11 +219,14 @@ def convert_openapi_to_mcp_tools(
217219
param_desc = param.get("description", "")
218220
param_required = param.get("required", False)
219221

220-
properties[param_name] = {
221-
"type": get_single_param_type_from_schema(param_schema),
222-
"title": param_name,
223-
"description": param_desc,
224-
}
222+
properties[param_name] = param_schema.copy()
223+
properties[param_name]["title"] = param_name
224+
if param_desc:
225+
properties[param_name]["description"] = param_desc
226+
227+
if "type" not in properties[param_name]:
228+
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)
229+
225230
if "default" in param_schema:
226231
properties[param_name]["default"] = param_schema["default"]
227232

@@ -233,10 +238,14 @@ def convert_openapi_to_mcp_tools(
233238
param_schema = param.get("schema", {})
234239
param_required = param.get("required", False)
235240

236-
properties[param_name] = {
237-
"type": get_single_param_type_from_schema(param_schema),
238-
"title": param_name,
239-
}
241+
properties[param_name] = param_schema.copy()
242+
properties[param_name]["title"] = param_name
243+
if param_desc:
244+
properties[param_name]["description"] = param_desc
245+
246+
if "type" not in properties[param_name]:
247+
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)
248+
240249
if "default" in param_schema:
241250
properties[param_name]["default"] = param_schema["default"]
242251

tests/test_openapi_conversion.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,28 @@ def test_request_body_handling(complex_fastapi_app: FastAPI):
271271
assert "create_order" in operation_map
272272
assert operation_map["create_order"]["path"] == "/orders"
273273
assert operation_map["create_order"]["method"] == "post"
274+
275+
276+
def test_missing_type_handling(complex_fastapi_app: FastAPI):
277+
openapi_schema = get_openapi(
278+
title=complex_fastapi_app.title,
279+
version=complex_fastapi_app.version,
280+
openapi_version=complex_fastapi_app.openapi_version,
281+
description=complex_fastapi_app.description,
282+
routes=complex_fastapi_app.routes,
283+
)
284+
285+
# Remove the type field from the product_id schema
286+
params = openapi_schema["paths"]["/products/{product_id}"]["get"]["parameters"]
287+
for param in params:
288+
if param.get("name") == "product_id" and "schema" in param:
289+
param["schema"].pop("type", None)
290+
break
291+
292+
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
293+
294+
get_product_tool = next(tool for tool in tools if tool.name == "get_product")
295+
get_product_props = get_product_tool.inputSchema["properties"]
296+
297+
assert "product_id" in get_product_props
298+
assert get_product_props["product_id"].get("type") == "string" # Default type applied

0 commit comments

Comments
 (0)