Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Puppeteer Server Stability and Error Handling #132

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: complete puppeteer server implementation with error handling
  • Loading branch information
gitw21 committed Nov 29, 2024
commit 2ea56300d7c4b1e75948c86a8258ccd0c915439c
208 changes: 2 additions & 206 deletions src/puppeteer/index.ts
Original file line number Diff line number Diff line change
@@ -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);