McpRPC.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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": "unbind_account",
  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. "student_num": {
  34. "type": "integer",
  35. "description": "Student number of the account to unbind"
  36. }
  37. },
  38. "required": ["sender", "student_num"]
  39. }
  40. },
  41. {
  42. "name": "get_account_info",
  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 get info"
  54. }
  55. },
  56. "required": ["sender", "student_num"]
  57. }
  58. }
  59. ]
  60. }
  61. } else if (method === 'tools/call') {
  62. const { name, arguments: args = {} } = params
  63. let output
  64. switch (name) {
  65. case "get_account_info":
  66. output = await MCP.get_account_info(args)
  67. break
  68. case "unbind_account":
  69. output = await MCP.unbind_account(args)
  70. break
  71. default:
  72. output = "未知工具"
  73. }
  74. result = { content: [{ type: "text", text: output }] }
  75. } else {
  76. result = { error: `未知方法: ${method}` }
  77. }
  78. // 返回标准 JSON-RPC 响应
  79. return res.json({
  80. jsonrpc: "2.0",
  81. id: req_id ?? null,
  82. result
  83. })
  84. } catch (e) {
  85. return res.json({
  86. jsonrpc: "2.0",
  87. id: req_id ?? null,
  88. error: { code: -32000, message: e.message }
  89. })
  90. }
  91. }
  92. }
  93. module.exports.McpRpc = McpRpc