| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- const API = require("../../lib/API")
- const MCP = require("../../lib/Lepao/Mcp.js").MCP
- class McpRpc extends API {
- constructor() {
- super()
- this.noEncrypt()
- this.setPath('/mcp')
- this.setMethod('POST')
- }
- async onRequest(req, res) {
- const { method, params = {}, id: req_id } = req.body
- try {
- let result
- if (method === 'initialize') {
- result = {
- protocolVersion: "1.0",
- capabilities: { "tools": {} },
- serverInfo: { name: "runforge", version: "1.0" }
- }
- } else if (method === 'tools/list') {
- result = {
- "tools": [
- {
- "name": "bind_account",
- "description": "绑定账号",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": { "type": "string" },
- "bind_code": { "type": "string" }
- },
- "required": ["sender", "bind_code"]
- }
- },
- {
- "name": "get_account_info",
- "description": "获取账号信息",
- "inputSchema": {
- "type": "object",
- "properties": { "sender": { "type": "string" } },
- "required": ["sender"]
- }
- },
- {
- "name": "unbind_account",
- "description": "解绑账号",
- "inputSchema": {
- "type": "object",
- "properties": { "sender": { "type": "string" } },
- "required": ["sender"]
- }
- },
- {
- "name": "set_notification",
- "description": "消息通知设置,mode参数:bot:智能机器人通知、email:邮件通知、none:关闭通知",
- "inputSchema": {
- "type": "object",
- "properties": {
- "sender": { "type": "string" },
- "mode": { "type": "string" }
- },
- "required": ["sender", "mode"]
- }
- }
- ]
- }
- } else if (method === 'tools/call') {
- const { name, arguments: args = {} } = params
- let output
- switch (name) {
- case "bind_account":
- output = await MCP.bind_account(args)
- break
- case "get_account_info":
- output = await MCP.get_account_info(args)
- break
- case "unbind_account":
- output = await MCP.unbind_account(args)
- break
- case "set_notification":
- output = await MCP.set_notification(args)
- break
- default:
- output = "未知工具"
- }
- result = { content: [{ type: "text", text: output }] }
- } else {
- result = { error: `未知方法: ${method}` }
- }
- // 返回标准 JSON-RPC 响应
- return res.json({
- jsonrpc: "2.0",
- id: req_id ?? null,
- result
- })
- } catch (e) {
- return res.json({
- jsonrpc: "2.0",
- id: req_id ?? null,
- error: { code: -32000, message: e.message }
- })
- }
- }
- }
- module.exports.McpRpc = McpRpc
|