Skip to content

Commit 13cc8b5

Browse files
committed
expose more info about inputschema
1 parent 7b954d2 commit 13cc8b5

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

fastapi_mcp/http_tools.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ def create_http_tool(
112112
if description:
113113
tool_description += f"\n\n{description}"
114114

115+
# Add response schema information to description
116+
if responses:
117+
response_info = "\n\n### Responses:\n"
118+
for status_code, response_data in responses.items():
119+
response_desc = response_data.get("description", "")
120+
response_info += f"\n**{status_code}**: {response_desc}"
121+
122+
# Add schema information if available
123+
if "content" in response_data:
124+
for content_type, content_data in response_data["content"].items():
125+
if "schema" in content_data:
126+
schema = content_data["schema"]
127+
response_info += f"\nContent-Type: {content_type}"
128+
129+
# Format schema information
130+
if "properties" in schema:
131+
response_info += "\n\nSchema:\n```json\n"
132+
response_info += json.dumps(schema, indent=2)
133+
response_info += "\n```"
134+
135+
tool_description += response_info
136+
115137
# Organize parameters by type
116138
path_params = []
117139
query_params = []
@@ -149,6 +171,51 @@ def create_http_tool(
149171
)
150172
)
151173

174+
# Create input schema properties for all parameters
175+
properties = {}
176+
required_props = []
177+
178+
# Add path parameters to properties
179+
for param_name, param in path_params:
180+
param_schema = param.get("schema", {})
181+
param_desc = param.get("description", "")
182+
param_required = param.get("required", True) # Path params are usually required
183+
184+
properties[param_name] = {
185+
"type": param_schema.get("type", "string"),
186+
"title": param_name,
187+
"description": param_desc,
188+
}
189+
190+
if param_required:
191+
required_props.append(param_name)
192+
193+
# Add query parameters to properties
194+
for param_name, param in query_params:
195+
param_schema = param.get("schema", {})
196+
param_desc = param.get("description", "")
197+
param_required = param.get("required", False)
198+
199+
properties[param_name] = {
200+
"type": param_schema.get("type", "string"),
201+
"title": param_name,
202+
"description": param_desc,
203+
}
204+
205+
if param_required:
206+
required_props.append(param_name)
207+
208+
# Add body parameters to properties
209+
for param_name, param in body_params:
210+
param_schema = param.get("schema", {})
211+
param_required = param.get("required", False)
212+
213+
properties[param_name] = param_schema
214+
properties[param_name]["title"] = param_name
215+
216+
if param_required:
217+
required_props.append(param_name)
218+
152219
# Function to dynamically call the API endpoint
153220
async def http_tool_function(**kwargs):
154221
# Prepare URL with path parameters
@@ -194,9 +261,21 @@ async def http_tool_function(**kwargs):
194261
except ValueError:
195262
return response.text
196263

264+
# Create a proper input schema for the tool
265+
input_schema = {"type": "object", "properties": properties, "title": f"{operation_id}Arguments"}
266+
267+
if required_props:
268+
input_schema["required"] = required_props
269+
197270
# Set the function name and docstring
198271
http_tool_function.__name__ = operation_id
199272
http_tool_function.__doc__ = tool_description
200273

201-
# Add tool to the MCP server
202-
mcp_server.add_tool(http_tool_function, name=operation_id, description=tool_description)
274+
# Monkey patch the function's schema for MCP tool creation
275+
http_tool_function._input_schema = input_schema
276+
277+
# Add tool to the MCP server with the enhanced schema
278+
tool = mcp_server._tool_manager.add_tool(http_tool_function, name=operation_id, description=tool_description)
279+
280+
# Update the tool's parameters to use our custom schema instead of the auto-generated one
281+
tool.parameters = input_schema

0 commit comments

Comments
 (0)