getLinks
Extract all hyperlinks from a webpage to collect URLs for analysis, navigation, or data gathering during browser automation tasks.
Instructions
Get all links from the current page
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "getLinks"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/controllers/playwright.ts:393-413 (handler)The main handler function for the getLinks tool in PlaywrightController. Extracts all anchor links (a[href]) from the page, returning array of {href, text, title?} using page.evaluate.
async getLinks(): Promise<Array<{href: string, text: string, title?: string}>> { try { if (!this.isInitialized()) { throw new Error('Browser not initialized'); } this.log('Getting page links'); const links = await this.state.page?.evaluate(() => { const linkElements = Array.from(document.querySelectorAll('a[href]')); return linkElements.map(link => ({ href: (link as HTMLAnchorElement).href, text: link.textContent?.trim() || '', title: link.getAttribute('title') || undefined })); }); this.log('Links retrieved:', links?.length); return links || []; } catch (error: any) { console.error('Get links error:', error); throw new BrowserError('Failed to get links', 'Check if the page is loaded'); } } - src/server.ts:153-161 (schema)Tool schema definition for getLinks, specifying no input parameters required.
const GET_LINKS_TOOL: Tool = { name: "getLinks", description: "Get all links from the current page", inputSchema: { type: "object", properties: {}, required: [] } }; - src/server.ts:529-529 (registration)Registration of the getLinks tool in the server's tools object for MCP capabilities.
getLinks: GET_LINKS_TOOL, - src/server.ts:726-731 (registration)MCP tool call dispatch in server request handler that calls the controller's getLinks and returns JSON serialized results.
case 'getLinks': { const links = await playwrightController.getLinks(); return { content: [{ type: "text", text: JSON.stringify(links, null, 2) }] }; }