read_url
Retrieve content from a web page by providing its URL. Specify a timeout to control request duration.
Instructions
Read URL
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| timeout | No |
Implementation Reference
- The main handler for read_url – readFileFromUrl fetches a URL via node-fetch, supports text, image, and binary content types, with timeout support.
async function readFileFromUrl(url, options = {}) { try { // Validate URL let validUrl; try { validUrl = new URL(url); } catch (error) { return { success: false, message: `Invalid URL: ${url}` }; } // Set timeout (default 30 seconds) const timeout = options.timeout || 30000; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); try { // Fetch the content const response = await fetch(url, { signal: controller.signal, headers: { 'User-Agent': 'ACF-MCP/1.0' } }); clearTimeout(timeoutId); if (!response.ok) { return { success: false, message: `HTTP error ${response.status}: ${response.statusText}` }; } // Get content type const contentType = response.headers.get('content-type') || 'text/plain'; const isText = contentType.startsWith('text/') || contentType.includes('json') || contentType.includes('xml') || contentType.includes('javascript'); // Handle different content types if (contentType.startsWith('image/')) { // Handle images const buffer = await response.buffer(); // Convert to a format that can be displayed let processedBuffer = buffer; let processedType = contentType; // If it's a format that might not be supported, convert to JPEG if (!['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(contentType.split(';')[0])) { try { processedBuffer = await sharp(buffer).jpeg().toBuffer(); processedType = 'image/jpeg'; } catch (sharpError) { logger.warn(`Could not process image with sharp: ${sharpError.message}`); } } return { success: true, content: processedBuffer.toString('base64'), mimeType: processedType, isText: false, isUrl: true, url: url, size: processedBuffer.length }; } else if (isText) { // Handle text content const text = await response.text(); return { success: true, content: text, mimeType: contentType, isText: true, isUrl: true, url: url, size: text.length }; } else { // Handle binary content const buffer = await response.buffer(); return { success: true, content: buffer.toString('base64'), mimeType: contentType, isText: false, isUrl: true, url: url, size: buffer.length }; } } catch (fetchError) { if (fetchError.name === 'AbortError') { return { success: false, message: `Request timeout after ${timeout}ms` }; } throw fetchError; } } catch (error) { logger.error(`Error reading from URL: ${error.message}`); return { success: false, message: `Failed to read from URL: ${error.message}` }; } } - src/mcp/server.js:107-107 (registration)Tool registration: defines the 'read_url' tool name, description, and inputSchema (requires 'path', optional 'timeout').
{ name:'read_url', description:'Read URL', inputSchema:{ type:'object', properties:{ path:{type:'string'}, timeout:{type:'number'} }, required:['path'] } }, - src/mcp/server.js:271-271 (handler)Dispatch: the case 'read_url' calls enhancedFsTools.readFileFromUrl(args.path, args).
case 'read_url': data = await enhancedFsTools.readFileFromUrl(args.path, args); break; - readFileEnhanced – higher-level helper that delegates to readFileFromUrl when the path is a URL.
async function readFileEnhanced(filePath, allowedDirs, options = {}) { try { // Check if it's a URL if (options.isUrl || (typeof filePath === 'string' && (filePath.startsWith('http://') || filePath.startsWith('https://')))) { return await readFileFromUrl(filePath, options); } // Otherwise, use the original readFile from filesystem_tools const filesystemTools = require('../filesystem_tools'); return filesystemTools.readFile(filePath, allowedDirs); } catch (error) { logger.error(`Error in readFileEnhanced: ${error.message}`); return { success: false, message: error.message }; } } - scripts/testing/generate-mcp-examples.js:122-122 (registration)Test example generator: returns a sample path for read_url.
case 'read_url': return { path: 'http://example.com' };