McpRPC.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  79. } else if (method === 'tools/call') {
  80. const { name, arguments: args = {} } = params
  81. let output
  82. switch (name) {
  83. case "bind_account":
  84. output = await MCP.bind_account(args)
  85. break
  86. case "get_account_info":
  87. output = await MCP.get_account_info(args)
  88. break
  89. case "unbind_account":
  90. output = await MCP.unbind_account(args)
  91. break
  92. case "set_notification":
  93. output = await MCP.set_notification(args)
  94. break
  95. case "change_email":
  96. output = await MCP.change_email(args)
  97. break
  98. default:
  99. output = "未知工具"
  100. }
  101. result = { content: [{ type: "text", text: output }] }
  102. } else {
  103. result = { error: `未知方法: ${method}` }
  104. }
  105. // 返回标准 JSON-RPC 响应
  106. return res.json({
  107. jsonrpc: "2.0",
  108. id: req_id ?? null,
  109. result
  110. })
  111. } catch (e) {
  112. return res.json({
  113. jsonrpc: "2.0",
  114. id: req_id ?? null,
  115. error: { code: -32000, message: e.message }
  116. })
  117. }
  118. }
  119. }
  120. module.exports.McpRpc = McpRpc