使用说明
终端MCP服务器
一个简单的MCP服务器,执行终端命令并提供终端命令备忘单。还演示了MCP服务器实现中的潜在安全漏洞。
特性
-
终端命令执行
- 执行任何终端命令
- 获取stdout、stderr和返回代码
- 错误处理
-
安全漏洞演示
- magicaltool演示了不安全的命令执行
- 展示如何执行恶意代码
- 强调安全实施的重要性
-
终端命令备忘单
- 30个基本终端命令
- 常见选项和标志
- 使用示例
安装
- 创建虚拟环境:
uv venv
- 激活虚拟环境:
source .venv/bin/activate
- 安装依赖项:
uv pip install -e .
添加到克劳德
- 找到Claude的桌面配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json
- 添加终端服务器配置:
{
"mcpServers": {
"terminal": {
"command": "/path/to/terminal-mcp-server/.venv/bin/python",
"args": [
"/path/to/terminal-mcp-server/src/server.py"
]
}
}
}
替换 /path/to/terminal-mcp-server 使用克隆此存储库的实际路径。
- 重新启动Claude以加载新配置。
MCP检验员测试
您可以使用以下工具测试MCP服务器 MCP检查员 该工具提供了一个用于与MCP服务器交互的web界面。
- 安装MCP检查器:
npm install -g @modelcontextprotocol/inspector
- 在另一个终端中,运行检查器:
npx @modelcontextprotocol/inspector
这将在本地启动一个web应用程序,您可以在其中:
- 连接到您的MCP服务器
- 测试所有可用工具
- 查看工具响应
- 检查资源
- 调试通信
用法
服务器提供:
run_command工具:
# Safe command execution
result = await run_command("ls -la")
print(result["stdout"]) # Command output
print(result["stderr"]) # Any errors
print(result["return_code"]) # Exit code
magical_tool:
# WARNING: Demonstrates unsafe command execution
# Shows how external commands can be executed without proper validation
result = await magical_tool()
print(result["stdout"]) # Executed command output
print(result["stderr"]) # Any errors
print(result["return_code"]) # Command exit code
terminal_commands_cheat_sheet资源:
# Access terminal commands cheat sheet
commands = await terminal_commands_cheat_sheet()
print(commands) # List of commands with examples
安全影响
这 magical_tool 演示了一个常见的安全漏洞,其中用户输入或文件路径在没有适当验证的情况下直接用于命令执行。这可能允许:
- 命令注入
- 任意文件访问
- 系统操作
这是有意作为一个教育性的例子,说明在生产系统中不应该做什么。
发展
- 克隆存储库
- 创建并激活虚拟环境,如上所示
- 安装依赖项
- 运行服务器:
uv run src/server.py
错误处理
- 所有工具都能优雅地处理错误
- 失败的命令在stderr中返回错误消息
- 资源访问失败返回错误消息
- 非零返回代码表示命令失败
许可证
麻省理工学院
