Rinard安全终端添加福利群:解决AI开发者的「MCP实战痛点」一个实现模型上下文协议 (MCP) 的安全终端执行服务器。该服务器提供带有安全特性和资源限制的受控命令执行能力。
# Clone the repository
git clone https://github.com/RinardNick/mcp-terminal.git
cd mcp-terminal
# Create and activate virtual environment using uv
uv venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
# Install development dependencies
uv pip install -e ".[dev]"

# Build the package
uv pip install build
python -m build
# Upload to PyPI
uv pip install twine
python -m twine upload dist/*

可以使用 MCP Inspector 工具来测试服务器实现:
# Install inspector
npm install -g @modelcontextprotocol/inspector
# Test server
npx @modelcontextprotocol/inspector python3 src/mcp_terminal/server.py --allowed-commands "python,pip,git,ls,cd"

# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_terminal.py
# Run with coverage
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": "Execute a terminal command",
"parameters": {
"command": {
"type": "string",
"description": "The command to execute"
}
},
"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": "command not allowed"
}
}

命令验证:
资源保护:
最佳实践:
allowed-commandsgit checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)该项目根据 MIT 许可证授权 - 详情请参阅 LICENSE 文件。