使用说明
命令
此存储库包含用于执行命令的模型上下文协议(MCP)服务器实现。它允许MCP兼容的AI助手在指定目录中执行命令。
关于模型上下文协议(MCP)
模型上下文协议(MCP)是Anthropic开发的一个开放标准,它实现了数据源和人工智能工具之间的安全双向连接。MCP服务器向AI应用程序公开数据和功能,使AI模型能够智能地检索、处理和利用私有数据源中的信息。
工具架构
这个MCP服务器提供了一个名为“命令”的通用工具,它为AI代理提供了在指定目录中执行命令的广泛能力。
{
"name": "command",
"description": "Execute commands in a specified directory",
"inputSchema": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "Command to execute"
},
"workingDir": {
"type": "string",
"description": "Working directory for command execution"
}
},
"required": ["command", "workingDir"]
}
}
参数说明
command:将要执行的shell命令(例如,“ls-la”、“dir”、“git status”)workingDir:执行命令的目录。这必须是配置中指定的允许目录之一。
项目结构
/command
├── config/ # Configuration-related files
│ ├── index.js # Main config exporter
│ └── validation.js # Configuration validation logic
├── src/
│ ├── core/ # Core server functionality
│ │ ├── server.js # Server initialization & management
│ │ └── handlers.js # MCP request handlers
│ ├── tools/ # Tool implementations
│ │ ├── index.js # Exports all tools
│ │ └── command/ # Command tool module
│ │ ├── index.js # Main command tool export
│ │ ├── schema.js # Command tool schema
│ │ ├── executor.js # Command execution logic
│ │ └── worker.js # Command worker implementation
│ ├── utils/ # Utility functions
│ │ ├── logging.js # Logging utilities
│ │ └── error.js # Error handling utilities
│ └── index.js # Application entry point
├── tests/ # Test directory
│ └── config.test.js # Configuration tests
├── logs/ # Log output directory
├── scripts/ # Scripts for running/building
│ ├── start-server.sh # Unix start script
│ └── start-server.bat # Windows start script
├── package.json # Project metadata and dependencies
└── README.md # Project documentation
安装说明
先决条件
- Node.js(v16或更高版本)
- npm(v7或更高版本)
本地设置
- 安装依赖项:
npm install
配置
要将此服务器与Claude一起使用,您需要更新Claude配置以包含Command服务器。
更新您的Claude配置文件(通常位于 ~/.config/claude/claude_desktop_config.json 或 ~/Library/Application Support/Claude/claude_desktop_config.json)包括以下内容:
{
"mcpServers": {
"command": {
"command": "/path/to/command/start-server.sh",
"args": [
"/path/to/allowed/directory1",
"/path/to/allowed/directory2",
"/path/to/allowed/directory3"
]
}
}
}
替换 /path/to/command 使用此存储库所在的实际路径。
替换 /path/to/allowed/directory1, /path/to/allowed/directory2等,并使用您要允许Claude在其中执行命令的目录。
对于Windows,请改用批处理文件:
{
"mcpServers": {
"command": {
"command": "C:\\path\\to\\command\\start-server.bat",
"args": [
"C:\\path\\to\\allowed\\directory1",
"C:\\path\\to\\allowed\\directory2",
"C:\\path\\to\\allowed\\directory3"
]
}
}
}
用法
配置后,Claude可以通过CLI MCP服务器在指定目录中执行命令。
服务器接受目录路径作为参数。只有这些目录可用于命令执行。
Claude中的示例用法:
Please run the following command:
command("ls -la", "/path/to/allowed/directory1")
在Windows上,您可以使用:
Please run the following command:
command("dir", "C:\\path\\to\\allowed\\directory1")
发展
可用脚本
npm start-运行服务器npm run dev-在文件更改时自动重新加载以运行服务器npm test-运行测试套件
安全考虑
- 服务器只允许在指定为命令行参数的目录中执行命令。
- 请小心您向Claude公开的目录,因为它可以在这些位置执行命令。
- 考虑使用权限有限的专用用户来运行服务器。
- 检查Claude执行的命令,以确保它们是安全和适当的。
- 服务器包括路径验证,以防止访问未经授权的目录。
