HomeProjects
1+ Years Experience

MCP + Playwright: Complete Guide

AutomationJan 16, 202615 min read

AI agents are becoming more powerful, but they need a safe and structured way to interact with the real world—especially web browsers. This is where MCP (Model Context Protocol) combined with Playwright comes in.

In this blog, you’ll learn:

  • What MCP is
  • What Playwright is
  • Why they work perfectly together
  • How to build an MCP Playwright server
  • Line-by-line code explanation

1. What is MCP (Model Context Protocol)?

MCP is a protocol that allows AI models to call tools, use external services, receive structured responses, and operate securely. Think of MCP as a bridge between AI models and real-world tools like browsers, files, and APIs.

2. What is Playwright?

Playwright is a browser automation library created by Microsoft. It allows you to open websites, click buttons, fill forms, take screenshots, and scrape content across Chromium, Firefox, and WebKit.

3. Why Use MCP with Playwright?

Without MCP

  • AI automation is unsafe
  • No standard tool interface
  • Hard to control permissions

With MCP + Playwright

  • Secure browser automation
  • Standardized tool interface
  • Works with any AI model

4. Step-by-Step Implementation

Step 1: Project Setup

bash
$ mkdir mcp-playwright
$ cd mcp-playwright
$ npm init -y

Step 2: Install Dependencies

bash
$ npm install @modelcontextprotocol/sdk playwright
$ npx playwright install

Step 3: Basic MCP Server Setup

Create a file called index.js and add the following:

javascript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { chromium } from "playwright";

const server = new Server(
  {
    name: "mcp-playwright-server",
    version: "1.0.0",
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

5. Creating Tools

Tool: Open a Web Page

javascript
server.tool(
  "open_page",
  {
    url: { type: "string" },
  },
  async ({ url }) => {
    const browser = await chromium.launch({ headless: true });
    const page = await browser.newPage();

    await page.goto(url);
    const title = await page.title();

    await browser.close();

    return {
      content: [{ type: "text", text: `Page title: ${title}` }],
    };
  }
);

Tool: Take Screenshot

javascript
server.tool(
  "take_screenshot",
  {
    url: { type: "string" },
  },
  async ({ url }) => {
    const browser = await chromium.launch({ headless: true });
    const page = await browser.newPage();

    await page.goto(url);
    await page.screenshot({ path: "screenshot.png" });

    await browser.close();

    return {
      content: [{ type: "text", text: "Screenshot saved successfully" }],
    };
  }
);

6. Running the Server

javascript
const transport = new StdioServerTransport();
await server.connect(transport);

// Run with:
// node index.js
MCP

Conclusion

MCP + Playwright is a game-changer for AI-driven automation. By giving LLMs direct access to the web, we move from simple text generation to actual task execution. Secure, structured, and powerful—this is the future of AI agents.

Have an idea?

Let's Connect

Copyright © 2025 Bibek Shah