|
1 | 1 | import base64 |
| 2 | +import logging |
2 | 3 | from pathlib import Path |
3 | 4 | from types import SimpleNamespace |
4 | 5 | from typing import Any |
@@ -1527,6 +1528,35 @@ def prompt_fn(name: str) -> str: ... # pragma: no branch |
1527 | 1528 | with pytest.raises(MCPError, match="Missing required arguments"): |
1528 | 1529 | await client.get_prompt("prompt_fn") |
1529 | 1530 |
|
| 1531 | + async def test_get_prompt_missing_args_logs_warning_without_traceback( |
| 1532 | + self, caplog: pytest.LogCaptureFixture |
| 1533 | + ) -> None: |
| 1534 | + """Regression for issue #3342: missing-argument ValueErrors are an expected |
| 1535 | + validation failure, so `MCPServer.get_prompt`'s own logger should emit |
| 1536 | + a plain warning without exc_info, instead of a full traceback. |
| 1537 | +
|
| 1538 | + Note: `jsonrpc_dispatcher` has a separate, intentional catch-all that |
| 1539 | + logs a traceback for any handler exception it doesn't recognize as |
| 1540 | + `MCPError`/`ValidationError`. However, that generic safety net is out of |
| 1541 | + scope here and unaffected by this fix PR #3347. |
| 1542 | + """ |
| 1543 | + mcp = MCPServer() |
| 1544 | + |
| 1545 | + @mcp.prompt() |
| 1546 | + def prompt_fn(name: str) -> str: ... # Pragma: no branch. |
| 1547 | + |
| 1548 | + with caplog.at_level(logging.WARNING, logger="mcp.server.mcpserver.server"): |
| 1549 | + async with Client(mcp, mode="legacy") as client: |
| 1550 | + with pytest.raises(MCPError, match="Missing required arguments"): |
| 1551 | + await client.get_prompt("prompt_fn") |
| 1552 | + |
| 1553 | + server_records = [r for r in caplog.records if r.name == "mcp.server.mcpserver.server"] |
| 1554 | + assert len(server_records) == 1 |
| 1555 | + |
| 1556 | + # ValueError should have warning log without exc_info. |
| 1557 | + assert server_records[0].levelno == logging.WARNING |
| 1558 | + assert not server_records[0].exc_info |
| 1559 | + |
1530 | 1560 |
|
1531 | 1561 | async def test_resource_decorator_rfc6570_reserved_expansion(): |
1532 | 1562 | # Regression: old regex-based param extraction couldn't see `path` |
|
0 commit comments