🐚 Bash中的MCP服务器
一个轻量级的、零开销的实现 模型上下文协议(MCP) 纯Bash中的服务器。
为什么? 大多数MCP服务器只是带有模式转换的API包装器。此实现为Node.js、Python或其他繁重的运行时提供了一种零开销的替代方案。
______________________________________________________________________
📋 特性
- ✅ stdio上的完整JSON-RPC 2.0协议
- ✅ 完成MCP协议的实施
- ✅ 通过函数命名约定进行动态工具发现
- ✅ 通过JSON文件进行外部配置
- ✅ 易于使用自定义工具进行扩展
______________________________________________________________________
🔧 需求
- Bash shell
jq用于JSON处理(brew install jq在macOS上)
______________________________________________________________________
🚀 快速开始
- 克隆仓库
git clone https://github.com/muthuishere/mcp-server-bash-sdk
cd mcp-server-bash-sdk- 使脚本可执行
chmod +x mcpserver_core.sh moviemcpserver.sh- 试试看
echo '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "get_movies"}, "id": 1}' | ./moviemcpserver.sh______________________________________________________________________
🏗️ 建筑
┌─────────────┐ ┌────────────────────────┐
│ MCP Host │ │ MCP Server │
│ (AI System) │◄──────► │ (moviemcpserver.sh) │
└─────────────┘ stdio └────────────────────────┘
│
┌───────┴──────────┐
▼ ▼
┌───────────────────┐ ┌───────────────┐
│ Protocol Layer │ │ Business Logic│
│(mcpserver_core.sh)│ │(tool_* funcs) │
└───────────────────┘ └───────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌───────────────┐
│ Configuration │ │ External │
│ (JSON Files) │ │ Services/APIs │
└─────────────────┘ └───────────────┘- mcpserver_core.sh:处理JSON-RPC和MCP协议
- moviemcpserver.sh:包含业务逻辑功能
- 资产/:JSON配置文件
______________________________________________________________________
🔌 创建自己的MCP服务器
工具功能指南
在为MCP服务器实现工具功能时,请遵循以下准则:
- 命名规范:所有工具函数必须以前缀
tool_后面是tools_list.json中定义的相同名称 - 参数:每个函数应接受一个参数
$1包含JSON参数 - 成功模式:对于成功的操作,回显结果并返回0
- 错误模式:对于验证错误,回显错误消息并返回1
- 自动发现:所有工具函数都会根据tools_list.json自动暴露给MCP服务器
实施步骤
- 创建您的业务逻辑文件(例如。,
weatherserver.sh)
#!/bin/bash
# Weather API implementation
# Override configuration paths BEFORE sourcing the core
MCP_CONFIG_FILE="$(dirname "${BASH_SOURCE[0]}")/assets/weatherserver_config.json"
MCP_TOOLS_LIST_FILE="$(dirname "${BASH_SOURCE[0]}")/assets/weatherserver_tools.json"
MCP_LOG_FILE="$(dirname "${BASH_SOURCE[0]}")/logs/weatherserver.log"
# MCP Server Tool Function Guidelines:
# 1. Name all tool functions with prefix "tool_" followed by the same name defined in tools_list.json
# 2. Function should accept a single parameter "$1" containing JSON arguments
# 3. For successful operations: Echo the expected result and return 0
# 4. For errors: Echo an error message and return 1
# 5. All tool functions are automatically exposed to the MCP server based on tools_list.json
# Source the core MCP server implementation
source "$(dirname "${BASH_SOURCE[0]}")/mcpserver_core.sh"
# Access environment variables
API_KEY="${MCP_API_KEY:-default_key}"
# Tool: Get current weather for a location
# Parameters: Takes a JSON object with location
# Success: Echo JSON result and return 0
# Error: Echo error message and return 1
tool_get_weather() {
local args="$1"
local location=$(echo "$args" | jq -r '.location')
# Parameter validation
if [[ -z "$location" ]]; then
echo "Missing required parameter: location"
return 1
fi
# Call external API
local weather=$(curl -s "https://api.example.com/weather?location=$location&apikey=$API_KEY")
echo "$weather"
return 0
}
# Start the MCP server
run_mcp_server "$@"- 创建
assets/weatherserver_tools.json
{
"tools": [
{
"name": "get_weather",
"description": "Get current weather for a location",
"inputSchema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name or coordinates"
}
},
"required": ["location"]
}
}
]
}- 创建
assets/weatherserver_config.json
{
"protocolVersion": "2025-03-26",
"serverInfo": {
"name": "WeatherServer",
"version": "1.0.0"
},
"capabilities": {
"tools": {
"listChanged": true
}
},
"instructions": "This server provides weather information."
}- 使文件可执行
chmod +x weatherserver.sh______________________________________________________________________
🖥️ 与VS Code和GitHub Copilot一起使用
- 更新VS代码设置.json
"mcp": {
"servers": {
"my-weather-server": {
"type": "stdio",
"command": "/path/to/your/weatherserver.sh",
"args": [],
"env": {
"MCP_API_KEY": "your-api-key"
}
}
}
}- 与GitHub Copilot聊天一起使用
/mcp my-weather-server get weather for New York______________________________________________________________________
🚫 局限性
- 无并发/并行处理
- 有限的内存管理
- 无流媒体响应
- 不是为高吞吐量而设计的
对于人工智能助手和本地工具执行来说,这些都不是阻塞问题。
______________________________________________________________________
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
博客:https://medium.com/@muthuiser/why-i内置-an-mcp服务器-dk-in-shell-y-bash-6f2192072279
