Skip to content

CLI Reference

Complete command-line reference, arguments, exit codes, and transport rules for apibear.

apibear runs as a Node.js process serving MCP tools over stdio.

npx -y apibear --spec <path-or-url> [options]

Or install it globally with npm install -g apibear and run apibear --spec <path-or-url> [options]. To pin a release, use npx -y apibear@<version>.

The CLI is a thin stdio server around the parser library in the same package. Every tool it serves comes from parseOpenApiToTools, so you can import the identical logic in your own code; see the Library Guide.


Command-Line Arguments

--spec <path|url> (Required)

The OpenAPI 3.0 or 3.1 specification to load.

  • Can be a local filesystem path (./openapi.json, ../spec.yaml) or a remote HTTPS URL (https://api.example.com/openapi.json).
  • Supports both JSON and YAML documents.
  • If loading or parsing fails, the runner exits with code 2.

--header <Name:Value> (Optional, Repeatable)

Header to inject into every upstream HTTP request made by tool calls.

  • Repeatable: Pass --header multiple times to send multiple headers:
    --header "Authorization: Bearer sk-12345" --header "X-Workspace-Id: ws_99"
  • Parsing rule: Split on the first colon (:). The header name and value are trimmed.
  • Safety check: Any --header argument without a colon or with an empty name causes the runner to exit immediately with code 2 (preventing silent auth failures).
  • Precedence: If duplicate header names are supplied, the last occurrence wins. CLI headers always override spec-declared header parameters.
  • Environment variables: ${VAR} in a value is replaced with that environment variable, so tokens can live in your MCP client’s env block instead of args. $${ gives a literal ${. An unset or empty variable exits with code 2 and names the variable, never the value:
    --header 'Authorization: Bearer ${API_TOKEN}'
  • Redirects: Injected headers are dropped once a redirect leaves the original origin, so keys are never forwarded to another host.

--only <id,id,...> (Optional)

A comma-separated list of operationIds to expose to the MCP client.

  • Useful for controlling context window usage when working with large specifications.
  • Unknown operation IDs trigger a diagnostic warning on stderr, but valid operation IDs will still be served.
  • Example: --only listPets,showPetById

--base-url <url> (Optional)

Overrides or supplies the upstream API base URL.

  • If the OpenAPI specification does not define a servers block, or specifies a relative URL (e.g. /api/v1), --base-url is mandatory.
  • If omitted when required, individual tool calls return a clear configuration error to the model.
  • Example: --base-url https://api.production.example.com

--timeout <ms> (Optional)

The timeout in milliseconds for each upstream HTTP request.

  • Default: 30000 (30 seconds).
  • If an upstream endpoint fails to respond within this window, the tool call safely aborts and returns an isError: true tool result to the client without terminating the MCP server process.

--list-tools (Optional)

Prints all parsed tool names and their descriptions to stdout and exits with code 0.

  • Does not start the MCP stdio server.
  • Designed for verification, debugging, and CI scripts.
  • This is the only command that prints non-JSON-RPC text to stdout.

--help

Prints the help screen to stdout and exits with code 0.

--version

Prints the installed version of apibear to stdout and exits with code 0.


Exit Codes

Exit Code Name Meaning
0 Success Normal termination (e.g. --list-tools, --help, or stdin closed by client).
2 Usage / Load Error Invalid arguments, malformed header format, missing spec, or zero tools generated.
1 Runtime Crash Unexpected runtime failure or process error.

Stdio Transport Protocol Rules

The Model Context Protocol stdio transport communicates over standard I/O streams using newline-delimited JSON-RPC messages.

CRITICAL RULE: STDOUT IS THE PROTOCOL TRANSPORT.

Nothing may ever be written to stdout except valid MCP protocol frames. A single stray console.log or warning on stdout will corrupt the client stream and crash the MCP session.

API Bear strictly enforces this:

  • stdout: Dedicated 100% to MCP JSON-RPC protocol messages (and --list-tools before exit).
  • stderr: Used for all runtime logging, diagnostics, startup messages, and warnings ([apibear] serving 3 tools...).
  • Security: Secret header values passed in --header are never logged to stderr; only header names are printed in startup summaries.