使用说明
Ubuntu MCP服务器
一个简单的模型上下文协议(MCP)服务器,允许Claude在您的Ubuntu机器上执行shell命令。
📋 概述
此MCP服务器由两个组件组成:
- ubuntu_mcp_server.py -执行命令的Flask API服务器
- mcp_client.py -将Claude连接到服务器的MCP协议处理程序
🚀 安装
先决条件
- Python 3.8+
- pip3
- 基于Ubuntu/Debian的Linux
再进行
cd /home/zilbon-admin/mcp
pip3 install mcp flask requests --break-system-packages
📦 文件
/home/zilbon-admin/mcp/
├── ubuntu_mcp_server.py # Flask API server
├── mcp_client.py # MCP protocol client
└── README.md # This file
🔧 用法
1.启动服务器
在使用tmux的Ubuntu机器上:
cd /home/zilbon-admin/mcp
# Start a new tmux session
tmux new -s mcp-server
# Run the server
python3 ubuntu_mcp_server.py
服务器将于启动 http://127.0.0.1:5000
Tmux命令:
- 脱离会话:按
Ctrl+B然后D - 列出会话:
tmux ls - 重新附加到会话:
tmux attach -t mcp-server - 终止会话:
tmux kill-session -t mcp-server
2.配置克劳德代码
创建MCP配置文件(例如。, mcp_list.json):
{
"mcpServers": {
"ubuntu-commands": {
"command": "ssh",
"args": [
"-q",
"-o", "LogLevel=ERROR",
"mobile-claude",
"python3 /home/zilbon-admin/mcp/mcp_client.py"
]
}
}
}
如果您有其他MCP服务器:
{
"mcpServers": {
"kali-tools": {
"command": "ssh",
"args": [
"-q",
"-o", "LogLevel=ERROR",
"kali-local",
"mcp-server --server http://127.0.0.1:5000"
]
},
"ubuntu-commands": {
"command": "ssh",
"args": [
"-q",
"-o", "LogLevel=ERROR",
"mobile-claude",
"python3 /home/zilbon-admin/mcp/mcp_client.py"
]
}
}
}
3.在MCP中使用克劳德代码
使用MCP配置运行Claude代码:
claude --mcp-config mcp_list.json
4.测试连接
在Claude Code中,尝试:
- “执行
uname -a在我的Ubuntu机器上” - “跑
ls -la /home在Ubuntu上” - “检查磁盘空间
df -h"
🧪 测试
直接测试Flask服务器
# Health check
curl http://localhost:5000/health
# Execute a command
curl -X POST http://localhost:5000/execute \
-H "Content-Type: application/json" \
-d '{"command": "ls -la", "timeout": 30}'
通过SSH(从本地计算机)进行测试
# Test SSH connection
ssh mobile-claude "echo 'SSH works'"
# Test MCP client
ssh mobile-claude "python3 /home/zilbon-admin/mcp/mcp_client.py"
# Test full command execution
ssh mobile-claude "curl -X POST http://localhost:5000/execute -H 'Content-Type: application/json' -d '{\"command\": \"whoami\"}'"
📡 API终点
健康检查
GET /health
返回服务器状态。
执行命令
POST /execute
Content-Type: application/json
{
"command": "ls -la",
"timeout": 300
}
答复:
{
"success": true,
"output": "total 48\ndrwxr-xr-x...",
"error": "",
"return_code": 0,
"timestamp": "2025-11-06T19:30:00"
}
🔒 安全说明
⚠️ 重要安全注意事项:
- 服务器运行在
127.0.0.1(仅限本地主机)-无法从网络访问 - 未实现身份验证-依赖于SSH安全
- 可以执行任何命令-确保只有受信任的用户才能访问
- 对于生产使用,请考虑:
- 添加命令白名单
- 实施身份验证
- 使用HTTPS
- 速率限制
- 审核日志记录
🛠️ 系统服务(可选)
要将服务器作为系统服务运行,请执行以下操作:
创建 /etc/systemd/system/ubuntu-mcp.service:
[Unit]
Description=Ubuntu MCP Server
After=network.target
[Service]
Type=simple
User=zilbon-admin
WorkingDirectory=/home/zilbon-admin/mcp
ExecStart=/usr/bin/python3 /home/zilbon-admin/mcp/ubuntu_mcp_server.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
启用并启动:
sudo systemctl daemon-reload
sudo systemctl enable ubuntu-mcp
sudo systemctl start ubuntu-mcp
sudo systemctl status ubuntu-mcp
查看日志:
sudo journalctl -u ubuntu-mcp -f
🐛 故障排除
服务器无法启动
# Check if port 5000 is already in use
sudo netstat -tulpn | grep 5000
# Or use a different port
python3 ubuntu_mcp_server.py --port 5001
Claude Code无法连接
# Test SSH connection
ssh mobile-claude "echo 'test'"
# Check if server is running
ssh mobile-claude "curl http://localhost:5000/health"
# Test with verbose output
claude --mcp-config mcp_list.json --verbose
# Check MCP configuration file
cat mcp_list.json
命令超时
# Increase timeout in the request
curl -X POST http://localhost:5000/execute \
-H "Content-Type: application/json" \
-d '{"command": "sleep 10", "timeout": 600}'
权限被拒绝错误
# Ensure scripts are executable
chmod +x /home/zilbon-admin/mcp/*.py
# Check file permissions
ls -la /home/zilbon-admin/mcp/
📝 示例命令
# System information
"Execute uname -a"
# List files
"Run ls -la /home"
# Check disk space
"Show disk usage with df -h"
# Network information
"Get IP address with ip addr show"
# Process list
"Show running processes with ps aux"
# Install a package
"Install htop using apt"
# Check system resources
"Show memory usage with free -h"
🔄 更新
要更新服务器,请执行以下操作:
cd /home/zilbon-admin/mcp
# Download new versions of the files
# Restart the server
pkill -f ubuntu_mcp_server.py
python3 ubuntu_mcp_server.py
📄 许可证
这是一个简单的实用程序脚本。使用风险自负。
🤝 支持
对于问题:
- 检查故障排除部分
- 查看服务器日志:
tail -f server.log - 单独测试组件(SSH、Flask、MCP客户端)
📚 额外资源
版本: 1.0
最后更新时间: 2025年11月6日
服务器位置: /home/zilbon-admin/mcp/
SSH配置名称: mobile-claude
