Library Guide
Use the apibear parser library in Node, browsers, and edge environments from a dependency-free entry point.
The same apibear package that ships the CLI also exports a pure TypeScript library that transforms OpenAPI 3.0 and 3.1 specifications into Model Context Protocol tool definitions and HTTP execution metadata.
Key Features
- Dependency-free entry point:
import ... from "apibear"loads only the parser. The CLI’s dependencies (the MCP SDK and Swagger parser) are installed alongside it but never imported, so bundlers leave them out. - Environment-agnostic: Runs anywhere modern JavaScript runs — Node ≥ 18.17, browsers, Cloudflare Workers, and Deno.
- Contract-stable: Designed as the foundational building block for both CLI runners and browser-based configurators.
Installation
npm install apibear
(This library was briefly published as @apibear/core; that package is deprecated in favor of apibear.)
Same Parser as the CLI
The apibear CLI is built on this library and ships in the same package, so for any given version the two always agree:
- Preview, then build.
npx -y apibear@<version> --spec ./openapi.json --list-toolsprints the same tool names your code gets fromparseOpenApiToToolsat that version. Use the CLI to check a spec, then write code against the tools you saw. - One version to pin. There is no separate parser version to keep in step with the CLI. Pin
apibearonce inpackage.jsonand your MCP client config, and both run identical parsing. - Same filters. The CLI’s
--only listPets,showPetByIdisfilter: { operationIds: ["listPets", "showPetById"] }in the library, with the sameunknown-filter-idwarnings.
Basic Usage
import { parseOpenApiToTools, toMcpTool } from "apibear";
const spec = {
openapi: "3.1.0",
info: { title: "Pet Store", version: "1.0.0" },
paths: {
"/pets": {
get: {
operationId: "listPets",
summary: "List all pets",
responses: { "200": { description: "ok" } }
}
}
}
};
const result = parseOpenApiToTools(spec);
console.log(result.api);
// { title: "Pet Store", version: "1.0.0", baseUrl: null }
console.log(result.tools.length);
// 1
// Strip execution metadata to send standard MCP tool definition over the wire:
const mcpTool = toMcpTool(result.tools[0]);
console.log(mcpTool);
// {
// name: "listPets",
// description: "List all pets",
// inputSchema: { type: "object", properties: {}, required: [], additionalProperties: false }
// }
Tool Naming Rules
MCP tool names must strictly conform to /^[a-zA-Z0-9_-]{1,64}$/.
- Explicit
operationId: Used as the base name if present, with non-compliant characters sanitized to_. - Fallback Synthesis: If no
operationIdis defined, API Bear synthesizes one from the HTTP method and path:GET /pets→get_petsGET /pets/{petId}→get_pets_petIdPOST /v1/users/{id}/deactivate→post_v1_users_id_deactivate
- Collision Deduplication: If two endpoints yield the same name, subsequent operations are suffixed with an incremental counter:
listPets,listPets_2,listPets_3. - Length Cap: Truncated to 64 characters while preserving deduplication suffixes.
Body Parameter Embedding
API Bear handles request bodies using two strategies:
1. merged Embedding (Default for JSON objects)
When a request body is a JSON object, its properties are promoted to top-level properties in the tool’s inputSchema.
The AI assistant provides them as flat arguments alongside path and query parameters. The runner automatically reconstructs the JSON body prior to making the HTTP call.
2. nested Embedding
If the request body schema is not an object (e.g. an array or primitive), or if a body property name collides with a path/query parameter name, the entire body is wrapped in a single top-level argument named body:
{
"properties": {
"body": { "type": "array", "items": { "type": "string" } }
}
}
Parser Warnings & Error Codes
parseOpenApiToTools never throws on non-fatal specification oddities; instead, it populates the warnings array:
| Warning Code | Meaning |
|---|---|
unknown-filter-id |
An operationId provided in filter.operationIds was not found in the spec. |
unresolved-ref |
An internal $ref could not be resolved. |
skipped-unsupported |
An operation or media type is unsupported (e.g. multipart/form-data). |
no-server-url |
The document has no servers block; runner requires --base-url. |
relative-server-url |
The document’s servers URL is relative; runner requires --base-url. |
Fatal errors (such as malformed documents or circular ref chains) throw OpenApiParseError with codes:
invalid-documentunresolvable-refref-cycle