McpRPC.js 16 KB

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