Execute MCP tool via the session with timeout protection.
(self, **kwargs)
| 87 | return self._parameters |
| 88 | |
| 89 | async def execute(self, **kwargs) -> ToolResult: |
| 90 | """Execute MCP tool via the session with timeout protection.""" |
| 91 | timeout = self._execute_timeout or _default_timeout_config.execute_timeout |
| 92 | |
| 93 | try: |
| 94 | # Wrap call_tool with timeout |
| 95 | async with asyncio.timeout(timeout): |
| 96 | result = await self._session.call_tool(self._name, arguments=kwargs) |
| 97 | |
| 98 | # MCP tool results are a list of content items |
| 99 | content_parts = [] |
| 100 | for item in result.content: |
| 101 | if hasattr(item, "text"): |
| 102 | content_parts.append(item.text) |
| 103 | else: |
| 104 | content_parts.append(str(item)) |
| 105 | |
| 106 | content_str = "\n".join(content_parts) |
| 107 | |
| 108 | is_error = result.isError if hasattr(result, "isError") else False |
| 109 | |
| 110 | return ToolResult(success=not is_error, content=content_str, error=None if not is_error else "Tool returned error") |
| 111 | |
| 112 | except TimeoutError: |
| 113 | return ToolResult( |
| 114 | success=False, |
| 115 | content="", |
| 116 | error=f"MCP tool execution timed out after {timeout}s. The remote server may be slow or unresponsive.", |
| 117 | ) |
| 118 | except Exception as e: |
| 119 | return ToolResult(success=False, content="", error=f"MCP tool execution failed: {str(e)}") |
| 120 | |
| 121 | |
| 122 | class MCPServerConnection: |
nothing calls this directly
no test coverage detected