MCPConsoleServer-模型上下文协议控制台服务器
用于控制台和命令执行操作的模型上下文协议(MCP)的C#实现,为LLM提供了以下能力:
- 创建持久控制台会话 在命令之间保持状态
- 使用环境持久性在CMD、PowerShell和WSL中执行命令
- 管理系统流程并获取全面的系统信息
- 处理长时间运行的操作而不超时(如安装)
🔄 持久会话-关键功能
主要优势:创建控制台会话 在命令之间保持不变,维护:
- ✅ 工作目录更改
- ✅ 环境变量
- ✅ 已安装软件状态
- ✅ 会话特定配置
- ✅ 长时间运行的流程 (安装、建造等)
之前vs之后
# OLD WAY: Each command is isolated
execute_cmd("cd /d C:\Projects") # Changes directory
execute_cmd("dir") # Back to original directory! ❌
# NEW WAY: Session maintains state
create_session("dev", "CMD") # Create persistent session
execute_in_session("dev", "cd /d C:\Projects") # Change directory
execute_in_session("dev", "dir") # Still in C:\Projects! ✅⚠️ 安全警告
此MCP服务器提供强大的系统访问功能。 请务必谨慎使用!
安全功能
- 命令筛选:默认情况下,危险命令被阻止
- 过程保护:不能终止关键系统进程
- 会话隔离:每个会话独立运行
- 安全模式:默认情况下在安全模式下运行(禁用危险命令)
启用危险命令(使用风险自负)
MCPConsoleServer.exe --allow-dangerous true可用工具(共29个)
🖥️ 会话管理(8个工具)
create_session-创建持久控制台会话execute_in_session-在会话中执行命令(保持状态!)list_sessions-查看所有活动会话get_session_info-获取详细的会话信息close_session-关闭特定会话cleanup_sessions-清理非活动会话get_session_directory-获取会话的当前目录change_session_directory-在会话中更改目录execute_batch_in_session-按顺序运行多个命令
💻 直接控制台操作(6个工具)
execute_cmd-运行单个CMD命令(非持久性)execute_powershell-执行PowerShell脚本(非持久性)execute_wsl-执行Linux命令(非持久性)get_current_directory-获取当前工作目录change_directory-更改工作目录list_directory-列出文件和目录
🔧 流程管理(6个工具)
list_processes-查看所有正在运行的进程get_process_info-获取详细的流程信息find_processes-按名称搜索进程kill_process-终止流程(进行安全检查)get_top_processes-CPU使用率最高的进程get_top_memory_processes-按内存使用情况排列的顶级进程
📊 系统信息(9个工具)
get_system_info-完整的系统概述check_wsl-WSL可用性和分布get_environment_variables-环境变量get_performance_info-CPU、内存、性能指标get_drive_info-磁盘空间信息get_powershell_version-PowerShell版本get_network_info-网络配置get_windows_features-Windows功能(需要管理员)get_event_log-系统事件日志
🚀 使用示例
开发环境设置
# Create a development session
create_session("dev-env", "PowerShell", workingDirectory="C:\Projects")
# Set up the environment (all in the same session!)
execute_in_session("dev-env", "Set-ExecutionPolicy RemoteSigned -Scope Process")
execute_in_session("dev-env", "cd MyProject")
execute_in_session("dev-env", "npm install") # Long-running - no timeout!
execute_in_session("dev-env", "dotnet restore")
# Later, continue working in the same environment
execute_in_session("dev-env", "npm run build")
execute_in_session("dev-env", "dotnet run")长期运行安装
# Create session for software installation
create_session("install-node", "CMD")
# Install Node.js (takes several minutes)
execute_in_session("install-node", "winget install OpenJS.NodeJS", timeoutSeconds=0)
# Verify installation in same environment
execute_in_session("install-node", "node --version")
execute_in_session("install-node", "npm --version")WSL开发环境
# Create WSL session with specific distribution
create_session("ubuntu-dev", "WSL", wslDistribution="Ubuntu")
# Set up development environment
execute_in_session("ubuntu-dev", "cd /home/user/projects")
execute_in_session("ubuntu-dev", "export NODE_ENV=development")
execute_in_session("ubuntu-dev", "sudo apt update") # No timeout needed
execute_in_session("ubuntu-dev", "sudo apt install -y docker.io")
# Continue working in the same environment
execute_in_session("ubuntu-dev", "docker --version")批量操作
# Execute multiple commands in sequence
execute_batch_in_session("dev-env", [
"git pull origin main",
"npm ci",
"npm run test",
"npm run build"
], stopOnError=true)🔧 命令行选项
MCPConsoleServer.exe [options]
Options:
--allow-dangerous Enable dangerous command execution (default: false)
--timeout Set default command timeout (default: 30)示例用法
# Safe mode with custom timeout
MCPConsoleServer.exe --timeout 60
# Enable dangerous commands (use with caution!)
MCPConsoleServer.exe --allow-dangerous true --timeout 120📁 配置文件
Claude桌面配置
{
"mcpServers": {
"console": {
"command": "path/to/MCPConsoleServer.exe",
"args": ["--timeout", "60"]
}
}
}mcpo(OpenAPI代理)配置
# Basic usage
uvx mcpo --port 8000 --api-key "your-key" -- path/to/MCPConsoleServer.exe
# With configuration file
mcpo --config mcpo-config.json --port 8000 --api-key "your-key"🔐 安全考虑
已阻止的命令(默认)
- 系统关机/重启命令
- 文件系统销毁命令(rm-rf、format等)
- 注册表操纵
- 网络攻击工具
- 广泛的过程杀戮
- 磁盘格式化操作
- 服务操纵
- 危险的PowerShell操作
进程终止保护
关键系统进程受到保护:
- 系统、smss、csrss、wininet、winlogon
- 服务、lsass、svchost、dwm、资源管理器
- 其他关键的Windows系统进程
会话安全
- 每个会话都在自己的进程中运行
- 会话不共享环境变量
- 命令筛选适用于所有会话
- 会话可以单独终止
💡 最佳实践
会话管理
- 使用描述性名称:
create_session("node-build", "CMD") - 完成后进行清理:
close_session("temp-session") - 检查会话状态:
list_sessions()在执行命令之前 - 使用合适的外壳:PowerShell用于Windows管理,WSL用于Linux工具
长期运行操作
- 集
timeoutSeconds=0用于安装和构建 - 在长时间操作期间监视会话状态
- 对多步骤流程使用批处理执行
安全
- 在安全模式下运行:除非需要,否则禁用危险命令
- 会话隔离:为不同的任务使用单独的会话
- 定期清理:使用
cleanup_sessions()删除无效会话 - 监视器使用情况:定期审查会议活动
🏗️ 建设项目
# Build and publish
cd MCPConsoleServer
build.bat
# Test the build
test.bat📦 依赖项
- .NET 9.0
- 模型上下文协议SDK
- 系统。管理(用于过程管理)
- 微软。扩展。托管
🎯 关键优势
- 环境持久性 -会话维护命令之间的状态
- 长期支持 -安装和构建没有超时
- 多壳支持 -CMD、PowerShell和WSL处于持久会话中
- 开发工作流程 -非常适合开发环境设置
- 会话管理 -创建、监视和管理多个控制台环境
- 安全第一 -默认情况下安全,具有覆盖选项
- MCP兼容 -与Claude Desktop和mcpo合作
- 生产就绪 -错误处理、会话监控和清理
非常适合:开发工作流程、软件安装、环境设置、系统管理以及任何需要持久控制台状态的任务! 🚀
______________________________________________________________________
⚠️ 重要:此工具提供强大的系统访问。在执行命令之前,始终要检查命令,并了解启用危险操作的安全影响。负责任地使用会话,定期清理不活跃的会话。
