Skip to content

fix: make sure items etc. is passed to mcp tools #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions fastapi_mcp/openapi/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ def convert_openapi_to_mcp_tools(
param_desc = param.get("description", "")
param_required = param.get("required", True) # Path params are usually required

properties[param_name] = {
"type": param_schema.get("type", "string"),
"title": param_name,
"description": param_desc,
}
properties[param_name] = param_schema.copy()
properties[param_name]["title"] = param_name
if param_desc:
properties[param_name]["description"] = param_desc

if "type" not in properties[param_name]:
properties[param_name]["type"] = param_schema.get("type", "string")

if param_required:
required_props.append(param_name)
Expand All @@ -217,11 +219,14 @@ def convert_openapi_to_mcp_tools(
param_desc = param.get("description", "")
param_required = param.get("required", False)

properties[param_name] = {
"type": get_single_param_type_from_schema(param_schema),
"title": param_name,
"description": param_desc,
}
properties[param_name] = param_schema.copy()
properties[param_name]["title"] = param_name
if param_desc:
properties[param_name]["description"] = param_desc

if "type" not in properties[param_name]:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)

if "default" in param_schema:
properties[param_name]["default"] = param_schema["default"]

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

properties[param_name] = {
"type": get_single_param_type_from_schema(param_schema),
"title": param_name,
}
properties[param_name] = param_schema.copy()
properties[param_name]["title"] = param_name
if param_desc:
properties[param_name]["description"] = param_desc

if "type" not in properties[param_name]:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)

if "default" in param_schema:
properties[param_name]["default"] = param_schema["default"]

Expand Down
25 changes: 25 additions & 0 deletions tests/test_openapi_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,28 @@ def test_request_body_handling(complex_fastapi_app: FastAPI):
assert "create_order" in operation_map
assert operation_map["create_order"]["path"] == "/orders"
assert operation_map["create_order"]["method"] == "post"


def test_missing_type_handling(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)

# Remove the type field from the product_id schema
params = openapi_schema["paths"]["/products/{product_id}"]["get"]["parameters"]
for param in params:
if param.get("name") == "product_id" and "schema" in param:
param["schema"].pop("type", None)
break

tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)

get_product_tool = next(tool for tool in tools if tool.name == "get_product")
get_product_props = get_product_tool.inputSchema["properties"]

assert "product_id" in get_product_props
assert get_product_props["product_id"].get("type") == "string" # Default type applied