Skip to content

Commit 5d49487

Browse files
committed
Add tests for enabling, disabling, and filtering tools in ToolManager
1 parent fed850c commit 5d49487

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/mcp/client/session_group.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ async def __aexit__(
154154
for exit_stack in self._session_exit_stacks.values():
155155
tg.start_soon(exit_stack.aclose)
156156

157-
158157
@property
159158
def sessions(self) -> list[mcp.ClientSession]:
160159
"""Returns the list of sessions being managed."""

tests/server/fastmcp/test_tool_manager.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,65 @@ def add(a: int, b: int) -> int:
502502
# Disable an already disabled tool (should not change state)
503503
await tool.disable()
504504
assert tool.enabled is False
505+
506+
@pytest.mark.anyio
507+
async def test_list_tools_filters_disabled(self):
508+
"""Test that list_tools only returns enabled tools."""
509+
510+
def add(a: int, b: int) -> int:
511+
"""Add two numbers."""
512+
return a + b
513+
514+
def subtract(a: int, b: int) -> int:
515+
"""Subtract two numbers."""
516+
return a - b
517+
518+
manager = ToolManager()
519+
tool1 = manager.add_tool(add)
520+
tool2 = manager.add_tool(subtract)
521+
522+
# Both tools should be listed initially
523+
tools = manager.list_tools()
524+
assert len(tools) == 2
525+
assert tool1 in tools
526+
assert tool2 in tools
527+
528+
# Disable one tool
529+
await tool1.disable()
530+
531+
# Only enabled tool should be listed
532+
tools = manager.list_tools()
533+
assert len(tools) == 1
534+
assert tool1 not in tools
535+
assert tool2 in tools
536+
537+
# Re-enable the tool
538+
await tool1.enable()
539+
540+
# Both tools should be listed again
541+
tools = manager.list_tools()
542+
assert len(tools) == 2
543+
assert tool1 in tools
544+
assert tool2 in tools
545+
546+
@pytest.mark.anyio
547+
async def test_call_disabled_tool_raises_error(self):
548+
"""Test that calling a disabled tool raises an error."""
549+
550+
def add(a: int, b: int) -> int:
551+
"""Add two numbers."""
552+
return a + b
553+
554+
manager = ToolManager()
555+
tool = manager.add_tool(add)
556+
557+
# Tool should work normally when enabled
558+
result = await manager.call_tool("add", {"a": 1, "b": 2})
559+
assert result == 3
560+
561+
# Disable the tool
562+
await tool.disable()
563+
564+
# Calling disabled tool should raise error
565+
with pytest.raises(ToolError, match="Tool is disabled: add"):
566+
await manager.call_tool("add", {"a": 1, "b": 2})

0 commit comments

Comments
 (0)