WebMCP Implementation Guidelines for AI Agents
Defines client-side WebMCP tool registration rules using JavaScript and declarative HTML forms, banning backend MCP servers.
What this file does
Defines client-side WebMCP tool registration rules using JavaScript and declarative HTML forms, banning backend MCP servers.
When to use it
- Adding agent-callable tools to a frontend-only web app
- Refactoring HTML forms into agent-discoverable interfaces
- Writing unit tests that mock WebMCP tool responses
- Documenting new tools in an llms.txt discovery file
Assumes this stack
WebMCP Implementation Guidelines for AI Agents
0. Architectural Directive
-
Target Environment: This project utilizes the client-side Web Machine Context Protocol (WebMCP).
-
Restriction: DO NOT create backend Model Context Protocol (MCP) servers using Python, Node.js, or Go. All tool execution logic must remain within the frontend client-side JavaScript context.
1. Dependencies and Setup
-
Install the global polyfill dependency:
npm install @mcp-b/global. -
Import the polyfill at the top of the relevant component or application entry point:
import '@mcp-b/global';.
2. Imperative API Implementation (JavaScript Tools)
Expose programmatic capabilities by registering tools via the global navigator object.
-
Registration: Use
window.navigator.modelContext.registerTool(toolObject). -
Tool Dictionary Schema:
-
name: A unique string identifier for the tool (e.g.,calculate\_sum). Tool name collisions will throw synchronous errors. -
description: A clear, positive natural language prompt explaining the tool's purpose and the logic behind its parameters. -
inputSchema: A strictly typed object adhering to JSON Schema Draft 2020-12 defining all expected arguments. Use specific enums, required arrays, and primitives. -
execute(args): An asynchronous callback function that executes the logic and MUST return aPromiseresolving to the content payload. -
Lifecycle Management (React/Vue): Tools must be registered inside component lifecycle hooks (e.g.,
useEffectin React). You must implement cleanup logic usingunregisterTool(name)when the component unmounts to prevent context collisions. -
Performance: For long-running tasks, stream intermediate updates or logs to prevent the agent from timing out. For database queries, enforce limits and pagination to avoid exceeding context windows.
3. Declarative API Implementation (HTML Forms)
When refactoring existing HTML forms, convert them into agent-callable tools by injecting declarative attributes.
-
Add
toolname="unique\_identifier"directly to the<form>tag. -
Add
tooldescription="Description of the form's purpose"to the<form>tag. -
Add
toolparamdescription="Description of expected data"to individual<input>,<select>, or<textarea>elements to automatically generate the tool's schema properties. -
(Optional) Add the
toolautosubmitboolean attribute to the<form>if the agent is authorized to submit the payload without waiting for manual human confirmation.
4. Implicit Actuation Optimization (Fallback)
Ensure the User Interface remains accessible for fallback agents relying on Document Object Model parsing rather than explicit APIs.
-
Strictly utilize semantic HTML5 elements (
<main>,<article>,<section>,<nav>, and native<button>tags). -
Apply explicit
aria-labelattributes to custom interactive elements (e.g., visually styled calculator operators). Do not use ARIA attributes if a native HTML element accomplishes the same task.
5. Testing Protocols
- When generating unit tests that execute in headless virtual machines, utilize the
window.navigator.modelContextTesting.setMockToolResponse("tool\_name", mockResponse)method. This bypasses the liveexecute()callback and simulates a successful agent execution loop.
6. Discovery Integration
- Upon creating a new WebMCP tool or exposing a new declarative form, you must update the
llms.txtfile located at the repository/domain root. Ensure the new tool endpoints and capabilities are documented in the markdown index so navigating agents can discover them.
What's inside
6 sections covering architecture, setup, imperative API, declarative API, testing, and discovery
Change this for your project
- Replace
npm install @mcp-b/globalwith your own polyfill package if different - Replace
llms.txtpath with your actual discovery file location
Where it goes
Save as AGENTS.md in your repository root. Read by Codex, Cursor and other agents that follow the AGENTS.md convention.
Worth borrowing
- Register tools in lifecycle hooks with cleanup to avoid collisions
- Use declarative HTML attributes to expose forms as agent tools without extra JS
Related Documents
Browser-only development
Guides AI assistants on an Electron + React + TypeScript desktop app for browsing and organizing AI-generated images locally.
Claude Agents — Reference & Recommendations
Catalogues 40+ Claude agents and marketing skills for building a cat adoption charity landing page, with a ready-to-paste prompt and backend API reference.
Golden DKG Prototype -- Master Plan
Defines an 8-phase implementation plan for a Rust prototype of the Golden non-interactive DKG protocol using BLS12-381 and tokio.
Swarms Examples Index
Lists 60+ example scripts for building single and multi-agent systems with the Swarms framework, organized by category and use case.