Skip to content

Commit 984636d

Browse files
committed
feat: add include_response_info parameter to control response documentation
Add parameter to disable automatic response info in tool descriptions. Defaults to True for backward compatibility. Resolves #212
1 parent 4cd0c0a commit 984636d

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

fastapi_mcp/openapi/convert.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def convert_openapi_to_mcp_tools(
1818
openapi_schema: Dict[str, Any],
1919
describe_all_responses: bool = False,
2020
describe_full_response_schema: bool = False,
21+
include_response_info: bool = True,
2122
) -> Tuple[List[types.Tool], Dict[str, Dict[str, Any]]]:
2223
"""
2324
Convert OpenAPI operations to MCP tools.
@@ -26,6 +27,7 @@ def convert_openapi_to_mcp_tools(
2627
openapi_schema: The OpenAPI schema
2728
describe_all_responses: Whether to include all possible response schemas in tool descriptions
2829
describe_full_response_schema: Whether to include full response schema in tool descriptions
30+
include_response_info: Whether to include response information in tool descriptions
2931
3032
Returns:
3133
A tuple containing:
@@ -70,7 +72,7 @@ def convert_openapi_to_mcp_tools(
7072

7173
# Add response information to the description
7274
responses = operation.get("responses", {})
73-
if responses:
75+
if responses and include_response_info:
7476
response_info = "\n\n### Responses:\n"
7577

7678
# Find the success response

fastapi_mcp/server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def __init__(
4646
bool,
4747
Doc("Whether to include full json schema for responses in tool descriptions"),
4848
] = False,
49+
include_response_info: Annotated[
50+
bool,
51+
Doc("Whether to include response information in tool descriptions"),
52+
] = True,
4953
http_client: Annotated[
5054
Optional[httpx.AsyncClient],
5155
Doc(
@@ -103,6 +107,7 @@ def __init__(
103107
self._base_url = "http://apiserver"
104108
self._describe_all_responses = describe_all_responses
105109
self._describe_full_response_schema = describe_full_response_schema
110+
self._include_response_info = include_response_info
106111
self._include_operations = include_operations
107112
self._exclude_operations = exclude_operations
108113
self._include_tags = include_tags
@@ -136,6 +141,7 @@ def setup_server(self) -> None:
136141
openapi_schema,
137142
describe_all_responses=self._describe_all_responses,
138143
describe_full_response_schema=self._describe_full_response_schema,
144+
include_response_info=self._include_response_info,
139145
)
140146

141147
# Filter tools based on operation IDs and tags

tests/test_configuration.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,84 @@ async def empty_tags():
581581
exclude_tags_mcp = FastApiMCP(app, exclude_tags=["items"])
582582
assert len(exclude_tags_mcp.tools) == 1
583583
assert {tool.name for tool in exclude_tags_mcp.tools} == {"empty_tags"}
584+
585+
586+
def test_include_response_info_default_behavior(simple_fastapi_app: FastAPI):
587+
"""Test the default behavior of include_response_info parameter."""
588+
mcp_server = FastApiMCP(simple_fastapi_app)
589+
590+
# Check default value
591+
assert mcp_server._include_response_info is True
592+
593+
# Check that response information is included by default
594+
for tool in mcp_server.tools:
595+
assert tool.description is not None
596+
if tool.name == "raise_error":
597+
pass
598+
elif tool.name != "delete_item":
599+
assert "### Responses:" in tool.description, "Response section should be present"
600+
assert "**200**" in tool.description, "200 status code should be present"
601+
assert "**Example Response:**" in tool.description, "Example response should be present"
602+
else:
603+
# The delete endpoint returns 204 with no response body
604+
assert "### Responses:" in tool.description, "Response section should be present"
605+
assert "**204**" in tool.description, "204 status code should be present"
606+
607+
608+
def test_include_response_info_false_simple_app(simple_fastapi_app: FastAPI):
609+
"""Test include_response_info=False with the simple app."""
610+
mcp_server = FastApiMCP(simple_fastapi_app, include_response_info=False)
611+
612+
# Check that response information is excluded
613+
for tool in mcp_server.tools:
614+
assert tool.description is not None
615+
assert "### Responses:" not in tool.description, "Response section should not be present"
616+
assert "**200**" not in tool.description, "200 status code should not be present"
617+
assert "**204**" not in tool.description, "204 status code should not be present"
618+
assert "**Example Response:**" not in tool.description, "Example response should not be present"
619+
assert "**Output Schema:**" not in tool.description, "Output schema should not be present"
620+
621+
# Basic tool information should still be present
622+
if tool.name == "list_items":
623+
assert "List all items" in tool.description
624+
elif tool.name == "get_item":
625+
assert "Get a specific item" in tool.description
626+
elif tool.name == "create_item":
627+
assert "Create a new item" in tool.description
628+
629+
630+
def test_include_response_info_combined_with_other_options(simple_fastapi_app: FastAPI):
631+
"""Test include_response_info combined with other response-related options."""
632+
# Test with include_response_info=False and describe_all_responses=True
633+
mcp_server = FastApiMCP(
634+
simple_fastapi_app,
635+
include_response_info=False,
636+
describe_all_responses=True,
637+
describe_full_response_schema=True,
638+
)
639+
640+
# Even with describe_all_responses=True and describe_full_response_schema=True,
641+
# response information should be excluded when include_response_info=False
642+
for tool in mcp_server.tools:
643+
assert tool.description is not None
644+
assert "### Responses:" not in tool.description, "Response section should not be present"
645+
assert "**Example Response:**" not in tool.description, "Example response should not be present"
646+
assert "**Output Schema:**" not in tool.description, "Output schema should not be present"
647+
648+
649+
def test_include_response_info_true_explicit(simple_fastapi_app: FastAPI):
650+
"""Test include_response_info=True explicitly."""
651+
mcp_server = FastApiMCP(simple_fastapi_app, include_response_info=True)
652+
653+
# Check that response information is included
654+
for tool in mcp_server.tools:
655+
assert tool.description is not None
656+
if tool.name == "raise_error":
657+
pass
658+
elif tool.name != "delete_item":
659+
assert "### Responses:" in tool.description, "Response section should be present"
660+
assert "**200**" in tool.description, "200 status code should be present"
661+
assert "**Example Response:**" in tool.description, "Example response should be present"
662+
else:
663+
assert "### Responses:" in tool.description, "Response section should be present"
664+
assert "**204**" in tool.description, "204 status code should be present"

0 commit comments

Comments
 (0)