使用说明
模块化MCP服务器
A. 配置驱动的模块化MCP(模型上下文协议)服务器 随着 零依赖性。只需编辑JSON配置文件即可启用/禁用工具和插件!
特性
- 配置驱动:通过编辑启用/禁用工具
config.json - 零依赖:纯Node.js(18+),不需要npm包
- 插件架构:易于添加新插件
- 安全:每个工具内置访问控制和沙盒
- 简单:~500行核心代码
- 快速:开销最小,直接执行工具
快速开始
# 1. Create or edit config.json
cp config.example.json config.json
# 2. Start the server
node server.js
# Or specify a config file
node server.js my-config.json
运作原理
配置文件结构
这 config.json 文件控制 一切:
{
"server": {
"name": "my-mcp-server",
"version": "1.0.0"
},
"plugins": [
{
"name": "utils",
"type": "builtin",
"module": "utils",
"enabled": true,
"tools": [
{
"name": "echo",
"enabled": true
},
{
"name": "calculate",
"enabled": true
}
]
}
]
}
就是这样! 添加/删除工具不需要更改代码。
添加工具
要启用文件操作吗?只需编辑配置:
{
"plugins": [
{
"name": "filesystem",
"type": "builtin",
"module": "filesystem",
"enabled": true,
"tools": [
{
"name": "read_file",
"enabled": true,
"allowedPaths": [".", "/tmp"]
},
{
"name": "write_file",
"enabled": true
}
]
}
]
}
重新启动服务器-完成!
禁用工具
集 "enabled": false:
{
"name": "delete_file",
"enabled": false // ← Tool won't be available
}
内置插件
文件系统插件
具有路径限制的文件和目录操作。
工具:
read_file-读取文件内容write_file-写入文件list_directory-列出目录内容file_info-获取文件元数据delete_file-删除文件
配置选项:
{
"name": "read_file",
"enabled": true,
"allowedPaths": [".", "/tmp"], // Restrict access
"readonly": false // Make filesystem readonly
}
Shell插件
执行支持白名单/黑名单的shell命令。
工具:
exec_command-执行shell命令
配置选项:
{
"name": "exec_command",
"enabled": true,
"allowedCommands": ["ls", "pwd", "cat"], // Whitelist
"blockedCommands": ["rm", "sudo"], // Blacklist
"maxBuffer": 1048576 // 1MB output limit
}
HTTP插件
发出带有域限制的HTTP请求。
工具:
http_get-GET请求http_post-POST请求
配置选项:
{
"name": "http_get",
"enabled": true,
"allowedDomains": ["api.github.com", "example.com"]
}
Utils插件
常见任务的实用功能。
工具:
echo-回声反馈输入(测试)calculate-安全数学评估timestamp-获取当前时间base64_encode-Base64编码base64_decode-Base64解码
创建自定义插件
1.创建插件文件
// plugins/my-plugin.js
export default {
// Optional initialization
async init(config) {
console.log('Plugin initialized:', config);
},
tools: {
my_tool: {
description: 'Description of what this tool does',
inputSchema: {
type: 'object',
properties: {
input: {
type: 'string',
description: 'Input parameter'
}
},
required: ['input']
},
async execute(args, config) {
// args = input parameters
// config = tool-specific config from config.json
return `Result: ${args.input}`;
}
}
}
};
2.添加到配置
{
"plugins": [
{
"name": "my-plugin",
"type": "builtin",
"module": "my-plugin",
"enabled": true,
"tools": [
{
"name": "my_tool",
"enabled": true,
"customOption": "value"
}
]
}
]
}
3.重新启动服务器
就是这样!您的工具现在可用。
外部插件
从项目外部加载插件:
{
"plugins": [
{
"name": "external-plugin",
"type": "external",
"module": "/absolute/path/to/plugin.js",
"enabled": true,
"tools": [...]
}
]
}
安全功能
路径限制
{
"name": "read_file",
"allowedPaths": ["./data", "/tmp"] // Only these paths accessible
}
命令白名单
{
"name": "exec_command",
"allowedCommands": ["ls", "cat", "grep"] // Only these commands
}
域名筛选
{
"name": "http_get",
"allowedDomains": ["trusted-api.com"] // Only these domains
}
只读模式
{
"name": "write_file",
"readonly": true // Prevent writes
}
配置参考
服务器配置
{
"server": {
"name": "server-name", // MCP server name
"version": "1.0.0" // Server version
}
}
插件配置
{
"name": "plugin-name", // Unique plugin identifier
"type": "builtin", // "builtin" or "external"
"module": "plugin-file", // Plugin file/path
"enabled": true, // Enable/disable entire plugin
"required": false, // Fail if plugin can't load
"tools": [...] // Array of tool configs
}
工具配置
{
"name": "tool-name", // Tool identifier
"enabled": true, // Enable/disable this tool
// ... custom options passed to tool's execute()
}
示例
示例1:只读文件系统
{
"plugins": [
{
"name": "filesystem",
"type": "builtin",
"module": "filesystem",
"enabled": true,
"tools": [
{
"name": "read_file",
"enabled": true,
"allowedPaths": ["/var/log"]
},
{
"name": "list_directory",
"enabled": true
}
]
}
]
}
示例2:安全外壳访问
{
"plugins": [
{
"name": "shell",
"type": "builtin",
"module": "shell",
"enabled": true,
"tools": [
{
"name": "exec_command",
"enabled": true,
"allowedCommands": ["ls", "pwd", "cat", "grep", "find"],
"blockedCommands": ["rm", "sudo", "su"],
"maxBuffer": 1048576
}
]
}
]
}
示例3:HTTP API客户端
{
"plugins": [
{
"name": "http",
"type": "builtin",
"module": "http",
"enabled": true,
"tools": [
{
"name": "http_get",
"enabled": true,
"allowedDomains": [
"api.github.com",
"httpbin.org"
]
}
]
}
]
}
测试
使用MCP检查器
# Install MCP Inspector
npm install -g @modelcontextprotocol/inspector
# Test your server
mcp-inspector node server.js
手动测试
通过stdin发送JSON-RPC请求:
# Initialize
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | node server.js
# List tools
echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | node server.js
# Call a tool
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello!"}}}' | node server.js
日志记录
通过环境变量控制日志级别:
# Debug mode
LOG_LEVEL=DEBUG node server.js
# Quiet mode
LOG_LEVEL=ERROR node server.js
日志级别: ERROR, WARN, INFO (默认), DEBUG
建筑
┌─────────────────────────────────────┐
│ MCP Client (Claude) │
└─────────────┬───────────────────────┘
│ JSON-RPC over stdio
┌─────────────▼───────────────────────┐
│ core/mcp-protocol.js │
│ (JSON-RPC handler, stdio I/O) │
└─────────────┬───────────────────────┘
│
┌─────────────▼───────────────────────┐
│ core/plugin-manager.js │
│ (Loads plugins from config.json) │
└─────────────┬───────────────────────┘
│
┌───────┴───────┬─────────────┐
│ │ │
┌─────▼─────┐ ┌──────▼──────┐ ┌──▼────┐
│filesystem │ │ shell │ │ http │
│ plugin │ │ plugin │ │ plugin│
└───────────┘ └─────────────┘ └───────┘
文件
.
├── server.js # Entry point
├── config.json # Your configuration
├── config.example.json # Example config
├── package.json # Project metadata
├── core/
│ ├── mcp-protocol.js # MCP/JSON-RPC implementation
│ ├── plugin-manager.js # Plugin loader
│ └── logger.js # Logging utility
├── plugins/
│ ├── filesystem.js # File operations
│ ├── shell.js # Shell commands
│ ├── http.js # HTTP requests
│ └── utils.js # Utilities
└── README.md # This file
常见问题解答
Q: 如何添加新工具?
A. 编辑 config.json 并将该工具添加到插件中 tools 阵列。重新启动服务器。
Q: 我可以创建自己的插件吗?
A. 对!创建一个 .js 文件在 plugins/ 遵循插件结构,然后在中引用它 config.json.
Q: 如何限制文件访问?
A. 使用 allowedPaths 文件系统工具配置中的选项。
Q: 这安全吗?
A. 它具有内置的安全功能(路径限制、命令白名单等),但将其视为任何执行代码的工具——仔细配置并以适当的权限运行。
Q: 我可以在生产中使用这个吗?
A. 是的,但请仔细检查安全设置。考虑在容器中运行或使用受限的用户权限运行。
许可证
麻省理工学院
贡献
欢迎投稿!代码库有意保持简单和零依赖性,以保持其可维护性。
