Skip to content

[Website] Add WebMCP support to the Playground website - #3298

Merged
bgrgicak merged 11 commits into
trunkfrom
explore/playground-website-webmcp-support
Mar 31, 2026
Merged

bgrgicak merged 11 commits into
trunkfrom
explore/playground-website-webmcp-support

Conversation

@bgrgicak

@bgrgicak bgrgicak commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation for the change, related issues

Adds WebMCP support to the Playground website, so browser-native AI agents (via Chrome's navigator.modelContext API) can interact with Playground directly — no WebSocket bridge or Node.js server needed.

The MCP server in #3290 requires a Node.js process as an intermediary between AI clients and the browser. Chrome's experimental WebMCP API (navigator.modelContext) lets the browser expose tools directly to built-in AI features, removing that hop entirely.

Implementation details

WebMCP tool registration

  • webmcp.ts — New module that registers all 16 playground tools with navigator.modelContext.registerTool(). Calls PlaygroundClient directly instead of going through the WebSocket bridge. Silently no-ops when navigator.modelContext is unavailable.
  • client.ts — Exports registerWebMCPTools alongside startMcpBridge from the @wp-playground/mcp/client entry point.
  • README.md — Adds a WebMCP section documenting the feature and its draft status.

Shared mcpConfig refactor

  • init-mcp-bridge.ts — Extracts the mcpConfig object (site listing, client lookup, save, rename) so it's created once and shared between registerWebMCPTools() and startMcpBridge(). WebMCP tools are registered unconditionally on page load; the WebSocket bridge still requires ?mcp=yes.

Test reorganization and infrastructure

  • Moves e2e tests from e2e/ to tests/e2e/ and unit tests from src/ to tests/unit/ for consistent structure.
  • Splits the test:mcp target in project.json into test:unit (Vitest) and test:e2e (Playwright), with test:mcp as a noop that depends on both.
  • Adds tsconfig.spec.json and Vitest config in vite.config.ts to support unit tests in the new tests/ directory.
  • webmcp.spec.ts — Playwright e2e tests that mock navigator.modelContext via addInitScript, then exercise tool registration and execution (PHP, filesystem, site management).

Testing Instructions

  • Go to chrome://flags and enable the "WebMCP for testing" flag in Chrome 148.0.7757.0 or higher
  • Install the WebMCP - Model Context Tool Inspector Chrome extension
  • Open the local version of the Playground website and start the Chrome extension
  • Run some test prompts like "Install plugin A", "Navigate to WP Admin"

@bgrgicak bgrgicak self-assigned this Feb 24, 2026
@bgrgicak
bgrgicak force-pushed the explore/playground-website-webmcp-support branch from 9078c79 to 897f99b Compare February 24, 2026 11:45
@bgrgicak
bgrgicak force-pushed the add/playground-website-mcp-server branch from 5c3bd15 to 40515af Compare March 5, 2026 10:26
Base automatically changed from add/playground-website-mcp-server to trunk March 10, 2026 12:34
@bgrgicak
bgrgicak force-pushed the explore/playground-website-webmcp-support branch from e39ba48 to e6daa82 Compare March 12, 2026 13:29
@bgrgicak
bgrgicak marked this pull request as ready for review March 17, 2026 14:31
@bgrgicak
bgrgicak requested review from a team, ashfame and Copilot March 17, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds WebMCP (navigator.modelContext) support to the Playground website so browser-native AI agents can invoke Playground tools directly, while preserving the existing WebSocket MCP bridge as an opt-in path.

Changes:

  • Registers Playground toolset via a new registerWebMCPTools() API and wires it into the website initialization flow.
  • Refactors bridge initialization to share a single mcpConfig between WebMCP registration and the WebSocket bridge.
  • Reorganizes tests and adds Vitest/Playwright configuration plus new WebMCP e2e coverage.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/playground/website/src/lib/state/redux/init-mcp-bridge.ts Creates shared mcpConfig, registers WebMCP tools on load, and gates only the WebSocket bridge behind ?mcp=yes.
packages/playground/mcp/vite.config.ts Adds Vitest configuration for unit tests under tests/unit.
packages/playground/mcp/tsconfig.spec.json Introduces TS config for tests/spec compilation.
packages/playground/mcp/tsconfig.json Adds project reference to tsconfig.spec.json.
packages/playground/mcp/tests/unit/bridge-server.spec.ts Updates import path after test directory restructure.
packages/playground/mcp/tests/e2e/webmcp.spec.ts Adds Playwright e2e tests for WebMCP tool registration/execution using a mocked navigator.modelContext.
packages/playground/mcp/tests/e2e/mcp-tools.spec.ts Fixes relative paths for server startup after moving e2e tests under tests/e2e.
packages/playground/mcp/src/webmcp.ts New module registering tools with navigator.modelContext and implementing site-management tools.
packages/playground/mcp/src/client.ts Re-exports registerWebMCPTools from the package entry point.
packages/playground/mcp/project.json Splits test target into test:unit (Vitest) and test:e2e (Playwright), with test:mcp depending on both.
packages/playground/mcp/playwright.config.ts Updates Playwright testDir to tests/e2e.
packages/playground/mcp/README.md Documents WebMCP support and draft-status caveat.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread packages/playground/mcp/src/webmcp.ts Outdated
Comment thread packages/playground/mcp/src/webmcp.ts
Comment thread packages/playground/mcp/tests/e2e/webmcp.spec.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

@bgrgicak I've opened a new pull request, #3416, to work on those changes. Once the pull request is ready, I'll request review from you.

bgrgicak and others added 2 commits March 19, 2026 10:53
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…3416)

`registerWebMCPTools()` would re-register tools on every call (HMR,
re-initialization), potentially causing duplicate tool exposure. The fix
checks `navigator.modelContext.tools` directly to skip
already-registered tools.

## Changes

- **`webmcp.ts`**: Add `readonly tools: ModelContextTool[]` to the
`ModelContext` interface; guard each `registerTool()` call by checking
`navigator.modelContext.tools` for an existing tool with the same name
- **`webmcp.ts`**: Remove unused `clearContext()` and `unregisterTool()`
from the `ModelContext` interface
- **`webmcp.spec.ts`**: Add a `tools` getter to the test mock returning
the registered tools array; remove the now-unused `clearContext` and
`unregisterTool` stubs

```ts
for (const tool of tools) {
    const alreadyRegistered = navigator.modelContext.tools.some(
        (t) => t.name === tool.name
    );
    if (!alreadyRegistered) {
        navigator.modelContext.registerTool(tool);
    }
}
```

<!-- START COPILOT CODING AGENT TIPS -->
---

📍 Connect Copilot coding agent with [Jira](https://gh.io/cca-jira-docs),
[Azure Boards](https://gh.io/cca-azure-boards-docs) or
[Linear](https://gh.io/cca-linear-docs) to delegate work to Copilot in
one click without leaving your project management tool.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgrgicak <1199991+bgrgicak@users.noreply.github.com>
Co-authored-by: Bero <berislav.grgicak@gmail.com>
@bgrgicak
bgrgicak requested a review from zaerl March 24, 2026 17:54

@zaerl zaerl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

Comment thread packages/playground/mcp/src/webmcp.ts Outdated
): ModelContextTool[] {
function getActiveSite() {
const sites = config.getSites();
const active = sites.find((s) => s.isActive);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have a generic getActiveSIte(config). We have the same code in getActiveClient

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I added a shared getActiveSite in dccde37

webmcpPage: async ({ page }, use) => {
// WebMCP (navigator.modelContext) is only available in
// Chrome 146+ Canary behind the "WebMCP for testing"
// flag. Playwright uses a stock Chromium that doesn't

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add chrome://flags/#enable-webmcp-testing

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b8ed695

bgrgicak and others added 2 commits March 31, 2026 17:36
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bgrgicak
bgrgicak merged commit 8ce0a0d into trunk Mar 31, 2026
86 of 93 checks passed
@bgrgicak
bgrgicak deleted the explore/playground-website-webmcp-support branch March 31, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants