http_request
Configure and send HTTP requests to REST APIs, specifying method, path, parameters, body, and headers for flexible API interaction.
Instructions
Make an HTTP request to the configured API
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| method | Yes | HTTP method | |
| path | Yes | API endpoint path | |
| params | No | Query parameters (for GET) or request body parameters | |
| body | No | Request body (for POST, PUT, PATCH) | |
| headers | No | Additional headers |
Implementation Reference
- src/index.ts:292-325 (handler)The handler function for the http_request tool. It validates that the REST client is configured, extracts method/path/params/body/headers from args, delegates to RestClient.makeRequest(), and formats the response.
private async makeHttpRequest(args: any) { if (!this.restClient) { throw new Error( "REST client not configured. Use configure_rest_client tool or provide configuration via CLI/environment variables." ); } const { method, path, params, body, headers } = args; const response = await this.restClient.makeRequest({ method, path, params, body, headers, }); return { content: [ { type: "text", text: JSON.stringify( { status: response.status, statusText: response.statusText, data: response.data, headers: response.headers, }, null, 2 ), }, ], }; } - src/rest-client.ts:89-135 (handler)The actual HTTP request execution logic in RestClient.makeRequest(). Uses axios to make GET/POST/PUT/DELETE/PATCH requests, handles error formatting, and returns ApiResponse.
async makeRequest<T = any>(options: RequestOptions): Promise<ApiResponse<T>> { const { method, path, params, body, headers } = options; try { let response: AxiosResponse<T>; const config = { headers: headers || {}, params: method === "GET" ? params : undefined, }; switch (method) { case "GET": response = await this.httpClient.get(path, config); break; case "POST": response = await this.httpClient.post(path, body || params, config); break; case "PUT": response = await this.httpClient.put(path, body || params, config); break; case "DELETE": response = await this.httpClient.delete(path, config); break; case "PATCH": response = await this.httpClient.patch(path, body || params, config); break; default: throw new Error(`Unsupported HTTP method: ${method}`); } return { data: response.data, status: response.status, statusText: response.statusText, headers: response.headers as Record<string, string>, }; } catch (error) { if (axios.isAxiosError(error)) { throw new Error( `HTTP ${error.response?.status || "Unknown"}: ${ error.response?.data?.message || error.message }` ); } throw error; } } - src/index.ts:128-158 (schema)Schema definition and registration of the http_request tool. Defines its name, description, and inputSchema (method enum, path string, optional params, body, headers). Required fields: method and path.
name: "http_request", description: "Make an HTTP request to the configured API", inputSchema: { type: "object", properties: { method: { type: "string", enum: ["GET", "POST", "PUT", "DELETE", "PATCH"], description: "HTTP method", }, path: { type: "string", description: "API endpoint path", }, params: { type: "object", description: "Query parameters (for GET) or request body parameters", }, body: { type: "object", description: "Request body (for POST, PUT, PATCH)", }, headers: { type: "object", description: "Additional headers", }, }, required: ["method", "path"], }, }, - src/types.ts:74-81 (schema)The RequestOptions interface that defines the type signature for the HTTP request parameters used by the handler.
// Request options export interface RequestOptions { method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; path: string; params?: Record<string, any>; body?: any; headers?: Record<string, string>; } - src/index.ts:220-262 (registration)The tool routing/dispatch in CallToolRequestSchema handler. The 'http_request' case (line 228-229) routes execution to makeHttpRequest().
this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { switch (name) { case "configure_rest_client": return await this.configureRestClient(args); case "http_request": return await this.makeHttpRequest(args); case "get_swagger_documentation": return await this.getSwaggerDocumentation(); case "search_endpoints": return await this.searchEndpoints(args); case "get_endpoint_info": return await this.getEndpointInfo(args); case "check_authentication": return await this.checkAuthentication(); case "logout": return await this.logout(); default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : "Unknown error" }`, }, ], isError: true, }; } });