Astro integration that exposes your site content via WebMCP for AI agents. Make your Astro site AI-agent ready in one line of code.
WebMCP is an experimental browser API that lets websites expose structured tools for AI agents, such as searching articles, navigating to a page, or reading metadata. The community draft is not a W3C Standard, and Chrome's experimental features can differ from that draft.
- Spec: https://webmachinelearning.github.io/webmcp/
- Chrome docs: https://developer.chrome.com/docs/ai/webmcp
- GitHub: https://github.com/webmachinelearning/webmcp
npm install @freshjuice/astro-webmcp// astro.config.mjs
import { defineConfig } from 'astro/config';
import webmcp from '@freshjuice/astro-webmcp';
export default defineConfig({
integrations: [webmcp()],
});The integration exposes metadata for generated pages through WebMCP tools in supported browsers. Development uses a Home-only stub manifest; SSR middleware does not build a complete index of dynamic content.
webmcp({
// Filter which collections to expose (default: all)
collections: ['blog', 'docs'],
// Custom tools: expose your own domain-specific functionality
customTools: [
{
name: 'search_products',
description: 'Search the product catalog by name, category, or keyword.',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search term' },
},
required: ['query'],
},
executeBody: `return safeOutput(await fetch('/api/search?q=' + encodeURIComponent(params.query), { signal })
.then(r => { if (!r.ok) throw new Error('Search failed'); return r.json(); }));`,
annotations: { readOnlyHint: true, untrustedContentHint: true },
},
],
// Auto-register annotated <form> elements as WebMCP tools
formScanning: true,
// Search backend for search_content (default: 'manifest')
search: {
backend: 'pagefind', // 'manifest' | 'pagefind' | 'orama'
oramaIndexUrl: '/search-index.json', // required for 'orama'
pagefindBundlePath: '/pagefind/', // default for 'pagefind'
},
// Security options
security: {
exposedTo: [], // origins allowed cross-origin access (default: none)
maxOutputLength: 1500, // max chars per tool output (default: 1500)
sanitizeOutputs: true, // heuristic output filtering (default: true)
},
})| Option | Type | Default | Description |
|---|---|---|---|
collections |
string[] |
undefined (all) |
List of collections to include in the manifest |
customTools |
CustomTool[] |
[] |
Domain-specific tools to register alongside built-in ones |
formScanning |
boolean |
false |
Auto-register <form toolname="..." tooldescription="..."> elements as tools |
search.backend |
'manifest' | 'pagefind' | 'orama' |
'manifest' |
Search backend for search_content |
search.oramaIndexUrl |
string |
Unset | URL of pre-built Orama index (required for 'orama') |
search.pagefindBundlePath |
string |
'/pagefind/' |
Compatibility option; load window.pagefind yourself |
security.exposedTo |
string[] |
[] |
Origins allowed to access tools cross-origin |
security.maxOutputLength |
number |
1500 |
Character limit per tool output |
security.sanitizeOutputs |
boolean |
true |
Strip patterns that resemble prompt injection |
The customTools array lets you expose your own site-specific functionality. Each tool needs:
name: unique tool identifier (1-128 ASCII letters, digits,_,-, or.).description: natural language description for AI agents.inputSchema: JSON Schema for the tool's parameters; validate actual inputs in application code too.executeBody: async function body string, run in the browser withparams,safeOutput, and the per-executionsignal. Supportsawait; passsignaltofetch()for cancellation.title: optional human-readable display title.annotations: optionalreadOnlyHint,untrustedContentHint,consequentialHint, anddebugginghints.readOnlyHintdefaults tofalse; declaretrueonly for non-mutating tools. Chrome documentsdebuggingfrom version 156.
safeOutput() returns the package's MCP-style { content: [{ type: 'text', text: '...' }] } envelope. WebMCP permits JSON-serializable results and does not require this envelope. If you set outputSchema, match the actual returned structure (either the safeOutput() envelope or your raw object). Tool code is public browser code: keep secrets and authorization checks on the server.
search_content supports three backends, with automatic fallback to manifest search:
| Backend | Description | Requires |
|---|---|---|
manifest (default) |
Substring search on the generated manifest | A valid manifest |
pagefind |
Full-text search via Pagefind | astro-pagefind or pagefind on the page |
orama |
Full-text search via Orama | @freshjuice/astro-search-plugin or similar, with oramaIndexUrl |
formScanning: true enables the package's opt-in form scanner. It accepts Chrome's experimental declarative attributes and legacy attributes. Declarative forms were removed from the community draft in October 2026; this scanner is a compatibility extension, not a standards-compliant polyfill.
<!-- Chrome experimental attributes -->
<form toolname="search_products" tooldescription="Search product catalog by keyword">
<input name="query" type="text" required toolparamdescription="Search term">
<button type="submit">Search</button>
</form>
<!-- legacy attributes (still works) -->
<form name="search_products" description="Search product catalog by keyword">
<input name="query" type="text" required>
<button type="submit">Search</button>
</form>The scanner derives input schemas from fields and fills them when called. It calls requestSubmit() only for forms with toolautosubmit; otherwise the user submits the filled form. It skips experimental native form tools already discoverable in the current window. Use unique names across built-in, custom, and scanned tools, and test native registration timing in your target browser. Scanner submission does not wait for your server-side operation to finish.
| Tool | Description |
|---|---|
search_content |
Search articles and pages by keyword (supports manifest, Pagefind, or Orama backends) |
list_sections |
List available content sections with item counts |
go_to |
Navigate to a specific page by slug; browser consent is not guaranteed |
get_page_info |
Get current page metadata (title, description, headings, language, word count, canonical URL) |
| declarative forms | Any <form toolname="..." tooldescription="..."> (or legacy name/description) when formScanning: true |
| your custom tools | Whatever you define via customTools |
┌──────────────────────────────────────────────────────────┐
│ BUILD TIME │
│ │
│ Astro pages ────→ Hook astro:build:done │
│ │ │
│ ▼ │
│ /_webmcp/manifest.json │
│ (titles, slugs, descriptions, │
│ tags, OG metadata, lang, word count) │
└──────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────┐
│ RUNTIME (Browser) │
│ │
│ Injected script (head-inline) │
│ │ │
│ ├─ fetch('/_webmcp/manifest.json') │
│ │ │
│ ├─ document.modelContext.registerTool() │
│ │ ├─ search_content (manifest/pagefind/orama) │
│ │ ├─ list_sections │
│ │ ├─ go_to (navigation) │
│ │ ├─ get_page_info (enhanced metadata) │
│ │ └─ custom tools (user-defined) │
│ │ │
│ └─ scanDeclarativeForms() (if formScanning: true) │
└──────────────────────────────────────────────────────────┘
WebMCP is experimental and available through Chrome's testing flag or origin trial. Use HTTPS (localhost is suitable for development), feature-detect document.modelContext, and consult Chrome Status and the trial page for current platforms, versions, and expiry. A version number alone does not guarantee availability. The integration retains navigator.modelContext only for older previews.
- Open
chrome://flags#enable-webmcp-testing - Enable the flag
- Restart Chrome
For production sites (no flag required for visitors), register for a Chrome Origin Trial token:
- Go to https://developer.chrome.com/origintrials/#/register_trial/4163014905550602241
- Register your domain and get a token
- Add the token to your site's
<head>:
<meta http-equiv="origin-trial" content="YOUR_TOKEN_HERE">In Astro, add this to your base layout (src/layouts/Layout.astro or similar):
<meta http-equiv="origin-trial" content={import.meta.env.WEBMCP_ORIGIN_TRIAL_TOKEN}>You can manage the token in
.envasWEBMCP_ORIGIN_TRIAL_TOKEN=your-token. The rendered token is public and origin-scoped, not a secret.
The trial is experimental deployment, not a stable-release commitment. Check the live trial requirements before shipping.
Read Chrome's tool security guidance before exposing application actions.
- Annotations describe behavior; they are not authorization or consent controls. Validate inputs and permissions in application code, and implement site-owned confirmation for consequential actions.
requestUserInteraction()is not in the current ModelContext IDL.go_touses it only if present for compatibility and otherwise navigates without that prompt.- Output truncation limits text size (default 1500 characters). The regexp sanitizer is a heuristic, not a prompt-injection security boundary.
- Cross-origin access requires secure origins in the provider's
exposedToand the caller'sgetTools({ fromOrigins: [...] }). Cross-origin frames also needallow="tools"; exposure alone does not delegate registration permission.
See the usage guide for automated tests, native-browser smoke checks, cancellation, and evaluation guidance.
This is a fork of the original astro-webmcp by fabricioctelles, maintained by Alex Zappa at FreshJuice with:
- Script injection through the
head-inlinestage on Astro v6 and v7 - Custom tools configured in
astro.config.mjs - Pagefind and Orama full-text search, with manifest fallback
- Opt-in registration of annotated forms
- Tags, OpenGraph, canonical URL, language, and word count in the manifest
- English documentation and comments
- Usage guide: detailed setup, testing, and troubleshooting
- Architecture: design decisions and component breakdown
- Changelog: version history