McpRPC.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_area",
  138. "description": "Set the running area for the user. If the user requests a random area, use '随机分配'",
  139. "inputSchema": {
  140. "type": "object",
  141. "properties": {
  142. "sender": {
  143. "type": "string",
  144. "description": "Unique user identifier from the chat platform"
  145. },
  146. "area": {
  147. "type": "string",
  148. "enum": [
  149. "兰花湖校区跑区",
  150. "主校区北跑区",
  151. "主校区南跑区",
  152. "重庆工商大学茶园校区",
  153. "随机分配"
  154. ],
  155. "description": "Running area. Must be one of the predefined areas, or '随机分配' for random assignment"
  156. }
  157. },
  158. "required": ["sender", "area"]
  159. }
  160. },
  161. {
  162. "name": "change_auto_time",
  163. "description": "Set the automatic running start hour. Use -1 if the user wants a random time assigned",
  164. "inputSchema": {
  165. "type": "object",
  166. "properties": {
  167. "sender": {
  168. "type": "string",
  169. "description": "Unique user identifier from the chat platform"
  170. },
  171. "auto_time": {
  172. "type": "integer",
  173. "minimum": -1,
  174. "maximum": 23,
  175. "description": "Hour of day to start running (7-23), or -1 to assign a random time"
  176. }
  177. },
  178. "required": ["sender", "auto_time"]
  179. }
  180. },
  181. {
  182. "name": "change_auto_day",
  183. "description": "Set the days of the week for automatic running",
  184. "inputSchema": {
  185. "type": "object",
  186. "properties": {
  187. "sender": {
  188. "type": "string",
  189. "description": "Unique user identifier from the chat platform"
  190. },
  191. "auto_day": {
  192. "type": "array",
  193. "items": {
  194. "type": "integer",
  195. "minimum": 0,
  196. "maximum": 6
  197. },
  198. "description": "Days of week to run (0=Sunday, 6=Saturday)"
  199. }
  200. },
  201. "required": ["sender", "auto_day"]
  202. }
  203. }
  204. ]
  205. }
  206. } else if (method === 'tools/call') {
  207. const { name, arguments: args = {} } = params
  208. let output
  209. switch (name) {
  210. case "bind_account":
  211. output = await MCP.bind_account(args)
  212. break
  213. case "get_account_info":
  214. output = await MCP.get_account_info(args)
  215. break
  216. case "unbind_account":
  217. output = await MCP.unbind_account(args)
  218. break
  219. case "set_notification":
  220. output = await MCP.set_notification(args)
  221. break
  222. case "change_email":
  223. output = await MCP.change_email(args)
  224. break
  225. case "create_work_order":
  226. output = await MCP.create_work_order(args)
  227. break
  228. case "change_auto_time":
  229. output = await MCP.change_auto_time(args)
  230. break
  231. case "change_auto_day":
  232. output = await MCP.change_auto_day(args)
  233. break
  234. default:
  235. output = "未知工具"
  236. }
  237. result = { content: [{ type: "text", text: output }] }
  238. } else {
  239. result = { error: `未知方法: ${method}` }
  240. }
  241. // 返回标准 JSON-RPC 响应
  242. return res.json({
  243. jsonrpc: "2.0",
  244. id: req_id ?? null,
  245. result
  246. })
  247. } catch (e) {
  248. return res.json({
  249. jsonrpc: "2.0",
  250. id: req_id ?? null,
  251. error: { code: -32000, message: e.message }
  252. })
  253. }
  254. }
  255. }
  256. module.exports.McpRpc = McpRpc