read_webpage
Extract text content from any webpage by providing its URL, enabling users to access and process web information without manual browsing.
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 arguments, fetches the webpage with axios, parses content using cheerio (removing scripts/styles), extracts title and cleaned body text, returns structured JSON or error response.} 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 ListTools handler, including name, description, and input schema requiring a 'url' string.{ 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-49 (schema)Type guard function for validating input arguments to the 'read_webpage' tool, ensuring it has a 'url' string property.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; }