browser_get_markdown
Extract webpage content as Markdown formatted text optimized for AI processing, supporting parallel browser instances and configurable content selection.
Instructions
Get page content in Markdown format, optimized for large language models
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instanceId | Yes | Instance ID | |
| includeLinks | No | Whether to include links | |
| maxLength | No | Maximum content length in characters | |
| selector | No | Optional CSS selector to extract content from specific element only |
Implementation Reference
- src/tools.ts:1001-1189 (handler)The primary handler function for 'browser_get_markdown'. It retrieves the browser instance and uses page.evaluate to run a comprehensive JavaScript function that traverses the DOM and converts HTML elements to Markdown format, handling headings, links, lists, tables, etc. Supports options like includeLinks, maxLength, and selector.private async getMarkdown(instanceId: string, options: { includeLinks: boolean; maxLength: number; selector?: string; }): Promise<ToolResult> { const instance = this.browserManager.getInstance(instanceId); if (!instance) { return { success: false, error: `Instance ${instanceId} not found` }; } try { // JavaScript function to extract page content and convert to Markdown const markdownContent = await instance.page.evaluate((opts) => { const { includeLinks, maxLength, selector } = opts; // Select the root element to process const rootElement = selector ? document.querySelector(selector) : document.body; if (!rootElement) { return 'Specified element or page content not found'; } // HTML to Markdown conversion function function htmlToMarkdown(element: any, depth = 0) { let markdown = ''; const indent = ' '.repeat(depth); for (const node of element.childNodes) { if (node.nodeType === Node.TEXT_NODE) { const text = node.textContent?.trim(); if (text) { markdown += text + ' '; } } else if (node.nodeType === Node.ELEMENT_NODE) { const el = node as Element; const tagName = el.tagName.toLowerCase(); switch (tagName) { case 'h1': markdown += `\n\n# ${el.textContent?.trim()}\n\n`; break; case 'h2': markdown += `\n\n## ${el.textContent?.trim()}\n\n`; break; case 'h3': markdown += `\n\n### ${el.textContent?.trim()}\n\n`; break; case 'h4': markdown += `\n\n#### ${el.textContent?.trim()}\n\n`; break; case 'h5': markdown += `\n\n##### ${el.textContent?.trim()}\n\n`; break; case 'h6': markdown += `\n\n###### ${el.textContent?.trim()}\n\n`; break; case 'p': const pText = htmlToMarkdown(el, depth); if (pText.trim()) { markdown += `\n\n${pText.trim()}\n`; } break; case 'br': markdown += '\n'; break; case 'strong': case 'b': markdown += `**${el.textContent?.trim()}**`; break; case 'em': case 'i': markdown += `*${el.textContent?.trim()}*`; break; case 'code': markdown += `\`${el.textContent?.trim()}\``; break; case 'pre': markdown += `\n\`\`\`\n${el.textContent?.trim()}\n\`\`\`\n`; break; case 'a': const href = el.getAttribute('href'); const linkText = el.textContent?.trim(); if (includeLinks && href && linkText) { if (href.startsWith('http')) { markdown += `[${linkText}](${href})`; } else { markdown += linkText; } } else { markdown += linkText || ''; } break; case 'ul': case 'ol': markdown += '\n'; const listItems = el.querySelectorAll('li'); listItems.forEach((li, index) => { const bullet = tagName === 'ul' ? '-' : `${index + 1}.`; markdown += `${indent}${bullet} ${li.textContent?.trim()}\n`; }); markdown += '\n'; break; case 'blockquote': const quoteText = el.textContent?.trim(); if (quoteText) { markdown += `\n> ${quoteText}\n\n`; } break; case 'div': case 'section': case 'article': case 'main': // Recursively process container elements markdown += htmlToMarkdown(el, depth); break; case 'table': // Simplified table processing const rows = el.querySelectorAll('tr'); if (rows.length > 0) { markdown += '\n\n'; rows.forEach((row, rowIndex) => { const cells = row.querySelectorAll('td, th'); const cellTexts = Array.from(cells).map(cell => cell.textContent?.trim() || ''); markdown += '| ' + cellTexts.join(' | ') + ' |\n'; if (rowIndex === 0) { markdown += '|' + ' --- |'.repeat(cells.length) + '\n'; } }); markdown += '\n'; } break; case 'script': case 'style': case 'nav': case 'footer': case 'aside': // Ignore these elements break; default: // For other elements, continue recursive processing of child elements markdown += htmlToMarkdown(el, depth); break; } } } return markdown; } // Extract page title const title = document.title; const url = window.location.href; // Generate Markdown content let content = `# ${title}\n\n**URL:** ${url}\n\n`; content += htmlToMarkdown(rootElement); // Clean up extra line breaks and spaces content = content .replace(/\n{3,}/g, '\n\n') .replace(/[ \t]+/g, ' ') .trim(); // Truncate content if exceeds maximum length if (content.length > maxLength) { content = content.substring(0, maxLength) + '\n\n[Content truncated...]'; } return content; }, options); return { success: true, data: { markdown: markdownContent, length: markdownContent.length, truncated: markdownContent.length >= options.maxLength, url: instance.page.url(), title: await instance.page.title() }, instanceId }; } catch (error) { return { success: false, error: `Get markdown failed: ${error instanceof Error ? error.message : error}`, instanceId }; } }
- src/tools.ts:464-486 (schema)Defines the input schema for the browser_get_markdown tool, specifying parameters like instanceId (required), includeLinks, maxLength, and optional selector.inputSchema: { type: 'object', properties: { instanceId: { type: 'string', description: 'Instance ID' }, includeLinks: { type: 'boolean', description: 'Whether to include links', default: true }, maxLength: { type: 'number', description: 'Maximum content length in characters', default: 10000 }, selector: { type: 'string', description: 'Optional CSS selector to extract content from specific element only' } }, required: ['instanceId']
- src/tools.ts:461-488 (registration)Registers the browser_get_markdown tool in the getTools() method by including it in the returned Tool[] array.{ name: 'browser_get_markdown', description: 'Get page content in Markdown format, optimized for large language models', inputSchema: { type: 'object', properties: { instanceId: { type: 'string', description: 'Instance ID' }, includeLinks: { type: 'boolean', description: 'Whether to include links', default: true }, maxLength: { type: 'number', description: 'Maximum content length in characters', default: 10000 }, selector: { type: 'string', description: 'Optional CSS selector to extract content from specific element only' } }, required: ['instanceId'] } }
- src/tools.ts:578-584 (registration)In the executeTools switch statement, routes calls to 'browser_get_markdown' to the getMarkdown handler method.case 'browser_get_markdown': return await this.getMarkdown(args.instanceId, { includeLinks: args.includeLinks ?? true, maxLength: args.maxLength || 10000, selector: args.selector });