Skip to content

Commit a30ae5d

Browse files
committed
improve tests
1 parent a1f3f9c commit a30ae5d

File tree

5 files changed

+61
-50
lines changed

5 files changed

+61
-50
lines changed

examples/run_example.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

fastapi_mcp/http_tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ async def http_tool_function(**kwargs):
490490
http_tool_function.__doc__ = tool_description
491491

492492
# Monkey patch the function's schema for MCP tool creation
493-
http_tool_function._input_schema = input_schema
493+
# TODO: Maybe revise this hacky approach
494+
http_tool_function._input_schema = input_schema # type: ignore
494495

495496
# Add tool to the MCP server with the enhanced schema
496497
tool = mcp_server._tool_manager.add_tool(http_tool_function, name=operation_id, description=tool_description)

fastapi_mcp/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def create_mcp_server(
4242
# Configure server capabilities if provided
4343
if capabilities:
4444
for capability, value in capabilities.items():
45-
mcp_server.settings.capabilities[capability] = value
45+
# TODO: Maybe revise this hacky approach
46+
mcp_server.settings.capabilities[capability] = value # type: ignore
4647

4748
return mcp_server
4849

tests/test_http_tools.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from pydantic import BaseModel
99
from typing import List, Optional, Dict, Any
1010

11-
from fastapi_mcp import create_mcp_server, add_mcp_server
11+
from fastapi_mcp import add_mcp_server
1212
from fastapi_mcp.http_tools import (
13-
create_http_tool,
1413
resolve_schema_references,
1514
clean_schema_for_display,
1615
)
@@ -174,15 +173,3 @@ def test_create_mcp_tools_from_complex_app(complex_app):
174173

175174
# We'll skip checking the body parameter in the update tool as it seems
176175
# the implementation handles it differently than we expected
177-
178-
179-
# We need to comment out the test_create_http_tool test as it requires directly calling
180-
# create_http_tool with many parameters that would be cumbersome to mock
181-
# This test would be better implemented at the unit level within the library itself
182-
"""
183-
def test_create_http_tool():
184-
# This test was designed for direct usage of create_http_tool
185-
# But the function signature has changed and requires more parameters than expected
186-
# It would be better to test this at the unit level within the library
187-
pass
188-
"""

tests/test_tool_generation.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ def test_tool_generation_basic(sample_app):
7171
assert hasattr(tool, "parameters"), "Tool missing 'parameters' property"
7272
assert hasattr(tool, "fn_metadata"), "Tool missing 'fn_metadata' property"
7373

74-
# Verify specific properties of the list_items tool
74+
# Skip MCP's internal tool that doesn't follow the same patterns
75+
if tool.name == "handle_mcp_connection_mcp_get":
76+
continue
77+
78+
# With describe_all_responses=False by default, description should only include success response code
79+
assert "200" in tool.description, f"Expected success response code in description for {tool.name}"
80+
assert "422" not in tool.description, f"Expected not to see 422 response in tool description for {tool.name}"
81+
82+
# With describe_full_response_schema=False by default, description should not include the full output schema, only an example
83+
assert "Example Response" in tool.description, f"Expected example response in description for {tool.name}"
84+
assert "Output Schema" not in tool.description, (
85+
f"Expected not to see output schema in description for {tool.name}"
86+
)
87+
88+
# Verify specific parameters are present in the appropriate tools
7589
list_items_tool = next((t for t in tools if t.name == "list_items_items__get"), None)
7690
assert list_items_tool is not None, "list_items tool not found"
7791
assert "skip" in list_items_tool.parameters["properties"], "Expected 'skip' parameter"
@@ -88,17 +102,16 @@ def test_tool_generation_with_full_schema(sample_app):
88102
# Extract tools for inspection
89103
tools = mcp_server._tool_manager.list_tools()
90104

91-
# Verify specific properties of the list_items tool
92-
list_items_tool = next((t for t in tools if t.name == "list_items_items__get"), None)
93-
assert list_items_tool is not None, "list_items tool not found"
94-
95-
# In the full schema mode, the item schema should be included somewhere in the tool
96-
# This might be in the description rather than the parameters
97-
description = list_items_tool.description
105+
# Check all tools have the appropriate schema information
106+
for tool in tools:
107+
# Skip MCP's internal tool that doesn't follow the same patterns
108+
if tool.name == "handle_mcp_connection_mcp_get":
109+
continue
98110

99-
# Check that the tool includes information about the Item schema
100-
assert "Item" in description, "Item schema should be included in the description"
101-
assert "price" in description, "Item properties should be included in the description"
111+
description = tool.description
112+
# Check that the tool includes information about the Item schema
113+
assert "Item" in description, f"Item schema should be included in the description for {tool.name}"
114+
assert "price" in description, f"Item properties should be included in the description for {tool.name}"
102115

103116

104117
def test_tool_generation_with_all_responses(sample_app):
@@ -111,12 +124,39 @@ def test_tool_generation_with_all_responses(sample_app):
111124
# Extract tools for inspection
112125
tools = mcp_server._tool_manager.list_tools()
113126

114-
# Find the read_item tool
115-
read_item_tool = next((t for t in tools if t.name == "read_item_items__item_id__get"), None)
116-
assert read_item_tool is not None, "read_item tool not found"
127+
# Check all API tools include all response status codes
128+
for tool in tools:
129+
# Skip MCP's internal tool that doesn't follow the same patterns
130+
if tool.name == "handle_mcp_connection_mcp_get":
131+
continue
132+
133+
assert "200" in tool.description, f"Expected success response code in description for {tool.name}"
134+
assert "422" in tool.description, f"Expected 422 response code in description for {tool.name}"
135+
136+
137+
def test_tool_generation_with_all_responses_and_full_schema(sample_app):
138+
"""Test that MCP tools include all possible responses and full schema when requested."""
139+
# Create MCP server with all response status codes and full schema
140+
mcp_server = add_mcp_server(
141+
sample_app,
142+
serve_tools=True,
143+
base_url="http://localhost:8000",
144+
describe_all_responses=True,
145+
describe_full_response_schema=True,
146+
)
147+
148+
# Extract tools for inspection
149+
tools = mcp_server._tool_manager.list_tools()
150+
151+
# Check all tools include all response status codes and the full output schema
152+
for tool in tools:
153+
# Skip MCP's internal tool that doesn't follow the same patterns
154+
if tool.name == "handle_mcp_connection_mcp_get":
155+
continue
117156

118-
# With describe_all_responses=True, description should include response status codes
119-
assert "200" in read_item_tool.description, "Expected success response code in description"
157+
assert "200" in tool.description, f"Expected success response code in description for {tool.name}"
158+
assert "422" in tool.description, f"Expected 422 response code in description for {tool.name}"
159+
assert "Output Schema" in tool.description, f"Expected output schema in description for {tool.name}"
120160

121161

122162
def test_custom_tool_addition(sample_app):

0 commit comments

Comments
 (0)