Metadata-Version: 2.4
Name: ad-cli
Version: 0.1.1
Summary: ad — Ascend Infra command line. Auto-discovered, AI-friendly.
Author: ad-cli contributors
License: Apache-2.0
Project-URL: Homepage, https://gitcode.com/openeuler/oed-cli
Project-URL: Documentation, https://gitcode.com/openeuler/oed-cli/blob/ad-cli/docs/cli-design.md
Project-URL: Bug Tracker, https://gitcode.com/openeuler/oed-cli/issues
Keywords: Ascend,cli,discovery,ai-agent,openapi
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.1
Requires-Dist: httpx>=0.27
Requires-Dist: keyring>=24
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: ruff>=0.5; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=5.0; extra == "dev"
Provides-Extra: socks
Requires-Dist: socksio>=1.0; extra == "socks"
Provides-Extra: os-keyring-windows
Requires-Dist: pywin32-ctypes; sys_platform == "win32" and extra == "os-keyring-windows"
Provides-Extra: os-keyring-linux
Requires-Dist: secretstorage; sys_platform == "linux" and extra == "os-keyring-linux"
Provides-Extra: os-keyring
Requires-Dist: pywin32-ctypes; sys_platform == "win32" and extra == "os-keyring"
Requires-Dist: secretstorage; sys_platform == "linux" and extra == "os-keyring"
Dynamic: license-file

<h1 align="center">ad-cli</h1>

<p align="center">
  <strong>One CLI for every Ascend Infra service — auto-discovered, JSON-first, built for humans and AI agents.</strong>
  <br />
  <em>Works with Claude Code, Cursor, VS Code, Codex, Gemini CLI, OpenClaw, Kimi Code, Kiro, Coze, and 10+ more agent clients — plus plain old shell pipes.</em>
</p>

<p align="center">
  <a href="README.md">English</a> | <a href="README_cn.md">简体中文</a>
</p>

<p align="center">
  <a href="https://pypi.org/project/ad-cli/"><img src="https://img.shields.io/pypi/v/ad-cli" alt="PyPI" /></a>
  <a href="https://www.python.org"><img src="https://img.shields.io/badge/python-3.10+-blue" alt="Python 3.10+" /></a>
  <a href="https://gitcode.com/openeuler/oed-cli/tree/ad-cli/LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-green" alt="License: Apache-2.0" /></a>
  <a href="#one-line-install-cli--mcp"><img src="https://img.shields.io/badge/MCP-ad--mcp-6f42c1" alt="MCP: ad-mcp" /></a>
  <a href=".claude/skills/ad-cli/SKILL.md"><img src="https://img.shields.io/badge/Agent_Skill-bundled-0969da" alt="Agent Skill: bundled" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Platforms-17%2B-orange" alt="Platforms: 17+" /></a>
</p>

<p align="center">
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Claude_Code-D97757" alt="Claude Code" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Cursor-000000" alt="Cursor" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Trae-7e22ce" alt="Trae" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Codex-1F2937" alt="Codex" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Gemini_CLI-4285F4" alt="Gemini CLI" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Qwen_Code-615CED" alt="Qwen Code" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/dsh-24292e" alt="dsh" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/OpenClaw-DC2626" alt="OpenClaw" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Hermes_Agent-BE185D" alt="Hermes Agent" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Kimi_Code-0F766E" alt="Kimi Code" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/VS_Code-007ACC" alt="VS Code" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Copilot_CLI-1F6FEB" alt="Copilot CLI" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Antigravity-8B5CF6" alt="Antigravity" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Pi-F59E0B" alt="Pi" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Nanobot-10B981" alt="Nanobot" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Kiro-FF9900" alt="Kiro" /></a>
  <a href="#multi-platform-support"><img src="https://img.shields.io/badge/Coze-4D6BFE" alt="Coze" /></a>
</p>

---

**`ad` doesn't ship a static list of commands.** It reads the Ascend Infra Discovery Service at runtime and builds its entire command surface dynamically. When a new service ships, `ad` picks it up automatically — no upgrade required.

## Why ad?

`ad` exists to solve one specific problem: a CLI that talks to _many evolving services_ without multiplying that complexity. Shipping a hand-written client per service per version is exactly the versioning pressure that the Zylos API versioning research warns against — and for AI agent consumers that pressure is acute: a renamed field silently breaks a tool call, a new required parameter crashes an otherwise healthy workflow. `ad` sidesteps the whole problem by being one client that **discovers every service at runtime**, so the only thing that has to be versioned is the gateway's OpenAPI spec itself.

- **Zero boilerplate.** No copy-pasted OpenAPI clients, no per-service SDKs, no `--data` to escape, no `User-Agent` headers to remember.
- **Runtime discovery.** The service list you see in `ad --help` is built from `https://api-gateway.osinfra.cn/discovery/apis` on every call. New services, new endpoints, and new schema fields show up without an `ad` upgrade. A 10-minute cache keeps CI bursts cheap; `ad cache refresh` forces an immediate re-fetch when you know the gateway just shipped.
- **AI-friendly output.** Single JSON object on stdout, deterministic exit codes (`0` success · `1` user · `2` network · `3` upstream · `4` not found). Logs and progress go to stderr so `| jq` is always safe.
- **Claude / Cursor ready.** Ships with `.claude/skills/ad-cli/SKILL.md` so agents know how to use it without a custom prompt.
- **MCP-ready.** The same three tools reach any MCP client through [`ad-mcp`](https://github.com/Hourunze1997/ad-mcp) (`npx -y ad-mcp`) — `install.sh` below wires it in one line.

## Install

```
pip install ad-cli
```

Or in an isolated environment (recommended for CI):

```
pipx install ad-cli
```

From source:

```
git clone -b ad-cli https://gitcode.com/openeuler/oed-cli && cd oed-cli
pip install -e .
```

Verify:

```
ad --version       # → ad, version 0.1.0
```

## Quick start: install, then call

```
pip install ad-cli
ad services                        # see what the ascend community exposes
ad mailman <operation> --help      # inspect one operation's flags
```

### Install the Agent Skill

Agent frameworks (Claude Code, Cursor, OpenCode, …) need the bundled [Agent Skill](.claude/skills/ad-cli/SKILL.md) installed before they call `ad` correctly — it is not part of the pip wheel. For Claude Code, copy it into the skills directory:

```
git clone --depth 1 -b ad-cli https://gitcode.com/openeuler/oed-cli /tmp/ad-cli \
  && mkdir -p ~/.claude/skills \
  && cp -r /tmp/ad-cli/.claude/skills/ad-cli ~/.claude/skills/
```

Other frameworks: drop it into their skill/rules directory.

### (optional) Call an AtomGit operation — store a token once

Operations on the `ag` (AtomGit) service authenticate via an `access_token` query parameter. Store a personal access token once, and every `ad ag ...` call injects it automatically:

```
ad ag login
```

That's it. `ad` discovers the service from the gateway, pulls its OpenAPI spec, derives `--<flag>` names from the declared parameters, fills WAF-safe browser headers, and ships the request through the production gateway. Output is plain JSON on stdout — pipe straight into `jq`:

```
ad <service> <operation> --flag value | jq '.response'
```

### Want to look around first? (optional)

```
ad info      # gateway snapshot: services_total, cache status, community
ad services  # full service list — pick one to call next
ad <service> --help                       # list that service's operations
ad <service> <operation> --help           # see every flag for one operation
ad <service> <operation> --dry-run --…    # preview the request, no network
```

The first `ad <service> <operation>` call after install will pull a fresh discovery feed + that service's OpenAPI spec; subsequent calls within 10 minutes reuse the local cache. Run `ad cache refresh` to force a re-fetch when you know the gateway just shipped something new.

## One-line install: CLI + MCP

The installer does two things: installs the `ad` CLI (pipx → uv → pip, skipped when `ad` is already on PATH), then registers the [`ad-mcp`](https://github.com/Hourunze1997/ad-mcp) MCP server with your agent client — so any MCP-capable agent can drive every Ascend service:

```bash
# auto-detect the client, or pick one explicitly
curl -fsSL https://raw.gitcode.com/openeuler/oed-cli/raw/ad-cli/install.sh | bash
curl -fsSL https://raw.gitcode.com/openeuler/oed-cli/raw/ad-cli/install.sh | bash -s cursor
```

pipx is required — `ad` installs into an isolated pipx venv: the system Python stays untouched, uninstall is clean, and PEP 668 (Ubuntu 23.04+/Debian 12+ forbid installing with the system pip) doesn't apply; the script installs pipx automatically if missing.

Windows (PowerShell 5.1+ / pwsh) — `install.ps1` is a full port of `install.sh` with the same clients and flags:

```powershell
powershell -c "irm https://raw.gitcode.com/openeuler/oed-cli/raw/ad-cli/install.ps1 | iex"
# pipe-to-iex cannot take arguments — use a scriptblock to pick a client or uninstall
powershell -c "& ([scriptblock]::Create((irm https://raw.gitcode.com/openeuler/oed-cli/raw/ad-cli/install.ps1))) cursor"
powershell -c "& ([scriptblock]::Create((irm https://raw.gitcode.com/openeuler/oed-cli/raw/ad-cli/install.ps1))) -Uninstall cursor"
```

Windows differences: the MCP launch command is registered as `cmd /c "npx -y ad-mcp"` (native clients cannot spawn `npx.cmd` directly), and the CLI is installed with the `ad-cli[os-keyring-windows]` keyring backend (DPAPI / Credential Manager — needed by `ad ag login` to store tokens).

What it does:

1. Installs the `ad` CLI and verifies `ad --version`.
2. Registers `npx -y ad-mcp` with the chosen client — merging the client's config in place and backing it up to `<file>.ad-backup` first. Prefers each client's own CLI when available (`claude mcp add`, `openclaw mcp add`, `copilot mcp add`, `pi mcp add`, `code --add-mcp`, `dsh plugin add`, `gemini/qwen extensions install`); edits config files directly for the rest (`mcp.json` for Cursor/Trae/Kimi Code, `config.toml` for Codex, `config.yaml` for Hermes Agent, `mcp_config.json` for Antigravity, `tools.mcpServers` for Nanobot, `~/.kiro/settings/mcp.json` for Kiro).
3. Is idempotent and reversible: re-running is safe; `bash install.sh --uninstall <client>` removes only the `ad` entry.

No secrets are handled. The ascend community services (mailman, etherpad) work anonymously; for AtomGit (`ag`) operations, run `ad ag login` yourself afterwards — see the auth section below.

## Multi-platform support

| Platform | Status | Install Method |
|----------|--------|----------------|
| Claude Code | Shell-install | `install.sh claude` · or `claude mcp add -s user ad -- npx -y ad-mcp` · or the bundled [Agent Skill](#install-the-agent-skill) |
| Cursor | Shell-install | `install.sh cursor` |
| Trae / Trae 中国版 | Shell-install | `install.sh trae` / `install.sh trae-cn` |
| Codex CLI | Shell-install | `install.sh codex` |
| Gemini CLI | Shell-install | `install.sh gemini` |
| Qwen Code | Native | `qwen extensions install ad-mcp` (npm channel) · or `install.sh qwen` |
| dsh | Shell-install | `install.sh dsh` |
| OpenClaw | Shell-install | `install.sh openclaw` · or `openclaw mcp add ad --command npx --arg -y --arg ad-mcp` |
| Hermes Agent | Shell-install | `install.sh hermes` · or add `ad` under `mcp_servers` in `~/.hermes/config.yaml` |
| Kimi Code CLI | Shell-install | `install.sh kimi` · or add `ad` to `mcpServers` in `~/.kimi-code/mcp.json` · or `/mcp-config` in the TUI |
| VS Code (Copilot Chat) | Shell-install | `install.sh vscode` · or `code --add-mcp '{"name":"ad","command":"npx","args":["-y","ad-mcp"]}'` |
| Copilot CLI | Shell-install | `install.sh copilot` · or `copilot mcp add ad -- npx -y ad-mcp` |
| Antigravity | Shell-install | `install.sh antigravity` (writes `~/.gemini/antigravity/mcp_config.json`) |
| Pi | Shell-install | `install.sh pi` · or `pi mcp add ad -- npx -y ad-mcp` |
| Nanobot | Shell-install | `install.sh nanobot` (writes `tools.mcpServers` in `~/.nanobot/config.json`) |
| Kiro CLI / IDE | Shell-install | `install.sh kiro` (writes `~/.kiro/settings/mcp.json`, shared by both) |
| Cline / any other MCP client | Shell-install | add `npx -y ad-mcp` to its config |
| Coze (cloud) | Native | Coze plugin store — search **ad** |

> **What the statuses mean** — status tokens are the same in both the English and Chinese README, not translated.
>
> - **Native** — the platform discovers and adds `ad` through its own mechanisms, no script involved: an in-app store search (Coze — plugin store, search **ad**) or an official package channel installable by name (Qwen Code — `qwen extensions install ad-mcp` pulls it from npm, which requires the `qwen-extension.json` manifest shipped in the npm package).
> - **Auto-discover** — the platform's catalog indexes `ad-mcp` on its own, no manual listing: an official crawler (Gemini CLI's [extension gallery](https://geminicli.com/extensions) watches the `gemini-cli-extension` GitHub topic) or an npm-crawling directory site (Glama below). No agent platform has this working for `ad` yet — Gemini CLI's gallery hasn't indexed the repo so far; once it does, Gemini CLI moves up to Auto-discover.
> - **Shell-install** — no catalog on the platform lists `ad` yet, so one command wires it up: `install.sh <client>`, or the equivalent manual command in the table. Gemini CLI sits here today — its native `extensions install` works fine through the script even before the gallery lists us.

MCP needs Node ≥ 18 (`npx`); the CLI needs Python ≥ 3.10. The MCP server bootstraps the `ad` CLI automatically on first use, so the client-side setup is the only step.

> **MCP directories** — where `ad-mcp` is already discoverable. Glama crawled npm on its own (true auto-discover); npm is our own publish; the rest are not listed yet:

| Directory | Listing | How it got there |
|----------|--------|------------------|
| npm | [npmjs.com/package/ad-mcp](https://www.npmjs.com/package/ad-mcp) | published by us |
| Glama | [glama.ai/mcp/servers/Hourunze1997/ad-mcp](https://glama.ai/mcp/servers/Hourunze1997/ad-mcp) | auto-crawled |
| Smithery | — | not listed yet (submission planned) |
| MCP Registry (official) | — | not listed yet (submission planned) |
| Cline marketplace | — | not listed yet (submission planned) |

From npm or either crawled directory, the install is the same one line: `npx -y ad-mcp`.

## "What flags does this operation take?" → `--help`

Don't guess. Each operation auto-derives its own flags from the OpenAPI schema:

```
ad <service> <operation> --help
```

```
{
  "help_for": "<operation>",
  "operation_id_raw": "<operation>",
  "method": "GET",
  "path": "/<service-path>/...",
  "url": "https://apig.osinfra.cn/...",
  "parameters": [
    {
      "name": "padId",
      "in": "query",
      "required": true,
      "flag": "--pad-id",
      "alt_flag": "--padId",
      "type": "string",
      "description": "..."
    }
  ],
  "usage": "ad <service> <operation> --pad-id <value> [--dry-run]"
}
```

Same idea at the service level — list every operation with one flag each:

```
ad <service> --help | jq '.operations | length'
```

## More examples

```
# Dry-run — preview the request without hitting the network
ad <service> <operation> --flag value --dry-run

# POST JSON body — fields go through --json, not --params
ad <service> <operation> --json '{"key":"value"}'
```

Run `ad services` to see the live ascend service list (currently `mailman` and `etherpad`), then `ad <service> --help` for the operation list of each.

## AtomGit (`ag`) authentication

AtomGit operations authenticate through the `access_token` query parameter declared on their spec. Store a personal access token once, and every `ad ag ...` call uses it automatically:

```
# Interactive (prompts for the token, never echoes it back)
ad ag login

# Non-interactive — good for CI / scripts
ad ag login --token <pat>

# Skip validating the token against AtomGit before storing
ad ag login --token <pat> --no-verify

# Just report whether a token is configured (no network, no prompt)
ad ag login --status

# Forget the stored token
ad ag logout
```

Token storage:

- macOS Keychain / Windows DPAPI / Linux SecretService when an OS keystore is reachable, otherwise a 0600 plaintext fallback file.
- Lives under a `tokens/` subdir of the cache dir (`tokens/ag.json` when keyring is unavailable); `ad cache clear` never touches credentials.

Auto-injection on real calls:

- If the operation declares `access_token` and you don't pass one, the stored token is filled in automatically — `ad ag listAuthenticatedUserIssues` just works.
- An explicit `--access-token <pat>` always wins over the stored one.
- If the operation requires a token and none is available anywhere, you get a clear `ag_token_missing` error with a hint, instead of an opaque gateway 401.
- `--dry-run` and request echo views mask the token as `<stored>` — the real value only ever goes out on the wire.

## Local development

### Clone and install (editable)

```
git clone -b ad-cli https://gitcode.com/openeuler/oed-cli && cd oed-cli
pip install -e ".[dev]"
```

`pip install -e .` makes source edits take effect on the next `ad` invocation. Drop it with `pip uninstall ad-cli` when you're done.

### Run the tests (~0.3 s, fully offline)

```
python -m pytest -q
```

The suite covers dispatch, the operation-help cheatsheet, per-parameter flag coercion, the `API_`-prefix alias, the `resolve_runtime_gateway` no-fallback semantics, every exit code path, and the `ag` token store + auto-injection. Tests monkeypatch the discovery layer so no gateway access is needed.

### Smoke-test against the live gateway

```
# 1) Health check — ascend community currently exposes mailman + etherpad
ad info
# → {"ok": true, "community": "ascend", ...}

# 2) Service list
ad services

# 3) Dry-run to inspect URL + body without hitting the network
ad <service> <operation> --dry-run --flag value

# 4) Verify the canonical URL routing
ad <service> <operation> --dry-run --flag value | jq '.url'
```

### Cache debugging

```
ad cache show      # path, size, age, TTL
ad cache refresh   # force re-fetch the discovery feed
ad cache clear     # delete the cache file
```

Cache lives at `~/.cache/ad-cli/` (XDG) or `C:\Users\<you>\AppData\Local\ad-cli\cache\` on Windows. Override with `AD_CACHE_DIR=...`. Per-service OpenAPI specs are cached next to it under `specs/<community>/<service>.json` with the same 10-minute TTL. AtomGit token fallback files live under the same `AD_CACHE_DIR` in a `tokens/` subdir.

### Keeping in sync with the gateway

`ad` does **not** warm up a fresh discovery feed on every invocation — only the first command in a 10-minute window actually hits the gateway, the rest read from `~/.cache/ad-cli/discovery.json`. That keeps CI scripts that run dozens of `ad` calls from hammering the gateway, and it means `ad --help` and `ad --version` never touch the network.

When the gateway adds a new service or operation, refresh the cache by hand:

```
# Fastest: drop the 10-minute TTL and re-pull the discovery feed
ad cache refresh

# Or, nuke and re-fetch from a known-clean state
ad cache clear && ad info

# Then confirm the new service is now visible
ad services | jq -r '.[] | .service_name'
```

The first call to a brand-new service will additionally pull its OpenAPI spec into `specs/<community>/<service>.json` (also 10-minute TTL); after that it's reused like any other spec.

**Why this isn't automatic.** Ascend Infra services are added on a weekly-to-quarterly cadence via review, not minute-to-minute. Auto-refreshing on every `ad` invocation would burn a network round-trip per CLI call for no practical benefit. The TTL exists to absorb CI bursts, not to delay visibility of new endpoints.

**Future** — `ad whatsnew` (planned) will diff the freshly-pulled feed against the previous cache and print only what changed, so you don't have to eyeball `ad services` output every week.

**Cache integrity.** The local cache is plain JSON and lives in a user-writable directory, so a malicious same-user process can in principle rewrite it. `ad` applies three defenses (issue #22): (1) a cached timestamp more than ~5 min in the future is treated as poisoned and discarded — this closes the "set the timestamp ahead so the entry never expires" trick; (2) a runtime `base_url` whose scheme is anything other than `https` (e.g. a poisoned `http://evil.com`) is hard-rejected before any credentials are attached, so a tampered host can't exfiltrate your Bearer token / `ag` PAT over plaintext; (3) cache writes are atomic (tmp + replace) so a crash mid-write can't leave a corrupt file. If you ever see `insecure_base_url`, run `ad cache clear` and retry. Note this is defense-in-depth — a same-user process that can write the cache can typically also read your keyring directly.

### Common errors

| Symptom | Cause | Fix |
| --- | --- | --- |
| `ModuleNotFoundError: ad_cli` | not installed in env | `pip install -e .` |
| `ad info` hangs or `waf_block` exit 2 | gateway unreachable / WAF | confirm `curl https://api-gateway.osinfra.cn`; see `context/discoverAPI.md` §6 |
| Chinese output garbled on Windows | console codepage not UTF-8 | `chcp 65001`, or pipe `| python`, or `PYTHONIOENCODING=utf-8 ad …` |
| `error="spec_missing"` (exit 4) on a known service | upstream hasn't published the spec yet | wait for the gateway-side OpenAPI yaml; nothing to do on the ad side |
| `error="body_fields_via_params"` (exit 1) on a POST | body fields were passed via `--params` (which only covers query/path) | resend the fields with `--json '{...}'` — `ad <service> <op> --help` lists the body schema |
| `error="ag_token_missing"` on an `ag` call | operation needs a token, none stored | `ad ag login` (or pass `--access-token <pat>`) |

### Offline mode

`ad --help`, `ad info` (uses cached feed), `pytest`, and any command against a service whose spec is in the local cache all work without network. To run `ad` from source without installing:

```
python -m ad_cli --help
# or
python -c "from ad_cli.main import main; sys.argv = ['ad','--help']; main()"
```

## Documentation

- Design doc — architecture, command contract, exit codes, packaging, roadmap.
- Local testing guide — install, smoke test, every per-parameter flag demo.
- Discovery API reference — the data source `ad` consumes, plus WAF caveats.

## Contributing

Issues and patches welcome on [gitcode.com/openeuler/oed-cli](https://gitcode.com/openeuler/oed-cli).

Dev install:

```
pip install -e ".[dev]"
pytest
ruff check src tests
```

## License

Apache-2.0. See [LICENSE](https://gitcode.com/openeuler/oed-cli/tree/ad-cli/LICENSE).

---

<p align="center">
  <strong>One client. Every Ascend service. Humans and agents alike.</strong>
</p>
