McpRPC.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const API = require("../../lib/API")
  2. const MCP = require("../../lib/Lepao/Mcp.js").MCP
  3. class McpRpc extends API {
  4. constructor() {
  5. super()
  6. this.noEncrypt()
  7. this.setPath('/mcp')
  8. this.setMethod('POST')
  9. }
  10. async onRequest(req, res) {
  11. const { method, params = {}, id: req_id } = req.body
  12. try {
  13. let result
  14. if (method === 'initialize') {
  15. result = {
  16. protocolVersion: "2024-11-05",
  17. capabilities: { "tools": {} },
  18. serverInfo: { name: "runforge", version: "1.0" }
  19. }
  20. } else if (method === 'tools/list') {
  21. result = {
  22. "tools": [
  23. {
  24. "name": "bind_account",
  25. "description": "绑定账号",
  26. "inputSchema": {
  27. "type": "object",
  28. "properties": {
  29. "sender": { "type": "string" },
  30. "bind_code": { "type": "string" }
  31. },
  32. "required": ["sender", "bind_code"]
  33. }
  34. },
  35. {
  36. "name": "get_account_info",
  37. "description": "获取账号信息,包含姓名学号、帐号状态、乐跑时间次数等",
  38. "inputSchema": {
  39. "type": "object",
  40. "properties": { "sender": { "type": "string" } },
  41. "required": ["sender"]
  42. }
  43. },
  44. {
  45. "name": "unbind_account",
  46. "description": "解绑账号",
  47. "inputSchema": {
  48. "type": "object",
  49. "properties": { "sender": { "type": "string" } },
  50. "required": ["sender"]
  51. }
  52. },
  53. {
  54. "name": "set_notification",
  55. "description": "消息通知设置,mode参数为:bot:智能机器人通知、email:邮件通知、none:关闭通知",
  56. "inputSchema": {
  57. "type": "object",
  58. "properties": {
  59. "sender": { "type": "string" },
  60. "mode": { "type": "string" }
  61. },
  62. "required": ["sender", "mode"]
  63. }
  64. },
  65. {
  66. "name": "change_email",
  67. "description": "更换通知邮箱,需传入合法邮箱地址:email",
  68. "inputSchema": {
  69. "type": "object",
  70. "properties": {
  71. "sender": { "type": "string" },
  72. "email": { "type": "string" }
  73. },
  74. "required": ["sender", "email"]
  75. }
  76. },
  77. {
  78. "name": "create_work_order",
  79. "description": "创建客服工单,遇到问题或建议可创建并等待人工处理,参数email为联系方式,工单标题title不超过20字,内容content不超过100字,标题和内容最好由你总结生成",
  80. "inputSchema": {
  81. "type": "object",
  82. "properties": {
  83. "sender": { "type": "string" },
  84. "email": { "type": "string" },
  85. "title": { "type": "string" },
  86. "content": { "type": "string" }
  87. },
  88. "required": ["sender", "email", "title", "content"]
  89. }
  90. }
  91. ]
  92. }
  93. } else if (method === 'tools/call') {
  94. const { name, arguments: args = {} } = params
  95. let output
  96. switch (name) {
  97. case "bind_account":
  98. output = await MCP.bind_account(args)
  99. break
  100. case "get_account_info":
  101. output = await MCP.get_account_info(args)
  102. break
  103. case "unbind_account":
  104. output = await MCP.unbind_account(args)
  105. break
  106. case "set_notification":
  107. output = await MCP.set_notification(args)
  108. break
  109. case "change_email":
  110. output = await MCP.change_email(args)
  111. break
  112. case "create_work_order":
  113. output = await MCP.create_work_order(args)
  114. break
  115. default:
  116. output = "未知工具"
  117. }
  118. result = { content: [{ type: "text", text: output }] }
  119. } else {
  120. result = { error: `未知方法: ${method}` }
  121. }
  122. // 返回标准 JSON-RPC 响应
  123. return res.json({
  124. jsonrpc: "2.0",
  125. id: req_id ?? null,
  126. result
  127. })
  128. } catch (e) {
  129. return res.json({
  130. jsonrpc: "2.0",
  131. id: req_id ?? null,
  132. error: { code: -32000, message: e.message }
  133. })
  134. }
  135. }
  136. }
  137. module.exports.McpRpc = McpRpc