Skip to content

feat(docs): add elastic docs api commands - #825

Open
akira28 wants to merge 13 commits into
elastic:mainfrom
akira28:feature/text-inside-external-tags
Open

akira28 wants to merge 13 commits into
elastic:mainfrom
akira28:feature/text-inside-external-tags

Conversation

@akira28

@akira28 akira28 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Adds elastic docs api search, elastic docs api get and elastic 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 bare elastic api would read as calling the Elastic APIs.

Command Docs API request MCP tool equivalent
elastic docs api search <query> --api <api> [--api-version] [--include-deprecated] [--page] [--size] GET /docs/_api/v1/apis/{api}/operations?q= search_api_operations with an API
elastic docs api get <operation-id> --api <api> [--api-version] [--include-body] GET /docs/_api/v1/apis/{api}/operations/{operationId} get_api_operation by id
elastic docs api get <endpoint> --api <api> [--api-version] [--include-body] GET /docs/_api/v1/apis/{api}/operations?endpoint=&view=full get_api_operation by endpoint
elastic docs api list GET /docs/_api/v1/apis list_api_operations
elastic docs api list <api> [--api-version] GET /docs/_api/v1/apis/{api}/groups list_api_operations with an API (group counts)
elastic docs api list <api> --group <group> [--page] [--size] GET /docs/_api/v1/apis/{api}/operations?group= list_api_operations with a group
  • Every command makes one request.
  • search and get require --api. Most operation ids and many routes exist in several APIs, for example elasticsearch and elasticsearch-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.
  • get treats an argument containing / as an endpoint, such as GET /_cluster/health, and anything else as an operation id. --endpoint and --operation-id are also available.
  • For an endpoint, get asks 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 to otherMatches. --json output has the same shape as a lookup by id.
  • The API key and operation id are URL-encoded into one path segment.
  • --json passes the server reply through unchanged, except for get by endpoint, which reshapes the results as described above. Text output lists method, path, operation id, URL and fields.
  • Input is checked against a JSON Schema before any request, and under --dry-run too. The schema keys are the query parameter names, so flags map straight onto the request. The schema rejects:
    • unknown keys and out-of-range --size or --page values
    • a missing or blank query for search, and a missing or blank --api for search and get
    • anything but exactly one non-blank --endpoint or --operation-id for get (oneOf)
    • for list, --api-version or --group without an API, and --page or --size without --group (dependencies)
    • a blank API key, for every command
  • The API version flag is --api-version, not --version, because elastic --version prints the CLI's own version. It is sent as the docs API's version query parameter.
  • Errors use the existing docs_error code, like the other docs commands. 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's detail, such as "No API operation matches nope in elasticsearch.", or its title when there is no detail. An HTML error page from the CDN is reduced to its status name, such as Not Found: www.elastic.co's CDN answers an API 404 with the site's HTML page, so a missing API or operation reads docs 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 with the reply is not JSON.
  • Requests accept application/json and application/problem+json, use redirect: '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 from docs search and docs ask.

Shared factory change

positionalArg in CommandConfig gains an optional toInput, which maps the positional value onto input fields before schema validation. Before, a positional value reached the handler only as parsed.arg, after validation, so a schema couldn't require it, and --dry-run reported success for invocations a real run would reject. Flags and JSON input win over the fields toInput sets. Commands that don't set it behave as before. test/factory.test.ts covers it.

Verify

npm run test:unit -- test/docs/api.test.ts
elastic docs api list
elastic docs api list --api elasticsearch --group "Cluster - Health"
elastic docs api search "retriever." --api elasticsearch --size 3
elastic docs api get --api elasticsearch --operation-id cluster-health
elastic docs api get --api elasticsearch --endpoint "GET /my-index/_search"
elastic docs api get --api elasticsearch --operation-id nope --json   # {"error":{"code":"docs_error",...}}, exit 1

The full unit suite, tsc --noEmit and eslint pass. docs/cli/schema.json is 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 list padded keys to 24 columns, so elasticsearch-serverless ran into its title. The column now fits the longest key.
  • A 404 read request failed. HTTP/2 has no status text, and the CDN replaces the API's 404 problem with an HTML page. It now reads Not Found.

🤖 Generated with Claude Code

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]>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 11:30
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🔍 Preview links for changed docs

⏳ Building and deploying preview... View progress

This comment will be updated with preview links when the build is complete.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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, and list commands 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

Comment thread src/docs/api.ts Resolved
akira28 and others added 2 commits November 17, 2026 12:58
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]>
@akira28
akira28 marked this pull request as ready for review November 18, 2026 10:21
@akira28 akira28 changed the title feat(docs): add elastic docs api commands [WAIT-FOR-API-IN-PROD] feat(docs): add elastic docs api commands Nov 18, 2026
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]>
@akira28 akira28 changed the title [WAIT-FOR-API-IN-PROD] feat(docs): add elastic docs api commands feat(docs): add elastic docs api commands Nov 20, 2026
akira28 and others added 2 commits November 20, 2026 16:18
…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]>
Comment thread src/docs/api.ts Outdated
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." },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/docs/api.ts Outdated
// 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." }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/docs/client.ts
Comment on lines +216 to +226
// 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}`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@akira28 akira28 Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/docs/client.ts Outdated
}
throw new Error(`docs api error (${resp.status}): ${message}`)
}
return JSON.parse(text) as unknown

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Positional-field merging converts invalid JSON root values into valid object inputs, bypassing schema rejection and allowing requests or successful dry runs.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/factory.ts
Comment on lines +874 to +877
if (merged !== undefined && positionalValue !== undefined && toInput != null) {
const given = typeof merged.input === 'object' && merged.input !== null ? merged.input : {}
merged.input = { ...toInput(positionalValue), ...given }
}

This branch was successfully deployed

1 active deployment
docs-preview — cafb60b2 Deployed Nov 23, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants