将任何 oclif CLI 转换为完全符合 MCP 2025-06-18 协议的服务器,以实现与 AI 助手的无缝集成
此插件会自动将您的 oclif CLI 命令转换为一个 完全符合 MCP 2025-06-18 协议的服务器,实现了最新的 模型上下文协议规范。它允许像 Claude、ChatGPT 和 Cursor 这样的 AI 助手通过对话自然地发现并执行您的 CLI 工具。
🎉 最新 MCP 规范:完全符合 MCP 2025-06-18 包括:
MCP-Protocol-Version: 2025-06-18 头支持模型上下文协议 (MCP) 是一个开放标准,使 AI 助手能够安全地连接到外部数据源和工具。有了 MCP,您的 CLI 成为了 AI 工作流中的一等公民,助手可以:
MCP-Protocol-Version: 2025-06-18 头处理在您的 CLI 的 package.json 中添加:
{
"dependencies": {
"oclif-plugin-mcp-server": "latest"
},
"oclif": {
"plugins": ["oclif-plugin-mcp-server"]
}
}
# 直接从 GitHub 安装(需要 oclif-plugin-plugins)
your-cli plugins install npjonath/oclif-plugin-mcp-server
# 验证安装
your-cli mcp --help
将您的 CLI 添加到 AI 助手的 MCP 配置中:
{
"mcpServers": {
"your-cli": {
"command": "your-cli",
"args": ["mcp"],
"env": {}
}
}
}
{
"mcpServers": {
"your-cli": {
"command": "your-cli",
"args": ["mcp"]
}
}
}
yarn buildnpx oclif manifestStdio 传输(默认):
{
"mcpServers": {
"your-cli-dev": {
"command": "node <path_to_project_folder>/bin/dev.js",
"args": ["mcp"]
}
}
}
HTTP 传输:
{
"mcpServers": {
"your-cli-dev-http": {
"command": "node <path_to_project_folder>/bin/dev.js",
"args": ["mcp", "--transport", "http", "--port", "3000"]
}
}
}
您的 AI 助手现在可以发现并使用您的 CLI 命令和资源:
👤 "将 my-app 部署到 staging 并显示部署日志"
🤖 "我将把您的应用程序部署到 staging 并获取部署日志。"
执行:deploy my-app --environment staging
✅ 正在将 my-app 部署到 staging
获取资源:logs://deployment/my-app
📊 部署成功完成!
🔍 日志:[部署详情...]
此插件支持 官方规范 中定义的两种 MCP 传输协议:
本地集成和命令行工具的默认传输方式。
# 使用 stdio 传输启动 MCP 服务器(默认)
your-cli mcp
your-cli mcp --transport stdio
适合:
基于 HTTP 的传输,使用服务器发送事件(SSE)进行 Web 集成。
# 使用 HTTP 传输启动 MCP 服务器
your-cli mcp --transport http --port 3000 --host 127.0.0.1
适合:
X-Session-Id 头的状态会话MCP-Protocol-Version: 2025-06-18Last-Event-ID 头支持/health 端点用于监控POST / - JSON-RPC 请求(客户端到服务器)GET /events/:sessionId - SSE 流(服务器到客户端)DELETE /sessions/:sessionId - 会话终止GET /health - 带有协议版本的健康检查GET /oauth/authorize - OAuth 2.1 授权端点(如果已配置)GET /oauth/callback - OAuth 2.1 回调端点(如果已配置)# 列出可用工具
curl -X POST http://localhost:3000/ \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
# 调用工具
curl -X POST http://localhost:3000/ \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"your-command","arguments":{"arg":"value"}},"id":2}'
# 订阅 SSE 流以接收会话更新
curl -N http://localhost:3000/events/your-session-id \
-H "Accept: text/event-stream"
# 带有协议版本的健康检查
curl http://localhost:3000/health
# 响应:{"status":"ok"},带有 MCP-Protocol-Version: 2025-06-18 头
# OAuth 授权流程(如果已配置)
curl http://localhost:3000/oauth/authorize?session_id=your-session
// 初始化支持 2025-06-18 的 HTTP MCP 客户端
const client = new MCPClient({
transport: 'http',
endpoint: 'http://localhost:3000/',
protocolVersion: '2025-06-18',
// 可选的 OAuth 配置
oauth: {
authorizationUrl: 'http://localhost:3000/oauth/authorize',
callbackUrl: 'http://localhost:3000/oauth/callback',
},
})
await client.connect()
const tools = await client.listTools()
| 使用场景 | 推荐传输 | 原因 |
|---|---|---|
| 本地 CLI 集成 | stdio | 简单、直接的进程通信 |
| VS Code 扩展 | stdio | 桌面集成的标准 |
| Claude Desktop | stdio | 桌面集成的标准 |
| Cursor IDE | stdio | 桌面集成的标准 |
| Web 应用程序 | http | 通过网络工作,支持多个客户端 |
| Docker 容器 | http | 对容器化部署更好 |
| 开发/调试 | http | 使用 curl/浏览器容易测试 |
| 生产服务器 | http | 可扩展,支持负载均衡 |
| CI/CD 管道 | http | 对自动化环境更好 |
此插件通过 MCP 协议将您的 CLI 命令暴露给 AI 助手。最新的 2025-06-18 规范包括增强的安全功能:
disableMCP 标志export default class SensitiveCommand extends Command {
static description = '此命令执行敏感操作'
static disableMCP = true // 🔒 不暴露给 MCP
async run() {
// 不应暴露给 AI 的敏感操作
}
}
// 为安全的 HTTP 传输配置 OAuth
const oauthConfig = {
authorizationServer: 'https://your-auth-server.com',
clientId: 'your-client-id',
clientSecret: 'your-client-secret', // 公共客户端可选
tokenEndpoint: 'https://your-auth-server.com/token',
scope: 'mcp:read mcp:write',
}
不同的 AI 提供商对有效处理的工具数量有不同的限制:
| 提供商 | 工具限制 | 备注 |
|---|---|---|
| VS Code | 128 个工具 | 平台强制执行的硬限制 |
| Cursor | 40 个工具 | 平台强制执行的硬限制 |
| Claude Desktop | 未知 | 根据模型和订阅层级变化 |
| ChatGPT | 未知 | 根据模型和订阅层级变化 |
| GitHub Copilot | 未知 | 根据模型和订阅层级变化 |
为了管理具有许多命令的大 CLI,您可以配置过滤以保持在这些限制内:
{
"oclif": {
"mcp": {
"toolLimits": {
"maxTools": 40,
"warnThreshold": 35
},
"topics": {
"include": ["auth", "deploy", "config"],
"exclude": ["debug", "internal", "experimental"]
}
}
}
}
{
"oclif": {
"mcp": {
"toolLimits": {
"maxTools": 80,
"strategy": "优先级"
},
"commands": {
"include": ["auth:*", "deploy:*", "config:get", "config:set", "status", "logs:*"],
"exclude": ["*:debug", "internal:*", "test:*", "*:experimental"],
"priority": ["auth:login", "deploy:production", "status", "logs:tail"]
}
}
}
}
{
"oclif": {
"mcp": {
"profiles": {
"development": {
"maxTools": 128,
"topics": {
"include": ["*"]
}
},
"production": {
"maxTools": 40,
"topics": {
"include": ["auth", "deploy", "config", "status", "logs"],
"exclude": ["debug", "test", "internal"]
}
},
"minimal": {
"maxTools": 20,
"commands": {
"include": ["auth:login", "auth:logout", "deploy:production", "status", "logs:tail"]
}
}
},
"defaultProfile": "production