McpRPC.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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": "Bind a user account when the user provides a bind code",
  26. "inputSchema": {
  27. "type": "object",
  28. "properties": {
  29. "sender": {
  30. "type": "string",
  31. "description": "Unique user identifier from the chat platform"
  32. },
  33. "bind_code": {
  34. "type": "string",
  35. "description": "Binding code provided by the user"
  36. }
  37. },
  38. "required": ["sender", "bind_code"]
  39. }
  40. },
  41. {
  42. "name": "unbind_account",
  43. "description": "Unbind the currently linked user account",
  44. "inputSchema": {
  45. "type": "object",
  46. "properties": {
  47. "sender": {
  48. "type": "string",
  49. "description": "Unique user identifier from the chat platform"
  50. }
  51. },
  52. "required": ["sender"]
  53. }
  54. },
  55. {
  56. "name": "get_account_info",
  57. "description": "Retrieve user account information such as name, student ID, account status, and running statistics",
  58. "inputSchema": {
  59. "type": "object",
  60. "properties": {
  61. "sender": {
  62. "type": "string",
  63. "description": "Unique user identifier from the chat platform"
  64. }
  65. },
  66. "required": ["sender"]
  67. }
  68. },
  69. {
  70. "name": "set_notification",
  71. "description": "Set notification preference when the user wants to enable or disable notifications",
  72. "inputSchema": {
  73. "type": "object",
  74. "properties": {
  75. "sender": {
  76. "type": "string",
  77. "description": "Unique user identifier from the chat platform"
  78. },
  79. "mode": {
  80. "type": "string",
  81. "enum": ["bot", "email", "none"],
  82. "description": "Notification mode: bot (chat bot), email (email notification), none (disable notifications)"
  83. }
  84. },
  85. "required": ["sender", "mode"]
  86. }
  87. },
  88. {
  89. "name": "change_email",
  90. "description": "Update the user's notification email address",
  91. "inputSchema": {
  92. "type": "object",
  93. "properties": {
  94. "sender": {
  95. "type": "string",
  96. "description": "Unique user identifier from the chat platform"
  97. },
  98. "email": {
  99. "type": "string",
  100. "format": "email",
  101. "description": "New email address for notifications"
  102. }
  103. },
  104. "required": ["sender", "email"]
  105. }
  106. },
  107. {
  108. "name": "create_work_order",
  109. "description": "Create a customer support ticket when the user reports a problem or suggestion",
  110. "inputSchema": {
  111. "type": "object",
  112. "properties": {
  113. "sender": {
  114. "type": "string",
  115. "description": "Unique user identifier from the chat platform"
  116. },
  117. "email": {
  118. "type": "string",
  119. "format": "email",
  120. "description": "User contact email"
  121. },
  122. "title": {
  123. "type": "string",
  124. "maxLength": 20,
  125. "description": "Short title of the issue (max 20 characters)"
  126. },
  127. "content": {
  128. "type": "string",
  129. "maxLength": 100,
  130. "description": "Detailed description of the issue (max 100 characters)"
  131. }
  132. },
  133. "required": ["sender", "email", "title", "content"]
  134. }
  135. },
  136. {
  137. "name": "change_auto_time",
  138. "description": "Set the automatic running start hour when the user wants to schedule running time",
  139. "inputSchema": {
  140. "type": "object",
  141. "properties": {
  142. "sender": {
  143. "type": "string",
  144. "description": "Unique user identifier from the chat platform"
  145. },
  146. "auto_time": {
  147. "type": "integer",
  148. "minimum": 7,
  149. "maximum": 23,
  150. "description": "Hour of day to start running (7-23)"
  151. }
  152. },
  153. "required": ["sender", "auto_time"]
  154. }
  155. },
  156. {
  157. "name": "change_auto_day",
  158. "description": "Set the days of the week for automatic running",
  159. "inputSchema": {
  160. "type": "object",
  161. "properties": {
  162. "sender": {
  163. "type": "string",
  164. "description": "Unique user identifier from the chat platform"
  165. },
  166. "auto_day": {
  167. "type": "array",
  168. "items": {
  169. "type": "integer",
  170. "minimum": 0,
  171. "maximum": 6
  172. },
  173. "description": "Days of week to run (0=Sunday, 6=Saturday)"
  174. }
  175. },
  176. "required": ["sender", "auto_day"]
  177. }
  178. }
  179. ]
  180. }
  181. } else if (method === 'tools/call') {
  182. const { name, arguments: args = {} } = params
  183. let output
  184. switch (name) {
  185. case "bind_account":
  186. output = await MCP.bind_account(args)
  187. break
  188. case "get_account_info":
  189. output = await MCP.get_account_info(args)
  190. break
  191. case "unbind_account":
  192. output = await MCP.unbind_account(args)
  193. break
  194. case "set_notification":
  195. output = await MCP.set_notification(args)
  196. break
  197. case "change_email":
  198. output = await MCP.change_email(args)
  199. break
  200. case "create_work_order":
  201. output = await MCP.create_work_order(args)
  202. break
  203. case "change_auto_time":
  204. output = await MCP.change_auto_time(args)
  205. break
  206. case "change_auto_day":
  207. output = await MCP.change_auto_day(args)
  208. break
  209. default:
  210. output = "未知工具"
  211. }
  212. result = { content: [{ type: "text", text: output }] }
  213. } else {
  214. result = { error: `未知方法: ${method}` }
  215. }
  216. // 返回标准 JSON-RPC 响应
  217. return res.json({
  218. jsonrpc: "2.0",
  219. id: req_id ?? null,
  220. result
  221. })
  222. } catch (e) {
  223. return res.json({
  224. jsonrpc: "2.0",
  225. id: req_id ?? null,
  226. error: { code: -32000, message: e.message }
  227. })
  228. }
  229. }
  230. }
  231. module.exports.McpRpc = McpRpc