UNPKG

bc_resource_mcp-beta

Version:

MCP server for Baichuan resource

176 lines (175 loc) 9.37 kB
// 封装 axios 请求, 设置baseURL, 设置超时时间, 设置请求头, 设置响应拦截器, 设置请求拦截器 import axios from "axios"; export class APIServer { config; instance; constructor(config) { this.config = config; this.instance = axios.create({ baseURL: "https://gateway-resource-platform.bcbook.cn", timeout: 10000, headers: {}, }); } async safeCall(fn) { try { const response = await fn(); if (response.status >= 400) { const errorData = response.data; const code = errorData.errorCode || -1; const message = errorData.message || "<unknown>"; throw new Error(`API error: ${code} ${message}`); } return response.data; } catch (error) { if (axios.isAxiosError(error)) { throw new Error(`Request failed: ${error.message}`); } throw error; } } async listBooks(params) { return this.safeCall(() => this.instance.get("/platform-webapi/page/books", { params: { ...params, moduleId: this.config.moduleId } })); } async listChapters(params) { return this.safeCall(() => this.instance.get("/platform-webapi/book/resource/getChapterAudios", { params: { ...params, moduleId: this.config.moduleId } })); // return new Promise<Result<Chapter[]>>((resolve, reject) => { // setTimeout(() => { // resolve({ // success: true, // message: "", // code: 200, // result: [ // { // id: "1712712034640326657", // name: "第一单元111", // parentId: "0", // type: "1", // children: [ // { // id: "1712712088592781313", // name: "1 古诗三首", // parentId: "1712712034640326657", // type: "1", // children: [ // { id: "1811314116563382274", name: "1.古诗三首——三衢道中", parentId: "1712712088592781313", type: "2", lock: 0 }, // { id: "1910687598473199617", name: "1.古诗三首——绝句", parentId: "1712712088592781313", type: "2", lock: 1 }, // { id: "1910687598473199618", name: "1.古诗三首——惠崇春江晚景", parentId: "1712712088592781313", type: "2", lock: 1 }, // ], // lock: 0, // }, // { id: "1910687598473199619", name: "2 燕子", parentId: "1712712034640326657", type: "2", lock: 1 }, // { id: "1910687598473199620", name: "3 荷花", parentId: "1712712034640326657", type: "2", lock: 1 }, // { id: "1910687598473199621", name: "4* 昆虫备忘录", parentId: "1712712034640326657", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712712392833888257", // name: "第二单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199622", name: "5 守株待兔", parentId: "1712712392833888257", type: "2", lock: 1 }, // { id: "1910687598473199623", name: "6 陶罐和铁罐", parentId: "1712712392833888257", type: "2", lock: 1 }, // { id: "1910687598473199624", name: "7 鹿角和鹿腿", parentId: "1712712392833888257", type: "2", lock: 1 }, // { id: "1910687598473199625", name: "8* 池子与河流", parentId: "1712712392833888257", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712712743109111809", // name: "第三单元", // parentId: "0", // type: "1", // children: [ // { // id: "1712712795042476034", // name: "9 古诗三首", // parentId: "1712712743109111809", // type: "1", // children: [ // { id: "1910687598473199626", name: "9.古诗三首——元日", parentId: "1712712795042476034", type: "2", lock: 1 }, // { id: "1910687598473199627", name: "9.古诗三首——清明", parentId: "1712712795042476034", type: "2", lock: 1 }, // { id: "1910687598473199628", name: "9.古诗三首——九月九日忆山东兄弟", parentId: "1712712795042476034", type: "2", lock: 1 }, // ], // lock: 0, // }, // { id: "1910687598473199629", name: "10 纸的发明", parentId: "1712712743109111809", type: "2", lock: 1 }, // { id: "1910687598473199630", name: "11 赵州桥", parentId: "1712712743109111809", type: "2", lock: 1 }, // { id: "1910687598473199631", name: "12* 一幅名扬中外的画", parentId: "1712712743109111809", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712713191132696577", // name: "第四单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199632", name: "13 花钟", parentId: "1712713191132696577", type: "2", lock: 1 }, // { id: "1910687598473199633", name: "14 蜜蜂", parentId: "1712713191132696577", type: "2", lock: 1 }, // { id: "1910687598473199634", name: "15* 小虾", parentId: "1712713191132696577", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712713549782315010", // name: "第五单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199635", name: "16 宇宙的另一边", parentId: "1712713549782315010", type: "2", lock: 1 }, // { id: "1910687598473199636", name: "17 我变成了一棵树", parentId: "1712713549782315010", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712713762962518017", // name: "第六单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199637", name: "18 童年的水墨画", parentId: "1712713762962518017", type: "2", lock: 1 }, // { id: "1910687598473199638", name: "19 剃头大师", parentId: "1712713762962518017", type: "2", lock: 1 }, // { id: "1910687598473199639", name: "20 肥皂泡", parentId: "1712713762962518017", type: "2", lock: 1 }, // { id: "1910687598473199640", name: "21* 我不能失信", parentId: "1712713762962518017", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712714061567094785", // name: "第七单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199641", name: "22 我们奇妙的世界", parentId: "1712714061567094785", type: "2", lock: 1 }, // { id: "1910687598473199642", name: "23 海底世界", parentId: "1712714061567094785", type: "2", lock: 1 }, // { id: "1910687598473199643", name: "24 火烧云", parentId: "1712714061567094785", type: "2", lock: 1 }, // ], // lock: 0, // }, // { // id: "1712714284322385921", // name: "第八单元", // parentId: "0", // type: "1", // children: [ // { id: "1910687598473199644", name: "25 慢性子裁缝和急性子顾客", parentId: "1712714284322385921", type: "2", lock: 1 }, // { id: "1910687598473199645", name: "26* 方帽子店", parentId: "1712714284322385921", type: "2", lock: 1 }, // { id: "1910687598473199646", name: "27 漏", parentId: "1712714284322385921", type: "2", lock: 1 }, // { id: "1910687598473199647", name: "28* 枣核", parentId: "1712714284322385921", type: "2", lock: 1 }, // ], // lock: 0, // }, // ], // timestamp: 1744378396994, // }); // }, 1000); // }); } async fetchResource(id) { return this.safeCall(() => this.instance.get("/platform-webapi/book/resource/getAudioDetail", { params: { id } })); } }