( path: string, method: string = "GET", data?: object )
| 9 | import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 10 | |
| 11 | export async function makeAdminAPIRequest( |
| 12 | path: string, |
| 13 | method: string = "GET", |
| 14 | data?: object |
| 15 | ): Promise<CallToolResult> { |
| 16 | const baseUrl = `${APISIX_SERVER_HOST}:${APISIX_ADMIN_API_PORT}${APISIX_ADMIN_API_PREFIX}`; |
| 17 | const url = `${baseUrl}${path}`; |
| 18 | |
| 19 | try { |
| 20 | const response = await axios({ |
| 21 | method, |
| 22 | url, |
| 23 | data, |
| 24 | headers: { |
| 25 | "X-API-KEY": APISIX_ADMIN_KEY, |
| 26 | "Content-Type": "application/json", |
| 27 | }, |
| 28 | }); |
| 29 | |
| 30 | return { |
| 31 | content: [ |
| 32 | { |
| 33 | type: "text", |
| 34 | text: JSON.stringify(response.data, null, 2), |
| 35 | }, |
| 36 | ], |
| 37 | }; |
| 38 | } catch (error) { |
| 39 | if (axios.isAxiosError(error)) { |
| 40 | console.error(`Request failed: ${method} ${url}`); |
| 41 | console.error( |
| 42 | `Status: ${error.response?.status}, Error: ${error.message}` |
| 43 | ); |
| 44 | |
| 45 | if (error.response?.data) { |
| 46 | try { |
| 47 | const stringifiedData = JSON.stringify(error.response.data); |
| 48 | console.error(`Response data: ${stringifiedData}`); |
| 49 | } catch { |
| 50 | console.error(`Response data: [Cannot parse as JSON]`); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return { |
| 55 | isError: true, |
| 56 | content: [ |
| 57 | { |
| 58 | type: "text", |
| 59 | text: JSON.stringify( |
| 60 | `Status: ${error.response?.status}\nMessage: ${error.message} |
| 61 | Data:\n${JSON.stringify(error.response?.data || {}, null, 2)}`, |
| 62 | null, |
| 63 | 2 |
| 64 | ), |
| 65 | }, |
| 66 | ], |
| 67 | }; |
| 68 | } else { |
no outgoing calls
no test coverage detected