What You’ll Learn
- How to set up and use MCP servers
- How to create a web browsing assistant
- How to handle streaming responses with tools
- Security considerations for MCP servers
Prerequisites
Code Example
Run the Example
Expected Output
How It Works
- MCP Server: Playwright MCP server provides web automation tools
- McpWorkbench: Manages the connection to the MCP server
- Tool Discovery: Agent automatically discovers available tools from the server
- Execution: Agent uses tools like
navigate,screenshot,clickto browse the web - Context Manager:
async withensures proper cleanup of resources
Available MCP Tools
The Playwright MCP server provides several tools:navigate(url): Navigate to a URLscreenshot(): Take a screenshot of the current pageclick(selector): Click an elementfill(selector, value): Fill a form fieldevaluate(script): Execute JavaScriptget_text(selector): Extract text from elements
Using Multiple MCP Servers
You can connect to multiple MCP servers simultaneously:Security Warning
Always review the MCP server’s source code before use in production.Common Use Cases
Research
Gather information from multiple websites automatically.
Testing
Automate web application testing and validation.
Data Extraction
Scrape structured data from web pages.
Monitoring
Track changes on websites over time.
Troubleshooting
Browser Not Found
If you get a “browser not found” error:Connection Timeout
Increase the timeout in server parameters:Next Steps
Code Execution
Execute code safely within agent workflows
Research Assistant
Build a complete research assistant application