README
¶
swagger-mcp
A local index for OpenAPI/Swagger specs plus an MCP query layer on top of it.
Index a spec once into ./swa-mcp/swa-mcp.db inside your project, and every MCP
tool answers from that SQLite file — no server, no hosting, no auth, no key. The
specs never leave your machine.
Why?
Large APIs have hundreds of endpoints. To build one frontend module you need request/response shapes, required fields, auth schemes and error codes that are scattered across a giant Swagger UI — and a naive LLM reading the whole document burns tens of thousands of tokens on things it never uses.
swagger-mcp is to OpenAPI what a code graph is to source: one explicit indexing
step, then cheap structured queries.
- Local by default. One SQLite store per project, written by the CLI. Queries read the store and never touch the network.
- No infrastructure. A static binary per platform; nothing to deploy, no token to rotate, nothing to trust with your private spec.
- Token-optimized by default. Compact TOON output, auto-limiting and guided tool descriptions keep explorations small instead of dumping whole documents.
- Multi-spec. Index several specs side by side, address them by name, sweep all of them, or diff two of them.
Quickstart
1. Install
# Go 1.25+
go install github.com/hnkatze/swagger-extractor-mcp/cmd/swagger-mcp@latest
Or take the release asset for your platform from GitHub Releases:
# Linux / macOS — archives are swagger-mcp_<version>_<os>_<arch>.tar.gz
tar -xzf swagger-mcp_<version>_linux_amd64.tar.gz
sudo mv swagger-mcp /usr/local/bin/
# Windows — extract the .zip and put swagger-mcp.exe on your PATH
From source:
git clone https://github.com/hnkatze/swagger-extractor-mcp.git
cd swagger-extractor-mcp
go build -o swagger-mcp ./cmd/swagger-mcp/
2. Index your spec(s)
Run this in the project that will query the API:
cd ~/proyectos/my-app
swagger-mcp index https://api.example.com/openapi.json
# indexed openapi: Example API 1.2.0 (43 endpoints, 17 schemas)
# store: /home/you/proyectos/my-app/swa-mcp/swa-mcp.db
Sources can be URLs or local files, and you can pass several at once:
swagger-mcp index ./openapi.yaml https://api.example.com/openapi.json
The command creates ./swa-mcp/swa-mcp.db if it does not exist. swagger-mcp help lists the subcommands.
Indexing from an agent without a shell
A client that cannot run commands does the same thing with the index_spec
tool, and gets the same size cap, validation rules, refusals and store:
index_spec(source="./openapi.yaml") → index a local file
index_spec(source="https://api.example.com/api.json") → index a URL
index_spec(source="./openapi.yaml", name="billing-v2") → choose the store name
index_spec(source="./openapi.yaml", refresh=true) → rewrite the stored copy
It answers with the name, title, version, endpoint/schema counts and the store
path. index_spec is the only tool that reads a source or touches the network;
every other tool is a read-only query over the store, so an agent can index and
then query in one session without a shell. unindex remains CLI-only.
3. Register the MCP server
The server reads the store it finds from its working directory, so register it per project (cwd = project root) with no arguments.
Claude Code — .mcp.json in the project root (or ~/.claude/.mcp.json):
{
"mcpServers": {
"swagger-mcp": {
"type": "stdio",
"command": "swagger-mcp",
"args": []
}
}
}
Claude Desktop — claude_desktop_config.json:
{
"mcpServers": {
"swagger-mcp": {
"command": "swagger-mcp",
"args": []
}
}
}
If the binary is not on your PATH, use its absolute path as command. On
startup the server logs the store it resolved to stderr, so a wrong working
directory is visible instead of silent:
[swagger-mcp] 2026/02/10 09:11:02 store: /home/you/proyectos/my-app/swa-mcp/swa-mcp.db
4. The first three calls
| Call | Answers |
|---|---|
list_specs |
What is indexed: name, title, version, source, fetch time, endpoint/schema counts. |
list_endpoints |
The endpoints, one line each — GET /pets — List all pets [pets]. Filter by tag, method, path_pattern, deprecated. |
get_endpoint |
One endpoint in full: parameters, request body, responses, resolved schemas. |
A minimal, cheap sequence:
list_specs() → the indexed specs and their counts
analyze_tags() → tags with endpoint counts; pick one
list_endpoints(tag="pets") → one line per endpoint in that tag
get_endpoint(method="GET", path="/pets/{petId}") → params, body, responses
The store
my-app/
└── swa-mcp/
└── swa-mcp.db # SQLite: the whole index for this project
- One file per project. The store belongs to the project that queries it, and
internal/storekeeps a single writer per file, so it is not a shared multi-tenant database. - Created by the indexer, never by hand. A query that finds no store fails
with
NO_STOREinstead of creating one, and the query layer opens the database read-only: it never creates the file, the schema or a migration. The two indexers —swagger-mcp indexand theindex_spectool — create it. - Discovery is SQL, detail is the document.
list_specs,list_endpoints,list_schemas,analyze_tagsandsearch_specanswer from the store's catalog tables, so listing or searching a large spec never decompresses or re-parses it. The detail tools (get_endpoint,get_schema,generate_types,diff_endpoints,security_schemes,validate_spec) load that one stored document, which is what they are for. - A broken document degrades the detail tools only. The catalog is written at index time, so a stored document that cannot be gzip-decoded or parsed still lists, searches and analyzes correctly; only the tools that need the document fail, with an error naming the refresh command.
- The store is versioned, and only the indexer migrates it.
PRAGMA user_versionis2. Opening an older store for a write (anyswagger-mcp indexrun) upgrades it in place: the catalog is re-derived from the documents it already holds, so no manual reindex is needed and nothing is lost. A query never migrates — a v1 store reportsSTORE_ERRORnaming theswagger-mcp indexcommand to run. - Discovery walks up. Starting from the working directory, the server and the
indexer use the nearest
swa-mcp/directory it finds above it, falling back to<cwd>/swa-mcp. SWA_MCP_DIRoverrides discovery with an explicit store directory (useful for a shared or out-of-tree index).- Gitignored.
swa-mcp/is in this repo's.gitignore; add the same entry to yours. The index is generated state, not source.
Reindexing
swagger-mcp index https://api.example.com/openapi.json # idempotent
swagger-mcp index --refresh https://api.example.com/openapi.json # force a rewrite
swagger-mcp index --name petstore-v3 ./openapi.json # store it under a name you choose
-
Reindexing the same source updates its row in place — it never duplicates a spec, with or without
--refresh. -
A name addresses exactly one source. Indexing a source whose name is derived to one that already belongs to a different source fails instead of replacing it:
$ swagger-mcp index v1/api.json v2/api.json indexed api: Billing API 1.0.0 (5 endpoints, 4 schemas) v2/api.json: cannot index v2/api.json as "api": that name already belongs to v1/api.json; pass --name to index this spec under a different nameNothing is written for the refused source, and the first spec is untouched.
-
Without
--refresh, an unchanged document is reported and not rewritten:unchanged openapi: Example API 1.2.0 (content identical, use --refresh to rewrite) -
--refreshre-reads the source and rewrites the stored copy (and itsfetched_at), even when the content is identical. -
--namesets the addressable name instead of the derived one. It accepts a single source only. The default name is the base name of the file or URL path without.json/.yaml/.yml(a URL with no path falls back to the hostname). -
A source larger than the size limit is refused, never truncated: a cut-off document could otherwise parse into a different API.
-
spec_statusprints the refresh command that keeps the stored name:swagger-mcp index --refresh --name billing https://api.example.com/v1/api.jsonwhenever the name was not derived from the source, so copying it can neither rename the spec nor collide with another source.
Indexing validates first: a spec that fails validation is rejected and nothing is written. It is validated with the same relaxed rules the query layer uses, so a spec you can index is a spec you can query.
Removing an indexed spec
A moved or renamed source leaves a row nobody can use. unindex deletes exactly
one spec — its document and every catalog row derived from it — in one
transaction:
swagger-mcp unindex petstore-v2
# removed petstore-v2: Petstore 1.0.0 (43 endpoints, 17 schemas)
# store: /home/you/proyectos/my-app/swa-mcp/swa-mcp.db
- The report names what went away: name, title, version, endpoint and schema counts.
- An unknown name is an error that lists the indexed names, exits non-zero, and
changes nothing. So does a project with no store:
unindexnever creates one. - Removing the last spec does not delete the store: the tools keep opening it and
report
NO_STORE(nothing is indexed) with the index command to run.
Tools
All answers come from the store. index_spec is the explicit write path — the
tool equivalent of swagger-mcp index — and the only tool that reads a source or
uses the network; there is no fetch-at-query tool and no implicit refresh. The
discovery tools (list_specs, list_endpoints,
list_schemas, analyze_tags, search_spec) are pure SQL over the catalog and
never load a document; the detail tools load the one stored document they need.
| Tool | What it answers | Parameters |
|---|---|---|
index_spec |
Index a file or URL with the same read, size cap, validation and refusals as swagger-mcp index. Answers with name, title, version, counts and store path. Writes the store. |
source*, name, refresh |
list_specs |
Every indexed spec with title, version, source, fetch time and counts. Start here when a call says several specs are indexed. | format |
list_endpoints |
Endpoints, one line each with method, path, summary and tags. | spec, tag, method, path_pattern, deprecated, all_specs, limit, offset, format |
get_endpoint |
One endpoint in full: params, request body, responses, resolved schemas. Shared schemas are shown once then referenced as $ref(Name). |
spec, method, path, resolve_depth, format |
get_schema |
A named schema/model with field types and descriptions. Use it to expand a $ref(Name) from get_endpoint. |
spec, name*, resolve_depth, format |
search_spec |
Keyword search across paths, summaries, descriptions, operation IDs and body field names, ranked by relevance. | spec, query*, all_specs, limit, offset, format |
list_schemas |
Component schemas with field count and how many endpoints use each. | spec, filter, all_specs, limit, offset, format |
security_schemes |
Auth scheme definitions (http/bearer/apiKey/OAuth2/OIDC) with how many endpoints use each. A requirement list is an OR of alternatives. | spec, format |
validate_spec |
VALID/INVALID plus the spec's real violations, reported exactly as the document wrote them. | spec, format |
analyze_tags |
Tags with endpoint counts and method breakdown — the map of the API. | spec, all_specs, format |
diff_endpoints |
Added, removed and changed endpoints between two indexed specs. | spec_old, spec_new, path, method, format |
spec_status |
Source, etag, fetch time, age, a stale verdict and the exact refresh command. Read-only; always JSON. | spec |
generate_types |
Copy-paste-ready TypeScript interfaces or Go structs from a tag, a named schema, or an endpoint. | spec, tag, method, path, schema, language, format |
usage_guide |
This server's workflow, token rules, output notation and example sequences, for an LLM that lacks setup context. | — |
* = required.
Addressing a spec
spec selects one indexed spec by the name the indexer stored.
- One spec indexed — omit
spec; that spec is used. - Several indexed, no
spec— the call fails withSPEC_AMBIGUOUSand the message lists the names. It never guesses. - Unknown name — the call fails with
SPEC_NOT_FOUND, and the same list of indexed names is returned so the caller can correct itself. - Nothing indexed — the call fails with
NO_STOREand namesswagger-mcp index. - No query accepts a
url. Addressing is by name, never by URL, and a call that still sends one fails withINVALID_QUERYexplaining that specs are indexed withswagger-mcp indexand queried withspec=<name>.index_specis the one exception to the naming: it takes the source itself assource.
Sweeping every spec
list_endpoints, search_spec, list_schemas and analyze_tags accept
all_specs=true, which reads every indexed spec and prefixes each result line
with the spec it came from (petstore: GET /pets — ...). It is the most
expensive call shape; prefer spec=<name> unless you really want the whole store.
Parameter details
format—toon(default, compact) orjson.spec_statusis always JSON andusage_guidetakes no arguments.limit/offset— onlist_endpoints,search_specandlist_schemas.limitdefaults to 50 and0means unlimited;offsetdefaults to 0. The result header shows what you got and the next offset, so page instead of raising the limit.resolve_depth— onget_endpointandget_schema: schema nesting depth0–10, default3.0= names only (cheapest); raise it for deep models.deprecated— onlist_endpoints:true= only deprecated,false= only live endpoints, omit for both.
Staleness
spec_status reports what the store knows about one spec — no network access:
{
"name": "billing",
"source": "https://api.example.com/v1/api.json",
"etag": "\"abc123\"",
"fetched_at": "2026-02-10T09:11:02Z",
"age": "9d3h",
"age_seconds": 795420,
"stale": true,
"stale_after_days": 7,
"refresh": "swagger-mcp index --refresh --name billing https://api.example.com/v1/api.json",
"endpoint_count": 43,
"schema_count": 17,
"store": "/home/you/proyectos/my-app/swa-mcp/swa-mcp.db"
}
Keys and the stale_after_days value are exact; the values are an illustration.
A stored copy older than 7 days is reported as stale: true. Nothing is
refreshed automatically: spec_status is read-only, no query fetches anything,
and the refresh field carries the exact command a human runs — including
--name <stored name> when the name was chosen rather than derived, so the
command refreshes that spec instead of renaming it. Reindexing is always
explicit; an agent without a shell uses
index_spec(source=<source>, refresh=true) instead of the CLI command.
Errors you may hit
Failures come back as tool errors prefixed with a stable code.
| Code | Meaning | What to do |
|---|---|---|
NO_STORE |
No store in this project, or an empty one — the message names the path it looked for. | Run swagger-mcp index <url|file> in this project; check the client's working directory (the server logs the store it resolved). |
INVALID_QUERY |
An argument was rejected: a tool call that still sends url, or search_spec without a query. |
Drop url (specs are indexed with swagger-mcp index, addressed with spec=<name>) and pass the required arguments. |
SPEC_AMBIGUOUS |
Several specs are indexed and no spec was passed. The message lists them. |
Pass spec=<name> (names come from list_specs), or all_specs=true to sweep. |
SPEC_NOT_FOUND |
The requested name is not indexed. The message lists the indexed names. | Retry with one of the listed names, or index the spec you meant. |
DIFF_NEEDS_SPECS |
Fewer than two specs are indexed, so there is nothing to compare. | swagger-mcp index the second spec (or the older version), then diff by name. |
STORE_ERROR |
The store exists but cannot be opened or read — permissions, a corrupt or truncated file, a store written by a newer schema version, or a v1 store a query refuses to migrate. A file that is not a usable database is reported with the store path, the driver's reason and the action: move or delete the swa-mcp/ directory and reindex. |
Fix access to swa-mcp/, upgrade swagger-mcp if the store reports a newer schema version, or run swagger-mcp index <url|file> once to upgrade a v1 store. For a corrupt or truncated file, do what the message says: move or delete swa-mcp/ and reindex with swagger-mcp index <url|file>. A query never rewrites or deletes the file, so it stays broken until you act. |
INDEX_FAILED |
index_spec refused a source: it cannot be read, it is over the size cap, it is not a valid OpenAPI document, or its name already belongs to a different source. The message names the source, the cap, the owner or the violation. Nothing was written. |
Fix the source; pass name=<another name> for a collision; index a smaller document for the cap. swagger-mcp index reports the same refusal with the same message. |
Migration from v0.7.0
v0.7.0 fetched specs on demand: the fetch_spec tool downloaded a URL and made it
the session default, every read tool accepted a url parameter, and
SWAGGER_MCP_DEFAULT_URL seeded that default at startup.
fetch_spec, refresh_spec, the url parameter and that environment variable
are all gone. Indexing is an explicit command,
and queries are addressed by the name of an indexed spec:
# before: the LLM fetched a URL to start a session
# now:
swagger-mcp index https://api.example.com/openapi.json
Then drop any url arguments and the old default-URL variable from your client
config. Pass spec=<name> only when the project indexes more than one spec.
Output formats
TOON (default)
Token-Optimized Object Notation — compact and readable, and cheaper per response than indented JSON.
# showing 50 of 790 — use tag/method/path_pattern filters to narrow results
GET /pets — List all pets [pets]
POST /pets — Create a pet [pets]
GET /pets/{petId} — Get a pet by ID [pets]
Detailed endpoint view:
GET /pets/{petId} — Get a pet by ID [pets]
parameters:
- petId (path, required): integer(int64) — The pet ID
responses:
200: Successful response
application/json:
id*: integer(int64)
name*: string
tag: string
404: Pet not found
Conventions:
*marks required fields- Types use compact notation:
string(email),[]Pet,enum(active, inactive) - List views strip descriptions — the summary is enough for browsing
- A shared schema is shown once, then referenced as
$ref(Name) - Truncated lists start with a metadata header and the next offset
- Diffs use
+(added),-(removed),~(changed)
JSON
Standard indented JSON for programmatic consumption (format=json). List answers
carry truncation metadata:
{
"total": 790,
"showing": 50,
"offset": 0,
"truncated": true,
"endpoints": [...]
}
Environment variables
| Variable | Default | Description |
|---|---|---|
SWA_MCP_DIR |
(empty) | Explicit store directory, overriding walk-up discovery. |
SWAGGER_MCP_DEFAULT_FORMAT |
toon |
Default output format (toon or json). |
SWAGGER_MCP_DEFAULT_LIMIT |
50 |
Default limit for list/search tools (0 = unlimited). |
SWAGGER_MCP_FETCH_TIMEOUT |
120s |
Timeout for a URL read during swagger-mcp index or index_spec (Go duration, must be > 0). |
LLM context guide
Add a snippet like this to CLAUDE.md, .cursorrules, or your system prompt so
the model uses the tools efficiently:
## API exploration (swagger-mcp)
Answers come from a local index. If a call reports no store, index the spec: run
`swagger-mcp index <url|file>...` when you have a shell, or call
`index_spec(source=<path-or-url>)` when you do not (you are in that project, so
the store lands where the tools look). Only ask the human when neither works.
### Workflow
1. `list_specs` — what is indexed, its source and how old it is
2. `analyze_tags` — map the API before listing anything
3. `list_endpoints` with tag/method/path filters
4. `get_endpoint` for the one endpoint you need in full
5. `generate_types` to emit TypeScript or Go instead of translating schemas
6. `get_schema` only to expand a `$ref(Name)` whose fields you actually need
### Rules
- Omit `spec` when the project indexes one spec; pass `spec=<name>` only when several are indexed
- Filter before listing: `analyze_tags` → filtered `list_endpoints` beats an unfiltered dump
- Page with `limit`/`offset` instead of raising the limit
- Default output is TOON; use `format=json` only when you need structured JSON
- Schemas resolve 3 levels deep by default; lower `resolve_depth` (or 0) to save tokens
- Refreshing is explicit: `spec_status` shows age and a `stale` verdict, and its
`refresh` field holds the command a human must run — never assume it happened
### Anti-patterns
- Don't call `list_endpoints` unfiltered on a large API
- Don't call `get_endpoint` for every endpoint — narrow down first
- Don't call `all_specs=true` unless you really need the whole store
- Don't expand every `$ref(Name)` — only the ones whose fields you need
For Cursor, put the same content in .cursorrules. The usage_guide tool returns
this guidance on demand, so a model with no setup context can self-orient.
Architecture
cmd/swagger-mcp/main.go Entry point — subcommands (index, unindex, help), else MCP over stdio
cmd/swagger-mcp/index.go The indexer: read, validate, write the store
cmd/swagger-mcp/unindex.go Remove one indexed spec and its catalog rows
internal/
├── config/config.go Defaults and env overrides
├── store/ SQLite index: layout/discovery, schema, records
├── specsource/ The indexer's read path: file or URL, size cap, parse+validate
├── tools/ MCP tool definitions, handlers, spec addressing
├── loader/ kin-openapi parsing, normalization, validation
├── analyzer/ List, search, tag analysis, spec diffing
├── extractor/ Endpoint detail, schema extraction, $ref resolution
├── generator/ TypeScript and Go type emitters
├── formatter/ TOON and JSON output
└── types/types.go Shared types and error codes
Design decisions
- The store is the product's data model. One SQLite file per project holds the
raw documents plus a normalized catalog for discovery:
specs,endpoints,endpoint_tags,endpoint_fields(the parameter, request-body and response-body namessearch_specmatches),schemas,tagsandtag_methods. Discovery is a query over those tables and never reads a document; the detail queries load that one stored document and reuse the existing extractor instead of reimplementing$refresolution in SQL. This is also why a corrupt document cannot break discovery. The catalog is derived with the analyzer's own functions, so the SQL answer and the document answer are the same answer, not two implementations of it. - Writes are explicit, reads never touch the network. Two entry points write
—
swagger-mcp indexand theindex_spectool — plusswagger-mcp unindexfor removal. Both indexes read throughinternal/specsource, so the CLI and the tool cannot disagree about the size cap, the resolved source or the validation rules; every query is a store read. Dropping URL addressing from the query path removes the whole class of query-time fetch problems. - Token-first defaults. TOON, auto-limits and per-endpoint
$refdedup keep real-world explorations small. - Description-forward schemas. Field descriptions are inlined while noisy
example/defaultpayloads are dropped: signal, not bulk. - Guided tool descriptions. Descriptions steer a model toward filter-first workflows instead of full dumps.
- stdio transport. Universal client compatibility; the same binary is also the indexer.
Development
go run ./cmd/swagger-mcp/ # serve MCP over stdio
go run ./cmd/swagger-mcp/ index ./testdata/petstore.json # writes ./swa-mcp/swa-mcp.db
go build -o swagger-mcp ./cmd/swagger-mcp/ # build
go test ./... # tests
go test -short ./... # unit tests only
go test -cover ./... # with coverage
Tech stack
- Go 1.25+
- mcp-go — MCP server SDK
- kin-openapi — OpenAPI parsing and validation
- modernc.org/sqlite — pure-Go SQLite (builds with
CGO_ENABLED=0)
License
MIT
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
swagger-mcp
command
|
|
|
internal
|
|
|
specsource
Package specsource is the indexer's read path: it turns a source (a local file or a URL) into the bytes the store will hold, and into the validated document those bytes must describe.
|
Package specsource is the indexer's read path: it turns a source (a local file or a URL) into the bytes the store will hold, and into the validated document those bytes must describe. |
|
store
Package store owns the local index: a SQLite database inside a swa-mcp/ directory that belongs to the consuming project (S2).
|
Package store owns the local index: a SQLite database inside a swa-mcp/ directory that belongs to the consuming project (S2). |