Ai & data science insights

Technical knowledge and expert perspectives from the field.

Build a Web-Browsing AI Agent in 50 Lines Using Tiny Agents and Playwright MCP

Albin Xavier

Fri, 25 Jul 2025

Build a Web-Browsing AI Agent in 50 Lines Using Tiny Agents and Playwright MCP

Build a Web-Browsing AI Agent in 50 Lines Using Tiny Agents and Playwright MCP

In the fast-moving world of AI tools, developers want two things: power and simplicity. That’s exactly what Tiny Agents offer—smart AI agents you can build in just 50 lines of code.

In this guide, we’ll walk through building a web-browsing agent that can open a website and fetch its title, using:

What Are Tiny Agents?

Tiny Agents are small AI programs built around:

  • a system prompt
  • a language model (LLM)
  • and one or more tool servers via Model Context Protocol (MCP)

Instead of stuffing everything into a huge framework, you keep it clean and modular. Tools can be anything from a file reader to a browser or a custom API.

Why Use Playwright MCP?

Playwright MCP gives your agent the power to:

  • Launch a browser
  • Visit any website
  • Click, type, or scroll
  • Read page content or take screenshots

What You’ll Need

Before writing code, make sure you’ve got:

  • Python installed
  • Node.js and npm (for Playwright MCP)
  • An OpenAI API key (or any LLM with function-calling support)
  • Packages installed:
    pip install tinyagent litellm

Build Your First Web Agent

Let’s create a simple agent that navigates to https://www.example.com and tells you the page title.

The Code

from tinyagent import TinyAgent
import asyncio

async def main():
    agent = TinyAgent(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY")
    await agent.connect_to_server("npx", ["@playwright/mcp@latest"])
    
    task = "Navigate to https://www.example.com and tell me the title of the page."
    result = await agent.run(task)
    
    print("Result:", result)
    await agent.close()

asyncio.run(main())

How It Works

  • TinyAgent connects to your model and tool server (Playwright MCP).
  • It sends the task to the model: "Navigate to X, tell me Y."
  • The model decides what tools to use (e.g., browser_navigate, browser_snapshot).
  • The MCP server runs those tools and returns data.
  • The result is displayed and the agent shuts down.

How to Run It

Step 1: Start the browser MCP server

npx @playwright/mcp@latest

To run in background/headless mode:

npx @playwright/mcp@latest --headless

Step 2: Run your script

Save your script as web_agent.py and run:

python web_agent.py

Expected Output:
Result: The title of the page is "Example Domain".

Go Further: Advanced Use Cases

  • Search & Summarize: Ask it to search Google and summarize results.
  • Form Fillers & Automation: Log in, submit forms, navigate flows.
  • Vision Mode: Enable with
    npx @playwright/mcp@latest --vision
    for screenshot-based tasks.

Benefits of Tiny Agents

  • Simple: Minimal setup, no boilerplate
  • Modular: Swap tools and models easily
  • Great for prototyping: Perfect for quick experiments

Limitations to Keep in Mind

  • Not ideal for large, multi-step workflows
  • Depends on external API (OpenAI, etc.)
  • Snapshot mode may not work on very dynamic websites

Troubleshooting Tips

  • MCP errors? Start the server before running the script.
  • API errors? Double-check the key and permissions.
  • Use --headless=false to debug visually.

Resources to Explore

Final Thoughts

You don’t need a massive framework to build a useful AI agent. Sometimes, all it takes is 50 lines of code and a well-defined task.

Tiny Agents give you everything you need to start building. Whether it’s scraping a website, reading a file, or automating a workflow—you can do it fast, and you can do it clean.

Ready to try it out? Fork the code, start your terminal, and give your AI a browser.

Written for The Data Career
Subscribe for more hands-on AI tutorials.

0 Comments

Leave a comment