mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-11 09:20:49 +08:00
重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
This commit is contained in:
parent
ba3836f00f
commit
4a23b715a2
498 changed files with 2760 additions and 2200 deletions
97
runtime/operations/mcp_env.py
Normal file
97
runtime/operations/mcp_env.py
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
from __future__ import annotations
|
||||
|
||||
"""MCP 相关进程环境:默认 allowlist + 可选本地 env 文件(首选 `oclaw/_local/mcp_local.env`)。"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from oclaw.platform.config.paths import PROJECT_ROOT, db_path
|
||||
|
||||
_DEFAULT_ALLOWLIST = (
|
||||
"BRAVE_API_KEY,GOOGLE_OAUTH_CREDENTIALS,GOOGLE_CALENDAR_MCP_TOKEN_PATH,"
|
||||
"GITHUB_PERSONAL_ACCESS_TOKEN,CONTEXT7_API_KEY"
|
||||
)
|
||||
|
||||
|
||||
def mcp_env_allowlist_keys() -> list[str]:
|
||||
raw = str(os.getenv("AIA_MCP_ENV_ALLOWLIST") or "").strip()
|
||||
if not raw:
|
||||
return [x.strip() for x in _DEFAULT_ALLOWLIST.split(",") if x.strip()]
|
||||
return [x.strip() for x in raw.split(",") if x.strip()]
|
||||
|
||||
|
||||
def _parse_env_file(path: Path) -> dict[str, str]:
|
||||
out: dict[str, str] = {}
|
||||
if not path.is_file():
|
||||
return out
|
||||
try:
|
||||
raw = path.read_text(encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
return out
|
||||
for line in raw.splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
if "=" not in line:
|
||||
continue
|
||||
k, _, v = line.partition("=")
|
||||
k = k.strip()
|
||||
v = v.strip().strip('"').strip("'")
|
||||
if k:
|
||||
out[k] = _expand_env_value(v)
|
||||
return out
|
||||
|
||||
|
||||
def _expand_env_value(v: str) -> str:
|
||||
s = v.strip()
|
||||
if "${PROJECT_ROOT}" in s:
|
||||
rel = s.split("${PROJECT_ROOT}", 1)[1].lstrip("/\\")
|
||||
s = str((PROJECT_ROOT / rel).resolve())
|
||||
return os.path.expandvars(s)
|
||||
|
||||
|
||||
def _mcp_local_env_paths_in_load_order() -> list[Path]:
|
||||
return [
|
||||
Path(db_path()).resolve().parent / "mcp_local.env",
|
||||
(PROJECT_ROOT / "oclaw" / "_local" / "mcp_local.env").resolve(),
|
||||
]
|
||||
|
||||
|
||||
def mcp_local_env_merged() -> dict[str, str]:
|
||||
out: dict[str, str] = {}
|
||||
for p in _mcp_local_env_paths_in_load_order():
|
||||
if p.is_file():
|
||||
out.update(_parse_env_file(p))
|
||||
return out
|
||||
|
||||
|
||||
def mcp_local_env_file_path() -> Path:
|
||||
return (PROJECT_ROOT / "oclaw" / "_local" / "mcp_local.env").resolve()
|
||||
|
||||
|
||||
def gateway_mcp_env_extras() -> dict[str, str]:
|
||||
extra: dict[str, str] = {}
|
||||
has_aia = str(os.getenv("AIA_MCP_ENV_ALLOWLIST") or "").strip()
|
||||
if not has_aia:
|
||||
extra["AIA_MCP_ENV_ALLOWLIST"] = _DEFAULT_ALLOWLIST
|
||||
file_vals = mcp_local_env_merged()
|
||||
for k, v in file_vals.items():
|
||||
if not v.strip():
|
||||
continue
|
||||
if not str(os.getenv(k) or "").strip():
|
||||
extra[k] = v
|
||||
return extra
|
||||
|
||||
|
||||
def apply_gateway_mcp_env_to_os() -> None:
|
||||
for k, v in gateway_mcp_env_extras().items():
|
||||
os.environ[k] = v
|
||||
|
||||
|
||||
__all__ = [
|
||||
"apply_gateway_mcp_env_to_os",
|
||||
"gateway_mcp_env_extras",
|
||||
"mcp_env_allowlist_keys",
|
||||
"mcp_local_env_file_path",
|
||||
"mcp_local_env_merged",
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue