使用说明
MCP Bash
一个用于执行bash命令的简单模型上下文协议(MCP)服务器。此项目允许您通过MCP服务器接口执行bash命令,但会带来所有潜在的安全风险!在撰写本文时,我在公共领域找不到其他类似的东西,所以我写了自己的。让Claude Desktop编写代码是一个福音(它可以运行测试、评估结果并一次性解决问题。)
描述
MCP Bash提供了一种从客户端应用程序执行Bash命令的简单方法。它将命令执行封装在受控环境中,并从执行的命令中返回stdout和stderr。
主要特点:
- 执行任意bash命令
- 跨命令执行设置和维护工作目录
- 通过模型上下文协议(MCP)清理界面
- 易于部署和扩展
安装
先决条件
- Python 3.10或更高版本
- pip或其他包管理器
设置
-
克隆存储库
git clone https://github.com/yourusername/mcp-bash.git cd mcp-bash -
创建并激活虚拟环境
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate -
安装依赖项
pip install -e .
用法
启动服务器
python -m mcp.cli.server --module server
Claude桌面配置
要将其与Claude Desktop一起使用,请将其添加到您的配置中(通常为/Users//库/应用程序支持/Claude/Claude_desktop_config.json,或参阅Claude桌面UI中的“设置->开发人员”):
{
"mcpServers": {
"Bash": {
"command": "/Users/<username>/.local/bin/uv",
"args": [
"run",
"--with",
"mcp[cli]",
"mcp",
"run",
"/path/to/server.py"
]
}
}
}
然后问Claude:“使用Bash MCP工具列出我当前目录中的文件”
使用API
服务器公开了两个主要功能:
set_cwd(path):设置bash命令的工作目录execute_bash(cmd):执行bash命令并返回stdout/stderr
客户端代码示例
from mcp.client import MCPClient
async def main():
# Connect to the MCP server
client = MCPClient("http://localhost:8000")
# Set working directory
await client.set_cwd("/path/to/your/directory")
# Execute a command
stdout, stderr = await client.execute_bash("ls -la")
print(f"Command output: {stdout}")
if stderr:
print(f"Error output: {stderr}")
if __name__ == "__main__":
import asyncio
asyncio.run(main())
安全考虑
此服务器直接执行bash命令,如果不加以适当限制,这可能会带来致命的安全风险。没有什么能阻止LLM运行危险的命令,比如 rm -rf /。对于个人使用,有用性可能超过风险,但在部署时要小心。
考虑:
- 在容器或受限环境中运行
- 添加命令验证或分配列表
- 使用适当的用户权限限制文件系统访问
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
贡献
欢迎投稿!请随时提交拉取请求。
