|
| 1 | +import logging |
1 | 2 | import os |
| 3 | +from typing import List, Optional |
2 | 4 |
|
| 5 | +from google.adk.agents.readonly_context import ReadonlyContext |
| 6 | +from google.adk.tools.base_tool import BaseTool |
| 7 | +from google.adk.tools.base_toolset import BaseToolset |
3 | 8 | from google.adk.tools.mcp_tool import McpToolset |
4 | 9 | from google.adk.tools.mcp_tool.mcp_session_manager import ( |
5 | 10 | StdioConnectionParams, |
6 | 11 | StdioServerParameters, |
7 | 12 | StreamableHTTPConnectionParams, |
8 | 13 | ) |
9 | 14 |
|
10 | | -all_mcps = [] |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class ResilientMcpToolset(BaseToolset): |
| 19 | + """Wraps an McpToolset so that connection failures log a warning |
| 20 | + instead of crashing the agent run.""" |
| 21 | + |
| 22 | + def __init__(self, inner: McpToolset, label: str = "MCP"): |
| 23 | + super().__init__( |
| 24 | + tool_filter=inner.tool_filter, |
| 25 | + tool_name_prefix=inner.tool_name_prefix, |
| 26 | + ) |
| 27 | + self._inner = inner |
| 28 | + self._label = label |
| 29 | + |
| 30 | + async def get_tools( |
| 31 | + self, readonly_context: Optional[ReadonlyContext] = None |
| 32 | + ) -> List[BaseTool]: |
| 33 | + try: |
| 34 | + return await self._inner.get_tools(readonly_context) |
| 35 | + except Exception as exc: |
| 36 | + logger.warning("%s unavailable, skipping its tools: %s", self._label, exc) |
| 37 | + return [] |
| 38 | + |
| 39 | + async def close(self) -> None: |
| 40 | + try: |
| 41 | + await self._inner.close() |
| 42 | + except Exception: |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +all_mcps: list[BaseToolset] = [] |
11 | 47 |
|
12 | 48 | if os.getenv("PARALLEL_API_KEY"): |
13 | | - parallel_search_mcp = McpToolset( |
14 | | - connection_params=StreamableHTTPConnectionParams( |
15 | | - url="https://search-mcp.parallel.ai/mcp", |
16 | | - headers={"Authorization": f"Bearer {os.getenv('PARALLEL_API_KEY')}"}, |
17 | | - timeout=600, |
| 49 | + parallel_search_mcp = ResilientMcpToolset( |
| 50 | + McpToolset( |
| 51 | + connection_params=StreamableHTTPConnectionParams( |
| 52 | + url="https://search-mcp.parallel.ai/mcp", |
| 53 | + headers={"Authorization": f"Bearer {os.getenv('PARALLEL_API_KEY')}"}, |
| 54 | + timeout=600, |
| 55 | + ), |
18 | 56 | ), |
| 57 | + label="Parallel Search MCP", |
19 | 58 | ) |
20 | 59 | all_mcps.append(parallel_search_mcp) |
21 | 60 |
|
22 | | -docling_mcp = McpToolset( |
23 | | - connection_params=StdioConnectionParams( |
24 | | - server_params=StdioServerParameters( |
25 | | - command="uvx", |
26 | | - args=["--from=docling-mcp", "docling-mcp-server"], |
| 61 | +docling_mcp = ResilientMcpToolset( |
| 62 | + McpToolset( |
| 63 | + connection_params=StdioConnectionParams( |
| 64 | + server_params=StdioServerParameters( |
| 65 | + command="uvx", |
| 66 | + args=["--from=docling-mcp", "docling-mcp-server"], |
| 67 | + ), |
| 68 | + timeout=120.0, |
27 | 69 | ), |
28 | | - timeout=120.0, |
29 | 70 | ), |
| 71 | + label="Docling MCP", |
30 | 72 | ) |
31 | 73 | all_mcps.append(docling_mcp) |
0 commit comments