McpRPC.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: "1.0",
  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. }
  67. } else if (method === 'tools/call') {
  68. const { name, arguments: args = {} } = params
  69. let output
  70. switch (name) {
  71. case "bind_account":
  72. output = await MCP.bind_account(args)
  73. break
  74. case "get_account_info":
  75. output = await MCP.get_account_info(args)
  76. break
  77. case "unbind_account":
  78. output = await MCP.unbind_account(args)
  79. break
  80. case "set_notification":
  81. output = await MCP.set_notification(args)
  82. break
  83. default:
  84. output = "未知工具"
  85. }
  86. result = { content: [{ type: "text", text: output }] }
  87. } else {
  88. result = { error: `未知方法: ${method}` }
  89. }
  90. // 返回标准 JSON-RPC 响应
  91. return res.json({
  92. jsonrpc: "2.0",
  93. id: req_id ?? null,
  94. result
  95. })
  96. } catch (e) {
  97. return res.json({
  98. jsonrpc: "2.0",
  99. id: req_id ?? null,
  100. error: { code: -32000, message: e.message }
  101. })
  102. }
  103. }
  104. }
  105. module.exports.McpRpc = McpRpc