|
2 | 2 |
|
3 | 3 | from dataclasses import dataclass
|
4 | 4 | from enum import Enum
|
5 |
| -from typing import Any, Dict, Optional |
| 5 | +from typing import Any, Dict, Optional, Union |
6 | 6 | from mcp_agent.workflows.llm.llm_constants import FINAL_RESPONSE_LOG_MESSAGE
|
7 | 7 |
|
8 | 8 |
|
@@ -51,18 +51,30 @@ def convert_log_event(event: Dict[str, Any]) -> Optional[ProgressEvent]:
|
51 | 51 | """Convert a log event to a progress event if applicable."""
|
52 | 52 | namespace = event.get("namespace", "")
|
53 | 53 | message = event.get("message", "")
|
| 54 | + |
54 | 55 | # Extract agent name from namespace if present
|
55 | 56 | agent_name = None
|
56 |
| - parts = namespace.split(".") |
57 |
| - if len(parts) > 3: |
58 |
| - if parts[0:3] == ["mcp_agent", "agents", "agent"]: |
59 |
| - agent_name = parts[3] |
60 |
| - elif ( |
61 |
| - len(parts) > 4 |
62 |
| - and parts[0:3] == ["mcp_agent", "workflows", "llm"] |
63 |
| - and parts[3].startswith("augmented_llm") |
64 |
| - ): |
65 |
| - agent_name = parts[4] |
| 57 | + |
| 58 | + # Known class namespaces that may have agent names |
| 59 | + CLASS_NAMESPACES = [ |
| 60 | + "mcp_agent.agents.agent", # TODO: Use Agent.__module__ |
| 61 | + ("mcp_agent.workflows.llm", "augmented_llm"), # Matches augmented_llm_* classes |
| 62 | + "mcp_agent.mcp.mcp_aggregator", # TODO: Use Finder.__module__ |
| 63 | + ] |
| 64 | + |
| 65 | + # Check if namespace starts with any of our class prefixes and has an additional part |
| 66 | + for class_ns in CLASS_NAMESPACES: |
| 67 | + if isinstance(class_ns, tuple): |
| 68 | + # Special case for augmented_llm_* classes |
| 69 | + base_ns, class_prefix = class_ns |
| 70 | + parts = namespace[len(base_ns) + 1 :].split(".") # +1 for the dot |
| 71 | + if len(parts) >= 2 and parts[0].startswith(class_prefix): |
| 72 | + agent_name = parts[1] |
| 73 | + break |
| 74 | + elif namespace.startswith(class_ns + "."): |
| 75 | + # Regular case - agent name is after the class namespace |
| 76 | + agent_name = namespace.split(".")[-1] |
| 77 | + break |
66 | 78 |
|
67 | 79 | # Handle MCP connection events
|
68 | 80 | if "mcp_connection_manager" in namespace:
|
@@ -90,9 +102,10 @@ def convert_log_event(event: Dict[str, Any]) -> Optional[ProgressEvent]:
|
90 | 102 | # Handle MCPServerAggregator tool calls
|
91 | 103 | if "mcp_aggregator" in namespace:
|
92 | 104 | tool_name = extract_from_aggregator(message)
|
| 105 | + target = f"{agent_name} ({tool_name})" if agent_name else tool_name |
93 | 106 | if tool_name:
|
94 | 107 | return ProgressEvent(
|
95 |
| - ProgressAction.CALLING_TOOL, tool_name, agent_name=agent_name |
| 108 | + ProgressAction.CALLING_TOOL, target, agent_name=agent_name |
96 | 109 | )
|
97 | 110 |
|
98 | 111 | # Handle LLM events
|
|
0 commit comments