15
15
from pydantic import Field
16
16
17
17
from .openapi_utils import (
18
- OPENAPI_PYTHON_TYPES_MAP ,
19
18
clean_schema_for_display ,
20
- extract_model_examples_from_components ,
21
19
generate_example_from_schema ,
22
- parse_parameters_for_args_schema ,
20
+ parse_param_schema_for_python_type_and_default ,
23
21
resolve_schema_references ,
24
22
PYTHON_TYPE_IMPORTS ,
25
23
)
@@ -54,7 +52,7 @@ def create_mcp_tools_from_openapi(
54
52
)
55
53
56
54
# Resolve all references in the schema at once
57
- openapi_schema = resolve_schema_references (openapi_schema , openapi_schema )
55
+ resolved_openapi_schema = resolve_schema_references (openapi_schema , openapi_schema )
58
56
59
57
if not base_url :
60
58
# Try to determine the base URL from FastAPI config
@@ -74,7 +72,7 @@ def create_mcp_tools_from_openapi(
74
72
base_url = base_url [:- 1 ]
75
73
76
74
# Process each path in the OpenAPI schema
77
- for path , path_item in openapi_schema .get ("paths" , {}).items ():
75
+ for path , path_item in resolved_openapi_schema .get ("paths" , {}).items ():
78
76
for method , operation in path_item .items ():
79
77
# Skip non-HTTP methods
80
78
if method not in ["get" , "post" , "put" , "delete" , "patch" ]:
@@ -97,24 +95,28 @@ def create_mcp_tools_from_openapi(
97
95
parameters = operation .get ("parameters" , []),
98
96
request_body = operation .get ("requestBody" , {}),
99
97
responses = operation .get ("responses" , {}),
100
- openapi_schema = openapi_schema ,
98
+ openapi_schema = resolved_openapi_schema ,
101
99
describe_all_responses = describe_all_responses ,
102
100
describe_full_response_schema = describe_full_response_schema ,
103
101
)
104
102
105
- def _create_http_tool_function (function_template : Callable , args_schema : Dict [str , Any ], additional_variables : Dict [str , Any ]) -> Callable :
103
+ def _create_http_tool_function (function_template : Callable , properties : Dict [str , Any ], additional_variables : Dict [str , Any ]) -> Callable :
106
104
# Build parameter string with type hints
107
105
param_list = []
108
- for name , type_info in args_schema .items ():
109
- type_hint = OPENAPI_PYTHON_TYPES_MAP .get (type_info , 'Any' )
110
- param_list .append (f"{ name } : { type_hint } " )
111
- parameters_str = ", " .join (param_list )
112
-
106
+ param_list_with_defaults = []
107
+ for name , schema in properties .items ():
108
+ type_hint , has_default_value = parse_param_schema_for_python_type_and_default (schema )
109
+ if has_default_value :
110
+ param_list_with_defaults .append (f"{ name } : { type_hint } " )
111
+ else :
112
+ param_list .append (f"{ name } : { type_hint } " )
113
+ parameters_str = ", " .join (param_list + param_list_with_defaults )
114
+
113
115
dynamic_function_body = f"""async def dynamic_http_tool_function({ parameters_str } ):
114
- kwargs = {{{ ', ' .join ([f"'{ k } ': { k } " for k in args_schema .keys ()])} }}
116
+ kwargs = {{{ ', ' .join ([f"'{ k } ': { k } " for k in properties .keys ()])} }}
115
117
return await http_tool_function_template(**kwargs)
116
118
"""
117
-
119
+
118
120
# Create function namespace with required imports
119
121
namespace = {
120
122
"http_tool_function_template" : function_template ,
@@ -207,13 +209,6 @@ def create_http_tool(
207
209
model_name = None
208
210
model_examples = None
209
211
items_model_name = None
210
- if "$ref" in schema : # This check is now just for information
211
- ref_path = schema ["$ref" ]
212
- if ref_path .startswith ("#/components/schemas/" ):
213
- model_name = ref_path .split ("/" )[- 1 ]
214
- response_info += f"\n Model: { model_name } "
215
- # Try to get examples from the model
216
- model_examples = extract_model_examples_from_components (model_name , openapi_schema )
217
212
218
213
# Check if this is an array of items
219
214
if schema .get ("type" ) == "array" and "items" in schema and "$ref" in schema ["items" ]:
@@ -390,7 +385,7 @@ def create_http_tool(
390
385
391
386
if required_props :
392
387
input_schema ["required" ] = required_props
393
-
388
+
394
389
# Dynamically create a function to call the API endpoint
395
390
async def http_tool_function_template (** kwargs ):
396
391
# Prepare URL with path parameters
@@ -437,9 +432,8 @@ async def http_tool_function_template(**kwargs):
437
432
return response .text
438
433
439
434
# Create the http_tool_function (with name and docstring)
440
- args_schema = parse_parameters_for_args_schema (parameters )
441
435
additional_variables = {"path_params" : path_params , "query_params" : query_params , "header_params" : header_params }
442
- http_tool_function = _create_http_tool_function (http_tool_function_template , args_schema , additional_variables )
436
+ http_tool_function = _create_http_tool_function (http_tool_function_template , properties , additional_variables )
443
437
http_tool_function .__name__ = operation_id
444
438
http_tool_function .__doc__ = tool_description
445
439
0 commit comments