request
Send HTTP requests with customizable methods, headers, and data formats to interact with web APIs and fetch resources.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | ||
| method | No | GET | |
| headers | No | ||
| formData | No | ||
| jsonData | No | ||
| cookies | No |
Implementation Reference
- src/handler.js:5-80 (handler)The fetchApiHandler function implements the core logic of the 'request' tool, handling HTTP requests with support for methods, headers, formData, jsonData, cookies, response parsing (JSON/text/binary as base64), and error handling.export async function fetchApiHandler({ url, method, headers = {}, formData, jsonData, cookies }) { try { // 准备请求选项 const options = { method, headers: { ...headers }, }; // 处理cookies if (cookies && Object.keys(cookies).length > 0) { const cookieString = Object.entries(cookies) .map(([key, value]) => `${key}=${value}`) .join('; '); options.headers.Cookie = cookieString; } // 处理请求体 if (method !== 'GET' && method !== 'HEAD') { if (formData && Object.keys(formData).length > 0) { // 处理表单数据 const form = new URLSearchParams(); Object.entries(formData).forEach(([key, value]) => { form.append(key, value); }); options.body = form.toString(); options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; } else if (jsonData) { // 处理JSON数据 options.body = JSON.stringify(jsonData); options.headers['Content-Type'] = 'application/json'; } } // 发送请求 const response = await fetch(url, options); // 获取响应头 const responseHeaders = {}; response.headers.forEach((value, key) => { responseHeaders[key] = value; }); // 尝试解析响应体 let data; const contentType = response.headers.get('content-type'); if (contentType?.includes('application/json')) { data = await response.json(); } else if (contentType?.includes('text/')) { data = await response.text(); } else { // 对于二进制数据,转换为base64字符串 const buffer = await response.arrayBuffer(); data = Buffer.from(buffer).toString('base64'); } // 返回结果 return { status: response.status, statusText: response.statusText, headers: responseHeaders, data, }; } catch (error) { // 处理错误 return { status: 500, statusText: 'Internal Server Error', headers: {}, data: { error: error.message, stack: error.stack, }, }; } }
- src/index.js:15-22 (schema)Zod input schema defining validation for 'request' tool parameters: url (required), method (default GET), optional headers, formData, jsonData, cookies.const inputSchema = { url: z.string().url(), method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']).default('GET'), headers: z.record(z.string()).optional(), formData: z.record(z.string()).optional(), jsonData: z.any().optional(), cookies: z.record(z.string()).optional(), };
- src/index.js:25-37 (registration)MCP server.tool registration for 'request' tool, providing schema, a thin wrapper handler calling fetchApiHandler and formatting MCP response, and a detailed description.server.tool( 'request', inputSchema, async (params) => { const result = await fetchApiHandler(params); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; }, { description: '发送API请求并返回结果。使用方法:传入url(必需)、method(可选,默认GET)、headers(可选)、formData(可选,用于表单数据)、jsonData(可选,用于JSON数据)和cookies(可选)参数。例如:{ "url": "https://example.com/api", "method": "POST", "headers": {"Content-Type": "application/json"}, "jsonData": {"key": "value"}, "cookies": {"key": "value"} }。GET请求示例:{ "url": "https://example.com/api" }。返回包含status、statusText、headers和data的响应对象。' } );