diff --git a/src/puppeteer/index.ts b/src/puppeteer/index.ts index c85c720f..77fb902f 100644 --- a/src/puppeteer/index.ts +++ b/src/puppeteer/index.ts @@ -1,207 +1,3 @@ - isError: true, - }, - }; - } - - case "puppeteer_screenshot": { - const width = args.width ?? config.screenshotOptions.defaultWidth; - const height = args.height ?? config.screenshotOptions.defaultHeight; - - try { - await page.setViewport({ width, height }); - - // Wait for any animations to complete - await page.waitForTimeout(500); - - let screenshot; - if (args.selector) { - const element = await withTimeout( - page.waitForSelector(args.selector, { timeout: 5000 }) - ); - if (!element) { - throw new Error(`Element not found: ${args.selector}`); - } - screenshot = await element.screenshot({ - encoding: "base64", - quality: config.screenshotOptions.quality - }); - } else { - screenshot = await page.screenshot({ - encoding: "base64", - fullPage: false, - captureBeyondViewport: false, - quality: config.screenshotOptions.quality - }); - } - - if (!screenshot) { - throw new Error('Screenshot capture failed'); - } - - screenshots.set(args.name, screenshot as string); - server.notification({ - method: "notifications/resources/list_changed", - }); - - return { - toolResult: { - content: [ - { - type: "text", - text: `Screenshot '${args.name}' taken at ${width}x${height}`, - } as TextContent, - { - type: "image", - data: screenshot, - mimeType: "image/png", - } as ImageContent, - ], - isError: false, - }, - }; - } catch (error) { - return { - toolResult: { - content: [{ - type: "text", - text: `Screenshot failed: ${(error as Error).message}`, - }], - isError: true, - }, - }; - } - } - - case "puppeteer_click": - try { - const element = await withTimeout( - page.waitForSelector(args.selector, { timeout: 5000 }) - ); - if (!element) { - throw new Error(`Element not found: ${args.selector}`); - } - await element.click(); - return { - toolResult: { - content: [{ - type: "text", - text: `Clicked: ${args.selector}`, - }], - isError: false, - }, - }; - } catch (error) { - return { - toolResult: { - content: [{ - type: "text", - text: `Failed to click ${args.selector}: ${(error as Error).message}`, - }], - isError: true, - }, - }; - } - - case "puppeteer_fill": - try { - const element = await withTimeout( - page.waitForSelector(args.selector, { timeout: 5000 }) - ); - if (!element) { - throw new Error(`Element not found: ${args.selector}`); - } - await element.type(args.value); - return { - toolResult: { - content: [{ - type: "text", - text: `Filled ${args.selector} with: ${args.value}`, - }], - isError: false, - }, - }; - } catch (error) { - return { - toolResult: { - content: [{ - type: "text", - text: `Failed to fill ${args.selector}: ${(error as Error).message}`, - }], - isError: true, - }, - }; - } - - case "puppeteer_select": - try { - const element = await withTimeout( - page.waitForSelector(args.selector, { timeout: 5000 }) - ); - if (!element) { - throw new Error(`Element not found: ${args.selector}`); - } - await page.select(args.selector, args.value); - return { - toolResult: { - content: [{ - type: "text", - text: `Selected ${args.selector} with: ${args.value}`, - }], - isError: false, - }, - }; - } catch (error) { - return { - toolResult: { - content: [{ - type: "text", - text: `Failed to select ${args.selector}: ${(error as Error).message}`, - }], - isError: true, - }, - }; - } - - case "puppeteer_hover": - try { - const element = await withTimeout( - page.waitForSelector(args.selector, { timeout: 5000 }) - ); - if (!element) { - throw new Error(`Element not found: ${args.selector}`); - } - await element.hover(); - return { - toolResult: { - content: [{ - type: "text", - text: `Hovered ${args.selector}`, - }], - isError: false, - }, - }; - } catch (error) { - return { - toolResult: { - content: [{ - type: "text", - text: `Failed to hover ${args.selector}: ${(error as Error).message}`, - }], - isError: true, - }, - }; - } - - case "puppeteer_evaluate": - try { - const result = await withTimeout( - page.evaluate((script) => { - const logs: string[] = []; - const originalConsole = { ...console }; - - ['log', 'info', 'warn', 'error'].forEach(method => { - (console as any)[method] = (...args: any[]) => { - logs.push(`[${method}] ${args.join(' ')}`); (originalConsole as any)[method](...args); }; }); @@ -310,7 +106,7 @@ server.setRequestHandler(ReadResourceRequestSchema, async (request) => { } throw new Error(`Resource not found: ${uri}`); -})); +}); server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS, @@ -334,4 +130,4 @@ async function runServer() { await server.connect(transport); } -runServer().catch(console.error); +runServer().catch(console.error); \ No newline at end of file