|
| 1 | +import asyncio |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +from agents import Agent, Runner, gen_trace_id, trace |
| 8 | +from agents.mcp import MCPServer, MCPServerStdio |
| 9 | + |
| 10 | + |
| 11 | +async def run(mcp_server: MCPServer): |
| 12 | + # Create a directory for screenshots if it doesn't exist |
| 13 | + screenshots_dir = os.path.join(os.path.dirname(__file__), "screenshots") |
| 14 | + os.makedirs(screenshots_dir, exist_ok=True) |
| 15 | + |
| 16 | + # agent with enhanced instructions for screenshot capability |
| 17 | + agent = Agent( |
| 18 | + name="Web Screenshot Assistant", |
| 19 | + instructions=""" |
| 20 | + You are a web screenshot assistant that can navigate to websites and capture screenshots. |
| 21 | +
|
| 22 | + Use the Playwright tools to: |
| 23 | + 1. Navigate to websites requested by the user |
| 24 | + 2. Take screenshots of the entire page or specific elements |
| 25 | + 3. Save screenshots with descriptive filenames that include the website name and current date |
| 26 | + 4. Report back with the location of saved screenshots |
| 27 | +
|
| 28 | + Follow these guidelines: |
| 29 | + - Always wait for the page to fully load before taking screenshots |
| 30 | + - If asked to capture a specific element, use page.locator() and then screenshot that element |
| 31 | + - If no specific path is provided, save screenshots to the "screenshots" directory |
| 32 | + - Generate descriptive filenames that include the website domain and current date |
| 33 | + - Always confirm successful screenshot capture and provide the filepath where it was saved |
| 34 | + - For full-page screenshots, capture the entire page scrolling content |
| 35 | +
|
| 36 | + When saving screenshots: |
| 37 | + - Use PNG format for better quality |
| 38 | + - Include helpful information in the filename (website, date, element if applicable) |
| 39 | + - Report success with the full path where the screenshot was saved |
| 40 | + """, |
| 41 | + mcp_servers=[mcp_server], |
| 42 | + ) |
| 43 | + |
| 44 | + # Gather the available tools to understand what we can do with Playwright |
| 45 | + tools = await agent.get_all_tools() |
| 46 | + print("Available tools:", [tool.name for tool in tools]) |
| 47 | + |
| 48 | + # Define the default screenshot path with current date |
| 49 | + today_date = datetime.now().strftime("%Y-%m-%d") |
| 50 | + default_screenshot_path = os.path.join(screenshots_dir, f"openai_{today_date}.png") |
| 51 | + |
| 52 | + # Run the agent with a screenshot task |
| 53 | + user_request = f""" |
| 54 | + Please perform these steps: |
| 55 | + 1. Navigate to https://example.com |
| 56 | + 2. Wait for the page to fully load |
| 57 | + 3. Take a screenshot of the entire page |
| 58 | + 4. Save it to {default_screenshot_path} |
| 59 | + 5. Then navigate to https://httpbin.org |
| 60 | + 6. Take another screenshot with a descriptive filename in the screenshots directory |
| 61 | + """ |
| 62 | + |
| 63 | + print(f"\nProcessing request: {user_request}\n") |
| 64 | + |
| 65 | + # Run the agent with our request |
| 66 | + result = await Runner.run(starting_agent=agent, input=user_request) |
| 67 | + |
| 68 | + # Display the result |
| 69 | + print("\nResult:", result) |
| 70 | + |
| 71 | + # Verify the screenshots were created |
| 72 | + if os.path.exists(default_screenshot_path): |
| 73 | + print(f"\nSuccessfully created primary screenshot at: {default_screenshot_path}") |
| 74 | + # Get the file size |
| 75 | + file_size = os.path.getsize(default_screenshot_path) / 1024 # KB |
| 76 | + print(f"Screenshot size: {file_size:.2f} KB") |
| 77 | + |
| 78 | + # List all screenshots created |
| 79 | + all_screenshots = glob.glob(os.path.join(screenshots_dir, "*.png")) |
| 80 | + print(f"\nAll screenshots in directory ({len(all_screenshots)}):") |
| 81 | + for screenshot in all_screenshots: |
| 82 | + file_size = os.path.getsize(screenshot) / 1024 # KB |
| 83 | + print(f" - {os.path.basename(screenshot)} ({file_size:.2f} KB)") |
| 84 | + |
| 85 | + |
| 86 | +async def main(): |
| 87 | + # Configure the MCP server |
| 88 | + # Note: remove --headless if you want to see the browser in action |
| 89 | + async with MCPServerStdio( |
| 90 | + name="Playwright Screenshot Server", |
| 91 | + params={ |
| 92 | + "command": "npx", |
| 93 | + "args": ["-y", "@playwright/mcp@latest", "--headless"], |
| 94 | + }, |
| 95 | + ) as server: |
| 96 | + trace_id = gen_trace_id() |
| 97 | + with trace(workflow_name="Playwright Screenshot Example", trace_id=trace_id): |
| 98 | + print(f"View trace: https://platform.openai.com/traces/{trace_id}\n") |
| 99 | + print(f"Starting Playwright server: {server}") |
| 100 | + |
| 101 | + # Run our screenshot example |
| 102 | + await run(server) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + asyncio.run(main()) |
0 commit comments