request
Send customizable HTTP requests with methods like GET, POST, or DELETE, set headers, include JSON or form data, and receive parsed responses using the Fetch API MCP Server.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cookies | No | ||
| formData | No | ||
| headers | No | ||
| jsonData | No | ||
| method | No | GET | |
| url | Yes |
Implementation Reference
- src/handler.js:5-80 (handler)The core handler function fetchApiHandler that executes the HTTP request logic: constructs fetch options handling method, headers, cookies, formData or jsonData; performs fetch; parses response data based on content-type (JSON, text, or base64 binary); returns structured object with status, statusText, headers, and data; includes 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-based input schema defining parameters for the 'request' tool: required url (validated as URL string), optional method (enum with GET default), headers, formData, jsonData (any), and cookies (all records of strings).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': specifies name, input schema, thin async wrapper handler that calls fetchApiHandler and returns MCP-formatted text content (JSON stringified result), and detailed usage description in Chinese.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的响应对象。' } );