mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-16 04:20:45 +08:00
fix(mcp): preflight python module check and surface install errors
- Verify python -m module import in preflight with fix hints - Pass env_schema defaults in health/sync; catch JSON install exceptions in UI Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
8787adc3cc
commit
4610989d72
5 changed files with 107 additions and 34 deletions
|
|
@ -5,6 +5,7 @@ import json
|
|||
import os
|
||||
from typing import Any
|
||||
|
||||
from runtime.tools.mcp.env_config import mcp_row_env_config
|
||||
from runtime.skills import SkillSpec, materialize_skills_from_tool_specs
|
||||
from runtime.tools.base import ToolSpec
|
||||
from runtime.tools.mcp.filesystem_argv import build_mcp_process_command
|
||||
|
|
@ -13,28 +14,7 @@ from runtime.tools.public.bailian_webparser_tool import bailian_webparser_tool
|
|||
|
||||
|
||||
def _mcp_row_env_config(row: dict[str, Any]) -> tuple[list[str], dict[str, str]]:
|
||||
"""Per-server env allowlist + defaults from registry ``env_schema`` (e.g. Cursor ``mcpServers.env``)."""
|
||||
from runtime.operations.mcp_env import mcp_env_allowlist_keys
|
||||
|
||||
schema = row.get("env_schema") if isinstance(row.get("env_schema"), dict) else {}
|
||||
defaults: dict[str, str] = {}
|
||||
schema_keys: list[str] = []
|
||||
for k, spec in schema.items():
|
||||
key = str(k or "").strip()
|
||||
if not key:
|
||||
continue
|
||||
schema_keys.append(key)
|
||||
if isinstance(spec, dict) and spec.get("default") is not None:
|
||||
dv = str(spec.get("default") or "").strip()
|
||||
if dv:
|
||||
defaults[key] = dv
|
||||
seen: set[str] = set()
|
||||
allowlist: list[str] = []
|
||||
for k in [*mcp_env_allowlist_keys(), *schema_keys]:
|
||||
if k and k not in seen:
|
||||
seen.add(k)
|
||||
allowlist.append(k)
|
||||
return allowlist, defaults
|
||||
return mcp_row_env_config(row)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
46
runtime/tools/mcp/env_config.py
Normal file
46
runtime/tools/mcp/env_config.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def mcp_row_env_config(row: dict[str, Any]) -> tuple[list[str], dict[str, str]]:
|
||||
"""Per-server env allowlist + defaults from registry ``env_schema``."""
|
||||
from runtime.operations.mcp_env import mcp_env_allowlist_keys
|
||||
|
||||
schema = row.get("env_schema") if isinstance(row.get("env_schema"), dict) else {}
|
||||
defaults: dict[str, str] = {}
|
||||
schema_keys: list[str] = []
|
||||
for k, spec in schema.items():
|
||||
key = str(k or "").strip()
|
||||
if not key:
|
||||
continue
|
||||
schema_keys.append(key)
|
||||
if isinstance(spec, dict) and spec.get("default") is not None:
|
||||
dv = str(spec.get("default") or "").strip()
|
||||
if dv:
|
||||
defaults[key] = dv
|
||||
seen: set[str] = set()
|
||||
allowlist: list[str] = []
|
||||
for k in [*mcp_env_allowlist_keys(), *schema_keys]:
|
||||
if k and k not in seen:
|
||||
seen.add(k)
|
||||
allowlist.append(k)
|
||||
return allowlist, defaults
|
||||
|
||||
|
||||
def mcp_runtime_for_row(row: dict[str, Any], *, store: Any) -> Any:
|
||||
from runtime.tools.mcp.filesystem_argv import build_mcp_process_command
|
||||
from runtime.tools.mcp.runtime import McpProcessRuntime
|
||||
|
||||
cmd = str(row.get("entry_command") or "").strip()
|
||||
args = [str(x) for x in (row.get("entry_args") or []) if str(x).strip()]
|
||||
allowlist, defaults = mcp_row_env_config(row)
|
||||
return McpProcessRuntime(
|
||||
build_mcp_process_command(cmd, args, store=store),
|
||||
timeout_s=float(row.get("timeout_s") or 30.0),
|
||||
env_allowlist=allowlist,
|
||||
env_defaults=defaults,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["mcp_row_env_config", "mcp_runtime_for_row"]
|
||||
|
|
@ -169,9 +169,54 @@ def preflight_mcp_server(manifest: McpServerManifest) -> dict[str, Any]:
|
|||
return {"ok": False, "error_code": "mcp_entry_not_found", "error": f"entry_command_not_found:{entry}", "warnings": warnings, "fix_suggestions": fix_suggestions}
|
||||
env_schema = manifest.env_schema if isinstance(manifest.env_schema, dict) else {}
|
||||
required_env = [str(k) for k, v in env_schema.items() if isinstance(v, dict) and bool(v.get("required"))]
|
||||
mod_err = _preflight_python_module(entry, manifest.entry_args or [])
|
||||
if mod_err is not None:
|
||||
return {**mod_err, "warnings": warnings, "fix_suggestions": fix_suggestions}
|
||||
return {"ok": True, "error_code": "", "error": "", "entry_command_path": found, "required_env": required_env, "warnings": warnings, "fix_suggestions": fix_suggestions}
|
||||
|
||||
|
||||
def _preflight_python_module(entry: str, entry_args: list[str]) -> dict[str, Any] | None:
|
||||
"""When entry is ``python -m <mod>``, verify that interpreter can import ``mod``."""
|
||||
argv = [str(x or "").strip() for x in entry_args if str(x or "").strip()]
|
||||
if str(entry or "").strip().lower() not in {"python", "python3", "py"}:
|
||||
return None
|
||||
if len(argv) < 2 or argv[0] != "-m":
|
||||
return None
|
||||
mod = argv[1].split(".")[0]
|
||||
if not mod:
|
||||
return None
|
||||
exe = shutil.which(entry) or entry
|
||||
try:
|
||||
cp = _run_command([exe, "-c", f"import {mod}"], timeout=12)
|
||||
except Exception as exc:
|
||||
return {
|
||||
"ok": False,
|
||||
"error_code": "mcp_python_module_check_failed",
|
||||
"error": str(exc),
|
||||
"fix_suggestions": [
|
||||
{
|
||||
"title": f"Install module for this Python ({exe})",
|
||||
"command": f'"{exe}" -m pip install "git+https://github.com/hansjone/netx.git#subdirectory=packages/netx-mcp"',
|
||||
}
|
||||
],
|
||||
}
|
||||
if cp.returncode == 0:
|
||||
return None
|
||||
err = (cp.stderr or cp.stdout or "").strip()
|
||||
return {
|
||||
"ok": False,
|
||||
"error_code": "mcp_python_module_missing",
|
||||
"error": err[:500] or f"cannot import {mod}",
|
||||
"fix_suggestions": [
|
||||
{
|
||||
"title": f"Install {mod} for the same Python oclaw uses",
|
||||
"command": f'"{exe}" -m pip install "git+https://github.com/hansjone/netx.git#subdirectory=packages/netx-mcp"',
|
||||
},
|
||||
{"title": "Verify import", "command": f'"{exe}" -c "import {mod}; print(\'ok\')"'},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def detect_local_dependencies() -> list[dict[str, Any]]:
|
||||
deps = [{"name": "git", "version_args": ["--version"]}, {"name": "node", "version_args": ["--version"]}, {"name": "npm", "version_args": ["--version"]}, {"name": "npx", "version_args": ["--version"]}, {"name": "python", "version_args": ["--version"]}, {"name": "pip", "version_args": ["--version"]}]
|
||||
out: list[dict[str, Any]] = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue