mcp-supervisor-core 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_supervisor_core-0.1.0/.gitignore +89 -0
- mcp_supervisor_core-0.1.0/PKG-INFO +6 -0
- mcp_supervisor_core-0.1.0/README.md +87 -0
- mcp_supervisor_core-0.1.0/pyproject.toml +13 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/__init__.py +7 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/debug.py +141 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/error_handler.py +450 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/i18n.py +392 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/log_writer.py +478 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/memory_monitor.py +521 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/paths.py +30 -0
- mcp_supervisor_core-0.1.0/src/mcp_supervisor_core/resource_manager.py +801 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
# Virtual Environment
|
|
24
|
+
venv/
|
|
25
|
+
env/
|
|
26
|
+
ENV/
|
|
27
|
+
.venv
|
|
28
|
+
|
|
29
|
+
# IDE
|
|
30
|
+
.vscode/
|
|
31
|
+
.idea/
|
|
32
|
+
*.swp
|
|
33
|
+
*.swo
|
|
34
|
+
*~
|
|
35
|
+
|
|
36
|
+
# OS
|
|
37
|
+
.DS_Store
|
|
38
|
+
Thumbs.db
|
|
39
|
+
|
|
40
|
+
# Logs
|
|
41
|
+
*.log
|
|
42
|
+
logs/
|
|
43
|
+
|
|
44
|
+
# Test
|
|
45
|
+
.pytest_cache/
|
|
46
|
+
.coverage
|
|
47
|
+
htmlcov/
|
|
48
|
+
|
|
49
|
+
# Rust / Tauri
|
|
50
|
+
packages/desktop/target/
|
|
51
|
+
packages/desktop/gen/
|
|
52
|
+
|
|
53
|
+
# MCP specific
|
|
54
|
+
.mcp/
|
|
55
|
+
*.mcp.json
|
|
56
|
+
|
|
57
|
+
# Workbench UI
|
|
58
|
+
packages/workbench-ui/node_modules/
|
|
59
|
+
packages/workbench-ui/dist/
|
|
60
|
+
packages/workbench-server/src/mcp_supervisor_workbench/static/assets/
|
|
61
|
+
packages/workbench-server/src/mcp_supervisor_workbench/static/index.html
|
|
62
|
+
src/mcp_ai_supervisor/workbench/static/assets/
|
|
63
|
+
src/mcp_ai_supervisor/workbench/static/index.html
|
|
64
|
+
|
|
65
|
+
# Node modules 兜底 — 任何子目录下的 node_modules 都不入库,防止未来重现 dashboard-ui/node_modules 这种事故
|
|
66
|
+
**/node_modules/
|
|
67
|
+
|
|
68
|
+
# Reference repositories (top-level only)
|
|
69
|
+
/references/
|
|
70
|
+
# Allow skill references subdirectories
|
|
71
|
+
!.cursor/skills/**/references/
|
|
72
|
+
!packages/workbench-server/resources/skills/**/references/
|
|
73
|
+
# tgit-cursor-hook-scaffold
|
|
74
|
+
.cursor/hooks.json
|
|
75
|
+
.cursor/hooks/after-file-edit.js
|
|
76
|
+
# tgit-cursor-hook-logs
|
|
77
|
+
.cursor/hooks/logs/
|
|
78
|
+
|
|
79
|
+
# Generated reports
|
|
80
|
+
code-stats-report.md
|
|
81
|
+
|
|
82
|
+
# PyPI credentials
|
|
83
|
+
.pypi-credentials
|
|
84
|
+
|
|
85
|
+
# cursor-memory
|
|
86
|
+
.cursor/memory/
|
|
87
|
+
.cursor/hooks/capture-user-message.sh
|
|
88
|
+
.cursor/hooks/capture-feedback-message.sh
|
|
89
|
+
.cursor/rules/memory-system.mdc
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# mcp-supervisor-core
|
|
2
|
+
|
|
3
|
+
MCP AI Supervisor 基础设施包,提供日志、路径、国际化、错误处理等底层工具。被 `mcp-server` 和 `workbench-server` 共同依赖。
|
|
4
|
+
|
|
5
|
+
## 包信息
|
|
6
|
+
|
|
7
|
+
- **包名**: `mcp-supervisor-core`
|
|
8
|
+
- **模块名**: `mcp_supervisor_core`
|
|
9
|
+
- **版本**: 0.1.0
|
|
10
|
+
- **依赖**: `psutil>=7.0.0`
|
|
11
|
+
|
|
12
|
+
## 模块结构
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
mcp_supervisor_core/
|
|
16
|
+
├── __init__.py # 包入口,导出版本号
|
|
17
|
+
├── log_writer.py # 结构化日志系统(核心模块)
|
|
18
|
+
├── paths.py # 路径标准化和项目哈希
|
|
19
|
+
├── i18n.py # 国际化(多语言支持)
|
|
20
|
+
├── error_handler.py # 统一错误处理
|
|
21
|
+
├── resource_manager.py # 资源管理(文件/目录生命周期)
|
|
22
|
+
├── memory_monitor.py # 内存使用监控
|
|
23
|
+
└── debug.py # 调试工具
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 核心模块
|
|
27
|
+
|
|
28
|
+
### `log_writer` — 结构化日志系统
|
|
29
|
+
|
|
30
|
+
所有进程共用的日志模块,替代散落的 `logging`/`print` 调用。
|
|
31
|
+
|
|
32
|
+
**特性**:
|
|
33
|
+
- 按进程类型(`agent`/`dashboard`/`tauri`)分目录存储
|
|
34
|
+
- 按天自动轮转,过期日志自动清理(默认 30 天)
|
|
35
|
+
- 结构化格式:`{timestamp} [{level}] [{module}] [{context}] {message}`
|
|
36
|
+
- 线程安全,支持上下文预绑定(`bind`)
|
|
37
|
+
|
|
38
|
+
**核心类**:`SupervisorLogger`
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from mcp_supervisor_core.log_writer import get_logger
|
|
42
|
+
|
|
43
|
+
logger = get_logger("agent", project_directory="/path/to/project")
|
|
44
|
+
session_logger = logger.bind(project="myapp", session="abc123")
|
|
45
|
+
session_logger.info("feedback", "[SESSION_CREATE] 新会话创建")
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### `paths` — 路径工具
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from mcp_supervisor_core.paths import normalize_path, project_hash
|
|
52
|
+
|
|
53
|
+
normalized = normalize_path("~/projects/my-app") # → /Users/xxx/projects/my-app
|
|
54
|
+
hash_id = project_hash("/Users/xxx/projects/my-app") # → "a1b2c3d4_my-app"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `i18n` — 国际化
|
|
58
|
+
|
|
59
|
+
提供多语言支持(中文、英文、繁体中文),根据环境变量 `MCP_LANGUAGE` 自动选择语言。
|
|
60
|
+
|
|
61
|
+
### `error_handler` — 错误处理
|
|
62
|
+
|
|
63
|
+
统一的异常捕获和格式化,用于 MCP 工具调用中的错误响应生成。
|
|
64
|
+
|
|
65
|
+
### `memory_monitor` — 内存监控
|
|
66
|
+
|
|
67
|
+
监控当前进程的内存使用,在内存超限时发出警告。
|
|
68
|
+
|
|
69
|
+
### `resource_manager` — 资源管理
|
|
70
|
+
|
|
71
|
+
管理临时文件和目录的生命周期(创建、清理、过期检测)。
|
|
72
|
+
|
|
73
|
+
## 安装
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 作为 workspace 依赖(推荐)
|
|
77
|
+
uv pip install -e packages/core
|
|
78
|
+
|
|
79
|
+
# 在其他包的 pyproject.toml 中引用
|
|
80
|
+
# dependencies = ["mcp-supervisor-core>=0.1.0"]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## 设计原则
|
|
84
|
+
|
|
85
|
+
1. **零外部依赖**(除 `psutil`):确保所有依赖此包的上层模块不会因 core 引入额外依赖
|
|
86
|
+
2. **纯工具性质**:不含业务逻辑,只提供基础设施
|
|
87
|
+
3. **线程安全**:所有模块均考虑多线程/多进程场景
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mcp-supervisor-core"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "MCP AI Supervisor 基础设施(日志、路径、错误处理、国际化、资源管理)"
|
|
5
|
+
requires-python = ">=3.11"
|
|
6
|
+
dependencies = ["psutil>=7.0.0"]
|
|
7
|
+
|
|
8
|
+
[build-system]
|
|
9
|
+
requires = ["hatchling"]
|
|
10
|
+
build-backend = "hatchling.build"
|
|
11
|
+
|
|
12
|
+
[tool.hatch.build.targets.wheel]
|
|
13
|
+
packages = ["src/mcp_supervisor_core"]
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
統一調試日誌模組
|
|
4
|
+
================
|
|
5
|
+
|
|
6
|
+
提供統一的調試日誌功能,確保調試輸出不會干擾 MCP 通信。
|
|
7
|
+
所有調試輸出都會發送到 stderr,並且只在調試模式啟用時才輸出。
|
|
8
|
+
|
|
9
|
+
使用方法:
|
|
10
|
+
```python
|
|
11
|
+
from .debug import debug_log
|
|
12
|
+
|
|
13
|
+
debug_log("這是一條調試信息")
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
環境變數控制:
|
|
17
|
+
- MCP_DEBUG=true/1/yes/on: 啟用調試模式
|
|
18
|
+
- MCP_DEBUG=false/0/no/off: 關閉調試模式(默認)
|
|
19
|
+
|
|
20
|
+
作者: Minidoracat
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import os
|
|
24
|
+
import sys
|
|
25
|
+
from datetime import datetime
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Any
|
|
28
|
+
|
|
29
|
+
# === 文件日志句柄(懒初始化,按日期自动切换) ===
|
|
30
|
+
_log_file = None
|
|
31
|
+
_log_date: str = ""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _get_file_logger():
|
|
35
|
+
"""获取当日的文件日志句柄,日期变化时自动切换新文件。"""
|
|
36
|
+
global _log_file, _log_date
|
|
37
|
+
today = datetime.now().strftime("%Y-%m-%d")
|
|
38
|
+
if _log_file is not None and _log_date == today:
|
|
39
|
+
return _log_file
|
|
40
|
+
# 关闭旧句柄
|
|
41
|
+
if _log_file is not None:
|
|
42
|
+
try:
|
|
43
|
+
_log_file.close()
|
|
44
|
+
except Exception:
|
|
45
|
+
pass
|
|
46
|
+
try:
|
|
47
|
+
log_dir = Path.home() / ".mcp" / "logs"
|
|
48
|
+
log_dir.mkdir(parents=True, exist_ok=True)
|
|
49
|
+
_log_file = open(log_dir / f"mcp-supervisor-{today}.log", "a", encoding="utf-8")
|
|
50
|
+
_log_date = today
|
|
51
|
+
except Exception:
|
|
52
|
+
_log_file = None
|
|
53
|
+
return _log_file
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def debug_log(message: Any, prefix: str = "DEBUG") -> None:
|
|
57
|
+
"""
|
|
58
|
+
输出调试信息到 stderr + 文件日志,避免污染 stdout(MCP JSON-RPC 通信通道)
|
|
59
|
+
|
|
60
|
+
- **stderr**: 仅在 ``MCP_DEBUG=true`` 时输出,供 IDE 捕获
|
|
61
|
+
- **文件日志**: 始终写入 ``~/.mcp/logs/mcp-supervisor-YYYY-MM-DD.log``,
|
|
62
|
+
确保即使 IDE 不捕获 stderr 也有日志可查
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
message: 要输出的调试信息
|
|
66
|
+
prefix: 调试信息的前缀标识,默认为 "DEBUG"
|
|
67
|
+
"""
|
|
68
|
+
try:
|
|
69
|
+
if not isinstance(message, str):
|
|
70
|
+
message = str(message)
|
|
71
|
+
except Exception:
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
75
|
+
formatted = f"[{prefix}] {message}"
|
|
76
|
+
|
|
77
|
+
# === 写入 stderr(仅 MCP_DEBUG 开启时) ===
|
|
78
|
+
if os.getenv("MCP_DEBUG", "").lower() in ("true", "1", "yes", "on"):
|
|
79
|
+
try:
|
|
80
|
+
print(formatted, file=sys.stderr, flush=True)
|
|
81
|
+
except UnicodeEncodeError:
|
|
82
|
+
safe = message.encode("ascii", errors="replace").decode("ascii")
|
|
83
|
+
print(f"[{prefix}] {safe}", file=sys.stderr, flush=True)
|
|
84
|
+
except Exception:
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
# === 写入文件日志(始终写入) ===
|
|
88
|
+
try:
|
|
89
|
+
fh = _get_file_logger()
|
|
90
|
+
if fh is not None:
|
|
91
|
+
fh.write(f"{timestamp} {formatted}\n")
|
|
92
|
+
fh.flush()
|
|
93
|
+
except Exception:
|
|
94
|
+
pass
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def i18n_debug_log(message: Any) -> None:
|
|
98
|
+
"""國際化模組專用的調試日誌"""
|
|
99
|
+
debug_log(message, "I18N")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def server_debug_log(message: Any) -> None:
|
|
103
|
+
"""伺服器模組專用的調試日誌"""
|
|
104
|
+
debug_log(message, "SERVER")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def web_debug_log(message: Any) -> None:
|
|
108
|
+
"""Web UI 模組專用的調試日誌"""
|
|
109
|
+
debug_log(message, "WEB")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# === 日志限流器 ===
|
|
113
|
+
_rate_limit_last: dict[str, float] = {}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def debug_log_throttled(
|
|
117
|
+
message: Any,
|
|
118
|
+
prefix: str = "DEBUG",
|
|
119
|
+
*,
|
|
120
|
+
key: str,
|
|
121
|
+
interval_seconds: float = 60.0,
|
|
122
|
+
) -> None:
|
|
123
|
+
"""限流版 debug_log:同一 key 在 interval_seconds 内最多输出一次。"""
|
|
124
|
+
import time
|
|
125
|
+
|
|
126
|
+
now = time.monotonic()
|
|
127
|
+
last = _rate_limit_last.get(key, 0.0)
|
|
128
|
+
if now - last < interval_seconds:
|
|
129
|
+
return
|
|
130
|
+
_rate_limit_last[key] = now
|
|
131
|
+
debug_log(message, prefix)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def is_debug_enabled() -> bool:
|
|
135
|
+
"""檢查是否啟用了調試模式"""
|
|
136
|
+
return os.getenv("MCP_DEBUG", "").lower() in ("true", "1", "yes", "on")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def set_debug_mode(enabled: bool) -> None:
|
|
140
|
+
"""設置調試模式(用於測試)"""
|
|
141
|
+
os.environ["MCP_DEBUG"] = "true" if enabled else "false"
|