incur-cli 0.10.0__tar.gz

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 (44) hide show
  1. incur_cli-0.10.0/.gitignore +8 -0
  2. incur_cli-0.10.0/PKG-INFO +376 -0
  3. incur_cli-0.10.0/README.md +363 -0
  4. incur_cli-0.10.0/pyproject.toml +96 -0
  5. incur_cli-0.10.0/src/incur/__init__.py +62 -0
  6. incur_cli-0.10.0/src/incur/_cli.py +2139 -0
  7. incur_cli-0.10.0/src/incur/_command.py +426 -0
  8. incur_cli-0.10.0/src/incur/_completions.py +170 -0
  9. incur_cli-0.10.0/src/incur/_errors.py +234 -0
  10. incur_cli-0.10.0/src/incur/_filter.py +173 -0
  11. incur_cli-0.10.0/src/incur/_formatter.py +112 -0
  12. incur_cli-0.10.0/src/incur/_gateway.py +173 -0
  13. incur_cli-0.10.0/src/incur/_help.py +260 -0
  14. incur_cli-0.10.0/src/incur/_http.py +389 -0
  15. incur_cli-0.10.0/src/incur/_js.py +467 -0
  16. incur_cli-0.10.0/src/incur/_jsonschema.py +320 -0
  17. incur_cli-0.10.0/src/incur/_mcp.py +872 -0
  18. incur_cli-0.10.0/src/incur/_mcpsource.py +226 -0
  19. incur_cli-0.10.0/src/incur/_model.py +348 -0
  20. incur_cli-0.10.0/src/incur/_openapi.py +640 -0
  21. incur_cli-0.10.0/src/incur/_parser.py +347 -0
  22. incur_cli-0.10.0/src/incur/_pm.py +49 -0
  23. incur_cli-0.10.0/src/incur/_schema.py +555 -0
  24. incur_cli-0.10.0/src/incur/_serve_http.py +236 -0
  25. incur_cli-0.10.0/src/incur/_skill.py +355 -0
  26. incur_cli-0.10.0/src/incur/_sync.py +403 -0
  27. incur_cli-0.10.0/src/incur/_syncmcp.py +188 -0
  28. incur_cli-0.10.0/src/incur/_tokens.py +130 -0
  29. incur_cli-0.10.0/src/incur/_toon.py +202 -0
  30. incur_cli-0.10.0/src/incur/_tree.py +273 -0
  31. incur_cli-0.10.0/src/incur/_update.py +373 -0
  32. incur_cli-0.10.0/src/incur/_yaml.py +651 -0
  33. incur_cli-0.10.0/src/incur/py.typed +0 -0
  34. incur_cli-0.10.0/tests/fixtures.py +1733 -0
  35. incur_cli-0.10.0/tests/test_api.py +621 -0
  36. incur_cli-0.10.0/tests/test_differential.py +333 -0
  37. incur_cli-0.10.0/tests/test_features.py +795 -0
  38. incur_cli-0.10.0/tests/test_http.py +133 -0
  39. incur_cli-0.10.0/tests/test_mcp.py +289 -0
  40. incur_cli-0.10.0/tests/test_readme.py +55 -0
  41. incur_cli-0.10.0/tests/test_schema.py +288 -0
  42. incur_cli-0.10.0/tests/test_skills.py +267 -0
  43. incur_cli-0.10.0/tests/test_update.py +102 -0
  44. incur_cli-0.10.0/uv.lock +339 -0
@@ -0,0 +1,8 @@
1
+ .venv/
2
+ __pycache__/
3
+ .pytest_cache/
4
+ .ruff_cache/
5
+ .coverage
6
+ .coverage.*
7
+ htmlcov/
8
+ dist/
@@ -0,0 +1,376 @@
1
+ Metadata-Version: 2.5
2
+ Name: incur-cli
3
+ Version: 0.10.0
4
+ Summary: Build CLIs for agents and humans: TOON output, LLM manifests, skills, and shell completions.
5
+ Project-URL: Repository, https://github.com/wevm/incur
6
+ License-Expression: MIT
7
+ Keywords: agents,cli,llm,mcp,toon
8
+ Classifier: Environment :: Console
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Typing :: Typed
11
+ Requires-Python: >=3.11
12
+ Description-Content-Type: text/markdown
13
+
14
+ <picture>
15
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/wevm/incur/main/.github/logo-dark.svg">
16
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/wevm/incur/main/.github/logo-light.svg">
17
+ <img alt="incur" src="https://raw.githubusercontent.com/wevm/incur/main/.github/logo-light.svg" width="100%" height="140px">
18
+ </picture>
19
+
20
+ <br/>
21
+
22
+ <p align="center">
23
+ <a href="https://github.com/wevm/incur/blob/main/LICENSE">
24
+ <picture>
25
+ <source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/license/wevm/incur?colorA=21262d&colorB=21262d&style=flat">
26
+ <img src="https://img.shields.io/github/license/wevm/incur?colorA=f6f8fa&colorB=f6f8fa&style=flat" alt="MIT License">
27
+ </picture>
28
+ </a>
29
+ </p>
30
+
31
+ <p align="center">
32
+ <a href="#features">Features</a> · <a href="#install">Install</a> · <a href="#usage">Usage</a> · <a href="#walkthrough">Walkthrough</a> · <a href="#license">License</a>
33
+ </p>
34
+
35
+ [incur](https://github.com/wevm/incur) for Python: build CLIs for agents and humans, with output that matches the TypeScript package byte for byte.
36
+
37
+ ## Features
38
+
39
+ - [**Agent discovery**](#agent-discovery): built-in Skills and MCP sync (`skills add`, `mcp add`) so agents find your CLI automatically
40
+ - [**Call-to-actions**](#call-to-actions): suggest next commands to agents and humans after a run
41
+ - **TOON output**: token-efficient default format, with JSON, YAML, Markdown, and JSONL alternatives
42
+ - [**`--llms` flag**](#agent-discovery): token-efficient command manifest in Markdown or JSON schema
43
+ - **Well-formed I/O**: schema classes for arguments, options, environment variables, and output
44
+ - **Typed contexts**: `Context[Args, Options, Env, Vars, Globals]` types handlers and middleware under strict pyright
45
+ - [**Global options**](#global-options): `--format`, `--full-output`, `--help`, `--json`, `--update`, `--version` on every CLI for free
46
+ - [**Mount APIs**](#mount-apis): fetch handlers, OpenAPI documents, and remote MCP servers as commands
47
+ - [**Serve over HTTP**](#serve-over-http): every CLI is an ASGI app with an MCP endpoint
48
+ - [**Middleware**](#middleware-and-variables): composable before/after hooks with typed variables via `@cli.use`
49
+ - **Async-native**: handlers and middleware may be `async def`, and streams async generators
50
+ - **Zero dependencies**: standard library only
51
+
52
+ ## Install
53
+
54
+ ```bash
55
+ pip install incur-cli
56
+ ```
57
+
58
+ ## Usage
59
+
60
+ ### Single-command CLI
61
+
62
+ Register a root command with `@cli.root()` for CLIs that do one thing.
63
+
64
+ ```python
65
+ from typing import Annotated
66
+
67
+ import incur
68
+
69
+
70
+ @incur.schema
71
+ class Args:
72
+ name: Annotated[str, incur.field("Name to greet")]
73
+
74
+
75
+ cli = incur.Cli("greet", description="A greeting CLI")
76
+
77
+
78
+ @cli.root()
79
+ def greet(c: incur.Context[Args]) -> dict[str, str]:
80
+ return {"message": f"hello {c.args.name}"}
81
+
82
+
83
+ if __name__ == "__main__":
84
+ cli.serve()
85
+ ```
86
+
87
+ ```sh
88
+ $ greet world
89
+ # → message: hello world
90
+ ```
91
+
92
+ ### Multi-command CLI
93
+
94
+ Decorate a function per subcommand.
95
+
96
+ ```python
97
+ from typing import Annotated
98
+
99
+ import incur
100
+
101
+
102
+ @incur.schema
103
+ class InstallArgs:
104
+ package: Annotated[str | None, incur.field("Package name")] = None
105
+
106
+
107
+ @incur.schema(rename_all="camelCase")
108
+ class InstallOptions:
109
+ save_dev: Annotated[bool | None, incur.field("Save as dev dependency", alias="D")] = None
110
+
111
+
112
+ cli = incur.Cli("my-cli", description="My CLI")
113
+
114
+
115
+ @cli.command("status", description="Show repo status")
116
+ def status(c: incur.Context) -> dict[str, bool]:
117
+ return {"clean": True}
118
+
119
+
120
+ @cli.command("install", description="Install a package")
121
+ def install(c: incur.Context[InstallArgs, InstallOptions]) -> dict[str, int]:
122
+ return {"added": 1, "packages": 451}
123
+
124
+
125
+ if __name__ == "__main__":
126
+ cli.serve()
127
+ ```
128
+
129
+ ```sh
130
+ $ my-cli status
131
+ # → clean: true
132
+
133
+ $ my-cli install express -D
134
+ # → added: 1
135
+ # → packages: 451
136
+ ```
137
+
138
+ ### Sub-command CLI
139
+
140
+ Create a separate `Cli` and mount it with `cli.group(...)` to nest command groups.
141
+
142
+ ```python
143
+ from typing import Literal
144
+
145
+ import incur
146
+
147
+ cli = incur.Cli("my-cli", description="My CLI")
148
+ pr = incur.Cli("pr", description="Pull request commands")
149
+
150
+
151
+ @incur.schema
152
+ class ListOptions:
153
+ state: Literal["open", "closed", "all"] = "open"
154
+
155
+
156
+ @pr.command("list", description="List pull requests")
157
+ def list_prs(c: incur.Context[None, ListOptions]) -> dict[str, object]:
158
+ return {"prs": [], "state": c.options.state}
159
+
160
+
161
+ cli.group(pr)
162
+
163
+ if __name__ == "__main__":
164
+ cli.serve()
165
+ ```
166
+
167
+ ```sh
168
+ $ my-cli pr list --state closed
169
+ # → prs: []
170
+ # → state: closed
171
+ ```
172
+
173
+ ## Walkthrough
174
+
175
+ ### Agent discovery
176
+
177
+ Agents can only use your CLI if they know it exists. incur ships three built-in discovery mechanisms:
178
+
179
+ ```sh
180
+ # Generate and install agent skill files (recommended – lighter on tokens)
181
+ $ my-cli skills add
182
+
183
+ # Register as an MCP server for your agents
184
+ $ my-cli mcp add
185
+
186
+ # Print a machine-readable manifest
187
+ $ my-cli --llms
188
+ ```
189
+
190
+ `--mcp` serves the commands as MCP tools over stdio. Set `package=` when the CLI's npm package differs from its name, `mcp=incur.McpOptions(...)` to configure the server, and `sync=False` to drop the skills commands. See [Agent discovery](https://github.com/wevm/incur#agent-discovery).
191
+
192
+ ### Call-to-actions
193
+
194
+ Return `incur.ok(data, cta=...)` to tell agents which commands to run next. `incur.Error(code, message, cta=...)` carries suggestions too.
195
+
196
+ ```python
197
+ import incur
198
+
199
+ cli = incur.Cli("my-cli")
200
+
201
+
202
+ @cli.command("list")
203
+ def list_items(c: incur.Context) -> incur.Outcome:
204
+ items = [{"id": 1, "title": "Fix bug"}]
205
+ return incur.ok({"items": items}, cta=incur.CtaBlock([incur.Cta("get 1", "View item")]))
206
+ ```
207
+
208
+ ```sh
209
+ $ my-cli list
210
+ # → items[1]{id,title}:
211
+ # → 1,Fix bug
212
+ # → cta:
213
+ # → description: "Suggested command:"
214
+ # → commands[1]{command,description}:
215
+ # → my-cli get 1,View item
216
+ ```
217
+
218
+ ### Streaming
219
+
220
+ Generators and async generators stream chunks: one line each in TOON, one record each with `--format jsonl`.
221
+
222
+ ```python
223
+ from collections.abc import AsyncIterator
224
+
225
+ import incur
226
+
227
+ cli = incur.Cli("my-cli")
228
+
229
+
230
+ @cli.command("logs", description="Tail logs")
231
+ async def logs(c: incur.Context) -> AsyncIterator[str]:
232
+ for line in ("connecting...", "streaming logs", "done"):
233
+ yield line
234
+ ```
235
+
236
+ ```sh
237
+ $ my-cli logs
238
+ # → connecting...
239
+ # → streaming logs
240
+ # → done
241
+ ```
242
+
243
+ ### Middleware and variables
244
+
245
+ `@cli.use` registers onion-style middleware that awaits `next_()`. Declare `vars` as a schema class whose fields have defaults: each run gets a fresh, typed instance as `c.var`.
246
+
247
+ ```python
248
+ import incur
249
+
250
+
251
+ @incur.schema
252
+ class Vars:
253
+ user: str = "anonymous"
254
+
255
+
256
+ cli = incur.Cli("my-cli", vars=Vars)
257
+
258
+
259
+ @cli.use
260
+ async def authenticate(c: incur.MiddlewareContext[Vars], next_: incur.Next) -> None:
261
+ c.var.user = "alice"
262
+ await next_()
263
+
264
+
265
+ @cli.command("whoami")
266
+ def whoami(c: incur.Context[None, None, None, Vars]) -> dict[str, str]:
267
+ return {"user": c.var.user}
268
+ ```
269
+
270
+ ```sh
271
+ $ my-cli whoami
272
+ # → user: alice
273
+ ```
274
+
275
+ Raise `incur.Error` to stop the chain. See [Middleware](https://github.com/wevm/incur#middleware).
276
+
277
+ ### Global options
278
+
279
+ Every CLI accepts `--filter-output`, `--format`, `--full-output`, `--help`, `--json`, `--llms`, `--mcp`, `--schema`, `--token-count`, `--token-limit`, `--token-offset`, `--update`, and `--version`. Declare your own with `globals=`, a schema class every handler reads as `c.globals`.
280
+
281
+ ### Config file
282
+
283
+ `config=incur.Config(flag="config", files=["my-cli.json"])` loads option defaults from `{"commands": {"<name>": {"options": {...}}}}`; a `loader` reads other formats. Precedence is argv, then config, then schema defaults. See [Config file](https://github.com/wevm/incur#config-file).
284
+
285
+ ### Mount APIs
286
+
287
+ `cli.gateway(name, fetch)` forwards curl-style arguments to a fetch handler: any function from `incur.Request` to `incur.Response`, plain or `async def`. `incur.from_request(url)` forwards to a hosted API.
288
+
289
+ ```python
290
+ import incur
291
+
292
+
293
+ def app(request: incur.Request) -> incur.Response:
294
+ if request.method == "POST":
295
+ return incur.Response.json({"created": True, **request.json()}, status=201)
296
+ return incur.Response.json({"users": [{"id": 1, "name": "Alice"}]})
297
+
298
+
299
+ cli = incur.Cli("my-cli")
300
+ cli.gateway("api", app)
301
+ ```
302
+
303
+ ```sh
304
+ $ my-cli api users
305
+ # → users[1]{id,name}:
306
+ # → 1,Alice
307
+
308
+ $ my-cli api users -X POST -d '{"name":"Bob"}'
309
+ # → created: true
310
+ # → name: Bob
311
+ ```
312
+
313
+ With `openapi=` (a document or its URL), each operation becomes a typed command; `openapi_config=incur.OpenapiConfig(mode="namespace")` nests them by path. `cli.mcp_source` turns a remote MCP server's tools into commands:
314
+
315
+ ```python
316
+ import incur
317
+
318
+ cli = incur.Cli("my-cli")
319
+ cli.gateway("pets", incur.from_request("https://petstore.example/v1/"), openapi="openapi.json")
320
+ cli.mcp_source("docs", "https://mcp.tempo.xyz/mcp")
321
+ ```
322
+
323
+ See [Mount APIs as CLIs](https://github.com/wevm/incur#mount-apis-as-clis).
324
+
325
+ ### Serve over HTTP
326
+
327
+ `await cli.fetch(request)` runs the command a path names, and `cli.asgi` is the same as an ASGI 3 app (`uvicorn app:cli.asgi`):
328
+
329
+ ```
330
+ GET /users?limit=5 → my-cli users --limit 5
331
+ POST /users {"name": "Bob"} → my-cli users --name Bob
332
+ POST /mcp → MCP over HTTP
333
+ GET /openapi.json → the commands as an OpenAPI document
334
+ GET /.well-known/agent-skills/index.json → Agent Skills discovery
335
+ ```
336
+
337
+ Responses use the `--full-output --format json` envelope, and streams are NDJSON. See [Serve CLIs as APIs](https://github.com/wevm/incur#serve-clis-as-apis).
338
+
339
+ ### Update notices
340
+
341
+ In a terminal, CLIs installed from npm check for updates once a day in a detached process and suggest `my-cli --update`. `update=incur.Update(check=..., install=...)` supplies custom checks and installers, and `update=False` disables both. See [Update notices](https://github.com/wevm/incur#update-notices).
342
+
343
+ ### Filtering and token pagination
344
+
345
+ `--filter-output` prunes output to key paths, and `--token-count`, `--token-limit`, and `--token-offset` page through large outputs.
346
+
347
+ ```python
348
+ import incur
349
+
350
+ cli = incur.Cli("my-cli")
351
+
352
+
353
+ @cli.command("users")
354
+ def users(c: incur.Context) -> dict[str, object]:
355
+ return {"users": [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "user"}]}
356
+ ```
357
+
358
+ ```sh
359
+ $ my-cli users --filter-output users[0,2].name
360
+ # → users[2]{name}:
361
+ # → Alice
362
+ # → Bob
363
+
364
+ $ my-cli users --token-count
365
+ # → 14
366
+ ```
367
+
368
+ ## Differences from TypeScript
369
+
370
+ - Integers in data keep their exact value in every format, where JavaScript rounds beyond 2^53.
371
+ - JSON Schema string formats other than `date-time` (such as `email` or `uuid`) are not validated.
372
+ - Standalone binaries and file-based commands are not ported.
373
+
374
+ ## License
375
+
376
+ [MIT](https://github.com/wevm/incur/blob/main/LICENSE)