|
1 | 1 | """Shared fixtures for integration tests.""" |
2 | 2 |
|
3 | 3 | import json |
| 4 | +from typing import Any |
| 5 | + |
4 | 6 | import pytest |
5 | 7 | from fastmcp import Client |
6 | 8 |
|
7 | 9 | _server_initialized = False |
8 | 10 |
|
9 | 11 |
|
| 12 | +def _extract_text(result: Any) -> str: |
| 13 | + """Extract text from MCP CallToolResult, handling content type union.""" |
| 14 | + if result.content: |
| 15 | + item = result.content[0] |
| 16 | + if hasattr(item, "text"): |
| 17 | + return item.text # type: ignore[no-any-return] |
| 18 | + return "[]" |
| 19 | + |
| 20 | + |
10 | 21 | @pytest.fixture(scope="session") |
11 | 22 | def mcp_server(): |
12 | 23 | """Build the full MCP server with all providers and transforms wired up. |
@@ -37,31 +48,30 @@ def mcp_server(): |
37 | 48 | return mcp |
38 | 49 |
|
39 | 50 |
|
40 | | -async def call_tool(mcp_server, tool_name: str, arguments: dict | None = None) -> dict: |
| 51 | +async def call_tool(mcp_server: Any, tool_name: str, arguments: dict | None = None) -> dict: |
41 | 52 | """Call a tool through the MCP Client layer and return parsed JSON.""" |
42 | 53 | async with Client(mcp_server) as client: |
43 | 54 | result = await client.call_tool('call_tool', { |
44 | 55 | 'name': tool_name, |
45 | 56 | 'arguments': arguments or {}, |
46 | 57 | }) |
47 | | - return json.loads(result.content[0].text) |
| 58 | + return json.loads(_extract_text(result)) # type: ignore[no-any-return] |
48 | 59 |
|
49 | 60 |
|
50 | | -async def discover(mcp_server, query: str) -> list[dict]: |
| 61 | +async def discover(mcp_server: Any, query: str) -> list[dict]: |
51 | 62 | """Search for tools via BM25 discover_tools and return parsed results.""" |
52 | 63 | async with Client(mcp_server) as client: |
53 | 64 | result = await client.call_tool('discover_tools', {'query': query}) |
54 | | - # discover_tools may return results in different formats |
55 | | - text = result.content[0].text if result.content else "[]" |
| 65 | + text = _extract_text(result) |
56 | 66 | try: |
57 | 67 | parsed = json.loads(text) |
58 | 68 | return parsed if isinstance(parsed, list) else [] |
59 | 69 | except json.JSONDecodeError: |
60 | 70 | return [] |
61 | 71 |
|
62 | 72 |
|
63 | | -async def call_direct_tool(mcp_server, tool_name: str, arguments: dict | None = None) -> dict: |
| 73 | +async def call_direct_tool(mcp_server: Any, tool_name: str, arguments: dict | None = None) -> dict: |
64 | 74 | """Call an always-visible tool directly (not through call_tool proxy).""" |
65 | 75 | async with Client(mcp_server) as client: |
66 | 76 | result = await client.call_tool(tool_name, arguments or {}) |
67 | | - return json.loads(result.content[0].text) |
| 77 | + return json.loads(_extract_text(result)) # type: ignore[no-any-return] |
0 commit comments