|
2 | 2 |
|
3 | 3 | import re
|
4 | 4 | from pathlib import Path
|
| 5 | +from unittest.mock import AsyncMock, patch |
5 | 6 |
|
6 | 7 | import pytest
|
7 | 8 | from inline_snapshot import snapshot
|
8 | 9 |
|
9 | 10 | from pydantic_ai.agent import Agent
|
10 |
| -from pydantic_ai.exceptions import UserError |
| 11 | +from pydantic_ai.exceptions import ModelRetry, UserError |
11 | 12 | from pydantic_ai.messages import (
|
12 | 13 | BinaryContent,
|
13 | 14 | ModelRequest,
|
|
23 | 24 | from .conftest import IsDatetime, IsStr, try_import
|
24 | 25 |
|
25 | 26 | with try_import() as imports_successful:
|
| 27 | + from mcp import ErrorData, McpError |
| 28 | + |
26 | 29 | from pydantic_ai.mcp import MCPServerSSE, MCPServerStdio
|
27 | 30 | from pydantic_ai.models.google import GoogleModel
|
28 | 31 | from pydantic_ai.models.openai import OpenAIModel
|
@@ -932,3 +935,18 @@ async def test_tool_returning_multiple_items(allow_model_requests: None, agent:
|
932 | 935 | ),
|
933 | 936 | ]
|
934 | 937 | )
|
| 938 | + |
| 939 | + |
| 940 | +async def test_mcp_server_raises_mcp_error(allow_model_requests: None, agent: Agent) -> None: |
| 941 | + server = agent._mcp_servers[0] # pyright: ignore[reportPrivateUsage] |
| 942 | + |
| 943 | + mcp_error = McpError(error=ErrorData(code=400, message='Test MCP error conversion')) |
| 944 | + |
| 945 | + async with agent.run_mcp_servers(): |
| 946 | + with patch.object( |
| 947 | + server._client, # pyright: ignore[reportPrivateUsage] |
| 948 | + 'call_tool', |
| 949 | + new=AsyncMock(side_effect=mcp_error), |
| 950 | + ): |
| 951 | + with pytest.raises(ModelRetry, match='Test MCP error conversion'): |
| 952 | + await server.call_tool('test_tool', {}) |
0 commit comments