Agent / foldkit-agent-webmcp

agent-webmcp

Giving an AI assistant running inside the browser tab access to your application usually relies on fragile DOM scraping or maintaining a separate API client. You want browser-resident agents to call typed tools backed by your live application state and update loop.

foldkit-agent-webmcp publishes a bound foldkit-agent runtime onto WebMCP (document.modelContext) and declarative HTML forms. Tool calls from the browser pass through your contract's schema decode, availability, and authorization rules before dispatching Messages into your running Foldkit app. See it in action in the live Todo agent demo.

Install

bash
pnpm add foldkit-agent foldkit-agent-webmcp

foldkit, effect, and foldkit-agent are peer dependencies.

A first WebMCP registration

Bind a contract to your live host (or Agent.headless in a minimal script) and register its available tools when WebMCP is present in the browser (see the Agent contract guide for Agent.host and Sync.mount setup):

ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Agent } from 'foldkit-agent'
import { AgentWebMcp } from 'foldkit-agent-webmcp'

const Model = Schema.Struct({ todos: Schema.Array(Schema.String) })
const Message = defineMessageUnion({
  RequestedCreateTodo: { title: Schema.String },
  DraftSubmitted: {},
})
const TodoAgent = Agent.forModel<typeof Model.Type>()
const AppAgent = TodoAgent.make({
  messages: TodoAgent.expose(Message, { RequestedCreateTodo: 'Create a todo' }),
})
type AppModel = typeof Model.Type
const { host } = Agent.headless({
  init: (): { model: AppModel; commands: [] } => ({ model: { todos: [] }, commands: [] }),
  update: (model: AppModel) => ({ model, commands: [] }),
})
const agentRuntime = TodoAgent.bind({ definition: AppAgent, host })

const modelContext = AgentWebMcp.pageModelContext()
if (modelContext !== undefined) {
  const registration = AgentWebMcp.register({ agent: agentRuntime, modelContext })
  await registration.refresh()
  window.addEventListener('pagehide', () => registration.unregister())
}

When the browser exposes document.modelContext (or navigator.modelContext in older WebMCP builds), AgentWebMcp.register registers requested_create_todo as a browser tool and reconciles whenever the Model changes. pageModelContext() returns undefined safely during SSR or in browsers without WebMCP; calling register() without a context throws rather than succeeding silently. Call registration.unregister() during component or runtime teardown as well as on pagehide, and call await registration.refresh() when callers need confirmation that the browser accepted the current tool set (passing onError for background reconcile errors).

How capabilities map to WebMCP tools

foldkit-agentWebMCP tool
Capability nameTool name
Variant descriptiondescription
Encoded input SchemainputSchema
AgentRuntime dispatchexecute
Completion contractResolved tool outcome

execute delegates directly to AgentRuntime, enforcing schema decoding, availability, authorization, cancellation, and Message or state completion without duplicating logic.