MCP Bash

MCP Bash

site icon
2025.04.15 0
Pythonbash 命令执行开发工具开发效率
MCP Bash 是一个简单的 Model-Context-Protocol (MCP) 服务器,用于执行 bash 命令。该项目允许通过 MCP 服务器接口执行 bash 命令,虽然存在潜在的安全风险,但在某些场景下非常有用,例如让 Claude Desktop 编写代码并运行测试。MCP Bash 提供了一个简单的方式来从客户端应用程序执行 bash 命令,并将命令执行包装在一个受控的环境中,返回执行的 stdout 和 stderr。
View on GitHub

Overview

基本能力

产品定位

MCP Bash 是一个用于执行 bash 命令的 MCP 服务器,主要用于开发环境中,帮助开发者通过 MCP 接口执行 bash 命令。

核心功能

  • 执行任意 bash 命令
  • 设置和维护跨命令执行的工作目录
  • 通过 Model-Context-Protocol (MCP) 提供简洁的接口
  • 易于部署和扩展

适用场景

  • 开发环境中自动化执行 bash 命令
  • 与 Claude Desktop 集成,用于代码编写和测试
  • 需要从客户端应用程序执行 bash 命令的场景

工具列表

  • set_cwd(path): 设置 bash 命令的工作目录
  • execute_bash(cmd): 执行 bash 命令并返回 stdout/stderr

常见问题解答

  • 安全风险: 该服务器直接执行 bash 命令,可能存在安全风险,如执行 rm -rf / 等危险命令。建议在容器或受限环境中运行,并添加命令验证或允许列表。

使用教程

使用依赖

  • Python 3.10 或更高版本
  • pip 或其他包管理器

安装教程

  1. 克隆仓库 bash git clone https://github.com/yourusername/mcp-bash.git cd mcp-bash

  2. 创建并激活虚拟环境 bash python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate

  3. 安装依赖 bash pip install -e .

调试方式

  1. 启动服务器 bash python -m mcp.cli.server --module server

  2. 使用 API ```python from mcp.client import MCPClient

async def main(): client = MCPClient("http://localhost:8000") await client.set_cwd("/path/to/your/directory") 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()) ```

许可证

该项目遵循 MIT 开源许可条款,请参阅 MIT 了解完整条款。