Skip to main content
Glama

Selenium MCP 服务器

铁匠徽章

一个使用 Selenium 与 WebDriver 实例交互的 MCP 服务器。使用MCP-Server-Starter模板构建。

概述

该服务器允许 AI 代理通过 Selenium WebDriver 控制 Web 浏览器会话,从而通过模型上下文协议实现 Web 抓取、自动化测试和表单填写等任务。

Related MCP server: mcp-selenium-python

核心组件

  • MCP 服务器:将 Selenium WebDriver 操作作为 MCP 工具公开。

  • Selenium WebDriver :与浏览器交互。

  • MCP 客户端:可以利用公开工具的 AI 主机(如 Cursor、Claude Desktop)。

先决条件

  • Node.js(v18 或更高版本)

  • npm(v7 或更高版本)

  • 已安装WebDriver 可执行文件(例如 ChromeDriver、GeckoDriver)并可在系统 PATH 中使用。

  • 兼容的网络浏览器(例如 Chrome、Firefox)。

入门

  1. 克隆存储库:

    git clone <your-repo-url> selenium-mcp-server
    cd selenium-mcp-server
  2. 安装依赖项:

    npm install
  3. 配置WebDriver:

    • 确保您的 WebDriver (例如chromedriver ) 已安装并位于您的 PATH 中。

    • 如果需要指定浏览器选项或 WebDriver 路径,请修改src/seleniumService.ts (您将创建此文件)。

  4. 构建服务器:

    npm run build
  5. 运行服务器:

    npm start

    或者,将其与 Cursor 或 Claude Desktop 等 MCP 主机集成(请参阅下面的集成部分)。

工具

该服务器将提供以下工具:

  • selenium_navigate :将浏览器导航到特定的 URL。

  • selenium_findElement :使用 CSS 选择器在页面上查找元素。

  • selenium_click :点击一个元素。

  • selenium_sendKeys :向元素发送击键。

  • selenium_getPageSource :检索当前页面源 HTML。

  • (根据需要添加更多工具)

TypeScript 实现

服务器使用@modelcontextprotocol/sdk和selenium-webdriver库。

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { Builder, By, Key, until, WebDriver } from 'selenium-webdriver';

// Basic server setup (details in src/index.ts)
const server = new Server({
  name: "selenium-mcp-server",
  version: "0.1.0",
  capabilities: {
    tools: {}, // Enable tools capability
  }
});

// Selenium WebDriver setup (details in src/seleniumService.ts)
let driver: WebDriver;

async function initializeWebDriver() {
  driver = await new Builder().forBrowser('chrome').build(); // Or 'firefox', etc.
}

// Example tool implementation (details in src/tools/)
server.registerTool('selenium_navigate', {
  description: 'Navigates the browser to a specific URL.',
  inputSchema: { /* ... zod schema ... */ },
  outputSchema: { /* ... zod schema ... */ },
  handler: async (params) => {
    await driver.get(params.url);
    return { success: true };
  }
});

// Connect transport
async function startServer() {
  await initializeWebDriver();
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.log("Selenium MCP Server connected via stdio.");

  // Graceful shutdown
  process.on('SIGINT', async () => {
    console.log("Shutting down WebDriver...");
    if (driver) {
      await driver.quit();
    }
    process.exit(0);
  });
}

startServer();

发展

  • 构建: npm run build

  • 运行: npm start (执行node build/index.js )

  • Lint: npm run lint

  • 格式: npm run format

调试

使用 MCP Inspector 或标准 Node.js 调试技术。

与 MCP 主机集成

(保留原始 README 中有关 Cursor、Claude Desktop、Smithery 等的相关部分,并根据需要更新路径和命令)

光标集成

  1. 构建你的服务器: npm run build

  2. 在光标中: Settings > Features > MCP :添加新的 MCP 服务器。

  3. 注册您的服务器:

    • 选择stdio作为传输类型。

    • 名称: Selenium Server (或类似名称)。

    • 命令: node /path/to/selenium-mcp-server/build/index.js 。

  4. 节省。

Claude 桌面集成

  1. 构建你的服务器: npm run build

  2. 修改claude_desktop_config.json :

    {
      "mcpServers": {
        "selenium-mcp-server": {
          "command": "node",
          "args": [
            "/path/to/selenium-mcp-server/build/index.js"
          ]
        }
      }
    }
  3. 重新启动 Claude Desktop 。

最佳实践

  • 使用 TypeScript 和 Zod 进行类型安全和验证。

  • 保持工具模块化(例如, src/tools/中每个工具一个文件)。

  • 优雅地处理 WebDriver 错误(例如,未找到元素、导航问题)。

  • 确保 WebDriver 正确关闭(例如,服务器退出时使用driver.quit() )。

  • 遵循 MCP 有关架构、错误处理和内容类型的最佳实践。

了解更多

致谢

根据 Seth Rose 创建的模板:

Available Tools

6 tools
browser_clickC

Perform click on a web page

Inputs: element (required), ref (required).

browser_navigateC

Navigate to a URL

Inputs: url (required).

browser_snapshotB

Capture accessibility snapshot of the current page

Inputs: no named parameters.

browser_take_screenshotC

Take a screenshot of the current page

Inputs: filename, raw.

browser_typeC

Type text into editable element

Inputs: element (required), ref (required), text (required), submit.

browser_wait_forC

Wait for text to appear or disappear or a specified time to pass

Inputs: text, textGone, time.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.5/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool has a clearly distinct purpose with no overlap: clicking, navigating, capturing snapshots, taking screenshots, typing text, and waiting for conditions. The descriptions make it easy to differentiate between them, eliminating any ambiguity in tool selection.

    Naming Consistency5/5

    All tools follow a consistent 'browser_' prefix with descriptive action names (e.g., click, navigate, snapshot). This uniform snake_case pattern makes the tool set predictable and easy to understand, enhancing usability for agents.

    Tool Count5/5

    With 6 tools, the server is well-scoped for web automation tasks. Each tool serves a specific, essential function in the Selenium domain, avoiding bloat while covering core interactions like navigation, input, and monitoring.

    Completeness4/5

    The tool set covers key web automation actions (navigation, interaction, monitoring), but lacks some common operations like form submission, element selection, or handling alerts. However, agents can work around these gaps using existing tools, making it mostly complete for basic workflows.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI agents to automate web tasks such as browsing, clicking, typing, and taking screenshots via the Model Context Protocol.
      1
      MIT