Skip to content
webmcpPublic

About

webmcp on npm: define WebMCP tools once, use them in the page, on an MCP server, and as a manifest

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

webmcp

Define WebMCP tools once. Register them in the page, serve the same list from an MCP server, or publish it as a static manifest.

WebMCP is a draft of the W3C Web Machine Learning Community Group: a page registers its functions as tools through document.modelContext, and agents in the browser call them. This package does not implement that API. For browsers without it, use the official @w3c/webmcp-polyfill. This package is the glue around the tool list.

Zero runtime dependencies. ESM. Node 18 or later for the server side; any modern browser for the page side.

Install

npm install webmcp

Or straight from the site, no build step:

<script type="module">
  import { register } from 'https://webmcp.org/webmcp/index.js';
</script>

Define tools

Tools use the draft's own shape: name, description, an optional JSON Schema inputSchema, optional annotations, and execute.

import { defineTools } from 'webmcp';

export const tools = defineTools([
  {
    name: 'add-todo',
    description: "Add an item to the user's todo list",
    inputSchema: {
      type: 'object',
      properties: { text: { type: 'string', description: 'The todo text' } },
      required: ['text'],
    },
    async execute({ text }) {
      await addTodo(text);
      return { content: [{ type: 'text', text: `Added "${text}"` }] };
    },
  },
]);

defineTools throws on a missing name, description or execute, on a duplicate name, and on a non-object schema, so mistakes show up when the module loads.

In the page

import { register, discover } from 'webmcp';
import { tools } from './tools.js';

const reg = await register(tools);          // document.modelContext.registerTool() for each
console.log(await discover());              // [{ name, description, inputSchema, annotations }]
reg.unregister();                           // aborts the shared signal; all tools go

Pass { context } to use a context other than document.modelContext, { signal } to tie the registration to your own controller, and { exposedTo: [...origins] } for cross-origin iframe exposure as in the draft. Without a context register rejects with a message saying so.

On an MCP server

The same list answers tools/list and tools/call:

import { toMcp, callTool } from 'webmcp';
import { tools } from './tools.js';

server.on('tools/list', () => toMcp(tools));
server.on('tools/call', ({ name, arguments: args }) => callTool(tools, name, args));

callTool returns { content, isError? } whatever the execute callback returned: a content array is passed through, a string becomes one text block, any other value is JSON-encoded, a thrown error becomes isError: true with the message.

As a manifest

import { toManifest } from 'webmcp';
import { tools } from './tools.js';

JSON.stringify(toManifest(tools, { name: 'My shop', url: 'https://shop.example/' }), null, 2);

A static, code-free description of the tools: what an agent could read before loading the page. The format is a draft and will follow whatever the group settles on for discovery.

API

Function Does
defineTools(tools) Validate and freeze a tool list.
describeTool(tool) The tool without its code.
getContext(doc?) document.modelContext or null.
register(tools, { context?, signal?, exposedTo? }) Register all; returns { tools, signal, unregister() }.
discover({ context?, fromOrigins? }) Tools the context exposes now.
toMcp(tools) A tools/list result.
callTool(tools, name, args?, { signal? }) A tools/call result.
toManifest(tools, { name?, url?, description? }) Static JSON description.

Develop

npm test

Source and issues: https://github.com/webmcp/webmcp. Site: https://webmcp.org/.

Licence

AGPL-3.0-only. Copyright Melvin Carvalho.

About

webmcp on npm: define WebMCP tools once, use them in the page, on an MCP server, and as a manifest

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages