swagger-digest 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +210 -135
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,101 +1,141 @@
1
1
  # swagger-digest
2
2
 
3
- `swagger-digest` is an interactive Node.js CLI that converts a Swagger UI page or raw OpenAPI/Swagger spec into a compact Markdown reference designed for AI coding agents and LLM context windows.
3
+ [![npm version](https://img.shields.io/npm/v/swagger-digest)](https://www.npmjs.com/package/swagger-digest)
4
+ [![license](https://img.shields.io/npm/l/swagger-digest)](https://github.com/VladLegkowski/swagger-digest)
5
+ [![node](https://img.shields.io/badge/node-%3E%3D20-blue)](https://www.npmjs.com/package/swagger-digest)
6
+ [![MCP server](https://img.shields.io/badge/MCP-server-8a5cf5)](https://modelcontextprotocol.io)
4
7
 
5
- ## Quick start
8
+ **An API context engine for AI coding agents.** Turn large Swagger/OpenAPI specifications into compact, structured, searchable API context — as Markdown digests or through an on-demand MCP server.
6
9
 
7
- ```bash
8
- npx swagger-digest
9
- ```
10
+ A 600-operation spec like Stripe's is ~2M tokens of raw JSON — several times most models' context windows, and mostly noise even when it fits. swagger-digest reduces it to ~560k tokens of structured Markdown (benchmark below), and its MCP server lets your agent pull in only the endpoints and schemas it needs, when it needs them.
10
11
 
11
- The CLI asks for a URL, a destination directory (Downloads by default), and an output filename such as `payments-api-digest.md`.
12
+ ## The problem
12
13
 
13
- You can also provide arguments:
14
+ - Real specifications contain hundreds of endpoints and thousands of lines of JSON/YAML.
15
+ - Pasting a raw spec into an AI coding agent burns context on boilerplate — descriptions repeated per operation, example payloads, schema trees.
16
+ - Finding the endpoints relevant to your task — and understanding their parameters and response shapes — means manually wading through the contract.
17
+ - Agents generating frontend code need accurate API contracts. Guessing field names from partial context produces broken integrations.
14
18
 
15
- ```bash
16
- npx swagger-digest \
17
- --url https://petstore.swagger.io/ \
18
- --out-dir ./docs \
19
- --filename petstore-digest.md
20
- ```
19
+ swagger-digest solves this at two levels: a **digest** (compact, complete, token-budgeted Markdown) and an **MCP server** (on-demand API context for agents).
20
+
21
+ ## Two ways to use it
22
+
23
+ ### Option 1: MCP server — recommended for AI coding agents
24
+
25
+ Run it as an [MCP](https://modelcontextprotocol.io) server and your agent explores APIs on its own: it calls the `swagger_digest` tool with a Swagger UI page or spec URL and receives a compact digest directly in its context — discovery, filtering and token budgets handled for it.
26
+
27
+ - **On-demand:** the agent fetches API context only when the task needs it.
28
+ - **Scoped:** filter to specific tags or paths so only relevant operations enter context.
29
+ - **Discovery built in:** point it at a Swagger UI page — it finds the spec automatically (initializer scripts, multi-spec dropdowns, common spec paths).
30
+ - **Ideal for ongoing development work**, where the agent revisits the same API repeatedly without you re-pasting documentation.
21
31
 
22
- For a direct spec URL:
32
+ Install in your MCP client (stdio transport; the client spawns the process):
23
33
 
24
34
  ```bash
25
- npx swagger-digest --url https://example.com/openapi.yaml --out-dir ./docs
35
+ # OpenCode
36
+ opencode mcp add swagger-digest -- npx -y swagger-digest-mcp
37
+ ```
38
+
39
+ ```json
40
+ // JSON-configured clients (Claude Desktop, Cursor, …)
41
+ {
42
+ "mcpServers": {
43
+ "swagger-digest": {
44
+ "command": ["npx", "-y", "swagger-digest-mcp"]
45
+ }
46
+ }
47
+ }
26
48
  ```
27
49
 
28
- If the Swagger UI initializer lists several spec definitions (`urls: [{ url, name }, …]`), the tool probes them in the order they appear and uses the first one that parses, printing a notice to stderr naming the pick and the alternatives. To choose a specific spec yourself, pass it explicitly:
50
+ Tool arguments and environment variables are documented in [MCP configuration](#mcp-configuration).
51
+
52
+ ### Option 2: Markdown digest
53
+
54
+ Generate a compact Markdown reference from any Swagger UI page or OpenAPI/Swagger spec — for documentation, sharing, or feeding API context into agents that don't use MCP.
29
55
 
30
56
  ```bash
31
- npx swagger-digest --url https://api.example.com/swagger-ui \
32
- --spec https://api.example.com/v2/tenant-a.json --out-dir ./docs
57
+ # Interactive
58
+ npx swagger-digest
59
+
60
+ # Direct: fetch, digest, save
61
+ npx swagger-digest --url https://petstore.swagger.io/ --out-dir ./docs
62
+
63
+ # To stdout (pipes stay clean; progress goes to stderr)
64
+ npx swagger-digest --url https://petstore.swagger.io/ --stdout | pbcopy
65
+
66
+ # Keep only the operations you need
67
+ npx swagger-digest --url https://api.example.com/openapi.json \
68
+ --include-tag Subscriptions --path-prefix /v1/ --out-dir ./docs
33
69
  ```
34
70
 
35
- `--spec` overrides all discovery: the given URL is fetched directly (with the same header scoping and size limits) and the Swagger UI page is never consulted when it parses successfully.
71
+ Output is a structured Markdown document: endpoint index, per-tag operation details (auth, parameters, request/response bodies, examples), and TypeScript-like data type declarations. Save it next to your code, paste it into a PR, or drop it into your agent's context manually.
36
72
 
37
- ### Writing to stdout
73
+ ## Quick start
38
74
 
39
75
  ```bash
40
- npx swagger-digest --url https://petstore.swagger.io/ --stdout > petstore.md
41
- npx swagger-digest --url https://api.example.com/openapi.json --stdout | pbcopy
76
+ # 1. Generate a digest from a public API (interactive prompts, saved to ~/Downloads)
77
+ npx swagger-digest --url https://petstore.swagger.io/
78
+
79
+ # 2. Or generate to stdout
80
+ npx swagger-digest --url https://petstore.swagger.io/ --stdout > petstore-digest.md
81
+
82
+ # 3. Register the MCP server (OpenCode example)
83
+ opencode mcp add swagger-digest -- npx -y swagger-digest-mcp
42
84
  ```
43
85
 
44
- `--stdout` prints the digest to standard output instead of saving a file — handy for piping into other tools or copying to the clipboard. Progress and warnings go to stderr, so the pipe receives only the Markdown. `--stdout` cannot be combined with `--out-dir` or `--filename`; the CLI exits with a clear error if you try.
86
+ Requires Node.js 20+. Run `npx swagger-digest --help` for all options.
45
87
 
46
- Run `npx swagger-digest --help` for all CLI options. The package requires Node.js 20 or newer.
88
+ ## Real-world example: building a subscriptions page
47
89
 
48
- ## MCP server
90
+ A React/TypeScript developer asks their AI coding agent:
49
91
 
50
- The package ships an [MCP](https://modelcontextprotocol.io) server exposing the same pipeline as a tool for AI agents, so they can fetch and digest API docs on their own instead of being handed a Markdown file.
92
+ > "Implement a subscription management page against our billing API — docs are at https://api.example.com/swagger-ui"
51
93
 
52
- ```bash
53
- npx swagger-digest-mcp
54
- ```
94
+ **Without swagger-digest**, the agent either guesses the API contract or you paste thousands of lines of raw JSON into the conversation.
55
95
 
56
- It speaks the stdio transport: the MCP client spawns the process and communicates over stdin/stdout. Register it in your client's configuration:
96
+ **With the MCP server**, the agent calls the tool itself:
57
97
 
58
98
  ```json
59
99
  {
60
- "mcp": {
61
- "swagger-digest": {
62
- "command": ["npx", "-y", "swagger-digest-mcp"]
63
- }
64
- }
100
+ "url": "https://api.example.com/swagger-ui",
101
+ "include_tags": ["Subscriptions"]
65
102
  }
66
103
  ```
67
104
 
68
- ### The `swagger_digest` tool
105
+ and receives only the relevant slice — in this illustrative excerpt of digest output:
106
+
107
+ ```markdown
108
+ #### `GET /v1/subscriptions/{id}`
69
109
 
70
- | Argument | Type | Description |
71
- | --------------- | ---------------- | -------------------------------------------------------------------------- |
72
- | `url` | string, required | Swagger UI page URL or direct OpenAPI spec URL |
73
- | `spec_url` | string, optional | Explicit spec URL; overrides discovery from the page |
74
- | `headers` | object, optional | Request headers (e.g. `Authorization`); sent only to the origin of `url` |
75
- | `include_tags` | string[] | Keep only operations with these tags |
76
- | `exclude_tags` | string[] | Drop operations with these tags |
77
- | `path_prefix` | string | Keep only paths starting with this prefix |
78
- | `max_tokens` | number | Trim the digest to an approximate token budget |
79
- | `write_to_file` | object, optional | `{ directory, filename }` — save the digest instead of returning it inline |
110
+ **Authentication:** Bearer
80
111
 
81
- By default the digest is returned directly in the tool result (so it lands in the agent's context), with a metadata footer: retrieval method, operation/model counts, and a token estimate.
112
+ **Parameters**
82
113
 
83
- ### Environment variables
114
+ - `id` (`path`, required) · `string` — Subscription identifier.
115
+ - `expand` (`query`, optional) · `string[]` — Fields to expand.
84
116
 
85
- - `SWAGGER_DIGEST_HEADER` — newline-separated `Name: value` pairs, merged with (and overridden by) the tool call's `headers` argument. Keeps secrets out of the conversation.
86
- - `SWAGGER_DIGEST_ALLOW_PRIVATE=1` — opt out of the private-network guard (see below).
117
+ **Responses**
87
118
 
88
- ### Security differences from the CLI
119
+ - **200 OK** — `Subscription` (application/json)
89
120
 
90
- - **SSRF guard on by default:** URLs resolving to loopback, private, or link-local (cloud metadata) addresses are rejected, because MCP clients supply URLs on the model's behalf. Opt out with `SWAGGER_DIGEST_ALLOW_PRIVATE=1` — for example to digest an internal corporate API — only if you trust the hosts involved.
91
- - **No headless-browser fallback:** the optional Playwright fallback is disabled in the MCP server.
92
- - Diagnostics go to stderr only; stdout carries protocol frames exclusively.
121
+ ## Data types
122
+
123
+ export interface Subscription {
124
+ id?: string;
125
+ status?: "active" | "canceled" | "past_due";
126
+ current_period_end?: number;
127
+ items?: SubscriptionItems;
128
+ [key: string]: unknown;
129
+ }
130
+ ```
131
+
132
+ From there the agent can: confirm the exact path, method and required parameters; read the `Subscription` schema and its referenced types; generate TypeScript types that match the contract; and implement the page without guessing. If the digest is too large, the agent re-calls with `path_prefix` or `max_tokens` to narrow it further.
93
133
 
94
134
  ## Benchmarks
95
135
 
96
136
  Ten public APIs, digest vs. raw spec JSON in an LLM context, ground truth from the parsed raw specs. Run in November 2026 against swagger-digest 0.2.0; token counts are heuristic estimates, not tokenizer-exact.
97
137
 
98
- **7/10 wins · 100% operation coverage on every digest produced · 1.6–3.7× lighter · 1 call vs 1–5**
138
+ **7/10 wins · 100% operation coverage on every digest produced · 1.6–3.7× lighter · 1 tool call vs 1–5 fetch/guess attempts**
99
139
 
100
140
  | Spec | Ops | Raw tokens | Digest tokens | Compression | Op coverage | Winner |
101
141
  | ------------------- | ----- | ---------------------- | ----------------------- | ----------- | ----------- | ------- |
@@ -112,6 +152,8 @@ Ten public APIs, digest vs. raw spec JSON in an LLM context, ground truth from t
112
152
 
113
153
  > For its target use case — understanding an API in an LLM context — the digest wins on the spec class that actually matters (up to ~600 operations): 100% endpoint coverage, never truncated, one call, and 1.6–3.7× lighter. The bigger the spec, the bigger the win.
114
154
 
155
+ **Why token efficiency matters:** an agent's context window is shared between your codebase, the conversation, and every tool result. A raw spec that consumes 500k tokens crowds out everything else; the same API as a 150k-token digest leaves room to work — and smaller digests mean faster, cheaper, more focused generations.
156
+
115
157
  \* Raw's three "wins" carry heavy caveats: the Petstore UI-page case needed brute-forced candidate URLs (the digest's discovery has since been fixed for that class of pages); GitHub's 3.55M-token raw spec would truncate in any model's context anyway; and the Kubernetes expansion is now prevented by the default token cap described below.
116
158
 
117
159
  ### Known failure modes (and their mitigations)
@@ -122,134 +164,174 @@ Ten public APIs, digest vs. raw spec JSON in an LLM context, ground truth from t
122
164
  | Mega-specs (Kubernetes-class, ≥1M tokens) | Digest can _expand_ past raw size — descriptions inlined per operation | **Default in 0.3.0**: automatic 250k-token cap (opt out with `--no-auto-trim` / `auto_trim: false`). Explicit `max_tokens` always wins |
123
165
  | Huge fetches (14 MB GitHub spec) | Fetch fails at the 10 MB response cap | Use a filtered direct spec URL |
124
166
 
125
- ### Filtering and token budget
167
+ Not every specification benefits: very large kitchen-sink specs are where compression can invert — handled by the default cap, but worth knowing.
126
168
 
127
- Large specs produce large digests. The CLI can remove whole operations before rendering and/or trim the remaining digest to an approximate token budget:
169
+ ## Feature overview
128
170
 
129
- ```bash
130
- # Keep only operations tagged "Payments", plus everything under /invoices
131
- npx swagger-digest --url https://api.example.com/openapi.json \
132
- --include-tag Payments --path-prefix /payments/ --out-dir ./docs
171
+ **Ingestion and discovery**
133
172
 
134
- # Drop a noisy tag
135
- npx swagger-digest --url https://api.example.com/openapi.json --exclude-tag Internal
173
+ - Fetches direct OpenAPI JSON/YAML URLs and Swagger UI pages (OpenAPI 2.0, 3.0, 3.1).
174
+ - Multi-strategy discovery: direct spec URL → page script configuration (`url:`, `urls:` dropdown, inline `spec:`, dynamically computed initializer URLs) → common spec paths (`/v3/api-docs`, `/swagger.json`, …) → optional headless-browser fallback.
175
+ - Multi-spec Swagger UI dropdowns: probes candidates in order, uses the first that parses, names the pick.
176
+ - External `$ref` bundling: references to other HTTP(S) documents are fetched and inlined under namespaced names (one level deep, at most 5 documents).
136
177
 
137
- # Fit the digest into a model's context window
138
- npx swagger-digest --url https://api.example.com/openapi.json --max-tokens 50000
139
- ```
178
+ **Markdown generation**
179
+
180
+ - Endpoint index plus per-tag operation details: auth requirements, path/query/header parameters, request bodies, success/error responses, examples.
181
+ - TypeScript-like type declarations for reachable schemas; `allOf`/`oneOf`/`anyOf`, enums, arrays, nullable fields, `additionalProperties` get compact representations.
182
+ - Unknown schema constructs render as `unknown` instead of failing the digest.
183
+ - Synthetic examples generated from schemas when none are declared, clearly marked as illustrative.
140
184
 
141
- How `--max-tokens` trims (in this fixed order, re-checked against the estimate after each step):
185
+ **MCP server**
142
186
 
143
- 1. Drop declared example payloads (synthetic examples are never emitted).
144
- 2. Shorten long descriptions.
145
- 3. Collapse parameter/response detail per tag — the **tag priority rule** is that the tag appearing **last in the document** is collapsed first (lowest priority), one tag per step.
146
- 4. Keep only the Quick index entries (no endpoint detail, no data types).
187
+ - Single `swagger_digest` tool over stdio; digest returned inline in the tool result with metadata (retrieval method, operation/model counts, token estimate), or written to a file.
188
+ - Same discovery and filtering pipeline as the CLI; optional Playwright fallback disabled.
189
+ - Private-network (SSRF) guard on by default, because URLs come from the model.
147
190
 
148
- A `## Trimmed` note at the end of the digest records exactly which steps were applied. After every step, schemas that became unreachable (referenced only by collapsed/removed operations) are dropped from the Data types section. The token estimator is a heuristic, **not** a real tokenizer — treat `--max-tokens` as a guide, not a guarantee. Unknown tag names passed to `--include-tag`/`--exclude-tag` produce a warning on stderr listing the tags that exist in the spec.
191
+ **Filtering and token management**
149
192
 
150
- **Default token cap:** without an explicit `--max-tokens`, digests are automatically trimmed to an estimated 250,000 tokens (a warning names the untrimmed estimate). Mega-specs can otherwise expand _past_ their raw size once descriptions are inlined per operation. Opt out with `--no-auto-trim`; an explicit `--max-tokens` always takes precedence. The MCP server behaves the same via its `auto_trim` argument (default true).
193
+ - Filter by tags (`include_tags`/`exclude_tags`) and path prefix, before rendering.
194
+ - `max_tokens` budget with deterministic trim order; `## Trimmed` note records exactly what was removed.
195
+ - Automatic 250,000-token default cap for unbounded digests.
151
196
 
152
- The same options are available programmatically via the third argument of `transformOpenApiToMarkdown(spec, sourceUrl, { includeTags, excludeTags, pathPrefix, maxTokens, onWarning })`.
197
+ **Authentication and remote access**
153
198
 
154
- ## What it extracts
199
+ - Origin-scoped custom headers and cookies: sent only to the origin you supply, never logged, never written to the digest.
200
+ - `SWAGGER_DIGEST_HEADER` environment variable for secrets outside the command line.
201
+ - Hardened fetching: response size cap (10 MB), redirect limit (5), timeouts, optional private-network blocking.
155
202
 
156
- The retrieval strategy deliberately avoids scraping rendered operation accordions or simulating UI clicks:
203
+ ## Configuration and advanced usage
157
204
 
158
- 1. Fetch and parse a direct `.json`, `.yaml`, or `.yml` URL.
159
- 2. Fetch the Swagger UI page and inspect script configuration for `url: "..."` or an inline `spec: { ... }` object, plus any spec-like URLs embedded in same-origin initializer scripts (covers initializers that compute the spec URL dynamically).
160
- 3. Try common spec endpoints on the same origin: `/v3/api-docs`, `/swagger/v1/swagger.json`, `/swagger.json`, `/openapi.json`, and `/api-docs`.
161
- 4. Optionally launch a headless Chromium browser and read the parsed specification from Swagger UI's in-memory state.
205
+ ### CLI reference
162
206
 
163
- The first three paths use ordinary HTTP requests. The browser fallback is optional so the normal install stays lightweight. To enable it for a difficult JavaScript-initialized Swagger UI deployment:
207
+ | Option | Description |
208
+ | ------------------------------ | --------------------------------------------------------- |
209
+ | `-u, --url <url>` | Swagger UI page URL or direct OpenAPI spec URL |
210
+ | `-d, --out-dir <directory>` | Destination directory for the digest file |
211
+ | `-f, --filename <filename>` | Output filename (defaults to `<api-title>-digest.md`) |
212
+ | `--stdout` | Print to stdout instead of writing a file |
213
+ | `--spec <url>` | Explicit spec URL; overrides all discovery |
214
+ | `--include-tag <tag>` | Keep only operations with this tag (repeatable) |
215
+ | `--exclude-tag <tag>` | Drop operations with this tag (repeatable) |
216
+ | `--path-prefix <prefix>` | Keep only operations whose path starts with this prefix |
217
+ | `--max-tokens <n>` | Trim the digest until the token estimate fits this budget |
218
+ | `--no-auto-trim` | Disable the automatic 250,000-token default cap |
219
+ | `--max-bytes <bytes>` | Maximum response size (default 10,000,000) |
220
+ | `-H, --header <"Name: value">` | Custom request header, repeatable; origin-scoped |
221
+ | `--cookie <string>` | Convenience for a Cookie header; origin-scoped |
222
+ | `--no-browser` | Disable the optional Playwright fallback |
223
+
224
+ ### MCP configuration
225
+
226
+ Register the server (stdio transport):
164
227
 
165
228
  ```bash
166
- npm install playwright
167
- npx playwright install chromium
229
+ # OpenCode
230
+ opencode mcp add swagger-digest -- npx -y swagger-digest-mcp
231
+ ```
232
+
233
+ ```json
234
+ // JSON-configured clients
235
+ {
236
+ "mcpServers": {
237
+ "swagger-digest": {
238
+ "command": ["npx", "-y", "swagger-digest-mcp"]
239
+ }
240
+ }
241
+ }
168
242
  ```
169
243
 
170
- Then run the CLI again. Use `--no-browser` to disable the fallback.
244
+ The `swagger_digest` tool:
171
245
 
172
- ### Authentication headers
246
+ | Argument | Type | Description |
247
+ | --------------- | ---------------- | ------------------------------------------------------------ |
248
+ | `url` | string, required | Swagger UI page URL or direct OpenAPI spec URL |
249
+ | `spec_url` | string | Explicit spec URL; overrides discovery |
250
+ | `headers` | object | Request headers; sent only to the origin of `url` |
251
+ | `include_tags` | string[] | Keep only operations with these tags |
252
+ | `exclude_tags` | string[] | Drop operations with these tags |
253
+ | `path_prefix` | string | Keep only paths starting with this prefix |
254
+ | `max_tokens` | number | Trim the digest to an approximate token budget |
255
+ | `auto_trim` | boolean | Default-cap digests without `max_tokens` (default true) |
256
+ | `write_to_file` | object | `{ directory, filename }` — save instead of returning inline |
173
257
 
174
- Some Swagger UI pages and spec URLs live behind authentication. Pass custom headers with `-H`/`--header` (repeatable) or `--cookie`:
258
+ MCP environment variables:
175
259
 
176
- ```bash
177
- npx swagger-digest \
178
- --url https://api.example.com/swagger-ui \
179
- --header "Authorization: Bearer eyJhbGciOi..." \
180
- --out-dir ./docs
181
- ```
260
+ - `SWAGGER_DIGEST_HEADER` — newline-separated `Name: value` pairs, merged with (and overridden by) the call's `headers`. Keeps secrets out of the conversation.
261
+ - `SWAGGER_DIGEST_ALLOW_PRIVATE=1` — opt out of the private-network guard; only for hosts you trust, e.g. internal corporate APIs.
262
+
263
+ MCP-specific behavior: URLs resolving to loopback/private/link-local addresses are rejected by default (MCP clients supply URLs on the model's behalf); diagnostics go to stderr, protocol frames to stdout.
182
264
 
183
- Or with a session cookie:
265
+ ### Authentication headers (CLI)
184
266
 
185
267
  ```bash
186
- npx swagger-digest --url https://api.example.com/swagger-ui --cookie "session=abc123"
187
- ```
268
+ npx swagger-digest --url https://api.example.com/swagger-ui \
269
+ --header "Authorization: Bearer eyJhbGciOi..." --out-dir ./docs
188
270
 
189
- You can also supply headers through the `SWAGGER_DIGEST_HEADER` environment variable, with one `Name: value` pair per line. `-H` flags override environment entries on conflicting header names:
271
+ # or a session cookie
272
+ npx swagger-digest --url https://api.example.com/swagger-ui --cookie "session=abc123"
190
273
 
191
- ```bash
274
+ # or the environment variable (-H flags win on conflicts)
192
275
  export SWAGGER_DIGEST_HEADER=$'Authorization: Bearer eyJ…\nX-Tenant: acme'
193
276
  npx swagger-digest --url https://api.example.com/swagger-ui
194
277
  ```
195
278
 
196
- **Scoping and safety:**
197
-
198
- - Custom headers are sent **only** to the origin of the URL you supply. If the discovered spec, a redirect, or a Swagger UI script lives on a different origin, that request is made without your headers and a warning naming the other origin is printed to stderr.
199
- - In the headless-browser fallback, headers are injected per-request (only for your origin) — they never reach CDNs or third-party scripts loaded by the page.
200
- - Header values are never logged, written to the digest, or included in error messages.
201
- - ⚠️ **Shell history:** secrets passed with `-H` can end up in your shell history file (`~/.bash_history`, `~/.zsh_history`). Prefer the `SWAGGER_DIGEST_HEADER` environment variable, or prefix the command with a space if your shell is configured to ignore leading-space entries.
279
+ **Scoping and safety:** custom headers are sent **only** to the origin of the URL you supply; cross-origin requests (redirects, discovered specs on other origins) proceed without them and produce a warning naming the other origin. In the headless-browser fallback, headers are injected per-request for your origin only — they never reach CDNs or third-party scripts. Header values are never logged, written to the digest, or included in error messages. Secrets passed with `-H` can end up in shell history — prefer the environment variable.
202
280
 
203
- ### Troubleshooting access through a VPN
281
+ ### Programmatic API
204
282
 
205
- The ordinary HTTP fetches use Node.js networking; they do not automatically reuse a browser's VPN extension, cookies, or signed-in session. If the Swagger page works in Chrome but not in the CLI, first test the same URL from the terminal:
283
+ ```ts
284
+ import { fetchOpenApiSpec, transformOpenApiToMarkdown, estimateTokens } from 'swagger-digest';
206
285
 
207
- ```bash
208
- curl -v -L 'https://your-host.example/path/to/swagger-ui' -o /tmp/swagger-page.html
286
+ const { spec, sourceUrl } = await fetchOpenApiSpec('https://api.example.com/openapi.json', {
287
+ blockPrivateNetworks: true, // recommended when URLs come from untrusted input
288
+ headers: { Authorization: 'Bearer …' },
289
+ });
290
+ const markdown = transformOpenApiToMarkdown(spec, sourceUrl, {
291
+ includeTags: ['Subscriptions'],
292
+ maxTokens: 50_000,
293
+ onWarning: console.warn,
294
+ });
295
+ console.log(estimateTokens(markdown).models.claude.tokens);
209
296
  ```
210
297
 
211
- If `curl` cannot connect or receives `401`/`403`, connect a system-level VPN that also routes terminal applications, or configure the relevant corporate proxy/authentication for the terminal. Installing Playwright alone does not make a browser-only VPN extension available to its separate Chromium instance. If terminal access succeeds, the extractor now also reads same-origin Swagger UI initializer/config scripts and tries spec paths relative to the supplied page's context path.
298
+ `transformOpenApiToMarkdown` options: `includeTags`, `excludeTags`, `pathPrefix`, `maxTokens`, `autoTrim` (default true), `onWarning`. `fetchOpenApiSpec` options include `specUrl`, `headers`, `maxBytes`, `blockPrivateNetworks`, `useBrowserFallback`.
212
299
 
213
- ## Security notes
300
+ ### Trimming order
214
301
 
215
- **What the tool fetches:** the URL you supply (a Swagger UI page or direct OpenAPI JSON/YAML URL), same-origin Swagger UI initializer/config scripts, the spec URL referenced by that page, and a short list of common spec paths on the same origin (`/v3/api-docs`, `/swagger.json`, …). The optional Playwright fallback loads the same page in a headless browser.
302
+ When a budget applies (explicit `--max_tokens` or the default cap), the digest is trimmed in this fixed order, re-checked after each step: 1) drop declared example payloads → 2) shorten long descriptions → 3) collapse parameter/response detail per tag (the tag appearing last in the document is collapsed first) → 4) keep only the Quick index. A `## Trimmed` note records exactly which steps applied; schemas referenced only by removed operations are dropped. The estimator is a heuristic — treat budgets as a guide, not a guarantee.
216
303
 
217
- **What it does not forward:** cookies or Authorization headers are never sent automatically. Custom headers you pass with `--header`/`--cookie` (see below) are sent **only** to the origin of the URL you supplied. If the page redirects to another origin, or the discovered spec lives on a different origin, those requests are made without your headers and a warning naming the other origin is printed to stderr. Response bodies are capped (10 MB by default, configurable with `--max-bytes`), and redirects are followed at most 5 times.
304
+ ## Supported specifications and limitations
218
305
 
219
- **Embedding in a server:** if you call `fetchOpenApiSpec` from a server that accepts user-supplied URLs, enable `blockPrivateNetworks: true` in the fetch options. Without it, a caller can point the tool at `http://169.254.169.254/…` or other internal addresses from your infrastructure (SSRF). The check rejects loopback, link-local (including the cloud-metadata address 169.254.169.254), private, and unique-local addresses, and applies after redirects. Note the documented DNS-rebinding limitation in the code — for hostile multi-tenant input, use a dedicated egress proxy.
306
+ **Supported:** OpenAPI 2.0, 3.0 and 3.1, in JSON or YAML, fetched over HTTP(S) from direct spec URLs or discovered through Swagger UI pages.
220
307
 
221
- ## Markdown format
308
+ **Limitations:**
222
309
 
223
- The generated document includes:
224
-
225
- - API title, OpenAPI/Swagger version, API version, base URL(s), and authentication schemes.
226
- - A compact endpoint index (`Method`, `Path`, `Summary`, and `Tags`).
227
- - Endpoint details grouped by tag, including auth requirements, path/query/header parameters, request bodies, success/error responses, and JSON example values.
228
- - TypeScript-like interface/type declarations for schemas reachable from the documented operations, with local `$ref` dependencies expanded as named type references rather than copied JSON Schema trees.
310
+ - The tool downloads specs exposed by the supplied server. It does not bypass authentication, browser access controls, or network restrictions. Ordinary HTTP fetches use Node.js networking — they do not reuse a browser's VPN extension, cookies, or signed-in session. If a page works in Chrome but not here, test `curl -v -L '<url>'` from the terminal; if that fails too, route the terminal through your VPN/proxy.
311
+ - External `$ref` documents are bundled best-effort (one level deep, at most 5 documents); unreachable references degrade to named type references and the digest still succeeds.
312
+ - Very large specs: fetches are capped at 10 MB by default (`--max-bytes`), and digests are capped at ~250k estimated tokens by default (see trimming above).
313
+ - A generated digest is a compact developer reference, not a replacement for the source OpenAPI contract.
229
314
 
230
- OpenAPI 2.0, 3.0, and 3.1 documents are handled through format normalization in the transformer. Schema combinations (`allOf`, `oneOf`, `anyOf`), enums, arrays, nullable fields, and `additionalProperties` receive compact TypeScript-like representations. Unknown or unsupported schema constructs are represented as `unknown` rather than causing the entire digest to fail.
315
+ **Security notes:** the tool fetches the URL you supply, same-origin Swagger UI initializer/config scripts, the spec URL referenced by that page, and a short list of common spec paths on the same origin. Cookies and Authorization headers are never sent automatically. If you call `fetchOpenApiSpec` from a server that accepts user-supplied URLs, enable `blockPrivateNetworks: true` — without it a caller can point the tool at cloud-metadata or other internal addresses (SSRF); the check rejects loopback, link-local, private, and unique-local addresses after redirects, but has a documented DNS-rebinding limitation — for hostile multi-tenant input, use a dedicated egress proxy. The MCP server enables this guard by default.
231
316
 
232
- ## Development
317
+ ## Development and contribution
233
318
 
234
319
  ```bash
235
320
  npm install
236
321
  npm run build
237
- npm test
322
+ npm test # typecheck + test suite (runs offline against fixtures)
238
323
  npm run lint
239
- npm link
240
- swagger-digest
324
+ npm link # then: swagger-digest / swagger-digest-mcp
241
325
  ```
242
326
 
243
- Test against the public Petstore UI:
327
+ Manual check against a live API:
244
328
 
245
329
  ```bash
246
330
  npm run build
247
331
  node dist/cli.js --url https://petstore.swagger.io/ --out-dir ./tmp --filename petstore-digest.md
248
332
  ```
249
333
 
250
- The test suite runs locally against fixtures and does not require a network connection. Network-based checks such as the Petstore example depend on that service being reachable from your environment.
251
-
252
- ## Project layout
334
+ Project layout:
253
335
 
254
336
  ```text
255
337
  src/
@@ -264,13 +346,6 @@ src/
264
346
  digest.ts Tool handler: fetch → transform → tool result
265
347
  ```
266
348
 
267
- ## Limitations
268
-
269
- - The tool downloads specs exposed by the supplied server. It does not bypass authentication, browser access controls, or network restrictions.
270
- - External `$ref` documents are bundled best-effort: references to other HTTP(S) documents are fetched (one level deep, at most 5 documents) and their schemas inlined under namespaced names such as `meta_SharedMeta`. If an external document is unreachable or unparsable, the reference degrades to a named type reference and the digest still succeeds.
271
- - Declared request/response examples are preserved. When an example is absent but a schema exists, a representative JSON example is generated from that schema and clearly marked as illustrative rather than spec-authored. The transformer includes the primary success response and applicable common error responses to balance useful payload context against digest size.
272
- - A generated digest is a compact developer reference, not a replacement for the source OpenAPI contract.
273
-
274
349
  ## License
275
350
 
276
351
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-digest",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Turn Swagger UI pages and OpenAPI specs into compact, AI-friendly Markdown API digests.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",