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: "2024-11-05", 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"] } }, { "name": "change_email", "description": "更换通知邮箱,需传入合法邮箱地址:email", "inputSchema": { "type": "object", "properties": { "sender": { "type": "string" }, "email": { "type": "string" } }, "required": ["sender", "email"] } }, { "name": "create_work_order", "description": "创建客服工单,遇到问题或建议可创建并等待人工处理,参数email为联系方式,工单标题title不超过20字,内容content不超过100字,标题和内容最好由你总结生成", "inputSchema": { "type": "object", "properties": { "sender": { "type": "string" }, "email": { "type": "string" }, "title": { "type": "string" }, "content": { "type": "string" } }, "required": ["sender", "email", "title", "content"] } } ] } } 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 case "change_email": output = await MCP.change_email(args) break case "create_work_order": output = await MCP.create_work_order(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