Expand description
Browser-side WebMCP for Rust.
WebMCP lets a page hand tools to whatever agent is driving the browser. Unlike a hosted MCP server, the tools run inside the session the user is already logged into: no API key, no second copy of your authorization rules, and the agent sees exactly what the user can see.
§What this crate adds over calling the API directly
js_sys::eval can register a tool in five lines. What it cannot do is keep
the JSON Schema the agent plans against in sync with the struct the handler
destructures, or guarantee the tool disappears when the UI behind it does.
Those are the two failure modes this crate exists to remove:
Tool::register_typedderivesinputSchemafrom the argument type, so the schema and the deserialization target are one declaration.ToolHandleunregisters on drop, which under the current spec means aborting theAbortSignalhanded toregisterTool().
§Targets
Off wasm — SSR, native, tests — every registration returns
Error::Unsupported instead of failing to compile, so the same component
code builds for both halves of a fullstack app.
§Spec drift
The browser API is not stable yet. This crate targets the surface as of the
April 2026 draft: registerTool(tool, { signal }) on document.modelContext,
falling back to the deprecated navigator.modelContext alias. The removed
provideContext() / clearContext() / unregisterTool() methods are not
implemented. See wasm::model_context for how the lookup degrades.
§Example
use webmcp::{Tool, ToolResult};
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct AddTodo {
/// The text content of the todo item.
text: String,
}
let handle = Tool::new("add_todo")
.description("Add an item to the user's todo list")
.register_typed(|args: AddTodo| async move {
Ok::<_, std::convert::Infallible>(ToolResult::text(format!("Added {}", args.text)))
})?;
// `handle` keeps the tool alive; dropping it unregisters.Re-exports§
pub use dioxus::use_tool;
Modules§
- dioxus
- Dioxus integration: tie a tool’s lifetime to a component’s.
- leptos
- Leptos integration: tie a tool’s lifetime to a component’s.
- wasm
- The browser side: raw access to
modelContextand the registration itself.
Structs§
- Tool
- Builder for a tool the page offers to agents.
- Tool
Handle - Keeps a tool registered. Dropping it unregisters the tool.
- Tool
Result - A tool’s reply.
Enums§
- Content
- One block of a tool’s reply. Only text blocks are modelled: the browser API
carries the same
contentarray as MCP proper, but a page has no business streaming binary blobs at an agent, and every richer block type would need a matching browser-side sanitiser. - Error
Functions§
- is_
supported - Whether the current page exposes a model context to register tools on.
- schema_
for - JSON Schema for
T, trimmed to what a tool descriptor wants.
Type Aliases§
- Result
- Result alias for registration operations.