Repository navigation
Conversation
Add `elastic docs api search`, `get` and `list` to look up operations in the Elastic REST API reference. They call the docs API's /_api/v1/api-reference endpoints, which serve the same lookups as the docs MCP server's API tools. Co-Authored-By: Claude Opus 5.5 <[email protected]>
🔍 Preview links for changed docs⏳ Building and deploying preview... View progress This comment will be updated with preview links when the build is complete. |
There was a problem hiding this comment.
🟡 Changes recommended
Command validation allows missing search queries and missing or conflicting operation selectors, causing backend rejections in normal runs and false success with --dry-run.
1 open issue
What changed in this PR
Adds elastic docs api commands for looking up Elastic REST API reference material directly, complementing the existing general documentation commands. Live use remains dependent on deployment of the linked docs-builder backend changes.
Changes:
- Adds
search,get, andlistcommands with schema-based inputs and text or JSON output. - Adds an API-reference client with encoded query parameters, redirect protection, and server error handling.
- Adds unit tests and updates the generated CLI schema.
| File | Description |
|---|---|
test/docs/api.test.ts |
Tests requests, validation, formatting, and error handling. |
src/docs/register.ts |
Registers the new docs api command group. |
src/docs/client.ts |
Adds API-reference HTTP requests and error parsing. |
src/docs/api.ts |
Defines the commands, input schemas, and text formatters. |
docs/cli/schema.json |
Documents the new commands and parameters in the generated schema. |
Review effort: Balanced
Patching process.stdout.write directly also swallowed the Node 22 test runner's serialized events, so the --json test parsed binary data. captureDuring forwards non-string chunks to the real stream. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The command reference turns 'Default:' in a description into a default value. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The docs API now serves the lookups as one resource: the collection filters by q or endpoint, one operation reads by id, and groups and apis are their own routes. `get` with an endpoint lists the matching operations, then reads the best one by id in its own API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each command is now one request. `get` reads /api-operations/{api}:{id}
with --api, and otherwise asks the collection for the full view of every
match. `list <api>` counts its groups, and `list <api> --group` pages
through the group's operations with --page and --size.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The docs API answers every error as application/problem+json. The error message is now the problem's detail, or its title when there is none, and requests accept application/problem+json. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A positional argument reached the handler only after schema validation, so the schema could not require it: `search` without a query and `get` with neither or both selectors passed validation, failed on the server, and reported success under --dry-run. positionalArg.toInput now maps the value into the input before validation, and the docs api schemas require a non-blank query, exactly one of endpoint or operation_id, and api for version, group, page and size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The docs API dropped lookups across APIs: most operation ids and many routes
exist in several APIs, so such a lookup could only guess. `search` and `get`
now require --api and call /apis/{api}/operations, and `get <id>` reads
/apis/{api}/operations/{operationId}.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
elastic/docs-infra#523 removed the CDN's cookie gate. elastic#829 drops it from search and ask on main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rror elastic docs api list padded keys to 24 columns, so elasticsearch-serverless ran into its title. Over HTTP/2 a reply has no status text, and www.elastic.co's CDN answers an API 404 with its HTML page, so the error read "request failed"; it now names the status. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| name: 'list', | ||
| description: 'List the Elastic REST APIs, the groups of one API, or the operations of one group', | ||
| properties: { | ||
| api: { type: 'string', description: "API key, e.g. 'elasticsearch'. Omit to list every API." }, |
There was a problem hiding this comment.
list accepts a blank API key. Running elastic docs api list ' ' or elastic docs api list --api '' passes validation, then sends GET /apis//groups which 404s. I'd add a test to ensure a blank value fails validation, then fix the code to support that.
There was a problem hiding this comment.
Fixed in 7661cef. list now uses the same non-blank API schema as search and get. A blank positional value (trimmed) and --api '' both fail validation before any request, under --dry-run too. Regression cases are in test/docs/api.test.ts.
| // Every operation lookup names its API: most operation ids and many routes exist in several APIs | ||
| // (elasticsearch and elasticsearch-serverless share most of theirs), so a lookup across APIs could only guess. | ||
| const api = { ...nonBlank, description: "API key, e.g. 'elasticsearch' or 'kibana'. `elastic docs api list` lists the keys." } | ||
| const version = { type: 'string', description: "API version: 'main' or a major such as '9'. Without it, the current version is used." } |
There was a problem hiding this comment.
Using --version here feels awkward. elastic --version / -V prints the CLI version. So elastic docs api get cluster-health --api elasticsearch --version 9 looks at a glance like it is asking for a CLI version. It works, but is a possible point of usability confusion for humans and agents.
Proposed alternatives: --api-version or --stack-version, then map the value to the version query param for each docs API request.
There was a problem hiding this comment.
Agreed, renamed in 7661cef. The flag is now --api-version (api_version in JSON input), and every request still sends it as the docs API's version query parameter. --version is now an unknown option on docs api commands, and a test checks that. I picked --api-version over --stack-version because Cloud APIs have versions that aren't stack versions, such as main only, or 4 and 3 for ECE.
| // The docs API answers RFC 9457 problems (application/problem+json): `detail` explains this occurrence, | ||
| // `title` names its type. A CDN or site error page is HTML and carries neither, and HTTP/2 has no status text. | ||
| let message = resp.statusText || STATUS_CODES[resp.status] || 'request failed' | ||
| try { | ||
| const problem = JSON.parse(text) as { detail?: unknown; title?: unknown } | ||
| if (typeof problem.detail === 'string' && problem.detail !== '') message = problem.detail | ||
| else if (typeof problem.title === 'string' && problem.title !== '') message = problem.title | ||
| } catch { | ||
| // keep the status text | ||
| } | ||
| throw new Error(`docs api error (${resp.status}): ${message}`) |
There was a problem hiding this comment.
The RFC 9457 parsing doesn't seem to work. elastic docs api get --api elasticsearch --operation-id foo --json returns {"error":{"code":"docs_error","message":"docs api error (404): Not Found"}}. Is this the API that is being pushed to prod soon?
There was a problem hiding this comment.
The API does answer RFC 9457 problems, and they are live in production now. CloudFront is what drops them, and only on 404. The elastic-docs-v3 distribution has a distribution-wide custom error response that replaces every 404, from any origin, with the site's HTML "Page not found" page. CloudFront can't exempt a path from that. Other statuses come through intact:
curl -s "https://www.elastic.co/docs/_api/v1/apis/elasticsearch/operations?q=a&endpoint=b"
# {"type":"…#invalid-arguments","title":"Invalid arguments","status":400,"detail":"Pass q or endpoint, not both.",…}
curl -si "https://www.elastic.co/docs/_api/v1/apis/elasticsearch/operations/foo" | grep -i content-type
# content-type: text/html (the site 404 page, not the API's problem)The infra fix is tracked in elastic/docs-eng-team#1231. Until then, a 404 reads docs api error (404): Not Found. Before, it read request failed, because HTTP/2 has no status text. The CLI picks up the problem detail without changes once #1231 lands.
| } | ||
| throw new Error(`docs api error (${resp.status}): ${message}`) | ||
| } | ||
| return JSON.parse(text) as unknown |
There was a problem hiding this comment.
nit: a 2xx reply that isn't JSON (for example a CDN or maintenance HTML page served with 200) makes JSON.parse throw a SyntaxError. The exit code is still 1, but the message will be confusing.
There was a problem hiding this comment.
Fixed in 7661cef. A 2xx reply that doesn't parse as JSON now fails with docs api error (200): the reply is not JSON, exit code 1, instead of a SyntaxError. A test covers it.
- `elastic --version` prints the CLI version, so `docs api` names the API version `--api-version` and still sends it as the `version` query parameter. - `docs api list` rejects a blank API key instead of requesting `/apis//groups`. - A 2xx reply that is not JSON, such as a CDN maintenance page, reports `the reply is not JSON` instead of a SyntaxError. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (merged !== undefined && positionalValue !== undefined && toInput != null) { | ||
| const given = typeof merged.input === 'object' && merged.input !== null ? merged.input : {} | ||
| merged.input = { ...toInput(positionalValue), ...given } | ||
| } |

Adds
elastic docs api search,elastic docs api getandelastic docs api list. They let agents and users look up operations in the Elastic REST API reference by route, operation id or field, without going through general docs search.Closes elastic/docs-eng-team#1204.
The docs API endpoints are live on www.elastic.co: elastic/docs-builder#4887, #4896, #4942 and #4943 are deployed to production.
Commands
The commands sit under
elastic docs, as the issue preferred. A bareelastic apiwould read as calling the Elastic APIs.elastic docs api search <query> --api <api> [--api-version] [--include-deprecated] [--page] [--size]GET /docs/_api/v1/apis/{api}/operations?q=search_api_operationswith an APIelastic docs api get <operation-id> --api <api> [--api-version] [--include-body]GET /docs/_api/v1/apis/{api}/operations/{operationId}get_api_operationby idelastic docs api get <endpoint> --api <api> [--api-version] [--include-body]GET /docs/_api/v1/apis/{api}/operations?endpoint=&view=fullget_api_operationby endpointelastic docs api listGET /docs/_api/v1/apislist_api_operationselastic docs api list <api> [--api-version]GET /docs/_api/v1/apis/{api}/groupslist_api_operationswith an API (group counts)elastic docs api list <api> --group <group> [--page] [--size]GET /docs/_api/v1/apis/{api}/operations?group=list_api_operationswith a groupsearchandgetrequire--api. Most operation ids and many routes exist in several APIs, for exampleelasticsearchandelasticsearch-serverless, so a lookup across APIs could only guess. The docs API has none, and neither does the CLI. The MCP tools still search across APIs.gettreats an argument containing/as an endpoint, such asGET /_cluster/health, and anything else as an operation id.--endpointand--operation-idare also available.getasks the API's operations for the full view: a route made only of parameters, such as/{index}, can serve several operations. The best route becomes the result, and the rest go tootherMatches.--jsonoutput has the same shape as a lookup by id.--jsonpasses the server reply through unchanged, except forgetby endpoint, which reshapes the results as described above. Text output lists method, path, operation id, URL and fields.--dry-runtoo. The schema keys are the query parameter names, so flags map straight onto the request. The schema rejects:--sizeor--pagevaluessearch, and a missing or blank--apiforsearchandget--endpointor--operation-idforget(oneOf)list,--api-versionor--groupwithout an API, and--pageor--sizewithout--group(dependencies)--api-version, not--version, becauseelastic --versionprints the CLI's own version. It is sent as the docs API'sversionquery parameter.docs_errorcode, like the otherdocscommands. The docs API answers errors as RFC 9457 problems (Publish an OpenAPI document for the docs API and answer errors as RFC 9457 problems docs-builder#4896). The message is the problem'sdetail, such as "No API operation matches nope in elasticsearch.", or itstitlewhen there is no detail. An HTML error page from the CDN is reduced to its status name, such asNot Found: www.elastic.co's CDN answers an API 404 with the site's HTML page, so a missing API or operation readsdocs api error (404): Not Found. elastic/docs-eng-team#1231 tracks letting the API's 404 problems through. A 2xx reply that isn't JSON, such as a maintenance page, fails withthe reply is not JSON.application/jsonandapplication/problem+json, useredirect: 'error', and URL-encode parameters. They send no cookie: elastic/docs-infra#523 dropped the production gate, and fix(docs): stop sending the docs API cookie #829 removed the cookie fromdocs searchanddocs ask.Shared factory change
positionalArginCommandConfiggains an optionaltoInput, which maps the positional value onto input fields before schema validation. Before, a positional value reached the handler only asparsed.arg, after validation, so a schema couldn't require it, and--dry-runreported success for invocations a real run would reject. Flags and JSON input win over the fieldstoInputsets. Commands that don't set it behave as before.test/factory.test.tscovers it.Verify
The full unit suite,
tsc --noEmitand eslint pass.docs/cli/schema.jsonis up to date.All of the above ran against production with the built CLI and returned the expected results. That run found two bugs, fixed in this PR:
docs api listpadded keys to 24 columns, soelasticsearch-serverlessran into its title. The column now fits the longest key.request failed. HTTP/2 has no status text, and the CDN replaces the API's 404 problem with an HTML page. It now readsNot Found.🤖 Generated with Claude Code