Skip to main content
Glama

fetch_website_nested

Crawl websites with nested URL navigation to extract and convert content into clean markdown format for structured data analysis.

Instructions

Fetch website content with nested URL crawling and convert to clean markdown

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesThe starting URL to fetch and crawl
maxDepthNoMaximum depth to crawl (default: 2)
maxPagesNoMaximum number of pages to fetch (default: 50)
sameDomainOnlyNoOnly crawl URLs from the same domain (default: true)
excludePatternsNoRegex patterns for URLs to exclude
includePatternsNoRegex patterns for URLs to include (if specified, only matching URLs will be processed)
timeoutNoRequest timeout in milliseconds (default: 10000)

Implementation Reference

  • Handler for executing the fetch_website_nested tool. Extracts parameters, creates options, calls scraper.scrapeWebsite, and returns markdown content.
    case "fetch_website_nested": {
      const {
        url,
        maxDepth = 2,
        maxPages = 50,
        sameDomainOnly = true,
        excludePatterns = [],
        includePatterns = [],
        timeout = 10000,
      } = args as any;
    
      if (!url) {
        throw new Error("URL is required");
      }
    
      try {
        const options: FetchOptions = {
          maxDepth,
          maxPages,
          sameDomainOnly,
          excludePatterns,
          includePatterns,
          timeout,
        };
    
        const markdown = await scraper.scrapeWebsite(url, options);
    
        return {
          content: [
            {
              type: "text",
              text: markdown,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to fetch website: ${error}`);
      }
    }
  • Input schema defining parameters for the fetch_website_nested tool, including URL, crawling depth, page limits, and filtering options.
    inputSchema: {
      type: "object",
      properties: {
        url: {
          type: "string",
          description: "The starting URL to fetch and crawl",
        },
        maxDepth: {
          type: "number",
          description: "Maximum depth to crawl (default: 2)",
          default: 2,
        },
        maxPages: {
          type: "number", 
          description: "Maximum number of pages to fetch (default: 50)",
          default: 50,
        },
        sameDomainOnly: {
          type: "boolean",
          description: "Only crawl URLs from the same domain (default: true)",
          default: true,
        },
        excludePatterns: {
          type: "array",
          items: { type: "string" },
          description: "Regex patterns for URLs to exclude",
        },
        includePatterns: {
          type: "array", 
          items: { type: "string" },
          description: "Regex patterns for URLs to include (if specified, only matching URLs will be processed)",
        },
        timeout: {
          type: "number",
          description: "Request timeout in milliseconds (default: 10000)",
          default: 10000,
        },
      },
      required: ["url"],
    },
  • src/server.ts:301-344 (registration)
    Registration of the fetch_website_nested tool in the TOOLS array, returned by list tools handler.
    {
      name: "fetch_website_nested",
      description: "Fetch website content with nested URL crawling and convert to clean markdown",
      inputSchema: {
        type: "object",
        properties: {
          url: {
            type: "string",
            description: "The starting URL to fetch and crawl",
          },
          maxDepth: {
            type: "number",
            description: "Maximum depth to crawl (default: 2)",
            default: 2,
          },
          maxPages: {
            type: "number", 
            description: "Maximum number of pages to fetch (default: 50)",
            default: 50,
          },
          sameDomainOnly: {
            type: "boolean",
            description: "Only crawl URLs from the same domain (default: true)",
            default: true,
          },
          excludePatterns: {
            type: "array",
            items: { type: "string" },
            description: "Regex patterns for URLs to exclude",
          },
          includePatterns: {
            type: "array", 
            items: { type: "string" },
            description: "Regex patterns for URLs to include (if specified, only matching URLs will be processed)",
          },
          timeout: {
            type: "number",
            description: "Request timeout in milliseconds (default: 10000)",
            default: 10000,
          },
        },
        required: ["url"],
      },
    },
  • Core helper method implementing nested website crawling, content extraction, and markdown formatting.
    async scrapeWebsite(startUrl: string, options: FetchOptions = {}): Promise<string> {
      const {
        maxDepth = 2,
        maxPages = 50,
        sameDomainOnly = true,
        timeout = 10000
      } = options;
    
      this.baseUrl = startUrl;
      this.visitedUrls.clear();
    
      const allContent: PageContent[] = [];
      const urlsToProcess: Array<{ url: string; depth: number }> = [{ url: startUrl, depth: 0 }];
    
      while (urlsToProcess.length > 0 && allContent.length < maxPages) {
        const { url, depth } = urlsToProcess.shift()!;
    
        if (depth > maxDepth || this.visitedUrls.has(url)) {
          continue;
        }
    
        const pageContent = await this.fetchPageContent(url, depth, options);
        
        if (pageContent) {
          allContent.push(pageContent);
    
          // Add child URLs for processing
          if (depth < maxDepth) {
            for (const link of pageContent.links) {
              if (!this.visitedUrls.has(link)) {
                urlsToProcess.push({ url: link, depth: depth + 1 });
              }
            }
          }
        }
    
        // Small delay to be respectful
        await new Promise(resolve => setTimeout(resolve, 500));
      }
    
      return this.formatAsMarkdown(allContent, startUrl);
    }
  • AdvancedWebScraper class providing all helper utilities for web scraping, link extraction, content cleaning, and markdown conversion. Used by the tool handler.
    class AdvancedWebScraper {
      private turndownService: TurndownService;
      private visitedUrls: Set<string> = new Set();
      private baseUrl: string = '';
    
      constructor() {
        this.turndownService = new TurndownService({
          headingStyle: 'atx',
          codeBlockStyle: 'fenced',
          bulletListMarker: '-',
        });
    
        // Custom rules for better markdown conversion
        this.turndownService.addRule('removeScripts', {
          filter: ['script', 'style', 'nav', 'header', 'footer', 'aside'],
          replacement: () => ''
        });
    
        this.turndownService.addRule('cleanCodeBlocks', {
          filter: 'pre',
          replacement: (content, node) => {
            const code = node.textContent || '';
            return '\n```\n' + code + '\n```\n';
          }
        });
      }
    
      private async fetchWithTimeout(url: string, timeout: number = 10000): Promise<Response> {
        const controller = new AbortController();
        const timeoutId = setTimeout(() => controller.abort(), timeout);
    
        try {
          const response = await fetch(url, {
            signal: controller.signal,
            headers: {
              'User-Agent': 'Mozilla/5.0 (compatible; MCP-WebScraper/1.0)',
              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            }
          });
          clearTimeout(timeoutId);
          return response as unknown as Response;
        } catch (error) {
          clearTimeout(timeoutId);
          throw error;
        }
      }
    
      private extractLinks($: cheerio.CheerioAPI, baseUrl: string): string[] {
        const links: string[] = [];
        
        $('a[href]').each((_, element) => {
          const href = $(element).attr('href');
          if (href) {
            try {
              const absoluteUrl = new URL(href, baseUrl).toString();
              links.push(absoluteUrl);
            } catch (error) {
              // Invalid URL, skip
            }
          }
        });
    
        return [...new Set(links)]; // Remove duplicates
      }
    
      private shouldProcessUrl(url: string, options: FetchOptions): boolean {
        const urlObj = new URL(url);
        const baseUrlObj = new URL(this.baseUrl);
    
        // Check if same domain only
        if (options.sameDomainOnly && urlObj.hostname !== baseUrlObj.hostname) {
          return false;
        }
    
        // Check exclude patterns
        if (options.excludePatterns) {
          for (const pattern of options.excludePatterns) {
            if (url.match(new RegExp(pattern, 'i'))) {
              return false;
            }
          }
        }
    
        // Check include patterns
        if (options.includePatterns && options.includePatterns.length > 0) {
          let matches = false;
          for (const pattern of options.includePatterns) {
            if (url.match(new RegExp(pattern, 'i'))) {
              matches = true;
              break;
            }
          }
          if (!matches) return false;
        }
    
        // Skip common non-content URLs
        const skipPatterns = [
          /\.(pdf|doc|docx|xls|xlsx|ppt|pptx|zip|rar|tar|gz)$/i,
          /^mailto:/,
          /^tel:/,
          /^javascript:/,
          /#$/,
          /\/search\?/,
          /\/login/,
          /\/register/,
          /\/cart/,
          /\/checkout/,
        ];
    
        return !skipPatterns.some(pattern => pattern.test(url));
      }
    
      private cleanContent($: cheerio.CheerioAPI): string {
        // Remove unwanted elements
        $('script, style, nav, header, footer, aside, .advertisement, .ads, .sidebar, .menu, .navigation').remove();
        
        // Find main content area
        let contentElement = $('main, article, .content, .main-content, #content, #main').first();
        
        if (contentElement.length === 0) {
          // Fallback to body if no main content area found
          contentElement = $('body');
        }
    
        return contentElement.html() || '';
      }
    
      private generateSectionTitle(url: string, title: string, depth: number): string {
        const urlObj = new URL(url);
        const pathParts = urlObj.pathname.split('/').filter(part => part && part !== 'index.html');
        
        let sectionTitle = title || pathParts[pathParts.length - 1] || urlObj.hostname;
        
        // Clean up title
        sectionTitle = sectionTitle
          .replace(/[-_]/g, ' ')
          .replace(/\b\w/g, l => l.toUpperCase())
          .trim();
    
        const headerLevel = '#'.repeat(Math.min(depth + 1, 6));
        return `${headerLevel} ${sectionTitle}`;
      }
    
      private async fetchPageContent(url: string, depth: number, options: FetchOptions): Promise<PageContent | null> {
        if (this.visitedUrls.has(url)) {
          return null;
        }
    
        this.visitedUrls.add(url);
    
        try {
          console.error(`Fetching: ${url} (depth: ${depth})`);
          
          const response = await this.fetchWithTimeout(url, options.timeout);
          
          if (!response.ok) {
            console.error(`Failed to fetch ${url}: ${response.status}`);
            return null;
          }
    
          const html = await response.text();
          const $ = cheerio.load(html);
    
          // Extract title
          const title = $('title').text().trim() || 
                       $('h1').first().text().trim() || 
                       'Untitled Page';
    
          // Clean and extract content
          const cleanHtml = this.cleanContent($);
          const markdownContent = this.turndownService.turndown(cleanHtml);
    
          // Extract links for potential further processing
          const links = this.extractLinks($, url);
    
          return {
            url,
            title,
            content: markdownContent,
            links: links.filter(link => this.shouldProcessUrl(link, options)),
            depth
          };
    
        } catch (error) {
          console.error(`Error fetching ${url}:`, error);
          return null;
        }
      }
    
      async scrapeWebsite(startUrl: string, options: FetchOptions = {}): Promise<string> {
        const {
          maxDepth = 2,
          maxPages = 50,
          sameDomainOnly = true,
          timeout = 10000
        } = options;
    
        this.baseUrl = startUrl;
        this.visitedUrls.clear();
    
        const allContent: PageContent[] = [];
        const urlsToProcess: Array<{ url: string; depth: number }> = [{ url: startUrl, depth: 0 }];
    
        while (urlsToProcess.length > 0 && allContent.length < maxPages) {
          const { url, depth } = urlsToProcess.shift()!;
    
          if (depth > maxDepth || this.visitedUrls.has(url)) {
            continue;
          }
    
          const pageContent = await this.fetchPageContent(url, depth, options);
          
          if (pageContent) {
            allContent.push(pageContent);
    
            // Add child URLs for processing
            if (depth < maxDepth) {
              for (const link of pageContent.links) {
                if (!this.visitedUrls.has(link)) {
                  urlsToProcess.push({ url: link, depth: depth + 1 });
                }
              }
            }
          }
    
          // Small delay to be respectful
          await new Promise(resolve => setTimeout(resolve, 500));
        }
    
        return this.formatAsMarkdown(allContent, startUrl);
      }
    
      private formatAsMarkdown(contents: PageContent[], startUrl: string): string {
        const urlObj = new URL(startUrl);
        const siteName = urlObj.hostname;
        
        let markdown = `# ${siteName} Documentation\n\n`;
        markdown += `*Scraped from: ${startUrl}*\n`;
        markdown += `*Generated on: ${new Date().toISOString()}*\n\n`;
        
        // Table of contents
        markdown += `## Table of Contents\n\n`;
        contents.forEach((content, index) => {
          const indent = '  '.repeat(content.depth);
          markdown += `${indent}- [${content.title}](#${this.slugify(content.title)})\n`;
        });
        markdown += '\n---\n\n';
    
        // Content sections
        contents.forEach(content => {
          const sectionTitle = this.generateSectionTitle(content.url, content.title, content.depth);
          markdown += `${sectionTitle}\n\n`;
          markdown += `*Source: [${content.url}](${content.url})*\n\n`;
          markdown += content.content;
          markdown += '\n\n---\n\n';
        });
    
        return markdown;
      }
    
      private slugify(text: string): string {
        return text
          .toLowerCase()
          .replace(/[^\w\s-]/g, '')
          .replace(/[\s_-]+/g, '-')
          .replace(/^-+|-+$/g, '');
      }
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions crawling and markdown conversion but fails to disclose critical traits like rate limits, authentication needs, error handling, or what happens when limits (maxDepth/maxPages) are reached. The description is too vague about operational behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core functionality. Every word earns its place, with no redundant or vague phrasing, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (7 parameters, crawling behavior) and lack of annotations and output schema, the description is insufficient. It omits details on return format, error cases, performance implications, and practical usage constraints, leaving significant gaps for an AI agent to operate effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all 7 parameters. The description adds no additional meaning beyond implying crawling behavior, which is already suggested by parameter names like 'maxDepth' and 'sameDomainOnly.' Baseline 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('fetch website content with nested URL crawling') and transformation ('convert to clean markdown'), providing a specific verb+resource combination. It distinguishes from the sibling tool 'fetch_website_single' by specifying 'nested URL crawling,' though it could be more explicit about the distinction.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus the sibling 'fetch_website_single' or other alternatives. It lacks context about scenarios where nested crawling is preferred over single-page fetching, such as for multi-page documentation or site-wide content extraction.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/flutterninja9/better-fetch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server