Skip to content

About

Generate browser side WebMCP tools from OpenAPI 3.x (JSON/YAML), outputs JavaScript ES modules via CLI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

openapi-to-webmcp

GitHub repository · npm package

Read the developer docs for the GitHub Pages guide.

Generate browser-side WebMCP tools from OpenAPI 3.0.x and 3.1.x documents. The CLI accepts JSON or YAML and emits a JavaScript ES module by default.

Install and generate

Requires Node.js 20 or newer.

npm install --save-dev openapi-to-webmcp
npx openapi-to-webmcp openapi.yaml
npx openapi-to-webmcp -i openapi.json -o src/webmcp.ts --typescript

JSON (.json), YAML (.yaml / .yml), and standard input (-i -) are supported. Without --output, the CLI writes webmcp.js or webmcp.ts to the current directory. Existing output files are protected; pass --force to replace one. Parent directories are created as needed. Successful runs print the package and OpenAPI versions, API title, input and output paths, output language, and the number of generated tools. --include / --exclude values that match nothing, and runs that select no operations, produce warnings.

npx openapi-to-webmcp --check openapi.yaml
cat openapi.yaml | npx openapi-to-webmcp -i - -o webmcp.js

Options

Option Description
-i, --input <file> Input JSON/YAML file, or - for standard input. A positional input path is also accepted.
-o, --output <file> Output path; .js and .ts extensions select the matching language.
--lang js|ts Select output language. JavaScript is the default.
-t, --typescript Shortcut for --lang ts.
--base-url <url> Override the first OpenAPI server URL with an absolute HTTP(S) URL.
--include <items> Include comma-separated tags, operation IDs, or generated tool names.
--exclude <items> Exclude comma-separated tags, operation IDs, or generated tool names.
--auth bearer|api-key|basic|custom Require matching runtime credentials, or caller-supplied headers for custom; secrets are not generated into source.
--check Parse and validate without writing output.
--force Allow replacing an existing output file.
--pretty Accepted for convenience; generated code is formatted by default.
-h, --help Show CLI help.
-V, --version Show package version.
--debug Include a stack trace when a command fails.

Conflicting language/output extensions and invalid option values are rejected. Usage errors exit with status 2; input, validation, and I/O failures exit with status 1.

Use generated tools

import { createWebMCPTools } from './webmcp.js';

const { tools, register } = createWebMCPTools({
  baseUrl: 'https://api.example.com',
  headers: { 'X-Client': 'my-app' },
  auth: { type: 'bearer', token: readTokenAtRuntime() },
});

await register(); // Uses document.modelContext.registerTool(tool)

You may instead pass a model context explicitly: await register(document.modelContext) (JavaScript and TypeScript output both default to document.modelContext). The returned tools can also be invoked directly with tool.execute(args). Generated handlers send requests using Fetch, map path/query/header/cookie parameters and request bodies, and return parsed JSON or plain text. Non-2xx responses reject with the HTTP status and response text.

Before sending a request, each tool checks its arguments: required arguments must be present, unknown arguments are rejected, and every provided argument is checked against its declared type (integer requires a whole number; nullable and 3.1 ["string", "null"] types accept null) and enum. Tools for GET, HEAD, and OPTIONS operations include annotations: { readOnlyHint: true }.

Request bodies use the operation's media type: JSON (application/json and *+json) is sent with JSON.stringify, application/x-www-form-urlencoded is sent as URL-encoded form fields, and text/* strings are sent as-is. Object query parameters are expanded to key=value pairs (OpenAPI form + explode default); array and object header/cookie values use comma-separated simple serialization.

Vendor extensions

Extension Effect
x-webmcp-skip: true Do not generate a tool for the operation.
x-webmcp-name: <name> Use this tool name instead of the operationId.
x-webmcp-description: <text> Use this tool description instead of the operation description / summary.

Runtime authentication examples:

{ type: 'bearer', token }
{ type: 'apiKey', in: 'header', name: 'X-API-Key', value }
{ type: 'apiKey', in: 'query', name: 'api_key', value }
{ type: 'basic', username, password }

Use headers for custom authentication. Supply secrets at runtime; do not put them in the OpenAPI document or generated module.

Supported input and limitations

  • OpenAPI 3.0.x and 3.1.x in JSON or YAML, including local JSON Pointer $ref references.
  • Path, query, header, and cookie parameters; common primitive, enum, array, and object schemas; required fields; and JSON, URL-encoded form, and text request bodies.
  • Recursive (self-referencing) schemas are expanded until the reference repeats; the repeated level accepts any value.
  • Server URL variables (for example https://{region}.example.com) are filled with their default values. A warning is shown when a variable has no default.
  • Missing operation IDs are derived from method and path. Duplicate tool names receive deterministic numeric suffixes.
  • Path template placeholders without a matching parameter declaration are added as required string arguments, with a warning.
  • The TypeScript return type comes from the first 2xx response that has content (falling back to default), not from error responses.
  • Operations with unsupported parameter locations, a parameter name used in more than one location, unresolved references, a required body with an unsupported media type (such as multipart/form-data), or Fetch-incompatible GET/HEAD bodies are skipped with warnings or rejected. Optional bodies with an unsupported media type are dropped, with a warning.
  • External references, advanced parameter serialization styles (deepObject, pipeDelimited, and similar), multipart/form-data and binary uploads, response validation, and remote $ref loading are not supported.
  • Output is a browser ESM module using fetch, Headers, URL, and, for Basic authentication, btoa. TypeScript output requires DOM library types.
  • Browser requests are subject to CORS and browser-controlled header restrictions (notably Cookie). Use only trusted API hosts; a server-side proxy may be needed for private APIs or restricted authentication flows.

Troubleshooting

  • Could not parse input: Check that the file is valid JSON or YAML and uses a supported extension. Parser-provided line and column information is shown when available.
  • Invalid OpenAPI document: Review the named OpenAPI field or path in the error and validate the document against its declared OpenAPI version.
  • Output already exists: Choose a different --output path or pass --force to replace it.
  • WebMCP or Fetch is unavailable: Run the generated module in a browser that exposes document.modelContext and Fetch, or pass a compatible model context and fetch implementation.
  • Browser request fails: Confirm the API permits the page origin via CORS and that authentication does not rely on browser-restricted headers.

Library API

import { parseOpenAPI, generateCode, selectOperations, validateOpenAPI } from 'openapi-to-webmcp';

selectOperations(api, { include, exclude }) returns the operations that generateCode would turn into tools with the same filters.

Browser library via CDN

The browser entry is an ES module for generating a browser-side WebMCP module from the normalized API object returned by parseOpenAPI. Parse the source specification as part of your build step; this browser entry does not bundle the Node-oriented YAML parser.

<script type="module">
  import { generateCode } from
    'https://cdn.jsdelivr.net/npm/openapi-to-webmcp@1/dist/browser.js';

  // `api` is the normalized result produced by parseOpenAPI during your build.
  const api = await fetch('/normalized-openapi.json').then((response) => response.json());
  const generated = generateCode(api);
  console.log(generated); // Generated ESM uses fetch and document.modelContext
</script>

The browser module itself is published at https://cdn.jsdelivr.net/npm/openapi-to-webmcp@1/dist/browser.js (latest 1.x) and can also be imported with import { generateCode } from 'openapi-to-webmcp/browser' in bundlers. Build it locally with npm run build:browser; npm pack builds it automatically. The generated module registers tools through document.modelContext.registerTool, available in browsers implementing the WebMCP API. The browser environment must provide Fetch, URL, Headers, and (for Basic auth) btoa.

About

Generate browser side WebMCP tools from OpenAPI 3.x (JSON/YAML), outputs JavaScript ES modules via CLI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages