mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 04:40:45 +08:00
feat(mcp): integrate netx via netx_mcp and improve MCP install
- Gate builtin netx tools; ops docs/skills use mcp__netx__* - Import Cursor mcpServers JSON; pass env_schema defaults to subprocess - Allow source_type=local; UTF-8 MCP stdio on Windows Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
bbd7b0300f
commit
a0a3f4b65f
17 changed files with 408 additions and 536 deletions
|
|
@ -5,7 +5,6 @@ import json
|
|||
import os
|
||||
from typing import Any
|
||||
|
||||
from runtime.operations.mcp_env import mcp_env_allowlist_keys
|
||||
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,6 +12,31 @@ from runtime.tools.mcp.runtime import McpProcessRuntime
|
|||
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
|
||||
|
||||
|
||||
@dataclass
|
||||
class _McpBoundTool:
|
||||
server_id: str
|
||||
|
|
@ -23,9 +47,15 @@ class _McpBoundTool:
|
|||
timeout_s: float = 30.0
|
||||
required_permissions: frozenset[str] = frozenset()
|
||||
env_allowlist: list[str] | None = None
|
||||
env_defaults: dict[str, str] | None = None
|
||||
|
||||
def to_spec(self) -> ToolSpec:
|
||||
rt = McpProcessRuntime(command=self.command, timeout_s=self.timeout_s, env_allowlist=self.env_allowlist)
|
||||
rt = McpProcessRuntime(
|
||||
command=self.command,
|
||||
timeout_s=self.timeout_s,
|
||||
env_allowlist=self.env_allowlist,
|
||||
env_defaults=self.env_defaults,
|
||||
)
|
||||
|
||||
def _handler(args: dict[str, Any]) -> dict[str, Any]:
|
||||
res = rt.call_tool(tool_name=self.tool_name, arguments=args or {})
|
||||
|
|
@ -112,7 +142,6 @@ def materialize_mcp_tools_for_specialist(
|
|||
return []
|
||||
out: list[ToolSpec] = []
|
||||
rows = store.list_mcp_servers(enabled_only=True) if store else []
|
||||
env_allowlist = mcp_env_allowlist_keys()
|
||||
for row in rows:
|
||||
server_id = str(row.get("server_id") or "").strip()
|
||||
cmd = str(row.get("entry_command") or "").strip()
|
||||
|
|
@ -120,6 +149,7 @@ def materialize_mcp_tools_for_specialist(
|
|||
continue
|
||||
if binding_server_ids is not None and sp and server_id not in binding_server_ids:
|
||||
continue
|
||||
env_allowlist, env_defaults = _mcp_row_env_config(row)
|
||||
raw_args = [x for x in (row.get("entry_args") or []) if isinstance(x, str)]
|
||||
command = build_mcp_process_command(
|
||||
cmd,
|
||||
|
|
@ -161,6 +191,7 @@ def materialize_mcp_tools_for_specialist(
|
|||
timeout_s=float(row.get("timeout_s") or 30.0),
|
||||
required_permissions=frozenset(str(x) for x in (row.get("required_permissions") or [])),
|
||||
env_allowlist=env_allowlist,
|
||||
env_defaults=env_defaults,
|
||||
).to_spec()
|
||||
out.append(spec)
|
||||
return out
|
||||
|
|
|
|||
|
|
@ -61,6 +61,8 @@ def _install_command(manifest: McpServerManifest) -> list[str]:
|
|||
if manifest.source_type == "pypi":
|
||||
pkg = manifest.source_ref + (f"=={manifest.version}" if manifest.version else "")
|
||||
return [sys.executable, "-m", "pip", "install", pkg]
|
||||
if manifest.source_type == "local":
|
||||
return []
|
||||
raise ValueError(f"unsupported_source_type:{manifest.source_type}")
|
||||
|
||||
|
||||
|
|
@ -85,6 +87,8 @@ def _uninstall_command(manifest: McpServerManifest) -> list[str]:
|
|||
return [sys.executable, "-m", "pip", "uninstall", "-y", str(manifest.source_ref or "").strip()]
|
||||
if manifest.source_type == "github":
|
||||
return []
|
||||
if manifest.source_type == "local":
|
||||
return []
|
||||
raise ValueError(f"unsupported_source_type:{manifest.source_type}")
|
||||
|
||||
|
||||
|
|
@ -93,6 +97,12 @@ def install_mcp_server(manifest: McpServerManifest, *, dry_run: bool = False) ->
|
|||
cmd = _install_command(manifest)
|
||||
except Exception as exc:
|
||||
return McpInstallResult(ok=False, error_code="mcp_invalid_source", error=str(exc))
|
||||
if manifest.source_type == "local" and not cmd:
|
||||
return McpInstallResult(
|
||||
ok=True,
|
||||
install_command="",
|
||||
details={"skipped": True, "reason": "local_source_no_package_install"},
|
||||
)
|
||||
cmd_text = " ".join(cmd)
|
||||
if dry_run:
|
||||
return McpInstallResult(ok=True, install_command=cmd_text, details={"dry_run": True})
|
||||
|
|
|
|||
|
|
@ -16,13 +16,17 @@ class McpProcessRuntime:
|
|||
command: list[str]
|
||||
timeout_s: float = 30.0
|
||||
env_allowlist: list[str] | None = None
|
||||
env_defaults: dict[str, str] | None = None
|
||||
_proc: subprocess.Popen[str] | None = None
|
||||
_lock: threading.Lock = field(default_factory=threading.Lock)
|
||||
_initialized: bool = False
|
||||
_request_id: int = 0
|
||||
|
||||
@staticmethod
|
||||
def _build_runtime_env(env_allowlist: list[str] | None) -> dict[str, str] | None:
|
||||
def _build_runtime_env(
|
||||
env_allowlist: list[str] | None,
|
||||
env_defaults: dict[str, str] | None = None,
|
||||
) -> dict[str, str] | None:
|
||||
if env_allowlist is None:
|
||||
return None
|
||||
from runtime.operations.mcp_env import mcp_local_env_merged
|
||||
|
|
@ -46,6 +50,16 @@ class McpProcessRuntime:
|
|||
key = str(k or "").strip()
|
||||
if key and key in os.environ and str(os.environ[key] or "").strip():
|
||||
env[key] = os.environ[key]
|
||||
for k, v in (env_defaults or {}).items():
|
||||
key = str(k or "").strip()
|
||||
val = str(v or "").strip()
|
||||
if not key or not val:
|
||||
continue
|
||||
if key not in env or not str(env.get(key) or "").strip():
|
||||
env[key] = val
|
||||
# MCP JSON-RPC on stdio is UTF-8; force child Python off Windows GBK console encoding.
|
||||
env.setdefault("PYTHONIOENCODING", "utf-8")
|
||||
env.setdefault("PYTHONUTF8", "1")
|
||||
return env
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -68,11 +82,19 @@ class McpProcessRuntime:
|
|||
def start(self) -> None:
|
||||
if self._proc and self._proc.poll() is None:
|
||||
return
|
||||
env = self._build_runtime_env(self.env_allowlist)
|
||||
env = self._build_runtime_env(self.env_allowlist, self.env_defaults)
|
||||
cmd = list(self.command or [])
|
||||
if cmd:
|
||||
cmd[0] = self._resolve_command(str(cmd[0]), env)
|
||||
popen_kwargs: dict[str, Any] = {"stdin": subprocess.PIPE, "stdout": subprocess.PIPE, "stderr": subprocess.PIPE, "text": True, "encoding": "utf-8", "env": env}
|
||||
popen_kwargs: dict[str, Any] = {
|
||||
"stdin": subprocess.PIPE,
|
||||
"stdout": subprocess.PIPE,
|
||||
"stderr": subprocess.PIPE,
|
||||
"text": True,
|
||||
"encoding": "utf-8",
|
||||
"errors": "replace",
|
||||
"env": env,
|
||||
}
|
||||
if os.name == "nt":
|
||||
startupinfo = subprocess.STARTUPINFO()
|
||||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue