Skip to content

Commit e02d7c2

Browse files
committed
logic fix to handle include/exclude conditions with len limit
1 parent 4388fc6 commit e02d7c2

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

fastapi_mcp/server.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ def _filter_tools(self, tools: List[types.Tool], openapi_schema: Dict[str, Any])
319319
Returns:
320320
Filtered list of tools
321321
"""
322-
if (
323-
self._include_operations is None
324-
and self._exclude_operations is None
325-
and self._include_tags is None
326-
and self._exclude_tags is None
327-
and self._max_tool_name_length is None
328-
):
322+
include_exclude_conditions_exist = (
323+
self._include_operations is not None
324+
or self._exclude_operations is not None
325+
or self._include_tags is not None
326+
or self._exclude_tags is not None
327+
)
328+
329+
if not include_exclude_conditions_exist and self._max_tool_name_length is None:
329330
return tools
330331

331332
operations_by_tag: Dict[str, List[str]] = {}
@@ -356,8 +357,6 @@ def _filter_tools(self, tools: List[types.Tool], openapi_schema: Dict[str, Any])
356357
operations_to_include.update(self._include_operations)
357358
elif self._exclude_operations is not None:
358359
operations_to_include.update(all_operations - set(self._exclude_operations))
359-
elif self._max_tool_name_length is not None:
360-
operations_to_include.update(all_operations) # all_operations
361360

362361
if self._include_tags is not None:
363362
for tag in self._include_tags:
@@ -369,6 +368,11 @@ def _filter_tools(self, tools: List[types.Tool], openapi_schema: Dict[str, Any])
369368

370369
operations_to_include.update(all_operations - excluded_operations)
371370

371+
# This condition means that no include/exclude conditions exist,
372+
# and we've reached this point because we have a max-length limit
373+
if not include_exclude_conditions_exist:
374+
operations_to_include = all_operations
375+
372376
if self._max_tool_name_length is not None:
373377
long_operations = {
374378
tool.name for tool in tools if len(self.get_combined_full_name(tool.name)) > self._max_tool_name_length

tests/test_configuration.py

+16
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ async def search_items_long():
414414

415415
max_long_name_mcp = FastApiMCP(app, name="mcp_server", max_tool_name_length=25)
416416
assert len(max_long_name_mcp.tools) == 6
417+
assert {tool.name for tool in max_long_name_mcp.tools} == {
418+
"list_items",
419+
"get_item",
420+
"create_item",
421+
"update_item",
422+
"delete_item",
423+
"search_items",
424+
}
425+
426+
combined_exclude_and_max_tags_mcp = FastApiMCP(app, exclude_tags=["write", "delete"], max_tool_name_length=25)
427+
assert len(combined_exclude_and_max_tags_mcp.tools) == 3
428+
assert {tool.name for tool in combined_exclude_and_max_tags_mcp.tools} == {
429+
"get_item",
430+
"list_items",
431+
"search_items",
432+
}
417433

418434
# Test invalid combinations
419435
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)