Format tool response for conversation history.
(self, result: Any)
| 1400 | return text |
| 1401 | |
| 1402 | def _format_tool_response(self, result: Any) -> str: |
| 1403 | """Format tool response for conversation history.""" |
| 1404 | if isinstance(result, dict): |
| 1405 | # Check for MCP response structure |
| 1406 | if "content" in result and hasattr(result["content"], "content"): |
| 1407 | tool_result_content = result["content"].content |
| 1408 | if isinstance(tool_result_content, list): |
| 1409 | text_parts = [] |
| 1410 | for block in tool_result_content: |
| 1411 | if ( |
| 1412 | isinstance(block, dict) |
| 1413 | and block.get("type") == ContentBlockType.TEXT.value |
| 1414 | ): |
| 1415 | text_parts.append(block.get("text", "")) |
| 1416 | if text_parts: |
| 1417 | return "\n".join(text_parts) |
| 1418 | |
| 1419 | try: |
| 1420 | return json.dumps(result, indent=2) |
| 1421 | except (TypeError, ValueError): |
| 1422 | return str(result) |
| 1423 | elif isinstance(result, list): |
| 1424 | try: |
| 1425 | return json.dumps(result, indent=2) |
| 1426 | except (TypeError, ValueError): |
| 1427 | return str(result) |
| 1428 | return str(result) |
| 1429 | |
| 1430 | def _truncate_tool_result(self, content: str, max_chars: int) -> str: |
| 1431 | """Truncate tool result if it exceeds max_chars. |