MCP应用程序监控服务器
增强的MCP服务器,用于跟踪应用程序使用数据,具有模块化架构、安全功能和全面的工具。
🚀 特性
- ✅ 模块化架构:通过专用模块实现关注点的清晰分离
- ✅ SQLite数据库:具有约束和索引的稳健模式
- ✅ 安全第一:速率限制、输入验证和审核日志记录
- ✅ 自动备份:可配置的数据库备份系统
- ✅ 健康监测:全面的日志记录和错误处理
- ✅ 开发工具:清理脚本和实用程序
- ✅ 交叉平台的:适用于Windows、macOS和Linux
📁 项目结构
mcp-app-monitor-server/
├── main.py # New entry point (start_mcp_server)
├── app_monitor_server.py # Original monolithic file (kept for reference)
├── requirements.txt # Dependencies
├── .env.template # Environment configuration template
├── .gitignore # Git ignore rules
├── README.md # This documentation
│
├── config/ # Configuration management
│ ├── __init__.py
│ └── settings.py # Config class and environment variables
│
├── database/ # Database layer
│ ├── __init__.py
│ └── connection.py # Database connection, initialization, backup
│
├── server/ # MCP Server components
│ ├── __init__.py
│ ├── mcp_server.py # FastMCP server setup and tools registration
│ └── decorators.py # Security decorators (rate limiting, audit, validation)
│
├── utils/ # Utilities
│ ├── __init__.py
│ └── logging_utils.py # Logging setup and configuration
│
├── scripts/ # Utility scripts
│ ├── cleanup.py # Python cleanup script (removes __pycache__, etc.)
│ ├── cleanup.bat # Windows batch wrapper
│ ├── cleanup.ps1 # PowerShell wrapper
│ └── README.md # Scripts documentation
│
├── data/ # Database files
│ ├── app_monitor.db # Main database (created at runtime)
│ └── backups/ # Database backups
│ └── .gitkeep
│
└── logs/ # Log files
├── mcp_server.log # Server logs (created when audit logging enabled)
└── .gitkeep🚀 快速开始
先决条件
- Python 3.7+
- FastMCP:
pip install fastmcp
安装和设置
- 克隆和设置:
git clone
cd mcp-app-monitor-server
pip install -r requirements.txt- 配置环境 (可选):
cp .env.template .env
# Edit .env with your preferred settings- 启动服务器:
python main.py- 使用自定义设置:
# Debug mode
python main.py --log-level DEBUG
# Custom limits
python main.py --max-records 500🛠️ 可用的MCP工具
服务器提供以下MCP工具用于应用程序监控:
核心数据操作
insert_app_usage_record()-添加新的应用程序使用数据delete_app_usage_record(id)-按ID删除记录get_all_app_usage_records(limit)-检索所有分页记录get_app_usage_by_user(user, limit)-按用户名筛选记录get_database_stats()-获取全面的使用统计数据
示例用法
# Insert a new record
insert_app_usage_record(
monitor_app_version="1.0.0",
platform="Windows",
user="john_doe",
application_name="VS Code",
application_version="1.85.0",
log_date="2025-01-15T10:30:00Z",
legacy_app=False,
duration_seconds=3600
)
# Get user-specific data
get_app_usage_by_user("john_doe", limit=50)
# Get system statistics
get_database_stats()⚙️ 配置
环境变量
创建一个 .env 从模板中选择文件并进行自定义:
# Logging Configuration
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, ERROR
ENABLE_AUDIT_LOG=true # Enable detailed audit logging
# Database Configuration
DB_BACKUP_ENABLED=true # Enable automatic backups
DB_BACKUP_INTERVAL=3600 # Backup interval in seconds (1 hour)
# Security Configuration
RATE_LIMIT_REQUESTS=100 # Max requests per window
RATE_LIMIT_WINDOW=60 # Rate limit window in seconds
MAX_QUERY_RESULTS=1000 # Maximum records per query
ADMIN_USER=admin # Admin username
# Optional Features
DB_ENCRYPTION_KEY= # Database encryption (future feature)命令行选项
python main.py --help
Options:
--log-level {DEBUG,INFO,WARNING,ERROR} Set logging level
--max-records MAX_RECORDS Maximum records per query
--timeout TIMEOUT Server timeout in seconds🧹 开发工具
清理脚本
该项目包括在 scripts/ 目录:
基本用法
# Clean Python cache files (__pycache__, .pyc)
python scripts/cleanup.py
# Preview what would be cleaned (safe)
python scripts/cleanup.py --dry-run
# Clean everything (cache + logs + temp files)
python scripts/cleanup.py --all平台特定脚本
# Windows Batch
scripts\cleanup.bat --dry-run
# PowerShell
.\scripts\cleanup.ps1 -DryRun -All什么被清理
始终清理(Python缓存):
__pycache__/目录和内容.pyc,.pyo,.pyd文件
可选配 --logs:
*.log文件在logs/目录
可选配 --temp:
.DS_Store(macOS),Thumbs.db(Windows)- 编辑器备份文件(
*~) - 系统临时文件(
*.tmp,*.temp)
开发工作流集成
VS代码任务 (添加到 .vscode/tasks.json):
{
"label": "Clean Python Cache",
"type": "shell",
"command": "python",
"args": ["scripts/cleanup.py"],
"group": "build"
}预提交钩子 (添加到 .git/hooks/pre-commit):
#!/bin/sh
python scripts/cleanup.py🏗️ 建筑与设计
关键架构优势
✅ 关注点分离:每个模块都有一个单一的职责\ ✅ 可维护性:小而集中的文件,而不是单片代码\ ✅ 可测试性:模块可以独立测试\ ✅ 安全:独立的安全功能(速率限制、验证、审计)\ ✅ 可扩展性:易于添加新功能,而不会影响现有代码\ ✅ 配置管理:具有环境支持的集中设置
模块职责
config/:应用程序配置和环境管理database/:数据库连接、架构和备份操作server/:MCP服务器设置、工具注册和安全装饰器utils/:常见实用程序,如日志记录和辅助函数scripts/:开发和维护公用设施
安全功能
- 速率限制:每个时间窗口的可配置请求限制
- 输入验证:自动消毒和长度检查
- 审计日志:详细的操作日志记录以确保合规性
- 安全数据库:连接超时和pragma设置
- 错误处理:优雅的错误处理,无信息泄露
🔄 从单片设计迁移
该项目成功地从单文件单片设计重构为模块化架构,同时保持:
- 完全向后兼容性:所有MCP工具的工作原理完全相同
- 保留功能:原始设计的每一个特征
- 增强的安全性:维护和改进所有安全功能
- 更好的性能:优化了数据库操作和连接
原版 app_monitor_server.py 保留以供参考。
📊 数据库模式
-- Main application usage table
CREATE TABLE app_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
monitor_app_version TEXT NOT NULL,
platform TEXT NOT NULL,
user TEXT NOT NULL,
application_name TEXT NOT NULL,
application_version TEXT NOT NULL,
log_date TEXT NOT NULL,
legacy_app BOOLEAN NOT NULL,
duration_seconds INTEGER NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Audit log table (if enabled)
CREATE TABLE audit_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT NOT NULL,
table_name TEXT NOT NULL,
record_id INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
details TEXT
);🚀 未来的增强功能
模块化基础可轻松添加:
- 单元测试:每个模块的综合测试套件
- 服务层:用于复杂操作的业务逻辑服务
- API文档:从MCP工具定义自动生成文档
- 健康监测:端点健康检查和指标收集
- CI/CD管道:自动化测试和部署工作流程
- 数据库迁移:架构版本管理
- 性能指标:查询优化和监控
- 认证:用户身份验证和授权
🤝 贡献
- 分叉存储库
- 创建要素分支:
git checkout -b feature/amazing-feature - 提交前运行清理:
python scripts/cleanup.py - 提交更改:
git commit -m 'Add amazing feature' - 推送到分支:
git push origin feature/amazing-feature - 打开拉取请求
📝 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
🛠️ 故障排除
常见问题
服务器无法启动:
- 检查Python版本(需要3.7+)
- 验证FastMCP安装:
pip install fastmcp - 检查中的日志文件
logs/目录
数据库错误:
- 确保
data/目录具有写入权限 - 检查数据库和备份的磁盘空间
- 查看审核日志表中的数据库日志
利率限制问题:
- 调整
RATE_LIMIT_REQUESTS和RATE_LIMIT_WINDOW在……里面.env - 检查中的当前速率限制状态
get_database_stats()
调试
启用调试日志记录以进行详细的故障排除:
python main.py --log-level DEBUG或设置在环境中:
export LOG_LEVEL=DEBUG
python main.py