实现模型上下文协议(MCP)的安全终端执行服务器。此服务器提供具有安全特性和资源限制的受控命令执行能力。
# 克隆仓库
git clone https://github.com/RinardNick/mcp-terminal.git
cd mcp-terminal
# 使用uv创建并激活虚拟环境
uv venv
source .venv/bin/activate # 或在Windows上使用 .venv\Scripts\activate
# 安装开发依赖
uv pip install -e ".[dev]"
# 构建包
uv pip install build
python -m build
# 上传到PyPI
uv pip install twine
python -m twine upload dist/*
MCP Inspector工具可用于测试服务器实现:
# 安装检查器
npm install -g @modelcontextprotocol/inspector
# 测试服务器
npx @modelcontextprotocol/inspector python3 src/mcp_terminal/server.py --allowed-commands "python,pip,git,ls,cd"
# 运行所有测试
pytest tests/
# 运行特定测试文件
pytest tests/test_terminal.py
# 带覆盖率运行
pytest --cov=mcp_terminal tests/
一旦包发布到PyPI:
安装UV(如果尚未安装):
pip install uv
使用UV安装包:
uv pip install mcp-terminal
配置Claude Desktop:
编辑你的Claude Desktop配置文件(通常位于macOS上的~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"terminal": {
"command": "uv",
"args": [
"pip",
"run",
"mcp-terminal",
"--allowed-commands",
"python,pip,git,ls,cd",
"--timeout-ms",
"30000",
"--max-output-size",
"1048576"
]
}
}
}
服务器实现了模型上下文协议(MCP),具有以下能力:
{
"protocol": "1.0.0",
"name": "terminal",
"version": "1.1.0",
"capabilities": {
"execute": {
"description": "执行终端命令",
"parameters": {
"command": {
"type": "string",
"description": "要执行的命令"
}
},
"returns": {
"type": "object",
"properties": {
"exitCode": { "type": "number" },
"stdout": { "type": "string" },
"stderr": { "type": "string" },
"startTime": { "type": "string" },
"endTime": { "type": "string" }
}
}
}
}
}
请求:
{
"type": "execute",
"data": {
"command": "echo 'hello world'"
}
}
响应:
{
"type": "result",
"data": {
"command": "echo 'hello world'",
"exitCode": 0,
"stdout": "hello world\n",
"stderr": "",
"startTime": "2024-01-20T12:34:56.789Z",
"endTime": "2024-01-20T12:34:56.790Z"
}
}
错误:
{
"type": "error",
"data": {
"message": "不允许的命令"
}
}
命令验证:
资源保护:
最佳实践:
allowed-commandsgit checkout -b feature/amazing-feature)git commit -m '添加一些惊人的功能')git push origin feature/amazing-feature)本项目采用MIT许可证——详情见LICENSE文件。 </中文翻译>