McpRPC.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. const API = require('../../../lib/API')
  2. const MCP = require('../../../lib/AIChat/AiAssistantMcp').AI_ASSISTANT_MCP
  3. const sender = {
  4. type: 'string',
  5. description: '当前会话中的 RunForge 用户 uuid。涉及用户私有数据的工具必须传入该字段,并且只能读取或修改该用户拥有的数据。'
  6. }
  7. const pageProps = {
  8. current: { type: 'integer', minimum: 1, description: '页码。未传时默认为 1。' },
  9. pagesize: { type: 'integer', minimum: 1, maximum: 20, description: '每页数量。未传时默认为 10,最大 20。' }
  10. }
  11. const tools = [
  12. {
  13. name: 'list_lepao_accounts',
  14. description: '查询当前 RunForge 用户绑定的乐跑账号列表。用户询问账号状态、跑区、自动乐跑设置、目标次数、乐跑时间、通知方式或绑定邮箱时,优先使用此工具。支持模糊筛选和分页返回,响应包含 list 和 pagination。',
  15. inputSchema: {
  16. type: 'object',
  17. properties: {
  18. sender,
  19. student_num: { type: 'string', description: '学号模糊筛选。' },
  20. name: { type: 'string', description: '姓名模糊筛选。' },
  21. area: { type: 'string', description: '跑区筛选。用户指定跑区时使用精确跑区名称;用户说随机跑区时使用空值。' },
  22. state: { type: 'integer', description: '-1 或不传表示全部;0 表示需重新登录,1 表示正常,2 表示异常。' },
  23. auto_run: { type: 'integer', enum: [0, 1], description: '按自动乐跑开关筛选:1 开启,0 关闭。' },
  24. ...pageProps
  25. },
  26. required: ['sender']
  27. }
  28. },
  29. {
  30. name: 'list_lepao_records',
  31. description: '查询当前用户拥有的乐跑记录。用户询问乐跑历史、成功或失败结果、乐跑模式、乐跑时间,或某个绑定账号的记录时使用。支持账号、姓名、结果文本模糊筛选,支持毫秒时间戳范围筛选和分页返回。',
  32. inputSchema: {
  33. type: 'object',
  34. properties: {
  35. sender,
  36. lepao_account: { type: 'string', description: '乐跑学号/账号模糊筛选。' },
  37. name: { type: 'string', description: '姓名模糊筛选。' },
  38. result: { type: 'string', description: '结果文本模糊筛选,适合查询成功、失败原因等。' },
  39. start_time: { type: 'integer', description: '开始时间戳,毫秒,包含该时间。' },
  40. end_time: { type: 'integer', description: '结束时间戳,毫秒,不包含该时间。' },
  41. ...pageProps
  42. },
  43. required: ['sender']
  44. }
  45. },
  46. {
  47. name: 'list_lepao_count_ledger',
  48. description: '查询当前用户的乐跑次数余额变动流水。用户询问乐跑次数为什么变化、如何扣减/返还/充值,或想看次数交易历史时使用。支持业务类型、备注关键词、毫秒时间戳范围和分页查询,不暴露内部 id。',
  49. inputSchema: {
  50. type: 'object',
  51. properties: {
  52. sender,
  53. biz_type: { type: 'string', description: '业务类型精确筛选,例如消费、返还、充值或流水表定义的其它类型。' },
  54. remark: { type: 'string', description: '备注关键词模糊筛选。' },
  55. start_time: { type: 'integer', description: '开始时间戳,毫秒,包含该时间。' },
  56. end_time: { type: 'integer', description: '结束时间戳,毫秒,包含该时间。' },
  57. ...pageProps
  58. },
  59. required: ['sender']
  60. }
  61. },
  62. {
  63. name: 'list_goods',
  64. description: '查询当前上架且可购买的商品列表。用户询问价格、套餐、库存、分类、权益或购买选项前使用。支持关键词、分类筛选和分页返回,响应不包含内部 id 和不必要的媒体字段。',
  65. inputSchema: {
  66. type: 'object',
  67. properties: {
  68. keyword: { type: 'string', description: '商品名称关键词模糊筛选。' },
  69. category: { type: 'string', description: '商品分类精确筛选。' },
  70. ...pageProps
  71. }
  72. }
  73. },
  74. {
  75. name: 'list_my_orders',
  76. description: '查询当前用户创建的订单列表。用户询问支付状态、订单历史、退款状态,或需要定位公开订单号时使用。支持订单状态、订单号筛选和分页返回。',
  77. inputSchema: {
  78. type: 'object',
  79. properties: {
  80. sender,
  81. state: { type: 'integer', description: '-1 或不传表示全部;0 待支付,1 已支付,2 已取消,3 异常,4 已退款。' },
  82. order_id: { type: 'string', description: '公开订单号精确筛选。这里是用户可见的 order_no/orderId,不是数据库内部 id。' },
  83. ...pageProps
  84. },
  85. required: ['sender']
  86. }
  87. },
  88. {
  89. name: 'save_lepao_account',
  90. description: '新增或部分更新当前用户拥有的一个乐跑账号。使用 student_num 识别账号。只传用户明确要设置的字段,未传字段会保持不变。已有账号可更新邮箱、跑区、自动乐跑开关/时间/星期、目标次数、通知方式或备注。不确定账号现状时先调用 list_lepao_accounts,不要用此工具读取账号数据。',
  91. inputSchema: {
  92. type: 'object',
  93. properties: {
  94. sender,
  95. student_num: { type: 'string', description: '乐跑学号/账号。必填,用于识别账号。' },
  96. email: { type: 'string', description: '该乐跑账号的通知邮箱。notice_type 为 email 时必填,且不能使用 icloud.com 域名。' },
  97. area: { type: 'string', description: '要使用的跑区。取值只能是:["兰花湖校区跑区", "主校区北跑区", "主校区南跑区", "重庆工商大学茶园校区"]。用户要求随机跑区时使用空值。' },
  98. auto_time: { type: 'integer', minimum: -1, maximum: 23, description: '自动乐跑开始小时,范围 7-21;使用 -1 表示随机时间。' },
  99. auto_run: { type: 'integer', enum: [0, 1], description: '自动乐跑开关:1 开启,0 关闭。' },
  100. target_count: { type: 'number', minimum: 0, maximum: 99, description: '目标总乐跑次数,范围 0-99。开启自动乐跑时,目标次数不能小于或等于账号已累计完成次数,除非设置为 0。' },
  101. auto_day: { type: 'array', items: { type: 'integer', minimum: 0, maximum: 6 }, description: '自动乐跑星期。0 周日,1 周一,依此类推,6 周六。' },
  102. notice_type: { type: 'string', enum: ['email', 'none', 'bot'], description: '乐跑后的通知渠道:email 邮件,bot 机器人,none 不通知。' },
  103. notes: { type: 'string', description: '该账号的用户可见备注。' }
  104. },
  105. required: ['sender', 'student_num']
  106. }
  107. },
  108. // {
  109. // name: 'save_power_task',
  110. // description: '新增或部分更新当前用户的电费余额监控任务。已有任务可用 task_id 兼容识别,也可用 area + building + room 识别。只传用户要设置的字段,未传字段保持不变。新增任务时必须提供 area、building、room 和 lowest 阈值。',
  111. // inputSchema: {
  112. // type: 'object',
  113. // properties: {
  114. // sender,
  115. // task_id: { type: 'integer', description: '已有任务 id,主要用于兼容。用户提供区域、楼栋、房间时优先使用 area + building + room。' },
  116. // area: { type: 'string', description: '校区或电费区域名称。' },
  117. // building: { type: 'string', description: '电费系统中的楼栋标识或名称。' },
  118. // room: { type: 'string', description: '电费系统中的房间标识或名称。' },
  119. // email: { type: 'string', description: '用于接收低余额提醒的邮箱。' },
  120. // lowest: { type: 'number', description: '低余额提醒阈值,余额低于该值时后端可能发送提醒。' },
  121. // notes: { type: 'string', description: '该电费任务的用户可见备注。' }
  122. // },
  123. // required: ['sender']
  124. // }
  125. // },
  126. {
  127. name: 'list_power_bills',
  128. description: '查询当前用户的电费监控任务列表。可按校区/电费区域、楼栋、房间筛选,返回分页任务列表;只用于查看任务本身和当前余额信息,不返回账单变动记录。查询某寝室电费账单记录请使用 list_power_bill_records。',
  129. inputSchema: {
  130. type: 'object',
  131. properties: {
  132. sender,
  133. area: { type: 'string', description: '校区或电费区域精确筛选。' },
  134. building: { type: 'string', description: '楼栋精确筛选。' },
  135. room: { type: 'string', description: '房间精确筛选。' },
  136. ...pageProps
  137. },
  138. required: ['sender']
  139. }
  140. },
  141. {
  142. name: 'list_power_bill_records',
  143. description: '查询某个寝室/房间的电费账单或余额变动记录。必须提供 sender、area、building、room 精确定位当前用户拥有的电费任务,建议先调用 list_power_bills 获取准确的寝室信息;返回匹配任务和分页 records。不暴露内部任务 id 或记录 id。',
  144. inputSchema: {
  145. type: 'object',
  146. properties: {
  147. sender,
  148. area: { type: 'string', description: '校区或电费区域,必填,需精确匹配。' },
  149. building: { type: 'string', description: '楼栋,必填,需精确匹配。' },
  150. room: { type: 'string', description: '寝室/房间,必填,需精确匹配。' },
  151. ...pageProps
  152. },
  153. required: ['sender', 'area', 'building', 'room']
  154. }
  155. },
  156. {
  157. name: 'send_lepao_count',
  158. description: '向其他用户赠送乐跑次数,系统审核通过后到账。需提供接收方用户名和赠送次数,次数范围为 1-9999。',
  159. inputSchema: {
  160. type: 'object',
  161. properties: {
  162. sender,
  163. username: { type: 'string', description: '接收方用户名,需精确匹配。' },
  164. count: { type: 'integer', minimum: 1, maximum: 9999, description: '赠送的乐跑次数,范围 1-9999。' }
  165. },
  166. required: ['sender', 'username', 'count']
  167. }
  168. },
  169. {
  170. name: 'list_send_count_requests',
  171. description: '查询当前用户的乐跑次数赠送记录。可按赠送方向和审核状态筛选,返回分页列表。',
  172. inputSchema: {
  173. type: 'object',
  174. properties: {
  175. sender,
  176. direction: { type: 'string', enum: ['sent', 'received'], description: '赠送方向筛选:sent 我发出的,received 我收到的。不传表示全部。' },
  177. status: { type: 'string', enum: ['pending', 'approved', 'rejected'], description: '审核状态筛选:pending 待审核,approved 已通过,rejected 已拒绝。不传表示全部。' },
  178. ...pageProps
  179. },
  180. required: ['sender']
  181. }
  182. },
  183. {
  184. name: 'list_work_orders',
  185. description: '查询当前用户提交的客服工单列表。返回分页列表,包含工单编号、标题、状态、邮箱、创建时间和更新时间。',
  186. inputSchema: {
  187. type: 'object',
  188. properties: {
  189. sender,
  190. ...pageProps
  191. },
  192. required: ['sender']
  193. }
  194. },
  195. {
  196. name: 'get_work_order_detail',
  197. description: '查询当前用户某个客服工单详情。必须提供 work_order_id,只能查询当前 sender 自己创建的工单;返回消息列表和消息发送者 userInfo。',
  198. inputSchema: {
  199. type: 'object',
  200. properties: {
  201. sender,
  202. work_order_id: { type: 'integer', minimum: 1, description: '工单编号,即 list_work_orders 返回的 work_order_id。' }
  203. },
  204. required: ['sender', 'work_order_id']
  205. }
  206. },
  207. {
  208. name: 'query_qxs_book_list',
  209. description: '使用用户在当前会话中提供的趣选书账号密码查询教材/书单。仅在用户明确要求查询教材、课程用书信息,并提供 username/password 时使用。查询到的结果会分页返回。',
  210. inputSchema: {
  211. type: 'object',
  212. properties: {
  213. sender,
  214. username: { type: 'string', description: '用户提供的趣选书用户名。' },
  215. password: { type: 'string', description: '用户为本次查询提供的趣选书密码。' },
  216. ...pageProps
  217. },
  218. required: ['sender', 'username', 'password']
  219. }
  220. },
  221. {
  222. name: 'submit_work_order',
  223. description: '为当前用户提交客服工单。当其它工具无法解决问题,或问题需要人工处理时使用。标题应简洁,内容应包含必要上下文。用户可在工单列表查看处理进度。',
  224. inputSchema: {
  225. type: 'object',
  226. properties: {
  227. sender,
  228. title: { type: 'string', maxLength: 80, description: '简短工单标题,最多 80 个字符。' },
  229. content: { type: 'string', maxLength: 4000, description: '详细问题描述和相关上下文,最多 4000 个字符。' },
  230. email: { type: 'string', description: '该工单的可选联系邮箱。' },
  231. files: { type: 'array', items: { type: 'string' }, maxItems: 6, description: '与问题相关的可选上传文件路径或 URL,最多 6 个。' }
  232. },
  233. required: ['sender', 'title', 'content']
  234. }
  235. }
  236. ]
  237. class McpRpc extends API {
  238. constructor() {
  239. super()
  240. this.noEncrypt()
  241. this.setPath('/aiAssistantMcp')
  242. this.setMethod('POST')
  243. }
  244. async onRequest(req, res) {
  245. const { method, params = {}, id: reqId } = req.body || {}
  246. try {
  247. let result
  248. if (method === 'initialize') {
  249. result = {
  250. protocolVersion: '2024-11-05',
  251. capabilities: { tools: {} },
  252. serverInfo: { name: 'runforgeAiAssistant', version: '1.0' }
  253. }
  254. } else if (method === 'tools/list') {
  255. result = { tools }
  256. } else if (method === 'tools/call') {
  257. const { name, arguments: args = {} } = params
  258. if (!MCP[name] || typeof MCP[name] !== 'function') {
  259. result = { content: [{ type: 'text', text: '未知工具' }] }
  260. } else {
  261. const output = await MCP[name](args)
  262. result = { content: [{ type: 'text', text: output }] }
  263. }
  264. } else {
  265. result = { error: `未知方法: ${method}` }
  266. }
  267. return res.json({ jsonrpc: '2.0', id: reqId ?? null, result })
  268. } catch (err) {
  269. return res.json({
  270. jsonrpc: '2.0',
  271. id: reqId ?? null,
  272. error: { code: -32000, message: err.message }
  273. })
  274. }
  275. }
  276. }
  277. module.exports.McpRpc = McpRpc