Bash MCP(主控制程序)
一个 TypeScript 应用程序,允许 Claude 在安全保障措施下安全地执行 Bash 命令。该项目实现了模型上下文协议 (MCP),为 Claude 等 AI 助手执行 Bash 命令提供了一个安全的接口。
特征
- 在受控环境中执行 bash 命令
- 支持无状态和有状态(交互式)命令执行
- 安全保障措施:
- 交互式命令的会话管理
- 综合日志记录
- 用于 AI 集成的 MCP 服务器实现
安装
npm install
npm run build
配置
配置存储在config/default.json
中。您可以自定义:
示例配置:
{
"allowedCommands": ["ls", "cat", "echo", "pwd"],
"allowedDirectories": ["/tmp", "/home"],
"session": {
"timeout": 300,
"maxActiveSessions": 5,
"defaultMode": "stateless"
},
"security": {
"validateCommandsStrictly": true,
"sanitizeOutput": true,
"maxOutputSize": 1048576,
"commandTimeout": 30
},
"logging": {
"level": "info",
"file": "logs/bash-mcp.log",
"maxSize": 10485760,
"maxFiles": 5
}
}
用法
作为图书馆
简单命令执行
import { executeCommand } from 'bash-mcp';
const result = await executeCommand('ls -la', { cwd: '/home/user' });
console.log(result.output);
互动环节
import { initBashMCP } from 'bash-mcp';
const mcp = await initBashMCP();
// Create a session
const session = mcp.createSession('/home/user');
const sessionId = session.sessionId;
// Execute a command in the session
const result1 = await mcp.executeCommand('ls -la', { sessionId });
console.log(result1.output);
// Send input to the session
const result2 = await mcp.sendInput({ sessionId, input: 'echo "Hello, world!"' });
console.log(result2.output);
// Close the session when done
mcp.closeSession(sessionId);
作为 MCP 服务器
该项目包括一个 MCP 服务器实现,可与 Claude Desktop 或其他 MCP 客户端一起使用:
# Start the TypeScript MCP server
npm run mcp
# Start the JavaScript MCP server
npm run mcp-js
# Start with MCP Inspector
npm run inspector
有关 MCP 服务器实现的详细文档,请参阅MCP.md
安全注意事项
此 MCP 在设计时充分考虑了安全性,但重要的是:
- 保持允许的命令和目录列表尽可能严格
- 定期检查并更新配置
- 监控日志中是否存在可疑活动
- 保持 MCP 及其依赖项保持最新
发展
建筑
代码检查和格式化
要检查 linting 问题:
要自动修复 linting 和格式问题:
或者手动:
npm run lint:fix # Fix linting issues
npm run format # Format code
测试
MCP SDK 版本
该项目使用 MCP SDK 版本 1.0.1。