McpRPC.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const API = require("../../../lib/API.js")
  2. const MCP = require("../../../lib/Lepao/WorkOrderMcp.js").WORKORDERMCP
  3. class McpRpc extends API {
  4. constructor() {
  5. super()
  6. this.noEncrypt()
  7. this.setPath('/workorderMcp')
  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: "runforgeWorkOrder", version: "1.0" }
  19. }
  20. } else if (method === 'tools/list') {
  21. result = {
  22. "tools": [
  23. {
  24. "name": "need_manual",
  25. "description": "提交转人工服务请求",
  26. "inputSchema": {
  27. "type": "object",
  28. "properties": {
  29. "sender": {
  30. "type": "string",
  31. "description": "Unique user identifier from the chat platform"
  32. },
  33. "order_id": {
  34. "type": "integer",
  35. "description": "Order ID of the work order to submit"
  36. }
  37. },
  38. "required": ["sender", "order_id"]
  39. }
  40. },
  41. {
  42. "name": "unbind_account",
  43. "description": "解绑被他人绑定的乐跑账号",
  44. "inputSchema": {
  45. "type": "object",
  46. "properties": {
  47. "sender": {
  48. "type": "string",
  49. "description": "Unique user identifier from the chat platform"
  50. },
  51. "student_num": {
  52. "type": "integer",
  53. "description": "Student number of the account to unbind"
  54. }
  55. },
  56. "required": ["sender", "student_num"]
  57. }
  58. },
  59. {
  60. "name": "get_account_info",
  61. "description": "按学号获取乐跑账号信息",
  62. "inputSchema": {
  63. "type": "object",
  64. "properties": {
  65. "sender": {
  66. "type": "string",
  67. "description": "Unique user identifier from the chat platform"
  68. },
  69. "student_num": {
  70. "type": "integer",
  71. "description": "Student number of the account to get info"
  72. }
  73. },
  74. "required": ["sender", "student_num"]
  75. }
  76. }
  77. ]
  78. }
  79. } else if (method === 'tools/call') {
  80. const { name, arguments: args = {} } = params
  81. let output
  82. switch (name) {
  83. case "need_manual":
  84. output = await MCP.need_manual(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. default:
  93. output = "未知工具"
  94. }
  95. result = { content: [{ type: "text", text: output }] }
  96. } else {
  97. result = { error: `未知方法: ${method}` }
  98. }
  99. // 返回标准 JSON-RPC 响应
  100. return res.json({
  101. jsonrpc: "2.0",
  102. id: req_id ?? null,
  103. result
  104. })
  105. } catch (e) {
  106. return res.json({
  107. jsonrpc: "2.0",
  108. id: req_id ?? null,
  109. error: { code: -32000, message: e.message }
  110. })
  111. }
  112. }
  113. }
  114. module.exports.McpRpc = McpRpc