Skip to main content

Crate webmcp

Crate webmcp 

Source
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_typed derives inputSchema from the argument type, so the schema and the deserialization target are one declaration.
  • ToolHandle unregisters on drop, which under the current spec means aborting the AbortSignal handed to registerTool().

§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 modelContext and the registration itself.

Structs§

Tool
Builder for a tool the page offers to agents.
ToolHandle
Keeps a tool registered. Dropping it unregisters the tool.
ToolResult
A tool’s reply.

Enums§

Content
One block of a tool’s reply. Only text blocks are modelled: the browser API carries the same content array 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.