read_webpage
Extract readable text content from any webpage URL for analysis or processing.
Instructions
Fetch and extract text content from a webpage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | URL of the webpage to read |
Implementation Reference
- src/index.ts:175-220 (handler)Handler for the 'read_webpage' tool: validates input arguments, fetches the webpage content using axios, parses HTML with cheerio to extract title and cleaned text, returns structured content as JSON or error.} else if (request.params.name === 'read_webpage') { if (!isValidWebpageArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid webpage arguments' ); } const { url } = request.params.arguments; try { const response = await axios.get(url); const $ = cheerio.load(response.data); // Remove script and style elements $('script, style').remove(); const content: WebpageContent = { title: $('title').text().trim(), text: $('body').text().trim().replace(/\s+/g, ' '), url: url, }; return { content: [ { type: 'text', text: JSON.stringify(content, null, 2), }, ], }; } catch (error) { if (axios.isAxiosError(error)) { return { content: [ { type: 'text', text: `Webpage fetch error: ${error.message}`, }, ], isError: true, }; } throw error; } }
- src/index.ts:109-122 (registration)Registration of the 'read_webpage' tool in the ListToolsRequestHandler, including name, description, and input schema definition.{ name: 'read_webpage', description: 'Fetch and extract text content from a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL of the webpage to read', }, }, required: ['url'], }, },
- src/index.ts:44-50 (helper)Helper function to validate input arguments for the 'read_webpage' tool.const isValidWebpageArgs = ( args: any ): args is { url: string } => typeof args === 'object' && args !== null && typeof args.url === 'string';
- src/index.ts:30-34 (schema)TypeScript interface defining the structure of the webpage content returned by the tool.interface WebpageContent { title: string; text: string; url: string; }