From 02bf7c10724e920081b7d666fc12675871eeef94 Mon Sep 17 00:00:00 2001 From: deci Date: Tue, 13 May 2025 18:54:41 -0500 Subject: [PATCH] Edit: Changed mcp.py to mcp_handler.py to prevent potential import confusion between 'mcp' package and 'mcp.py'. Edited dockerfile to 'latest' so it doesnt spin up old builds after image creation. Also edited preinstall to ensure the mcp related dependencies can load non-interactive. --- agent.py | 2 +- docker/run/docker-compose.yml | 2 +- docker/run/fs/ins/install_A0.sh | 21 ++++++++++++++++--- docker/run/fs/ins/pre_install.sh | 17 ++++++++++++--- initialize.py | 7 ++++--- .../system_prompt/_10_system_prompt.py | 2 +- python/helpers/{mcp.py => mcp_handler.py} | 4 ++++ python/helpers/settings.py | 2 +- 8 files changed, 44 insertions(+), 13 deletions(-) rename python/helpers/{mcp.py => mcp_handler.py} (99%) diff --git a/agent.py b/agent.py index 6b9525adf..288e1127d 100644 --- a/agent.py +++ b/agent.py @@ -691,7 +691,7 @@ class Agent: # Try getting tool from MCP first try: - import python.helpers.mcp as mcp_helper + import python.helpers.mcp_handler as mcp_helper mcp_tool_candidate = mcp_helper.MCPConfig.get_instance().get_tool(self, tool_name) if mcp_tool_candidate: tool = mcp_tool_candidate diff --git a/docker/run/docker-compose.yml b/docker/run/docker-compose.yml index 725be771a..d2a828b31 100644 --- a/docker/run/docker-compose.yml +++ b/docker/run/docker-compose.yml @@ -1,7 +1,7 @@ services: agent-zero: container_name: agent-zero - image: frdel/agent-zero-run:testing + image: frdel/agent-zero:latest volumes: - ./agent-zero:/a0 ports: diff --git a/docker/run/fs/ins/install_A0.sh b/docker/run/fs/ins/install_A0.sh index c89c2cecf..8fb6eeb84 100644 --- a/docker/run/fs/ins/install_A0.sh +++ b/docker/run/fs/ins/install_A0.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Exit immediately if a command exits with a non-zero status. +# set -e + # branch from parameter if [ -z "$1" ]; then echo "Error: Branch parameter is empty. Please provide a valid branch name." @@ -7,10 +10,11 @@ if [ -z "$1" ]; then fi BRANCH="$1" -# clone project repo branch -git clone -b "$BRANCH" "https://github.com/frdel/agent-zero" "/git/agent-zero" +git clone -b "$BRANCH" "https://github.com/frdel/agent-zero" "/git/agent-zero" || { + echo "CRITICAL ERROR: Failed to clone repository. Branch: $BRANCH" + exit 1 +} -# setup python environment . "/ins/setup_venv.sh" "$@" # Ensure the virtual environment and pip setup @@ -19,9 +23,20 @@ pip install --upgrade pip ipython requests # Install some packages in specific variants pip install torch --index-url https://download.pytorch.org/whl/cpu +pip install -v mcp==1.3.0 || { + echo "ERROR: Failed during separate attempt to install mcp==1.3.0. Will proceed to full requirements.txt install anyway." +} +python -c "import mcp; from mcp import ClientSession; print(f'DEBUG: mcp and mcp.ClientSession imported successfully after separate install. mcp path: {mcp.__file__}')" || { + echo "ERROR: mcp package or mcp.ClientSession NOT importable after separate mcp==1.3.0 installation attempt. Full requirements.txt will run next." +} + # Install remaining A0 python packages pip install -r /git/agent-zero/requirements.txt +python -c "import mcp; from mcp import ClientSession; print(f'DEBUG: mcp and mcp.ClientSession imported successfully after requirements.txt. mcp path: {mcp.__file__}')" || { + echo "CRITICAL ERROR: mcp package or mcp.ClientSession not found or failed to import after requirements.txt processing." +} + # install playwright bash /ins/install_playwright.sh "$@" diff --git a/docker/run/fs/ins/pre_install.sh b/docker/run/fs/ins/pre_install.sh index 35cb14b9d..d901d1200 100644 --- a/docker/run/fs/ins/pre_install.sh +++ b/docker/run/fs/ins/pre_install.sh @@ -5,20 +5,24 @@ chmod 0644 /etc/cron.d/* echo "=====BEFORE UPDATE=====" +# Set DEBIAN_FRONTEND to noninteractive to prevent prompts +export DEBIAN_FRONTEND=noninteractive + # Update and install necessary packages apt clean -apt-get update && apt-get upgrade -y && apt-get install -y \ +apt-get update && apt-get upgrade -y && apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install -y \ python3 \ python3-venv \ python3-pip \ nodejs \ + npm \ openssh-server \ sudo \ curl \ wget \ git \ ffmpeg \ - nginx\ + nginx \ supervisor \ cron \ libmagic-dev \ @@ -40,6 +44,7 @@ echo "=====MID UPDATE=====" # for some reason npm crashes builds on amd64 in this version and has to be installed separately # A0 can install it when needed +# The line below is now redundant as npm is included in the main install list above # apt-get install -y \ # npm @@ -59,7 +64,13 @@ echo "=====AFTER UPDATE=====" # fi # Install npx for use by local MCP Servers -npm i -g npx shx +echo "DEBUG: Installing npx and shx globally using npm..." +npm i -g npx shx || { + echo "CRITICAL ERROR: Failed to install npx and shx using npm." + # exit 1 # Optionally exit if this is critical enough +} +echo "DEBUG: npx and shx installation attempt finished." + # Prepare SSH daemon bash /ins/setup_ssh.sh "$@" diff --git a/initialize.py b/initialize.py index 92961689c..9e92a2270 100644 --- a/initialize.py +++ b/initialize.py @@ -76,16 +76,17 @@ def initialize(): # update config with runtime args args_override(config) - import python.helpers.mcp as mcp_helper + import python.helpers.mcp_handler as mcp_helper import agent as agent_helper import python.helpers.print_style as print_style_helper if not mcp_helper.MCPConfig.get_instance().is_initialized(): try: mcp_helper.MCPConfig.update(config.mcp_servers) except Exception as e: - if agent_helper.AgentContext.first(): + first_context = agent_helper.AgentContext.first() + if first_context: ( - agent_helper.AgentContext.first().log + first_context.log .log(type="warning", content=f"Failed to update MCP settings: {e}", temp=False) ) ( diff --git a/python/extensions/system_prompt/_10_system_prompt.py b/python/extensions/system_prompt/_10_system_prompt.py index 1ff461551..dda842fd3 100644 --- a/python/extensions/system_prompt/_10_system_prompt.py +++ b/python/extensions/system_prompt/_10_system_prompt.py @@ -1,7 +1,7 @@ from datetime import datetime from typing import Any, Optional from python.helpers.extension import Extension -from python.helpers.mcp import MCPConfig +from python.helpers.mcp_handler import MCPConfig from agent import Agent, LoopData from python.helpers.localization import Localization diff --git a/python/helpers/mcp.py b/python/helpers/mcp_handler.py similarity index 99% rename from python/helpers/mcp.py rename to python/helpers/mcp_handler.py index cc2fb6f3f..fdd935ab8 100644 --- a/python/helpers/mcp.py +++ b/python/helpers/mcp_handler.py @@ -5,6 +5,10 @@ import asyncio from contextlib import AsyncExitStack from shutil import which from datetime import timedelta + +import os +print(f"DEBUG: Listing /opt/venv/lib/python3.11/site-packages/ before mcp import: {os.listdir('/opt/venv/lib/python3.11/site-packages/')}") + from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from mcp.client.sse import sse_client diff --git a/python/helpers/settings.py b/python/helpers/settings.py index e8c6e5cd4..9e45f2b87 100644 --- a/python/helpers/settings.py +++ b/python/helpers/settings.py @@ -886,7 +886,7 @@ def _apply_settings(previous: Settings | None): memory_reload() # update mcp settings if necessary - from python.helpers.mcp import MCPConfig + from python.helpers.mcp_handler import MCPConfig async def update_mcp_settings(mcp_servers: str): PrintStyle(background_color="black", font_color="white", padding=True).print("Updating MCP config...")