|
9 | 9 | from mcp.types import CallToolResult, ImageContent, TextContent, Tool as MCPTool |
10 | 10 | from pydantic import BaseModel, TypeAdapter |
11 | 11 |
|
| 12 | +import agents._debug as _debug |
12 | 13 | from agents import Agent, FunctionTool, RunContextWrapper, default_tool_error_function |
13 | 14 | from agents.exceptions import AgentsException, MCPToolCancellationError, ModelBehaviorError |
14 | 15 | from agents.mcp import MCPServer, MCPUtil |
@@ -222,6 +223,54 @@ async def test_mcp_invoke_bad_json_errors(caplog: pytest.LogCaptureFixture): |
222 | 223 | assert "Invalid JSON input for tool test_tool_1" in caplog.text |
223 | 224 |
|
224 | 225 |
|
| 226 | +@pytest.mark.asyncio |
| 227 | +async def test_mcp_invoke_bad_json_redacts_payload_when_dont_log_tool_data( |
| 228 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 229 | +): |
| 230 | + caplog.set_level(logging.DEBUG) |
| 231 | + monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", True) |
| 232 | + |
| 233 | + server = FakeMCPServer() |
| 234 | + server.add_tool("test_tool_1", {}) |
| 235 | + |
| 236 | + ctx = RunContextWrapper(context=None) |
| 237 | + tool = MCPTool(name="test_tool_1", inputSchema={}) |
| 238 | + bad_json = '{"secret":"SECRET_TOKEN_123"' |
| 239 | + |
| 240 | + with pytest.raises(ModelBehaviorError) as exc_info: |
| 241 | + await MCPUtil.invoke_mcp_tool(server, tool, ctx, bad_json) |
| 242 | + |
| 243 | + assert str(exc_info.value) == "Invalid JSON input for tool test_tool_1" |
| 244 | + assert exc_info.value.__cause__ is None |
| 245 | + assert exc_info.value.__context__ is None |
| 246 | + assert "SECRET_TOKEN_123" not in str(exc_info.value) |
| 247 | + assert "SECRET_TOKEN_123" not in caplog.text |
| 248 | + |
| 249 | + |
| 250 | +@pytest.mark.asyncio |
| 251 | +async def test_mcp_invoke_bad_json_includes_payload_when_tool_logging_enabled( |
| 252 | + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 253 | +): |
| 254 | + caplog.set_level(logging.DEBUG) |
| 255 | + monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False) |
| 256 | + |
| 257 | + server = FakeMCPServer() |
| 258 | + server.add_tool("test_tool_1", {}) |
| 259 | + |
| 260 | + ctx = RunContextWrapper(context=None) |
| 261 | + tool = MCPTool(name="test_tool_1", inputSchema={}) |
| 262 | + bad_json = '{"secret":"SECRET_TOKEN_123"' |
| 263 | + |
| 264 | + with pytest.raises(ModelBehaviorError) as exc_info: |
| 265 | + await MCPUtil.invoke_mcp_tool(server, tool, ctx, bad_json) |
| 266 | + |
| 267 | + assert str(exc_info.value) == f"Invalid JSON input for tool test_tool_1: {bad_json}" |
| 268 | + assert isinstance(exc_info.value.__cause__, json.JSONDecodeError) |
| 269 | + assert exc_info.value.__cause__.doc == bad_json |
| 270 | + assert "SECRET_TOKEN_123" in str(exc_info.value) |
| 271 | + assert "SECRET_TOKEN_123" in caplog.text |
| 272 | + |
| 273 | + |
225 | 274 | class CrashingFakeMCPServer(FakeMCPServer): |
226 | 275 | async def call_tool( |
227 | 276 | self, |
|
0 commit comments