|
4 | 4 | from mcp import ClientSession, Tool as MCPTool |
5 | 5 | from mcp.types import CallToolResult, ListToolsResult |
6 | 6 |
|
| 7 | +from agents.exceptions import UserError |
7 | 8 | from agents.mcp.server import _MCPServerWithClientSession |
8 | 9 |
|
9 | 10 |
|
@@ -62,3 +63,88 @@ async def test_list_tools_unlimited_retries(): |
62 | 63 | assert len(tools) == 1 |
63 | 64 | assert tools[0].name == "tool" |
64 | 65 | assert session.list_tools_attempts == 4 |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_call_tool_validates_required_parameters_before_remote_call(): |
| 70 | + session = DummySession() |
| 71 | + server = DummyServer(session=session, retries=0) |
| 72 | + server._tools_list = [ # noqa: SLF001 |
| 73 | + MCPTool( |
| 74 | + name="tool", |
| 75 | + inputSchema={ |
| 76 | + "type": "object", |
| 77 | + "properties": {"param_a": {"type": "string"}}, |
| 78 | + "required": ["param_a"], |
| 79 | + }, |
| 80 | + ) |
| 81 | + ] |
| 82 | + |
| 83 | + with pytest.raises(UserError, match="missing required parameters: param_a"): |
| 84 | + await server.call_tool("tool", {}) |
| 85 | + |
| 86 | + assert session.call_tool_attempts == 0 |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.asyncio |
| 90 | +async def test_call_tool_with_required_parameters_still_calls_remote_tool(): |
| 91 | + session = DummySession() |
| 92 | + server = DummyServer(session=session, retries=0) |
| 93 | + server._tools_list = [ # noqa: SLF001 |
| 94 | + MCPTool( |
| 95 | + name="tool", |
| 96 | + inputSchema={ |
| 97 | + "type": "object", |
| 98 | + "properties": {"param_a": {"type": "string"}}, |
| 99 | + "required": ["param_a"], |
| 100 | + }, |
| 101 | + ) |
| 102 | + ] |
| 103 | + |
| 104 | + result = await server.call_tool("tool", {"param_a": "value"}) |
| 105 | + assert isinstance(result, CallToolResult) |
| 106 | + assert session.call_tool_attempts == 1 |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_call_tool_skips_validation_when_tool_is_missing_from_cache(): |
| 111 | + session = DummySession() |
| 112 | + server = DummyServer(session=session, retries=0) |
| 113 | + server._tools_list = [MCPTool(name="different_tool", inputSchema={"required": ["param_a"]})] # noqa: SLF001 |
| 114 | + |
| 115 | + await server.call_tool("tool", {}) |
| 116 | + assert session.call_tool_attempts == 1 |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_call_tool_skips_validation_when_required_list_is_absent(): |
| 121 | + session = DummySession() |
| 122 | + server = DummyServer(session=session, retries=0) |
| 123 | + server._tools_list = [MCPTool(name="tool", inputSchema={"type": "object"})] # noqa: SLF001 |
| 124 | + |
| 125 | + await server.call_tool("tool", None) |
| 126 | + assert session.call_tool_attempts == 1 |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_call_tool_validates_required_parameters_when_arguments_is_none(): |
| 131 | + session = DummySession() |
| 132 | + server = DummyServer(session=session, retries=0) |
| 133 | + server._tools_list = [MCPTool(name="tool", inputSchema={"required": ["param_a"]})] # noqa: SLF001 |
| 134 | + |
| 135 | + with pytest.raises(UserError, match="missing required parameters: param_a"): |
| 136 | + await server.call_tool("tool", None) |
| 137 | + |
| 138 | + assert session.call_tool_attempts == 0 |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.asyncio |
| 142 | +async def test_call_tool_rejects_non_object_arguments_before_remote_call(): |
| 143 | + session = DummySession() |
| 144 | + server = DummyServer(session=session, retries=0) |
| 145 | + server._tools_list = [MCPTool(name="tool", inputSchema={"required": ["param_a"]})] # noqa: SLF001 |
| 146 | + |
| 147 | + with pytest.raises(UserError, match="arguments must be an object"): |
| 148 | + await server.call_tool("tool", cast(dict[str, object] | None, ["bad"])) |
| 149 | + |
| 150 | + assert session.call_tool_attempts == 0 |
0 commit comments