-
Notifications
You must be signed in to change notification settings - Fork 512
feat(experimental): Add WebMCP Adapter #1222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Muhammad-Bin-Ali
wants to merge
14
commits into
main
Choose a base branch
from
feat/webmcp-adapter-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
17ac08e
feat(examples): add WebMCP adapter demo
alekrumkamp 3903d65
fix(examples): correct Chrome flag names for WebMCP
alekrumkamp 10052f6
feat(experimental): add headers and getHeaders options for authentica…
alekrumkamp 82c0825
fix(examples): correct Chrome flags and remove built-in AI agent refe…
alekrumkamp d86da38
Extend tests
Muhammad-Bin-Ali 3c86aa6
Swap out custom built MCP client for SDK MCP Client
Muhammad-Bin-Ali 6458061
Update AGENTS.md
Muhammad-Bin-Ali 6cd084c
Address Devin and Bonk
Muhammad-Bin-Ali ba41d25
Fix npm run check
Muhammad-Bin-Ali dbcbc29
Address devin
Muhammad-Bin-Ali 1a770b8
Fix misleading log
Muhammad-Bin-Ali 149fa88
Fix pacakge-lock
Muhammad-Bin-Ali 9cff39c
Fix version conflict
Muhammad-Bin-Ali 5c4b45b
Add Abort Controller
Muhammad-Bin-Ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": minor | ||
| --- | ||
|
|
||
| Add experimental WebMCP adapter (`agents/experimental/webmcp`) that bridges MCP server tools to Chrome's native `navigator.modelContext` API, enabling browser-native AI agents to discover and call tools registered on a Cloudflare McpAgent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # WebMCP Adapter | ||
|
|
||
| > **WARNING: EXPERIMENTAL.** This example uses `agents/experimental/webmcp` which is under active development and **will break** between releases. Google's WebMCP API (`navigator.modelContext`) is still in early preview. | ||
|
|
||
| Bridges tools registered on an `McpAgent` to Chrome's native `navigator.modelContext` API using the experimental WebMCP adapter. | ||
|
|
||
| ## What it demonstrates | ||
|
|
||
| - **`registerWebMcp()`** — one-line adapter that discovers MCP tools and registers them with Chrome's WebMCP | ||
| - **Feature detection** — graceful fallback when `navigator.modelContext` is unavailable | ||
| - **Dynamic sync** — listens for `tools/list_changed` notifications and re-syncs automatically | ||
| - **McpAgent tools** — same `McpAgent` server as the `mcp` example, with `add`, `greet`, and `get_counter` tools | ||
|
|
||
| ## Running | ||
|
|
||
| ```sh | ||
| npm install | ||
| npm start | ||
| ``` | ||
|
|
||
| Open in Chrome Canary with `#enable-webmcp-testing` and `#enable-experimental-web-platform-features` enabled at `chrome://flags` to see full WebMCP integration. On other browsers, the adapter detects the missing API and shows a status message. | ||
|
|
||
| ## How it works | ||
|
|
||
| The server defines tools using `McpAgent` as usual: | ||
|
|
||
| ```typescript | ||
| export class MyMCP extends McpAgent<Env, State, {}> { | ||
| server = new McpServer({ name: "WebMCP Demo", version: "1.0.0" }); | ||
|
|
||
| async init() { | ||
| this.server.registerTool( | ||
| "greet", | ||
| { | ||
| description: "Greet someone by name", | ||
| inputSchema: { name: z.string() } | ||
| }, | ||
| async ({ name }) => ({ | ||
| content: [{ type: "text", text: `Hello, ${name}!` }] | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default MyMCP.serve("/mcp", { binding: "MyMCP" }); | ||
| ``` | ||
|
|
||
| The client uses the adapter to bridge those tools to Chrome's WebMCP: | ||
|
|
||
| ```typescript | ||
| import { registerWebMcp } from "agents/experimental/webmcp"; | ||
|
|
||
| const handle = await registerWebMcp({ url: "/mcp" }); | ||
| console.log("Registered tools:", handle.tools); | ||
|
|
||
| // Clean up when done | ||
| handle.dispose(); | ||
| ``` | ||
|
|
||
| The adapter: | ||
|
|
||
| 1. Connects to the `/mcp` endpoint via MCP Streamable HTTP | ||
| 2. Calls `tools/list` to discover all registered tools | ||
| 3. Registers each tool with `navigator.modelContext.registerTool()` | ||
| 4. Relays tool execution calls from Chrome's agent back to the MCP server | ||
| 5. Listens for `tools/list_changed` to dynamically sync | ||
|
|
||
| ## Related examples | ||
|
|
||
| - [`mcp`](../mcp/) — stateful MCP server with built-in tool tester UI | ||
| - [`mcp-client`](../mcp-client/) — connecting to MCP servers as a client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* eslint-disable */ | ||
| // Generated by Wrangler by running `wrangler types env.d.ts --include-runtime false` (hash: placeholder) | ||
| declare namespace Cloudflare { | ||
| interface GlobalProps { | ||
| mainModule: typeof import("./src/server"); | ||
| durableNamespaces: "MyMCP"; | ||
| } | ||
| interface Env {} | ||
| } | ||
| interface Env extends Cloudflare.Env {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <!doctype html> | ||
| <html lang="en" data-theme="workers"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="icon" href="/favicon.ico" /> | ||
| <title>WebMCP Adapter Demo</title> | ||
| <script> | ||
| (() => { | ||
| const stored = localStorage.getItem("theme"); | ||
| const mode = stored || "light"; | ||
| document.documentElement.setAttribute("data-mode", mode); | ||
| document.documentElement.style.colorScheme = mode; | ||
| })(); | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/client.tsx"></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "@cloudflare/agents-webmcp-example", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "vite dev", | ||
| "deploy": "vite build && wrangler deploy", | ||
| "types": "wrangler types env.d.ts --include-runtime false" | ||
| }, | ||
| "dependencies": { | ||
| "@cloudflare/kumo": "^1.17.0", | ||
| "@modelcontextprotocol/sdk": "1.29.0", | ||
| "@phosphor-icons/react": "^2.1.10", | ||
| "agents": "*", | ||
| "zod": "^4.3.6" | ||
| }, | ||
| "devDependencies": { | ||
| "@tailwindcss/vite": "^4", | ||
| "tailwindcss": "^4.2.2" | ||
| } | ||
| } |
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.