1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Playwright MCPをforkしてUserAgentとViewportを指定できるようにしたよ【Cursor】

Last updated at Posted at 2025-04-15

やること

  • Playwright MCPを使ってみる
  • UAとViewportを設定してみる(forkして自分で作る)

MCP(Model Context Protocol)とは

  • この方のスライドがネット上で一番わかりやすかったです(2025/4/11現在)
  • この記事ではくわしく解説しませんが、MCPという共通規格を通じて、AIエージェントが外部のツールやリソースへアクセスできるようになりました
  • 以下みたいなイメージ
    • MCPクライアント:CursorやClaude Desktop、VSCodeなどのAIエージェント
    • MCPサーバー:playwright-mcpや天気予報ツールなど、MCPクライアントの要求を基に実サービスへの中継とデータの返却を行う

Playwright MCP

  • ブラウザの自動操作ライブラリであるPlaywright(from Microsoft)が、MCPサーバ化しました
  • CursorなどのMCPクライアントから指示を受け取って、ブラウザ操作として実行します

できること(2025/4/11時点)

  • ブラウザの操作
    • クリック
    • ホバー
    • ドラッグアンドドロップ
    • スクリーンショット
    • テキスト入力
  • タブ管理
    • 開く・閉じる
    • 一覧を取得する
  • 遷移
    • URL指定でアクセス
    • 次へ・戻る
  • ファイルアップロード

などなど

できないこと(2025/4/11時点)

困ったことと解決方法

困ったこと

  • UserAgentやViewportによってレスポンシブ対応するようなWebサイトのUIテストをやりたい時に困る

解決方法

  • Playwright MCPをforkして、自分用にカスタマイズしたバージョンのeno-playwright-mcpを作ってみました
修正内容のサマリ by Cursor(一部編集済み)

はい、各ファイルの変更点について詳細に説明させていただきます。

コミットの詳細分析

1. src/context.ts

  • 型定義の拡張

    • ContextOptions インターフェースに新しいオプションを追加
      contextOptions?: playwright.BrowserContextOptions;
      
  • ブラウザコンテキスト起動ロジックの変更

    • _launchPersistentContext メソッドの修正
    • 以前:
      return await browserType.launchPersistentContext(this.options.userDataDir, this.options.launchOptions);
      
    • 変更後:
      return await browserType.launchPersistentContext(this.options.userDataDir, {
        ...this.options.launchOptions,
        ...this.options.contextOptions,
      });
      

2. src/index.ts

  • import文の変更

    -import type { LaunchOptions } from 'playwright';
    +import type { LaunchOptions, BrowserContextOptions } from 'playwright';
    
  • Options型の拡張

    type Options = {
      // 既存のオプション
      -cdpEndpoint?: string;
      +contextOptions?: BrowserContextOptions;
      vision?: boolean;
      capabilities?: ToolCapability[];
      +cdpEndpoint?: string;  // 位置の移動のみ
    };
    
  • createServer関数の修正

    export function createServer(options?: Options): Server {
      // 既存のオプション
      +contextOptions: options?.contextOptions,
    });
    

3. src/program.ts

  • CLIオプションの追加

    .option('--viewport-size <size>', 'Browser viewport size in format "width,height". Example: 1280,720')
    .option('--user-agent <string>', 'User agent string to use')
    
  • 新しいコンテキストオプション処理の追加

    const contextOptions: any = {};
    
    // ビューポートサイズの処理
    if (options.viewportSize) {
      const [width, height] = options.viewportSize.split(',').map(Number);
      if (!isNaN(width) && !isNaN(height)) {
        contextOptions.viewport = { width, height };
      }
    }
    
    // ユーザーエージェントの処理
    if (options.userAgent) {
      contextOptions.userAgent = options.userAgent;
    }
    
  • ServerList作成時のオプション追加

    const serverList = new ServerList(() => createServer({
      // 既存のオプション
      +contextOptions,
      vision: !!options.vision,
      // その他の既存オプション
    }));
    

このコミットでは、3つのファイルに対して変更が行われ、主にPlaywrightのブラウザコンテキストオプションを柔軟に設定できるように機能拡張が実施されました。特に、ビューポートサイズとユーザーエージェントの設定が可能になり、より詳細なブラウザ環境のカスタマイズが実現されました。

実際動かしてみた

mcpサーバのビルド & 実行

$ npm install

$ npm run build

$ npx ts-node ./cli.js --port 8931 --viewport-size 777,777 --user-agent "EnokiWebKit"

ここまで実施すると、MCPクライアント側での設定情報を出力してくれます。

Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Listening on http://localhost:8931
Put this in your client config:
{
  "mcpServers": {
    "eno-playwright": {
      "url": "http://localhost:8931/sse"
    }
  }
}

CursorでMCPサーバを追加する

image.png

Ctrl + Shift + P(Macなら、Command + Shift + P)で「cursor settings」と入力してCursorの設定画面を開きます。

image.png

右上の「Add new global MCP server」から、先ほどのterminalで控えた設定内容をコピペすると、MCPクライアント側の設定は完了です。

Cursorにお願いしてみる

image.png

UAの設定と、Viewportの設定ができましたね。

まとめ

今度はiframe内の要素をどう扱うかをちゃんと調べたいなー

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?