McpRPC.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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: '该乐跑账号的通知邮箱。' },
  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', description: '目标总乐跑次数。达到目标后后端逻辑可能自动关闭自动乐跑。' },
  101. auto_day: { type: 'array', items: { type: 'integer', minimum: 0, maximum: 6 }, description: '自动乐跑星期。0 周日,1 周一,依此类推,6 周六。' },
  102. notice_type: { type: 'string', enum: ['email', 'none'], description: '乐跑后的通知渠道:email 邮件,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 精确定位当前用户拥有的电费任务;返回匹配任务和分页 records。所有时间字段均以北京时间 YYYY-MM-DD HH:mm:ss 返回,不暴露内部任务 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: 'query_qxs_book_list',
  158. description: '使用用户在当前会话中提供的趣选书账号密码查询教材/书单。仅在用户明确要求查询教材、课程用书信息,并提供 username/password 时使用。查询到的结果会分页返回。',
  159. inputSchema: {
  160. type: 'object',
  161. properties: {
  162. sender,
  163. username: { type: 'string', description: '用户提供的趣选书用户名。' },
  164. password: { type: 'string', description: '用户为本次查询提供的趣选书密码。' },
  165. ...pageProps
  166. },
  167. required: ['sender', 'username', 'password']
  168. }
  169. },
  170. {
  171. name: 'submit_work_order',
  172. description: '为当前用户提交客服工单。当其它工具无法解决问题,或问题需要人工处理时使用。标题应简洁,内容应包含必要上下文。用户可在工单列表查看处理进度。',
  173. inputSchema: {
  174. type: 'object',
  175. properties: {
  176. sender,
  177. title: { type: 'string', maxLength: 80, description: '简短工单标题,最多 80 个字符。' },
  178. content: { type: 'string', maxLength: 4000, description: '详细问题描述和相关上下文,最多 4000 个字符。' },
  179. email: { type: 'string', description: '该工单的可选联系邮箱。' },
  180. files: { type: 'array', items: { type: 'string' }, maxItems: 6, description: '与问题相关的可选上传文件路径或 URL,最多 6 个。' }
  181. },
  182. required: ['sender', 'title', 'content']
  183. }
  184. }
  185. ]
  186. class McpRpc extends API {
  187. constructor() {
  188. super()
  189. this.noEncrypt()
  190. this.setPath('/aiAssistantMcp')
  191. this.setMethod('POST')
  192. }
  193. async onRequest(req, res) {
  194. const { method, params = {}, id: reqId } = req.body || {}
  195. try {
  196. let result
  197. if (method === 'initialize') {
  198. result = {
  199. protocolVersion: '2024-11-05',
  200. capabilities: { tools: {} },
  201. serverInfo: { name: 'runforgeAiAssistant', version: '1.0' }
  202. }
  203. } else if (method === 'tools/list') {
  204. result = { tools }
  205. } else if (method === 'tools/call') {
  206. const { name, arguments: args = {} } = params
  207. if (!MCP[name] || typeof MCP[name] !== 'function') {
  208. result = { content: [{ type: 'text', text: '未知工具' }] }
  209. } else {
  210. const output = await MCP[name](args)
  211. result = { content: [{ type: 'text', text: output }] }
  212. }
  213. } else {
  214. result = { error: `未知方法: ${method}` }
  215. }
  216. return res.json({ jsonrpc: '2.0', id: reqId ?? null, result })
  217. } catch (err) {
  218. return res.json({
  219. jsonrpc: '2.0',
  220. id: reqId ?? null,
  221. error: { code: -32000, message: err.message }
  222. })
  223. }
  224. }
  225. }
  226. module.exports.McpRpc = McpRpc