Agent skill
mcp
MCP(Model Context Protocol)集成技能,支持自动安装、配置和使用MCP服务器,连接各种数据源和工具。当用户需要让AI模型安全访问外部数据源、数据库、API或文件系统时使用此技能。
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/mcp-chengyihua-nanobot-ts
Metadata
Additional technical details for this skill
- nanobot
-
{ "emoji": "\ud83d\udd0c", "install": [ { "id": "mcp-server", "kind": "command", "label": "Install MCP Servers", "command": "npm install -g @modelcontextprotocol/server @modelcontextprotocol/server-filesystem @modelcontextprotocol/server-sqlite @modelcontextprotocol/server-http" } ], "requires": { "bins": [ "node", "npm" ] } }
SKILL.md
MCP(Model Context Protocol)集成技能
什么是MCP?
MCP是Anthropic推出的协议,允许AI模型安全地访问外部工具和数据源,而无需直接集成到模型中。
核心优势:
- 安全性:模型不能直接执行代码,只能通过MCP服务器访问
- 灵活性:可以连接各种数据源(数据库、API、文件系统等)
- 标准化:统一的协议,不同模型都可以使用
快速开始
1. 安装MCP服务器
# 安装常用MCP工具
npm install -g @modelcontextprotocol/server-filesystem @modelcontextprotocol/server-sqlite @modelcontextprotocol/server-http
2. 启动MCP服务器
# 启动文件系统服务器
npx @modelcontextprotocol/server-filesystem --directory /path/to/data
# 启动SQLite服务器
npx @modelcontextprotocol/server-sqlite --database /path/to/database.db
# 启动HTTP服务器
npx @modelcontextprotocol/server-http --port 8080
3. 在nanobot中配置MCP
// 在nanobot配置中添加MCP支持
const { createMCPClient } = require('@ai-sdk/openai');
const mcpClient = createMCPClient({
transport: 'stdio',
command: 'mcp-server',
args: ['filesystem', '--directory', '/path/to/data']
});
常用MCP服务器
1. 文件系统服务器
# 访问本地文件系统
mcp-server filesystem --directory /Users/chengyihua/Documents
# 使用示例:读取文件列表
curl -X POST http://localhost:8080/tools/list \
-H "Content-Type: application/json" \
-d '{"path": "/Users/chengyihua/Documents"}'
2. SQLite服务器
# 连接SQLite数据库
mcp-server sqlite --database /path/to/database.db
# 使用示例:执行SQL查询
curl -X POST http://localhost:8080/tools/query \
-H "Content-Type: application/json" \
-d '{"sql": "SELECT * FROM users LIMIT 10"}'
3. HTTP服务器
# 启动HTTP API服务器
mcp-server http --port 8080
# 使用示例:调用API
curl -X POST http://localhost:8080/tools/fetch \
-H "Content-Type: application/json" \
-d '{"url": "https://api.example.com/data", "method": "GET"}'
4. PostgreSQL服务器
# 连接PostgreSQL数据库
mcp-server postgres --connection-string "postgresql://user:password@localhost:5432/dbname"
5. GitHub服务器
# 访问GitHub API
mcp-server github --token YOUR_GITHUB_TOKEN
在nanobot中使用MCP
1. 配置MCP客户端
// 在nanobot的agent配置中添加
const mcpConfig = {
servers: [
{
name: 'filesystem',
transport: 'stdio',
command: 'mcp-server',
args: ['filesystem', '--directory', process.cwd()]
},
{
name: 'sqlite',
transport: 'stdio',
command: 'mcp-server',
args: ['sqlite', '--database', '/path/to/data.db']
}
]
};
2. 使用MCP工具
// 通过MCP访问文件系统
const files = await mcpClient.callTool('filesystem.list', {
path: '/Users/chengyihua/Documents'
});
// 通过MCP执行SQL查询
const results = await mcpClient.callTool('sqlite.query', {
sql: 'SELECT * FROM products WHERE price > 100'
});
// 通过MCP调用HTTP API
const response = await mcpClient.callTool('http.fetch', {
url: 'https://api.example.com/data',
method: 'GET'
});
自动安装脚本
安装脚本 (scripts/install_mcp.sh)
#!/bin/bash
# MCP自动安装脚本
echo "开始安装MCP..."
# 检查Node.js
if ! command -v node &> /dev/null; then
echo "安装Node.js..."
brew install node
fi
# 安装MCP服务器
echo "安装MCP服务器..."
npm install -g @modelcontextprotocol/server
# 安装常用MCP工具
echo "安装MCP工具..."
npm install -g @modelcontextprotocol/server-filesystem
npm install -g @modelcontextprotocol/server-sqlite
npm install -g @modelcontextprotocol/server-http
npm install -g @modelcontextprotocol/server-postgres
npm install -g @modelcontextprotocol/server-github
# 验证安装
echo "验证安装..."
mcp-server --version
echo "MCP安装完成!"
启动脚本 (scripts/start_mcp.sh)
#!/bin/bash
# 启动MCP服务器
echo "启动MCP服务器..."
# 启动文件系统服务器
mcp-server filesystem --directory /Users/chengyihua/Documents &
# 启动SQLite服务器(如果有数据库)
if [ -f "/path/to/database.db" ]; then
mcp-server sqlite --database /path/to/database.db &
fi
# 启动HTTP服务器
mcp-server http --port 8080 &
echo "MCP服务器已启动"
echo "文件系统服务器: 访问本地文件"
echo "HTTP服务器: http://localhost:8080"
使用示例
示例1:通过MCP读取文件
# 使用curl调用MCP服务器
curl -X POST http://localhost:8080/tools/read \
-H "Content-Type: application/json" \
-d '{"path": "/Users/chengyihua/Documents/example.txt"}'
示例2:通过MCP查询数据库
# 查询SQLite数据库
curl -X POST http://localhost:8080/tools/query \
-H "Content-Type: application/json" \
-d '{"sql": "SELECT name, price FROM products ORDER BY price DESC LIMIT 5"}'
示例3:通过MCP调用GitHub API
# 获取GitHub仓库信息
curl -X POST http://localhost:8080/tools/github \
-H "Content-Type: application/json" \
-d '{"endpoint": "/repos/owner/repo", "method": "GET"}'
故障排除
常见问题
1. MCP服务器无法启动
# 检查端口占用
lsof -i :8080
# 检查MCP安装
mcp-server --version
2. 连接被拒绝
# 检查防火墙设置
sudo ufw status
# 检查MCP服务器日志
tail -f /var/log/mcp-server.log
3. 权限问题
# 检查文件权限
ls -la /path/to/data
# 以正确用户运行
sudo -u username mcp-server filesystem --directory /path/to/data
调试模式
# 启用调试日志
DEBUG=mcp:* mcp-server filesystem --directory /path/to/data
# 详细日志
mcp-server filesystem --directory /path/to/data --verbose
安全最佳实践
1. 访问控制
# 使用认证令牌
mcp-server filesystem --directory /path/to/data --token SECRET_TOKEN
# 限制访问IP
mcp-server http --port 8080 --allowed-ips 192.168.1.0/24
2. 数据加密
# 使用HTTPS
mcp-server http --port 8443 --ssl-cert /path/to/cert.pem --ssl-key /path/to/key.pem
3. 审计日志
# 启用访问日志
mcp-server filesystem --directory /path/to/data --log-file /var/log/mcp-access.log
高级配置
1. 多服务器配置
{
"servers": [
{
"name": "production-db",
"transport": "stdio",
"command": "mcp-server",
"args": ["postgres", "--connection-string", "postgresql://user:pass@prod-db:5432/prod"]
},
{
"name": "development-fs",
"transport": "stdio",
"command": "mcp-server",
"args": ["filesystem", "--directory", "/home/dev/data"]
}
]
}
2. 负载均衡
# 启动多个MCP实例
mcp-server http --port 8081 &
mcp-server http --port 8082 &
mcp-server http --port 8083 &
# 使用负载均衡器
nginx -c /path/to/nginx-mcp.conf
3. 监控和指标
# 启用Prometheus指标
mcp-server http --port 8080 --metrics-port 9090
# 查看健康检查
curl http://localhost:8080/health
参考资料
官方文档
社区资源
相关工具
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?