Files
computer/libs/python/mcp-server/scripts/start_mcp_server.sh
Adam 37864faef2 Fix MCP server startup and tool schema issues in server.py and start_mcp_server.sh
Changes
- Refactored `server.py`:
  - Delayed server instantiation to avoid double-import issues and RuntimeWarning.
  - Reworked `run_cua_task` and `run_multi_cua_tasks` to yield structured output
    (text + screenshots) in a Pydantic-compatible format.
  - Added helper functions for normalising message content, extracting text,
    and serialising tool outputs.
  - Improved logging and error handling for clearer debugging.

- Updated `start_mcp_server.sh`:
  - Ensures Python path points to repo-local `.venv`.
  - Sets `PYTHONPATH` correctly for all `libs/python/*` packages.
  - Added clear debug output for python binary, repo dir, and PYTHONPATH.

Why
- Fixed `PydanticSchemaGenerationError` when returning `Image` objects from
  tool functions.
- Prevented `FastMCP.__init__()` errors by aligning with supported arguments.
- Removed duplicate module loading at startup that triggered RuntimeWarnings.
- Improved clarity of server startup logs for easier integration with Claude Desktop.

Outcome
The MCP server now starts cleanly, registers all tools, and stays connected
with Claude Desktop without schema errors or double-import issues.
2025-09-30 18:52:04 -04:00

44 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# --- Resolve repo root from this script's location ---
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
CUA_REPO_DIR="$( cd "$SCRIPT_DIR/../../../.." &> /dev/null && pwd )"
# --- Choose a Python interpreter (prefer repo-root venv) ---
CANDIDATES=(
"$CUA_REPO_DIR/.venv/bin/python"
"$CUA_REPO_DIR/libs/.venv/bin/python"
"$(command -v python3 || true)"
"$(command -v python || true)"
)
PYTHON_PATH=""
for p in "${CANDIDATES[@]}"; do
if [[ -n "$p" && -x "$p" ]]; then
PYTHON_PATH="$p"
break
fi
done
if [[ -z "${PYTHON_PATH}" ]]; then
>&2 echo "[cua-mcp] ERROR: No suitable Python found. Tried:"
for p in "${CANDIDATES[@]}"; do >&2 echo " - $p"; done
>&2 echo "[cua-mcp] Tip: create venv: python3 -m venv $CUA_REPO_DIR/.venv && \"$CUA_REPO_DIR/.venv/bin/pip\" install -e \"$CUA_REPO_DIR/libs/python/mcp-server\""
exit 127
fi
# --- Export PYTHONPATH so module imports work during dev ---
export PYTHONPATH="$CUA_REPO_DIR/libs/python/mcp-server:$CUA_REPO_DIR/libs/python/agent:$CUA_REPO_DIR/libs/python/computer:$CUA_REPO_DIR/libs/python/core:$CUA_REPO_DIR/libs/python/pylume"
# --- Helpful startup log for Claude's mcp.log ---
>&2 echo "[cua-mcp] using python: $PYTHON_PATH"
>&2 echo "[cua-mcp] repo dir : $CUA_REPO_DIR"
>&2 echo "[cua-mcp] PYTHONPATH : $PYTHONPATH"
if [[ -n "${CUA_MODEL_NAME:-}" ]]; then
>&2 echo "[cua-mcp] CUA_MODEL_NAME=$CUA_MODEL_NAME"
fi
# --- Run the MCP server module ---
exec "$PYTHON_PATH" -m mcp_server.server