Agent Zero AI framework
mcp wip
frdel64a7e0f1202a
diff --git a/docker/base/fs/ins/install_base_packages.sh b/docker/base/fs/ins/install_base_packages.shindex b6071b2e9..cedd2183c 100644--- a/docker/base/fs/ins/install_base_packages.sh+++ b/docker/base/fs/ins/install_base_packages.sh@@ -3,11 +3,15 @@ set -eecho "====================BASE PACKAGES START===================="+apt-get update && apt-get upgrade -y+apt-get install -y --no-install-recommends \nodejs npm openssh-server sudo curl wget git ffmpeg supervisor cronecho "====================BASE PACKAGES NPM===================="-npm i -g npx shx+# we shall not install npx separately, it's discontinued and some versions are broken+# npm i -g npx+npm i -g shxecho "====================BASE PACKAGES END===================="diff --git a/docker/base/fs/ins/install_python.sh b/docker/base/fs/ins/install_python.shindex 2cb8d4c33..3c8379a3a 100644--- a/docker/base/fs/ins/install_python.sh+++ b/docker/base/fs/ins/install_python.sh@@ -49,4 +49,8 @@ pip install --upgrade pip ipython requestspip install torch==2.7.0+cpu torchvision==0.22.0+cpu --index-url https://download.pytorch.org/whl/cpu+echo "====================PYTHON UV ===================="++curl -Ls https://astral.sh/uv/install.sh | sh+echo "====================PYTHON END===================="diff --git a/initialize.py b/initialize.pyindex 9c85acf68..501cf8605 100644--- a/initialize.py+++ b/initialize.py@@ -82,9 +82,10 @@ def initialize():args_override(config)# initialize MCP in deferred task to prevent blocking the main thread- async def initialize_mcp_async(mcp_servers_config: str):- return initialize_mcp(mcp_servers_config)- defer.DeferredTask(thread_name="mcp-initializer").start_task(initialize_mcp_async, config.mcp_servers)+ # async def initialize_mcp_async(mcp_servers_config: str):+ # return initialize_mcp(mcp_servers_config)+ # defer.DeferredTask(thread_name="mcp-initializer").start_task(initialize_mcp_async, config.mcp_servers)+ initialize_mcp(config.mcp_servers)# import python.helpers.mcp_handler as mcp_helper# import agent as agent_helperdiff --git a/python/api/mcp_server_get_log.py b/python/api/mcp_server_get_log.pynew file mode 100644index 000000000..eadf25bb8--- /dev/null+++ b/python/api/mcp_server_get_log.py@@ -0,0 +1,19 @@+from python.helpers.api import ApiHandler+from flask import Request, Response++from typing import Any++from python.helpers.mcp_handler import MCPConfig+++class McpServerGetLog(ApiHandler):+ async def process(self, input: dict[Any, Any], request: Request) -> dict[Any, Any] | Response:++ # try:+ server_name = input.get("server_name")+ if not server_name:+ return {"success": False, "error": "Missing server_name"}+ log = MCPConfig.get_instance().get_server_log(server_name)+ return {"success": True, "log": log}+ # except Exception as e:+ # return {"success": False, "error": str(e)}diff --git a/python/helpers/errors.py b/python/helpers/errors.pyindex 7a9132603..4d6c20e04 100644--- a/python/helpers/errors.py+++ b/python/helpers/errors.py@@ -18,35 +18,46 @@ def format_error(e: Exception, start_entries=6, end_entries=4):# Split the traceback into lineslines = traceback_text.split("\n")- # Find all "File" lines- file_indices = [- i for i, line in enumerate(lines) if line.strip().startswith("File ")- ]-- # If we found at least one "File" line, trim the middle if there are more than start_entries+end_entries lines- if len(file_indices) > start_entries + end_entries:- start_index = max(0, len(file_indices) - start_entries - end_entries)- trimmed_lines = (- lines[: file_indices[start_index]]- + [- f"\n>>> {len(file_indices) - start_entries - end_entries} stack lines skipped <<<\n"- ]- + lines[file_indices[start_index + end_entries] :]- )+ if not start_entries and not end_entries:+ trimmed_lines = []else:- # If no "File" lines found, or not enough to trim, just return the original traceback- trimmed_lines = lines++ # Find all "File" lines+ file_indices = [+ i for i, line in enumerate(lines) if line.strip().startswith("File ")+ ]++ # If we found at least one "File" line, trim the middle if there are more than start_entries+end_entries lines+ if len(file_indices) > start_entries + end_entries:+ start_index = max(0, len(file_indices) - start_entries - end_entries)+ trimmed_lines = (+ lines[: file_indices[start_index]]+ + [+ f"\n>>> {len(file_indices) - start_entries - end_entries} stack lines skipped <<<\n"+ ]+ + lines[file_indices[start_index + end_entries] :]+ )+ else:+ # If no "File" lines found, or not enough to trim, just return the original traceback+ trimmed_lines = lines# Find the error message at the enderror_message = ""- for line in reversed(trimmed_lines):+ for line in reversed(lines):if re.match(r"\w+Error:", line):error_message = linebreak# Combine the trimmed traceback with the error message- result = "Traceback (most recent call last):\n" + "\n".join(trimmed_lines)- if error_message:- result += f"\n\n{error_message}"+ if not trimmed_lines:+ result = error_message+ else:+ result = "Traceback (most recent call last):\n" + "\n".join(trimmed_lines)+ if error_message:+ result += f"\n\n{error_message}"++ # at least something+ if not result:+ result = str(e)return resultdiff --git a/python/helpers/mcp_handler.py b/python/helpers/mcp_handler.pyindex d96b62f7f..68c3de190 100644--- a/python/helpers/mcp_handler.py+++ b/python/helpers/mcp_handler.py@@ -1,19 +1,36 @@from abc import ABC, abstractmethodimport re-from typing import List, Dict, Optional, Any, Union, Literal, Annotated, ClassVar, cast, Callable, Awaitable, TypeVar+from typing import (+ List,+ Dict,+ Optional,+ Any,+ Union,+ Literal,+ Annotated,+ ClassVar,+ cast,+ Callable,+ Awaitable,+ TypeVar,+)import threadingimport asynciofrom contextlib import AsyncExitStackfrom shutil import whichfrom datetime import timedeltaimport json+from python.helpers import errors+from python.helpers import settingsimport os+# print(f"DEBUG: Listing /opt/venv/lib/python3.11/site-packages/ before mcp import: {os.listdir('/opt/venv/lib/python3.11/site-packages/')}") # This line caused FileNotFoundError, **FOR CUDA CHANGE TO '3.12'**from mcp import ClientSession, StdioServerParametersfrom mcp.client.stdio import stdio_clientfrom mcp.client.sse import sse_client+from mcp.shared.message import SessionMessagefrom mcp.types import CallToolResult, ListToolsResult, JSONRPCMessagefrom anyio.streams.memory import (MemoryObjectReceiveStream,@@ -26,39 +43,52 @@ from python.helpers.dirty_json import DirtyJsonfrom python.helpers.print_style import PrintStylefrom python.helpers.tool import Tool, Response+def normalize_name(name: str) -> str:# Lowercase and strip whitespacename = name.strip().lower()# Replace all non-alphanumeric (unicode) chars with underscore# \W matches non-alphanumeric, but also matches underscore, so use [^\w] with re.UNICODE# To also replace underscores from non-latin chars, use [^a-zA-Z0-9] with re.UNICODE- name = re.sub(r'[^\w]', '_', name, flags=re.UNICODE)+ name = re.sub(r"[^\w]", "_", name, flags=re.UNICODE)return name-def initialize_mcp(mcp_servers_config:str):++def initialize_mcp(mcp_servers_config: str):if not MCPConfig.get_instance().is_initialized():try:MCPConfig.update(mcp_servers_config)except Exception as e:from agent import AgentContext- first_context = AgentContext.first() # TODO replace with better reporting++ first_context = AgentContext.first() # TODO replace with better reportingif first_context:(- first_context.log- .log(type="warning", content=f"Failed to update MCP settings: {e}", temp=False)+ first_context.log.log(+ type="warning",+ content=f"Failed to update MCP settings: {e}",+ temp=False,+ ))(- PrintStyle(background_color="black", font_color="red", padding=True)- .print(f"Failed to update MCP settings: {e}")+ PrintStyle(+ background_color="black", font_color="red", padding=True+ ).print(f"Failed to update MCP settings: {e}"))+class MCPTool(Tool):"""MCP Tool wrapper"""+async def execute(self, **kwargs: Any):error = ""try:- response: CallToolResult = await MCPConfig.get_instance().call_tool(self.name, kwargs)- message = "\n\n".join([item.text for item in response.content if item.type == "text"])+ response: CallToolResult = await MCPConfig.get_instance().call_tool(+ self.name, kwargs+ )+ message = "\n\n".join(+ [item.text for item in response.content if item.type == "text"]+ )if response.isError:error = messageexcept Exception as e:@@ -69,7 +99,9 @@ class MCPTool(Tool):PrintStyle(background_color="#CC34C3", font_color="white", bold=True, padding=True).print(f"MCPTool::Failed to call mcp tool {self.name}:")- PrintStyle(background_color="#AA4455", font_color="white", padding=False).print(error)+ PrintStyle(+ background_color="#AA4455", font_color="white", padding=False+ ).print(error)self.agent.context.log.log(type="warning",@@ -80,43 +112,60 @@ class MCPTool(Tool):async def before_execution(self, **kwargs: Any):(- PrintStyle(font_color="#1B4F72", padding=True, background_color="white", bold=True)- .print(f"{self.agent.agent_name}: Using tool '{self.name}'")+ PrintStyle(+ font_color="#1B4F72", padding=True, background_color="white", bold=True+ ).print(f"{self.agent.agent_name}: Using tool '{self.name}'"))self.log = self.get_log_object()for key, value in self.args.items():- PrintStyle(font_color="#85C1E9", bold=True).stream(self.nice_key(key) + ": ")- PrintStyle(font_color="#85C1E9", padding=isinstance(value, str) and "\n" in value).stream(value)+ PrintStyle(font_color="#85C1E9", bold=True).stream(+ self.nice_key(key) + ": "+ )+ PrintStyle(+ font_color="#85C1E9", padding=isinstance(value, str) and "\n" in value+ ).stream(value)PrintStyle().print()async def after_execution(self, response: Response, **kwargs: Any):raw_tool_response = response.message.strip() if response.message else ""if not raw_tool_response:- PrintStyle(font_color="red").print(f"Warning: Tool '{self.name}' returned an empty message.")+ PrintStyle(font_color="red").print(+ f"Warning: Tool '{self.name}' returned an empty message."+ )# Even if empty, we might still want to provide context for the agentraw_tool_response = "[Tool returned no textual content]"# Prepare user message context- user_message_text = "No specific user message context available for this exact step."- if self.agent and self.agent.last_user_message and self.agent.last_user_message.content:+ user_message_text = (+ "No specific user message context available for this exact step."+ )+ if (+ self.agent+ and self.agent.last_user_message+ and self.agent.last_user_message.content+ ):content = self.agent.last_user_message.contentif isinstance(content, dict):# Attempt to get a 'message' field, otherwise stringify the dict- user_message_text = content.get("message", json.dumps(content, indent=2))+ user_message_text = content.get(+ "message", json.dumps(content, indent=2)+ )elif isinstance(content, str):user_message_text = contentelse:# Fallback for any other types (e.g. list, if that were possible for content)- user_message_text = str(content)-+ user_message_text = str(content)+# Ensure user_message_text is a string before length check and slicing- user_message_text = str(user_message_text)+ user_message_text = str(user_message_text)# Truncate user message context if it's too long to avoid overwhelming the prompt- max_user_context_len = 500 # characters+ max_user_context_len = 500 # charactersif len(user_message_text) > max_user_context_len:- user_message_text = user_message_text[:max_user_context_len] + "... (truncated)"+ user_message_text = (+ user_message_text[:max_user_context_len] + "... (truncated)"+ )contextual_block = f"""\n--- End of Results for MCP Tool: {self.name} ---@@ -139,13 +188,22 @@ If this action is part of an ongoing sequence, consider the next step with thisself.agent.hist_add_tool_result(self.name, final_text_for_agent)(- PrintStyle(font_color="#1B4F72", background_color="white", padding=True, bold=True)- .print(f"{self.agent.agent_name}: Response from tool '{self.name}' (plus context added)")+ PrintStyle(+ font_color="#1B4F72", background_color="white", padding=True, bold=True+ ).print(+ f"{self.agent.agent_name}: Response from tool '{self.name}' (plus context added)"+ ))# Print only the raw response to console for brevity, agent gets the full context.- PrintStyle(font_color="#85C1E9").print(raw_tool_response if raw_tool_response else "[No direct textual output from tool]")+ PrintStyle(font_color="#85C1E9").print(+ raw_tool_response+ if raw_tool_response+ else "[No direct textual output from tool]"+ )if self.log:- self.log.update(content=final_text_for_agent) # Log includes the full context+ self.log.update(+ content=final_text_for_agent+ ) # Log includes the full contextclass MCPServerRemote(BaseModel):@@ -165,6 +223,14 @@ class MCPServerRemote(BaseModel):self.__client = MCPClientRemote(self)self.update(config)+ def get_error(self) -> str:+ with self.__lock:+ return self.__client.error # type: ignore++ def get_log(self) -> str:+ with self.__lock:+ return self.__client.get_log() # type: ignore+def get_tools(self) -> List[dict[str, Any]]:"""Get all tools from the server"""with self.__lock:@@ -175,7 +241,9 @@ class MCPServerRemote(BaseModel):with self.__lock:return self.__client.has_tool(tool_name) # type: ignore- async def call_tool(self, tool_name: str, input_data: Dict[str, Any]) -> CallToolResult:+ async def call_tool(+ self, tool_name: str, input_data: Dict[str, Any]+ ) -> CallToolResult:"""Call a tool with the given input data"""with self.__lock:# We already run in an event loop, dont believe Pylance@@ -184,7 +252,15 @@ class MCPServerRemote(BaseModel):def update(self, config: dict[str, Any]) -> "MCPServerRemote":with self.__lock:for key, value in config.items():- if key in ["name", "description", "url", "headers", "timeout", "sse_read_timeout", "disabled"]:+ if key in [+ "name",+ "description",+ "url",+ "headers",+ "timeout",+ "sse_read_timeout",+ "disabled",+ ]:if key == "name":value = normalize_name(value)setattr(self, key, value)@@ -203,7 +279,9 @@ class MCPServerLocal(BaseModel):args: list[str] = Field(default_factory=list)env: dict[str, str] | None = Field(default_factory=dict[str, str])encoding: str = Field(default="utf-8")- encoding_error_handler: Literal["strict", "ignore", "replace"] = Field(default="strict")+ encoding_error_handler: Literal["strict", "ignore", "replace"] = Field(+ default="strict"+ )disabled: bool = Field(default=False)__lock: ClassVar[threading.Lock] = PrivateAttr(default=threading.Lock())@@ -214,6 +292,14 @@ class MCPServerLocal(BaseModel):self.__client = MCPClientLocal(self)self.update(config)+ def get_error(self) -> str:+ with self.__lock:+ return self.__client.error # type: ignore++ def get_log(self) -> str:+ with self.__lock:+ return self.__client.get_log() # type: ignore+def get_tools(self) -> List[dict[str, Any]]:"""Get all tools from the server"""with self.__lock:@@ -224,7 +310,9 @@ class MCPServerLocal(BaseModel):with self.__lock:return self.__client.has_tool(tool_name) # type: ignore- async def call_tool(self, tool_name: str, input_data: Dict[str, Any]) -> CallToolResult:+ async def call_tool(+ self, tool_name: str, input_data: Dict[str, Any]+ ) -> CallToolResult:"""Call a tool with the given input data"""with self.__lock:# We already run in an event loop, dont believe Pylance@@ -233,7 +321,16 @@ class MCPServerLocal(BaseModel):def update(self, config: dict[str, Any]) -> "MCPServerLocal":with self.__lock:for key, value in config.items():- if key in ["name", "description", "command", "args", "env", "encoding", "encoding_error_handler", "disabled"]:+ if key in [+ "name",+ "description",+ "command",+ "args",+ "env",+ "encoding",+ "encoding_error_handler",+ "disabled",+ ]:if key == "name":value = normalize_name(value)setattr(self, key, value)@@ -247,10 +344,10 @@ class MCPServerLocal(BaseModel):MCPServer = Annotated[Union[- Annotated[MCPServerRemote, Tag('MCPServerRemote')],- Annotated[MCPServerLocal, Tag('MCPServerLocal')]+ Annotated[MCPServerRemote, Tag("MCPServerRemote")],+ Annotated[MCPServerLocal, Tag("MCPServerLocal")],],- Discriminator(lambda v: "MCPServerRemote" if "url" in v else "MCPServerLocal")+ Discriminator(lambda v: "MCPServerRemote" if "url" in v else "MCPServerLocal"),]@@ -272,30 +369,42 @@ class MCPConfig(BaseModel):with cls.__lock:servers_data: List[Dict[str, Any]] = [] # Default to empty list- if config_str and config_str.strip(): # Only parse if non-empty and not just whitespace+ if (+ config_str and config_str.strip()+ ): # Only parse if non-empty and not just whitespacetry:# Try with standard json.loads first, as it should handle escaped strings correctlyparsed_value = dirty_json.try_parse(config_str)normalized = cls.normalize_config(parsed_value)-+if isinstance(normalized, list):valid_servers = []for item in normalized:if isinstance(item, dict):valid_servers.append(item)else:- PrintStyle(background_color="yellow", font_color="black", padding=True).print(+ PrintStyle(+ background_color="yellow",+ font_color="black",+ padding=True,+ ).print(f"Warning: MCP config item (from json.loads) was not a dictionary and was ignored: {item}")servers_data = valid_serverselse:- PrintStyle(background_color="red", font_color="white", padding=True).print(+ PrintStyle(+ background_color="red", font_color="white", padding=True+ ).print(f"Error: Parsed MCP config (from json.loads) top-level structure is not a list. Config string was: '{config_str}'")# servers_data remains empty- except Exception as e_json: # Catch json.JSONDecodeError specifically if possible, or general Exception- PrintStyle.error(f"Error parsing MCP config string: {e_json}. Config string was: '{config_str}'")-+ except (+ Exception+ ) as e_json: # Catch json.JSONDecodeError specifically if possible, or general Exception+ PrintStyle.error(+ f"Error parsing MCP config string: {e_json}. Config string was: '{config_str}'"+ )+# # Fallback to DirtyJson or log error if standard json.loads fails# PrintStyle(background_color="orange", font_color="black", padding=True).print(# f"Standard json.loads failed for MCP config: {e_json}. Attempting DirtyJson as fallback."@@ -322,13 +431,15 @@ class MCPConfig(BaseModel):# f"Error parsing MCP config string with DirtyJson as well: {e_dirty}. Config string was: '{config_str}'"# )# # servers_data remains empty, allowing graceful degradation-+# Initialize/update the singleton instance with the (potentially empty) list of server datainstance = cls.get_instance()# Directly update the servers attribute of the existing instance or re-initialize carefully# For simplicity and to ensure __init__ logic runs if needed for setup:- new_instance_data = {'servers': servers_data} # Prepare data for re-initialization or update-+ new_instance_data = {+ "servers": servers_data+ } # Prepare data for re-initialization or update+# Option 1: Re-initialize the existing instance (if __init__ is idempotent for other fields)instance.__init__(servers_list=servers_data)@@ -345,7 +456,7 @@ class MCPConfig(BaseModel):# PrintStyle(background_color="grey", font_color="red", padding=True).print(# f"MCPConfig.update: Failed to create MCPServer from item '{server_item_data.get('name', 'Unknown')}': {e_init}"# )-+cls.__initialized = Truereturn instance@@ -368,21 +479,23 @@ class MCPConfig(BaseModel):if isinstance(server, dict):normalized.append(server)else:- normalized.append(servers) # single server?+ normalized.append(servers) # single server?return normalized-def __init__(self, servers_list: List[Dict[str, Any]]):from collections.abc import Mapping, Iterable# DEBUG: Print the received servers_list- if servers_list: PrintStyle(background_color="blue", font_color="white", padding=True).print(f"MCPConfig.__init__ received servers_list: {servers_list}")+ if servers_list:+ PrintStyle(background_color="blue", font_color="white", padding=True).print(+ f"MCPConfig.__init__ received servers_list: {servers_list}"+ )# This empties the servers list if MCPConfig is a Pydantic model and servers is a field.- # If servers is a field like `servers: List[MCPServer] = Field(default_factory=list)`,+ # If servers is a field like `servers: List[MCPServer] = Field(default_factory=list)`,# then super().__init__() might try to initialize it.# We are re-assigning self.servers later in this __init__.- super().__init__()+ super().__init__()# Clear any servers potentially initialized by super().__init__() before we populate based on servers_listself.servers = []@@ -391,8 +504,9 @@ class MCPConfig(BaseModel):if not isinstance(servers_list, Iterable):(- PrintStyle(background_color="grey", font_color="red", padding=True)- .print("MCPConfig::__init__::servers_list must be a list")+ PrintStyle(+ background_color="grey", font_color="red", padding=True+ ).print("MCPConfig::__init__::servers_list must be a list"))return@@ -401,15 +515,22 @@ class MCPConfig(BaseModel):# log the errorerror_msg = "server_item must be a mapping"(- PrintStyle(background_color="grey", font_color="red", padding=True)- .print(f"MCPConfig::__init__::{error_msg}")+ PrintStyle(+ background_color="grey", font_color="red", padding=True+ ).print(f"MCPConfig::__init__::{error_msg}"))# add to failed servers with generic name- self.disconnected_servers.append({- "config": server_item if isinstance(server_item, dict) else {"raw": str(server_item)},- "error": error_msg,- "name": "invalid_server_config"- })+ self.disconnected_servers.append(+ {+ "config": (+ server_item+ if isinstance(server_item, dict)+ else {"raw": str(server_item)}+ ),+ "error": error_msg,+ "name": "invalid_server_config",+ }+ )continueif server_item.get("disabled", False):@@ -418,13 +539,15 @@ class MCPConfig(BaseModel):# normalize server name if it existsif server_name != "unnamed_server":server_name = normalize_name(server_name)-+# add to failed servers- self.disconnected_servers.append({- "config": server_item,- "error": "Disabled in config",- "name": server_name- })+ self.disconnected_servers.append(+ {+ "config": server_item,+ "error": "Disabled in config",+ "name": server_name,+ }+ )continueserver_name = server_item.get("name", "__not__found__")@@ -432,15 +555,18 @@ class MCPConfig(BaseModel):# log the errorerror_msg = "server_name is required"(- PrintStyle(background_color="grey", font_color="red", padding=True)- .print(f"MCPConfig::__init__::{error_msg}")+ PrintStyle(+ background_color="grey", font_color="red", padding=True+ ).print(f"MCPConfig::__init__::{error_msg}"))# add to failed servers- self.disconnected_servers.append({- "config": server_item,- "error": error_msg,- "name": "unnamed_server"- })+ self.disconnected_servers.append(+ {+ "config": server_item,+ "error": error_msg,+ "name": "unnamed_server",+ }+ )continuetry:@@ -453,15 +579,23 @@ class MCPConfig(BaseModel):# log the errorerror_msg = str(e)(- PrintStyle(background_color="grey", font_color="red", padding=True)- .print(f"MCPConfig::__init__: Failed to create MCPServer '{server_name}': {error_msg}")+ PrintStyle(+ background_color="grey", font_color="red", padding=True+ ).print(+ f"MCPConfig::__init__: Failed to create MCPServer '{server_name}': {error_msg}"+ ))# add to failed servers- self.disconnected_servers.append({- "config": server_item,- "error": error_msg,- "name": server_name- })+ self.disconnected_servers.append(+ {"config": server_item, "error": error_msg, "name": server_name}+ )++ def get_server_log(self, server_name: str) -> str:+ with self.__lock:+ for server in self.servers:+ if server.name == server_name:+ return server.get_log() # type: ignore+ return ""def get_servers_status(self) -> list[dict[str, Any]]:"""Get status of all servers"""@@ -474,29 +608,33 @@ class MCPConfig(BaseModel):# get tool counttool_count = len(server.get_tools())# check if server is connected- connected = tool_count > 0+ connected = True # tool_count > 0# get error message if any- error = ""-+ error = server.get_error()+# add server status to result- result.append({- "name": name,- "connected": connected,- "error": error,- "tool_count": tool_count- })-+ result.append(+ {+ "name": name,+ "connected": connected,+ "error": error,+ "tool_count": tool_count,+ }+ )+# add failed serversfor disconnected in self.disconnected_servers:- result.append({- "name": disconnected["name"],- "connected": False,- "error": disconnected["error"],- "tool_count": 0- })-+ result.append(+ {+ "name": disconnected["name"],+ "connected": False,+ "error": disconnected["error"],+ "tool_count": 0,+ }+ )+return result-+def is_initialized(self) -> bool:"""Check if the client is initialized"""with self.__lock:@@ -541,7 +679,7 @@ class MCPConfig(BaseModel):if "input_schema" in tool and "properties" in tool["input_schema"]:properties: dict[str, Any] = tool["input_schema"]["properties"]for key, value in properties.items():- tool_args += f" \"{key}\": \"...\",\n"+ tool_args += f' "{key}": "...",\n'examples = ""description = ""if "examples" in value:@@ -557,11 +695,11 @@ class MCPConfig(BaseModel):f"#### Usage:\n"f"~~~json\n"f"{{\n"- f" \"observations\": [\"...\"],\n"- f" \"thoughts\": [\"...\"],\n"- f" \"reflection\": [\"...\"],\n"+ f' "observations": ["..."],\n'+ f' "thoughts": ["..."],\n'+ f' "reflection": ["..."],\n'f" \"tool_name\": \"{server_name}.{tool['name']}\",\n"- f" \"tool_args\": {{\n"+ f' "tool_args": {{\n'f"{tool_args}"f" }}\n"f"}}\n"@@ -586,7 +724,9 @@ class MCPConfig(BaseModel):return Nonereturn MCPTool(agent=agent, name=tool_name, method=None, args={}, message="")- async def call_tool(self, tool_name: str, input_data: Dict[str, Any]) -> CallToolResult:+ async def call_tool(+ self, tool_name: str, input_data: Dict[str, Any]+ ) -> CallToolResult:"""Call a tool with the given input data"""if "." not in tool_name:raise ValueError(f"Tool {tool_name} not found")@@ -598,7 +738,8 @@ class MCPConfig(BaseModel):raise ValueError(f"Tool {tool_name} not found")-T = TypeVar('T')+T = TypeVar("T")+class MCPClientBase(ABC):# server: Union[MCPServerLocal, MCPServerRemote] # Defined in __init__@@ -609,81 +750,117 @@ class MCPClientBase(ABC):def __init__(self, server: Union[MCPServerLocal, MCPServerRemote]):self.server = server- self.tools: List[dict[str, Any]] = [] # Tools are cached on the client instance+ self.tools: List[dict[str, Any]] = [] # Tools are cached on the client instance+ self.error: str = ""+ self.log: List[str] = []# Protected method@abstractmethod- async def _create_stdio_transport(self, current_exit_stack: AsyncExitStack) -> tuple[MemoryObjectReceiveStream[JSONRPCMessage | Exception], MemoryObjectSendStream[JSONRPCMessage]]:+ async def _create_stdio_transport(+ self, current_exit_stack: AsyncExitStack+ ) -> tuple[+ MemoryObjectReceiveStream[SessionMessage | Exception],+ MemoryObjectSendStream[SessionMessage],+ ]:"""Create stdio/write streams using the provided exit_stack."""...- async def _execute_with_session(self, coro_func: Callable[[ClientSession], Awaitable[T]]) -> T:+ async def _execute_with_session(+ self,+ coro_func: Callable[[ClientSession], Awaitable[T]],+ read_timeout_seconds=60,+ ) -> T:"""Manages the lifecycle of an MCP session for a single operation.Creates a temporary session, executes coro_func with it, and ensures cleanup."""- operation_name = coro_func.__name__ # For logging+ operation_name = coro_func.__name__ # For logging# PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name}): Creating new session for operation '{operation_name}'...")- async with AsyncExitStack() as temp_stack:- try:- stdio, write = await self._create_stdio_transport(temp_stack)- # PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name} - {operation_name}): Transport created. Initializing session...")- session = await temp_stack.enter_async_context(- ClientSession(- stdio,- write,- read_timeout_seconds=timedelta(seconds=600)- )- )+ # Store the original exception outside the async block+ original_exception = None+ try:+ async with AsyncExitStack() as temp_stack:try:- # Add timeout to session.initialize- await asyncio.wait_for(session.initialize(), timeout=10)- except Exception as e:- PrintStyle(background_color="#AA4455", font_color="white", padding=False).print(- f"MCPClientBase ({self.server.name} - {operation_name}): Session initialization failed: {type(e).__name__}: {e}"+ async def log_callback(params):+ msg = getattr(params, "message", str(params))+ self.log.append(f"[{self.server.name}] [session]: {msg}")++ stdio, write = await self._create_stdio_transport(temp_stack)+ # PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name} - {operation_name}): Transport created. Initializing session...")+ session = await temp_stack.enter_async_context(+ ClientSession(+ stdio, # type: ignore+ write, # type: ignore+ read_timeout_seconds=timedelta(seconds=read_timeout_seconds),+ # logging_callback=log_callback,+ ))- raise- # PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name} - {operation_name}): Session initialized.")-- result = await coro_func(session)-- # PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name} - {operation_name}): Operation successful.")- return result- except Exception as e:- PrintStyle(background_color="#AA4455", font_color="white", padding=False).print(- f"MCPClientBase ({self.server.name} - {operation_name}): Error during operation: {type(e).__name__}: {e}"- )- raise # Re-raise the exception to be handled by the caller of update_tools/call_tool- finally:- PrintStyle(font_color="cyan").print(- f"MCPClientBase ({self.server.name} - {operation_name}): Session and transport will be closed by AsyncExitStack."- )+ await session.initialize()+ # PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name} - {operation_name}): Session initialized.")++ result = await coro_func(session)++ # PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name} - {operation_name}): Operation successful.")+ return result+ except Exception as e:+ # Store the original exception and raise a dummy exception+ original_exception = e+ # Create a dummy exception to break out of the async block+ raise RuntimeError("Dummy exception to break out of async block")+ except Exception as e:+ # Check if this is our dummy exception+ if original_exception is not None:+ e = original_exception+ # We have the original exception stored+ PrintStyle(+ background_color="#AA4455", font_color="white", padding=False+ ).print(+ f"MCPClientBase ({self.server.name} - {operation_name}): Error during operation: {type(e).__name__}: {e}"+ )+ raise e # Re-raise the original exception+ finally:+ PrintStyle(font_color="cyan").print(+ f"MCPClientBase ({self.server.name} - {operation_name}): Session and transport will be closed by AsyncExitStack."+ )# This line should ideally be unreachable if the try/except/finally logic within the 'async with' is exhaustive.# Adding it to satisfy linters that might not fully trace the raise/return paths through async context managers.- raise RuntimeError(f"MCPClientBase ({self.server.name} - {operation_name}): _execute_with_session exited 'async with' block unexpectedly.")+ raise RuntimeError(+ f"MCPClientBase ({self.server.name} - {operation_name}): _execute_with_session exited 'async with' block unexpectedly."+ )async def update_tools(self) -> "MCPClientBase":# PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name}): Starting 'update_tools' operation...")-+async def list_tools_op(current_session: ClientSession):response: ListToolsResult = await current_session.list_tools()- with self.__lock:- self.tools = [{- "name": tool.name,- "description": tool.description,- "input_schema": tool.inputSchema- } for tool in response.tools]- PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name}): Tools updated. Found {len(self.tools)} tools.")+ with self.__lock:+ self.tools = [+ {+ "name": tool.name,+ "description": tool.description,+ "input_schema": tool.inputSchema,+ }+ for tool in response.tools+ ]+ PrintStyle(font_color="green").print(+ f"MCPClientBase ({self.server.name}): Tools updated. Found {len(self.tools)} tools."+ )try:- await self._execute_with_session(list_tools_op)+ set = settings.get_settings()+ await self._execute_with_session(list_tools_op, read_timeout_seconds=set["mcp_client_init_timeout"])except Exception as e:+ # e = eg.exceptions[0]+ error_text = errors.format_error(e, 0, 0)# Error already logged by _execute_with_session, this is for specific handling if needed- PrintStyle(background_color="#CC34C3", font_color="white", bold=True, padding=True).print(- f"MCPClientBase ({self.server.name}): 'update_tools' operation failed: {e}"+ PrintStyle(+ background_color="#CC34C3", font_color="white", bold=True, padding=True+ ).print(+ f"MCPClientBase ({self.server.name}): 'update_tools' operation failed: {error_text}")with self.__lock:- self.tools = [] # Ensure tools are cleared on failure+ self.tools = [] # Ensure tools are cleared on failure+ self.error = f"Failed to initialize. {error_text}" # store error from tools fetchreturn selfdef has_tool(self, tool_name: str) -> bool:@@ -699,19 +876,32 @@ class MCPClientBase(ABC):with self.__lock:return self.tools- async def call_tool(self, tool_name: str, input_data: Dict[str, Any]) -> CallToolResult:+ async def call_tool(+ self, tool_name: str, input_data: Dict[str, Any]+ ) -> CallToolResult:# PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name}): Preparing for 'call_tool' operation for tool '{tool_name}'.")if not self.has_tool(tool_name):- PrintStyle(font_color="orange").print(f"MCPClientBase ({self.server.name}): Tool '{tool_name}' not in cache for 'call_tool', refreshing tools...")- await self.update_tools() # This will use its own properly managed session+ PrintStyle(font_color="orange").print(+ f"MCPClientBase ({self.server.name}): Tool '{tool_name}' not in cache for 'call_tool', refreshing tools..."+ )+ await self.update_tools() # This will use its own properly managed sessionif not self.has_tool(tool_name):- PrintStyle(font_color="red").print(f"MCPClientBase ({self.server.name}): Tool '{tool_name}' not found after refresh. Raising ValueError.")- raise ValueError(f"Tool {tool_name} not found after refreshing tool list for server {self.server.name}.")- PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name}): Tool '{tool_name}' found after updating tools.")+ PrintStyle(font_color="red").print(+ f"MCPClientBase ({self.server.name}): Tool '{tool_name}' not found after refresh. Raising ValueError."+ )+ raise ValueError(+ f"Tool {tool_name} not found after refreshing tool list for server {self.server.name}."+ )+ PrintStyle(font_color="green").print(+ f"MCPClientBase ({self.server.name}): Tool '{tool_name}' found after updating tools."+ )async def call_tool_op(current_session: ClientSession):+ set = settings.get_settings()# PrintStyle(font_color="cyan").print(f"MCPClientBase ({self.server.name}): Executing 'call_tool' for '{tool_name}' via MCP session...")- response: CallToolResult = await current_session.call_tool(tool_name, input_data)+ response: CallToolResult = await current_session.call_tool(+ tool_name, input_data, read_timeout_seconds=timedelta(seconds=set["mcp_client_tool_timeout"])+ )# PrintStyle(font_color="green").print(f"MCPClientBase ({self.server.name}): Tool '{tool_name}' call successful via session.")return response@@ -719,36 +909,92 @@ class MCPClientBase(ABC):return await self._execute_with_session(call_tool_op)except Exception as e:# Error logged by _execute_with_session. Re-raise a specific error for the caller.- PrintStyle(background_color="#AA4455", font_color="white", padding=True).print(+ PrintStyle(+ background_color="#AA4455", font_color="white", padding=True+ ).print(f"MCPClientBase ({self.server.name}): 'call_tool' operation for '{tool_name}' failed: {type(e).__name__}: {e}")- raise ConnectionError(f"MCPClientBase::Failed to call tool '{tool_name}' on server '{self.server.name}'. Original error: {type(e).__name__}: {e}")+ raise ConnectionError(+ f"MCPClientBase::Failed to call tool '{tool_name}' on server '{self.server.name}'. Original error: {type(e).__name__}: {e}"+ )class MCPClientLocal(MCPClientBase):- async def _create_stdio_transport(self, current_exit_stack: AsyncExitStack) -> tuple[MemoryObjectReceiveStream[JSONRPCMessage | Exception], MemoryObjectSendStream[JSONRPCMessage]]:+ def get_log(self):+ # read and return lines from self.log_file, do not close it+ if not hasattr(self, 'log_file') or self.log_file is None:+ return ""+ self.log_file.seek(0)+ try:+ log = self.log_file.read()+ except Exception:+ log = ""+ return log++ def __del__(self):+ # close the log file if it exists+ if hasattr(self, 'log_file') and self.log_file is not None:+ try:+ self.log_file.close()+ except Exception:+ pass+ self.log_file = None++ async def _create_stdio_transport(+ self, current_exit_stack: AsyncExitStack+ ) -> tuple[+ MemoryObjectReceiveStream[SessionMessage | Exception],+ MemoryObjectSendStream[SessionMessage],+ ]:"""Connect to an MCP server, init client and save stdio/write streams"""server: MCPServerLocal = cast(MCPServerLocal, self.server)+ if not server.command:+ raise ValueError("Command not specified")if not which(server.command):- raise ValueError(f"Command {server.command} not found")+ raise ValueError(f"Command '{server.command}' not found")server_params = StdioServerParameters(command=server.command,args=server.args,env=server.env,encoding=server.encoding,- encoding_error_handler=server.encoding_error_handler+ encoding_error_handler=server.encoding_error_handler,)- stdio_transport = await current_exit_stack.enter_async_context(stdio_client(server_params))+ # create a custom error log handler that will capture error output+ import tempfile++ # use a temporary file for error logging (text mode) if not already present+ if not hasattr(self, 'log_file') or self.log_file is None:+ self.log_file = tempfile.TemporaryFile(mode="w+", encoding="utf-8")++ # use the stdio_client with our error log file+ stdio_transport = await current_exit_stack.enter_async_context(+ stdio_client(server_params, errlog=self.log_file)+ )+ # do not read or close the file here, as stdio is asyncreturn stdio_transportclass MCPClientRemote(MCPClientBase):- async def _create_stdio_transport(self, current_exit_stack: AsyncExitStack) -> tuple[MemoryObjectReceiveStream[JSONRPCMessage | Exception], MemoryObjectSendStream[JSONRPCMessage]]:++ def get_log(self):+ return "Logging not implemented for remote servers yet"++ async def _create_stdio_transport(+ self, current_exit_stack: AsyncExitStack+ ) -> tuple[+ MemoryObjectReceiveStream[SessionMessage | Exception],+ MemoryObjectSendStream[SessionMessage],+ ]:"""Connect to an MCP server, init client and save stdio/write streams"""server: MCPServerRemote = cast(MCPServerRemote, self.server)stdio_transport = await current_exit_stack.enter_async_context(- sse_client(url=server.url, headers=server.headers, timeout=server.timeout, sse_read_timeout=server.sse_read_timeout)+ sse_client(+ url=server.url,+ headers=server.headers,+ timeout=server.timeout,+ sse_read_timeout=server.sse_read_timeout,+ ))return stdio_transportdiff --git a/python/helpers/settings.py b/python/helpers/settings.pyindex c2078cde8..62ae8671f 100644--- a/python/helpers/settings.py+++ b/python/helpers/settings.py@@ -44,7 +44,6 @@ class Settings(TypedDict):agent_prompts_subdir: stragent_memory_subdir: stragent_knowledge_subdir: str- mcp_servers: strapi_keys: dict[str, str]@@ -64,6 +63,9 @@ class Settings(TypedDict):stt_silence_duration: intstt_waiting_timeout: int+ mcp_servers: str+ mcp_client_init_timeout: int+ mcp_client_tool_timeout: intmcp_server_enabled: bool@@ -706,6 +708,26 @@ def convert_out(settings: Settings) -> SettingsOutput:})+ mcp_client_fields.append(+ {+ "id": "mcp_client_init_timeout",+ "title": "MCP Client Init Timeout",+ "description": "Timeout for MCP client initialization (in seconds). Higher values might be required for complex MCPs, but might also slowdown system startup.",+ "type": "number",+ "value": settings["mcp_client_init_timeout"],+ }+ )++ mcp_client_fields.append(+ {+ "id": "mcp_client_tool_timeout",+ "title": "MCP Client Tool Timeout",+ "description": "Timeout for MCP client tool execution. Higher values might be required for complex tools, but might also result in long responses with failing tools.",+ "type": "number",+ "value": settings["mcp_client_tool_timeout"],+ }+ )+mcp_client_section: SettingsSection = {"id": "mcp_client","title": "External MCP Servers",@@ -913,6 +935,8 @@ def get_default_settings() -> Settings:stt_silence_duration=1000,stt_waiting_timeout=2000,mcp_servers='{\n "mcpServers": {}\n}',+ mcp_client_init_timeout=5,+ mcp_client_tool_timeout=120,mcp_server_enabled=False,)diff --git a/webui/components/settings/mcp/client/mcp-servers-log.html b/webui/components/settings/mcp/client/mcp-servers-log.htmlnew file mode 100644index 000000000..90ee3badf--- /dev/null+++ b/webui/components/settings/mcp/client/mcp-servers-log.html@@ -0,0 +1,34 @@+<html>++<head>+ <title>MCP Server Log</title>++ <script type="module">+ import { store } from "/components/settings/mcp/client/mcp-servers-store.js";+ </script>+</head>++<body>+ <div x-data>+ <template x-if="$store.mcpServersStore">+ <div id="mcp-servers-log">+ <p x-text="$store.mcpServersStore.serverLog && $store.mcpServersStore.serverLog.trim() ? $store.mcpServersStore.serverLog : 'Log empty'"></p>+ </div>+ </template>+ </div>++ <style>+ #mcp-servers-log {+ width: 100%;+ }++ #mcp-servers-log p {+ font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;+ font-size: 0.8em;+ white-space: pre-wrap;+ }+ </style>++</body>++</html>\ No newline at end of filediff --git a/webui/components/settings/mcp/client/mcp-servers-store.js b/webui/components/settings/mcp/client/mcp-servers-store.jsindex 9b0b24598..e87ae874e 100644--- a/webui/components/settings/mcp/client/mcp-servers-store.js+++ b/webui/components/settings/mcp/client/mcp-servers-store.js@@ -8,6 +8,7 @@ const model = {servers: [],loading: false,statusCheck: false,+ serverLog: "",async initialize() {// Initialize the JSON Viewer after the modal is rendered@@ -104,6 +105,17 @@ const model = {}this.loading = false;},++ async getServerLog(serverName) {+ this.serverLog = "";+ const resp = await API.callJsonApi("mcp_server_get_log", {+ server_name: serverName,+ });+ if (resp.success) {+ this.serverLog = resp.log;+ openModal("settings/mcp/client/mcp-servers-log.html")+ }+ },};const store = createStore("mcpServersStore", model);diff --git a/webui/components/settings/mcp/client/mcp-servers.html b/webui/components/settings/mcp/client/mcp-servers.htmlindex eba391bb4..81e67b5d6 100644--- a/webui/components/settings/mcp/client/mcp-servers.html+++ b/webui/components/settings/mcp/client/mcp-servers.html@@ -26,21 +26,24 @@<div class="server-list"><template x-for="server in $store.mcpServersStore.servers" :key="server.name"><div class="server-item">- <div class="status-icon" style="margin-right: 0.5em;" x-data="{ connected: server.connected }">+ <div class="status-icon" style="margin-right: 0.5em;"+ x-data="{ connected: server.connected }"><svg viewBox="0 0 30 30">- <!-- Connected State (filled circle) -->- <circle class="connected-circle" cx="15" cy="15" r="8"- x-bind:fill="server.connected ? '#00c340' : 'none'" x-bind:opacity="server.connected ? 1 : 0" />+ <!-- Connected State (filled circle, green or orange) -->+ <circle x-bind:class="server.error ? 'disconnected-circle' : 'connected-circle'" cx="15" cy="15" r="8" x-bind:fill="server.connected+ ? (server.error ? '#e40138' : (server.tool_count === 0 ? '#e40138' : '#00c340'))+ : 'none'" x-bind:opacity="server.connected ? 1 : 0" /><!-- Disconnected State (outline circle) -->- <circle class="disconnected-circle" cx="15" cy="15" r="9" fill="none" stroke="#e40138"- stroke-width="3" x-bind:opacity="server.connected ? 0 : 1" />+ <circle x-bind:class="server.error ? 'disconnected-circle' : 'connected-circle'" cx="15" cy="15" r="9" fill="none"+ stroke="#e40138" stroke-width="3" x-bind:opacity="server.connected ? 0 : 1" /></svg></div><span class="server-name" x-text="server.name"></span><span class="server-tools"x-text="'- ' + (server.tool_count ? server.tool_count : 0) + ' tools'"></span><span class="server-error" x-show="server.error" x-text="server.error"></span>+ <span class="server-links"><a href="#" @click="$store.mcpServersStore.getServerLog(server.name)">View Log</a></span></div></template><div x-show="$store.mcpServersStore.servers.length === 0" class="no-servers">@@ -107,12 +110,16 @@color: var(--c-fg2);font-style: italic;}-+.server-error {color: #F44336;font-size: 0.9em;margin-left: 0.8em;}++ .server-links {+ margin-left: 0.8em;+ }</style></body>diff --git a/webui/js/modals.js b/webui/js/modals.jsindex 756c09fed..c39d9b2f4 100644--- a/webui/js/modals.js+++ b/webui/js/modals.js@@ -1,7 +1,6 @@// Import the component loader and page utilitiesimport { importComponent } from "/js/components.js";-// Modal functionalityconst modalStack = [];@@ -64,6 +63,7 @@ function createModalElement(name) {}});+// Create modal structurenewModal.innerHTML = `<div class="modal-inner">@@ -77,6 +77,11 @@ function createModalElement(name) {</div>`;+ // Setup close button handler for this specific modal+ const close_button = newModal.querySelector(".modal-close");+ close_button.addEventListener("click", () => closeModal());++// Add modal to DOMdocument.body.appendChild(newModal);@@ -90,7 +95,7 @@ function createModalElement(name) {element: newModal,title: newModal.querySelector(".modal-title"),body: newModal.querySelector(".modal-bd"),- close: newModal.querySelector(".modal-close"),+ close: close_button,styles: [],scripts: [],};@@ -113,9 +118,6 @@ export function openModal(modalPath) {// Already added to stack above- // Setup close button handler for this specific modal- modal.close.addEventListener("click", () => closeModal());-// Use importComponent to load the modal content// This handles all HTML, styles, scripts and nested components// Updated path to use the new folder structure with modal.html@@ -140,9 +142,6 @@ export function openModal(modalPath) {// Update modal z-indexesupdateModalZIndexes();-- // Setup close button handler for this specific modal- modal.close.addEventListener("click", () => closeModal());} catch (error) {console.error("Error loading modal content:", error);resolve();@@ -151,7 +150,7 @@ export function openModal(modalPath) {}// Function to close modal-export function closeModal(modalName=null) {+export function closeModal(modalName = null) {if (modalStack.length === 0) return;let modalIndex = modalStack.length - 1; // Default to last modal