fastapi-apcore 0.3.2__tar.gz → 0.5.1__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.
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/PKG-INFO +69 -12
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/README.md +64 -7
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/pyproject.toml +12 -5
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/cli.py +16 -4
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/client.py +150 -13
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/shortcuts.py +12 -4
- fastapi_apcore-0.5.1/src/fastapi_apcore/governance.py +105 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/output/registry_writer.py +17 -28
- fastapi_apcore-0.5.1/src/fastapi_apcore/scanners/base.py +202 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/scanners/native.py +34 -24
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/scanners/openapi.py +30 -9
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/PKG-INFO +69 -12
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/SOURCES.txt +3 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/requires.txt +4 -4
- fastapi_apcore-0.5.1/tests/test_acl_demo.py +58 -0
- fastapi_apcore-0.5.1/tests/test_client.py +598 -0
- fastapi_apcore-0.5.1/tests/test_governance.py +85 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_integration.py +25 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_scanner.py +137 -1
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_writers.py +68 -0
- fastapi_apcore-0.3.2/src/fastapi_apcore/scanners/base.py +0 -114
- fastapi_apcore-0.3.2/tests/test_client.py +0 -238
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/setup.cfg +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/__init__.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/__init__.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/config.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/context.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/extensions.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/observability.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/registry.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/serializers.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/engine/tasks.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/output/__init__.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/output/yaml_writer.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/py.typed +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore/scanners/__init__.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/dependency_links.txt +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/entry_points.txt +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/src/fastapi_apcore.egg-info/top_level.txt +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_config.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_context.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_extensions.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_observability.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_public_api.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_registry.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_serializers.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_shortcuts.py +0 -0
- {fastapi_apcore-0.3.2 → fastapi_apcore-0.5.1}/tests/test_tasks.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastapi-apcore
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: FastAPI integration for apcore AI-Perceivable Core — exposes FastAPI routes as apcore modules with auto-discovery, schema extraction from Pydantic models, and OpenAPI-based scanning.
|
|
5
5
|
Author-email: aiperceivable <team@aiperceivable.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -10,12 +10,12 @@ Keywords: fastapi,apcore,mcp,llm,ai,tools,pydantic,schema,module,cli,auto-discov
|
|
|
10
10
|
Requires-Python: >=3.11
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
12
12
|
Requires-Dist: fastapi>=0.100
|
|
13
|
-
Requires-Dist: apcore>=0.
|
|
14
|
-
Requires-Dist: apcore-toolkit>=0.
|
|
13
|
+
Requires-Dist: apcore>=0.32.2
|
|
14
|
+
Requires-Dist: apcore-toolkit>=0.14.1
|
|
15
15
|
Provides-Extra: mcp
|
|
16
|
-
Requires-Dist: apcore-mcp>=0.
|
|
16
|
+
Requires-Dist: apcore-mcp>=0.23.3; extra == "mcp"
|
|
17
17
|
Provides-Extra: cli
|
|
18
|
-
Requires-Dist: apcore-cli>=0.
|
|
18
|
+
Requires-Dist: apcore-cli>=0.12.6; extra == "cli"
|
|
19
19
|
Requires-Dist: click>=8.0; extra == "cli"
|
|
20
20
|
Provides-Extra: all
|
|
21
21
|
Requires-Dist: fastapi-apcore[cli,mcp]; extra == "all"
|
|
@@ -38,7 +38,8 @@ FastAPI integration for [apcore](https://github.com/aiperceivable/apcore-python)
|
|
|
38
38
|
- **Annotation inference** -- `GET` -> readonly+cacheable, `DELETE` -> destructive, `PUT` -> idempotent
|
|
39
39
|
- **Pydantic schema extraction** -- input/output schemas extracted from Pydantic models and OpenAPI spec
|
|
40
40
|
- **Two scanner backends** -- OpenAPI-based (accurate) and native route inspection (fast)
|
|
41
|
-
- **
|
|
41
|
+
- **Display overlay** -- `binding_path=` applies a sparse `binding.yaml` display overlay (§5.13) to control CLI/MCP/A2A surface aliases, descriptions, and guidance without touching code
|
|
42
|
+
- **Simplified module IDs** -- `simplify_ids=True` extracts clean function names from FastAPI operationIds *(deprecated — use `binding_path` with `display.cli.alias` instead)*
|
|
42
43
|
- **`@module` decorator** -- define standalone AI-callable modules with full schema enforcement
|
|
43
44
|
- **YAML binding** -- zero-code module definitions via external `.binding.yaml` files
|
|
44
45
|
- **MCP server** -- stdio, streamable-http, and SSE transports via `fastapi-apcore serve`
|
|
@@ -58,8 +59,10 @@ FastAPI integration for [apcore](https://github.com/aiperceivable/apcore-python)
|
|
|
58
59
|
|
|
59
60
|
- Python >= 3.11
|
|
60
61
|
- FastAPI >= 0.100
|
|
61
|
-
- apcore >= 0.
|
|
62
|
-
- apcore-toolkit >= 0.
|
|
62
|
+
- apcore >= 0.32.2
|
|
63
|
+
- apcore-toolkit >= 0.14.1
|
|
64
|
+
- apcore-mcp >= 0.23.3 (optional, `[mcp]` extra)
|
|
65
|
+
- apcore-cli >= 0.12.6 (optional, `[cli]` extra)
|
|
63
66
|
|
|
64
67
|
## Installation
|
|
65
68
|
|
|
@@ -288,7 +291,8 @@ apcore.create_mcp_server(
|
|
|
288
291
|
app,
|
|
289
292
|
transport="streamable-http",
|
|
290
293
|
port=9090,
|
|
291
|
-
|
|
294
|
+
binding_path="bindings/", # apply display overlay (§5.13) — sets MCP tool names/descriptions
|
|
295
|
+
commands_dir="commands/", # convention-scanned plain functions (§5.14)
|
|
292
296
|
explorer=True,
|
|
293
297
|
)
|
|
294
298
|
|
|
@@ -301,9 +305,13 @@ apcore.create_mcp_server(
|
|
|
301
305
|
)
|
|
302
306
|
```
|
|
303
307
|
|
|
308
|
+
`binding_path` accepts a single `.binding.yaml` file or a directory of `*.binding.yaml` files. When set, `DisplayResolver` resolves `metadata["display"]["mcp"]` on all scanned modules before they are registered, so MCP tool names and descriptions reflect the overlay.
|
|
309
|
+
|
|
310
|
+
`commands_dir` points to a directory of plain Python function files. When set, `ConventionScanner` from `apcore-toolkit` scans for public functions and registers them as additional modules alongside the scanned API routes (§5.14).
|
|
311
|
+
|
|
304
312
|
## `create_cli()` Reference
|
|
305
313
|
|
|
306
|
-
Generate a Click CLI group that proxies to your running FastAPI server:
|
|
314
|
+
Generate a Click CLI group that proxies to your running FastAPI server. Commands are auto-grouped by namespace prefix using `GroupedModuleGroup` (e.g., `myapp-cli product list` instead of `myapp-cli product.list`):
|
|
307
315
|
|
|
308
316
|
```python
|
|
309
317
|
from fastapi_apcore import FastAPIApcore
|
|
@@ -314,7 +322,8 @@ cli = apcore.create_cli(
|
|
|
314
322
|
app,
|
|
315
323
|
prog_name="myapp-cli",
|
|
316
324
|
base_url="http://localhost:8000",
|
|
317
|
-
|
|
325
|
+
binding_path="bindings/", # apply display overlay (§5.13) — sets CLI command names/descriptions
|
|
326
|
+
commands_dir="commands/", # convention-scanned plain functions (§5.14)
|
|
318
327
|
max_content_width=160, # wider help output for long command names
|
|
319
328
|
)
|
|
320
329
|
|
|
@@ -322,7 +331,32 @@ if __name__ == "__main__":
|
|
|
322
331
|
cli(standalone_mode=True)
|
|
323
332
|
```
|
|
324
333
|
|
|
325
|
-
Each scanned route becomes a CLI command. The commands forward HTTP requests to the running API using `HTTPProxyRegistryWriter`. Built-in subcommands include `list`, `describe`, `completion`, and `man`.
|
|
334
|
+
Each scanned route becomes a CLI command named after `display.cli.alias` from the binding overlay (or the module_id if no overlay is set). The commands forward HTTP requests to the running API using `HTTPProxyRegistryWriter`. Built-in subcommands include `list`, `describe`, `completion`, and `man`.
|
|
335
|
+
|
|
336
|
+
`commands_dir` points to a directory of plain Python function files. When set, `ConventionScanner` from `apcore-toolkit` scans for public functions and registers them as additional modules alongside the scanned API routes (§5.14).
|
|
337
|
+
|
|
338
|
+
### Display Overlay (`binding_path`)
|
|
339
|
+
|
|
340
|
+
Control how each route appears to CLI users and MCP clients without changing your FastAPI code:
|
|
341
|
+
|
|
342
|
+
```yaml
|
|
343
|
+
# bindings/myapp.binding.yaml
|
|
344
|
+
bindings:
|
|
345
|
+
- module_id: product.get_product_product__product_id_.get
|
|
346
|
+
display:
|
|
347
|
+
alias: product-get # base alias for all surfaces
|
|
348
|
+
description: Get a product by its ID
|
|
349
|
+
cli:
|
|
350
|
+
alias: get-product # CLI command name: myapp-cli get-product
|
|
351
|
+
mcp:
|
|
352
|
+
alias: get_product # MCP tool name (must match [a-zA-Z_][a-zA-Z0-9_-]*)
|
|
353
|
+
guidance: |
|
|
354
|
+
Always pass product_id as a UUID string, not an integer.
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Resolution chain per field: surface-specific override > `display` default > scanner value.
|
|
358
|
+
|
|
359
|
+
> **Note:** `simplify_ids=True` is deprecated. Use `binding_path` with `display.cli.alias` / `display.mcp.alias` in `binding.yaml` instead.
|
|
326
360
|
|
|
327
361
|
## Configuration
|
|
328
362
|
|
|
@@ -362,6 +396,29 @@ All settings are read from environment variables with the `APCORE_` prefix:
|
|
|
362
396
|
| `APCORE_ACL_PATH` | — | Path to ACL YAML rules file |
|
|
363
397
|
| `APCORE_MIDDLEWARES` | — | Comma-separated middleware dotted paths |
|
|
364
398
|
|
|
399
|
+
#### Access control (ACL)
|
|
400
|
+
|
|
401
|
+
Set `APCORE_ACL_PATH` to a YAML rules file and apcore enforces role-based
|
|
402
|
+
`allow`/`deny` on every module call. Roles come from the request's
|
|
403
|
+
authenticated user: `FastAPIContextFactory` reads `request.state.user`
|
|
404
|
+
(`roles` / `groups` / `scopes`) into the apcore `Identity` that ACL conditions
|
|
405
|
+
match against. A denied call raises `ACLDeniedError`, which your route maps to
|
|
406
|
+
HTTP 403.
|
|
407
|
+
|
|
408
|
+
```yaml
|
|
409
|
+
# acl.yaml
|
|
410
|
+
default_effect: deny
|
|
411
|
+
rules:
|
|
412
|
+
- description: Admins may call any module
|
|
413
|
+
callers: ["*"]
|
|
414
|
+
targets: ["*"]
|
|
415
|
+
effect: allow
|
|
416
|
+
conditions: { roles: ["admin"] }
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
See **[`examples/acl_demo/`](examples/acl_demo/)** for a runnable FastAPI app
|
|
420
|
+
plus integration tests (`tests/test_acl_demo.py`).
|
|
421
|
+
|
|
365
422
|
### Observability
|
|
366
423
|
|
|
367
424
|
| Variable | Default | Description |
|
|
@@ -8,7 +8,8 @@ FastAPI integration for [apcore](https://github.com/aiperceivable/apcore-python)
|
|
|
8
8
|
- **Annotation inference** -- `GET` -> readonly+cacheable, `DELETE` -> destructive, `PUT` -> idempotent
|
|
9
9
|
- **Pydantic schema extraction** -- input/output schemas extracted from Pydantic models and OpenAPI spec
|
|
10
10
|
- **Two scanner backends** -- OpenAPI-based (accurate) and native route inspection (fast)
|
|
11
|
-
- **
|
|
11
|
+
- **Display overlay** -- `binding_path=` applies a sparse `binding.yaml` display overlay (§5.13) to control CLI/MCP/A2A surface aliases, descriptions, and guidance without touching code
|
|
12
|
+
- **Simplified module IDs** -- `simplify_ids=True` extracts clean function names from FastAPI operationIds *(deprecated — use `binding_path` with `display.cli.alias` instead)*
|
|
12
13
|
- **`@module` decorator** -- define standalone AI-callable modules with full schema enforcement
|
|
13
14
|
- **YAML binding** -- zero-code module definitions via external `.binding.yaml` files
|
|
14
15
|
- **MCP server** -- stdio, streamable-http, and SSE transports via `fastapi-apcore serve`
|
|
@@ -28,8 +29,10 @@ FastAPI integration for [apcore](https://github.com/aiperceivable/apcore-python)
|
|
|
28
29
|
|
|
29
30
|
- Python >= 3.11
|
|
30
31
|
- FastAPI >= 0.100
|
|
31
|
-
- apcore >= 0.
|
|
32
|
-
- apcore-toolkit >= 0.
|
|
32
|
+
- apcore >= 0.32.2
|
|
33
|
+
- apcore-toolkit >= 0.14.1
|
|
34
|
+
- apcore-mcp >= 0.23.3 (optional, `[mcp]` extra)
|
|
35
|
+
- apcore-cli >= 0.12.6 (optional, `[cli]` extra)
|
|
33
36
|
|
|
34
37
|
## Installation
|
|
35
38
|
|
|
@@ -258,7 +261,8 @@ apcore.create_mcp_server(
|
|
|
258
261
|
app,
|
|
259
262
|
transport="streamable-http",
|
|
260
263
|
port=9090,
|
|
261
|
-
|
|
264
|
+
binding_path="bindings/", # apply display overlay (§5.13) — sets MCP tool names/descriptions
|
|
265
|
+
commands_dir="commands/", # convention-scanned plain functions (§5.14)
|
|
262
266
|
explorer=True,
|
|
263
267
|
)
|
|
264
268
|
|
|
@@ -271,9 +275,13 @@ apcore.create_mcp_server(
|
|
|
271
275
|
)
|
|
272
276
|
```
|
|
273
277
|
|
|
278
|
+
`binding_path` accepts a single `.binding.yaml` file or a directory of `*.binding.yaml` files. When set, `DisplayResolver` resolves `metadata["display"]["mcp"]` on all scanned modules before they are registered, so MCP tool names and descriptions reflect the overlay.
|
|
279
|
+
|
|
280
|
+
`commands_dir` points to a directory of plain Python function files. When set, `ConventionScanner` from `apcore-toolkit` scans for public functions and registers them as additional modules alongside the scanned API routes (§5.14).
|
|
281
|
+
|
|
274
282
|
## `create_cli()` Reference
|
|
275
283
|
|
|
276
|
-
Generate a Click CLI group that proxies to your running FastAPI server:
|
|
284
|
+
Generate a Click CLI group that proxies to your running FastAPI server. Commands are auto-grouped by namespace prefix using `GroupedModuleGroup` (e.g., `myapp-cli product list` instead of `myapp-cli product.list`):
|
|
277
285
|
|
|
278
286
|
```python
|
|
279
287
|
from fastapi_apcore import FastAPIApcore
|
|
@@ -284,7 +292,8 @@ cli = apcore.create_cli(
|
|
|
284
292
|
app,
|
|
285
293
|
prog_name="myapp-cli",
|
|
286
294
|
base_url="http://localhost:8000",
|
|
287
|
-
|
|
295
|
+
binding_path="bindings/", # apply display overlay (§5.13) — sets CLI command names/descriptions
|
|
296
|
+
commands_dir="commands/", # convention-scanned plain functions (§5.14)
|
|
288
297
|
max_content_width=160, # wider help output for long command names
|
|
289
298
|
)
|
|
290
299
|
|
|
@@ -292,7 +301,32 @@ if __name__ == "__main__":
|
|
|
292
301
|
cli(standalone_mode=True)
|
|
293
302
|
```
|
|
294
303
|
|
|
295
|
-
Each scanned route becomes a CLI command. The commands forward HTTP requests to the running API using `HTTPProxyRegistryWriter`. Built-in subcommands include `list`, `describe`, `completion`, and `man`.
|
|
304
|
+
Each scanned route becomes a CLI command named after `display.cli.alias` from the binding overlay (or the module_id if no overlay is set). The commands forward HTTP requests to the running API using `HTTPProxyRegistryWriter`. Built-in subcommands include `list`, `describe`, `completion`, and `man`.
|
|
305
|
+
|
|
306
|
+
`commands_dir` points to a directory of plain Python function files. When set, `ConventionScanner` from `apcore-toolkit` scans for public functions and registers them as additional modules alongside the scanned API routes (§5.14).
|
|
307
|
+
|
|
308
|
+
### Display Overlay (`binding_path`)
|
|
309
|
+
|
|
310
|
+
Control how each route appears to CLI users and MCP clients without changing your FastAPI code:
|
|
311
|
+
|
|
312
|
+
```yaml
|
|
313
|
+
# bindings/myapp.binding.yaml
|
|
314
|
+
bindings:
|
|
315
|
+
- module_id: product.get_product_product__product_id_.get
|
|
316
|
+
display:
|
|
317
|
+
alias: product-get # base alias for all surfaces
|
|
318
|
+
description: Get a product by its ID
|
|
319
|
+
cli:
|
|
320
|
+
alias: get-product # CLI command name: myapp-cli get-product
|
|
321
|
+
mcp:
|
|
322
|
+
alias: get_product # MCP tool name (must match [a-zA-Z_][a-zA-Z0-9_-]*)
|
|
323
|
+
guidance: |
|
|
324
|
+
Always pass product_id as a UUID string, not an integer.
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Resolution chain per field: surface-specific override > `display` default > scanner value.
|
|
328
|
+
|
|
329
|
+
> **Note:** `simplify_ids=True` is deprecated. Use `binding_path` with `display.cli.alias` / `display.mcp.alias` in `binding.yaml` instead.
|
|
296
330
|
|
|
297
331
|
## Configuration
|
|
298
332
|
|
|
@@ -332,6 +366,29 @@ All settings are read from environment variables with the `APCORE_` prefix:
|
|
|
332
366
|
| `APCORE_ACL_PATH` | — | Path to ACL YAML rules file |
|
|
333
367
|
| `APCORE_MIDDLEWARES` | — | Comma-separated middleware dotted paths |
|
|
334
368
|
|
|
369
|
+
#### Access control (ACL)
|
|
370
|
+
|
|
371
|
+
Set `APCORE_ACL_PATH` to a YAML rules file and apcore enforces role-based
|
|
372
|
+
`allow`/`deny` on every module call. Roles come from the request's
|
|
373
|
+
authenticated user: `FastAPIContextFactory` reads `request.state.user`
|
|
374
|
+
(`roles` / `groups` / `scopes`) into the apcore `Identity` that ACL conditions
|
|
375
|
+
match against. A denied call raises `ACLDeniedError`, which your route maps to
|
|
376
|
+
HTTP 403.
|
|
377
|
+
|
|
378
|
+
```yaml
|
|
379
|
+
# acl.yaml
|
|
380
|
+
default_effect: deny
|
|
381
|
+
rules:
|
|
382
|
+
- description: Admins may call any module
|
|
383
|
+
callers: ["*"]
|
|
384
|
+
targets: ["*"]
|
|
385
|
+
effect: allow
|
|
386
|
+
conditions: { roles: ["admin"] }
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
See **[`examples/acl_demo/`](examples/acl_demo/)** for a runnable FastAPI app
|
|
390
|
+
plus integration tests (`tests/test_acl_demo.py`).
|
|
391
|
+
|
|
335
392
|
### Observability
|
|
336
393
|
|
|
337
394
|
| Variable | Default | Description |
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "fastapi-apcore"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.5.1"
|
|
8
8
|
description = "FastAPI integration for apcore AI-Perceivable Core — exposes FastAPI routes as apcore modules with auto-discovery, schema extraction from Pydantic models, and OpenAPI-based scanning."
|
|
9
9
|
requires-python = ">=3.11"
|
|
10
10
|
readme = "README.md"
|
|
@@ -28,13 +28,13 @@ keywords = [
|
|
|
28
28
|
]
|
|
29
29
|
dependencies = [
|
|
30
30
|
"fastapi>=0.100",
|
|
31
|
-
"apcore>=0.
|
|
32
|
-
"apcore-toolkit>=0.
|
|
31
|
+
"apcore>=0.32.2",
|
|
32
|
+
"apcore-toolkit>=0.14.1",
|
|
33
33
|
]
|
|
34
34
|
|
|
35
35
|
[project.optional-dependencies]
|
|
36
|
-
mcp = ["apcore-mcp>=0.
|
|
37
|
-
cli = ["apcore-cli>=0.
|
|
36
|
+
mcp = ["apcore-mcp>=0.23.3"]
|
|
37
|
+
cli = ["apcore-cli>=0.12.6", "click>=8.0"]
|
|
38
38
|
all = ["fastapi-apcore[mcp,cli]"]
|
|
39
39
|
dev = [
|
|
40
40
|
"apdev[dev]>=0.2.3",
|
|
@@ -64,6 +64,13 @@ src = ["src"]
|
|
|
64
64
|
target-version = "py311"
|
|
65
65
|
line-length = 120
|
|
66
66
|
|
|
67
|
+
[tool.ruff.lint]
|
|
68
|
+
# Pin the rule set explicitly. Ruff's implicit default selection changes between
|
|
69
|
+
# releases (0.16 widened it from E4/E7/E9/F to a much larger set), which made CI
|
|
70
|
+
# fail on a newer ruff while an older local ruff passed. Widening the selection
|
|
71
|
+
# is a deliberate change, not a side effect of upgrading the linter.
|
|
72
|
+
select = ["E4", "E7", "E9", "F"]
|
|
73
|
+
|
|
67
74
|
[tool.mypy]
|
|
68
75
|
python_version = "3.11"
|
|
69
76
|
strict = true
|
|
@@ -136,6 +136,12 @@ def create_cli() -> Any:
|
|
|
136
136
|
tags: str = typer.Option(None, "--tags", help="Comma-separated tags filter."),
|
|
137
137
|
prefix: str = typer.Option(None, "--prefix", help="Module ID prefix filter."),
|
|
138
138
|
log_level: str = typer.Option(None, "--log-level", help="Log level: DEBUG, INFO, WARNING, ERROR."),
|
|
139
|
+
policy: str = typer.Option(
|
|
140
|
+
None,
|
|
141
|
+
"--policy",
|
|
142
|
+
help="Path to an external governance policy YAML (marks requires_approval / destructive "
|
|
143
|
+
"on matching modules without touching the app's source).",
|
|
144
|
+
),
|
|
139
145
|
) -> None:
|
|
140
146
|
"""Start an MCP server exposing registered apcore modules as tools."""
|
|
141
147
|
app = _load_app(app_path)
|
|
@@ -144,7 +150,7 @@ def create_cli() -> Any:
|
|
|
144
150
|
from fastapi_apcore.client import FastAPIApcore
|
|
145
151
|
|
|
146
152
|
apcore = FastAPIApcore.get_instance()
|
|
147
|
-
apcore.init_app(app)
|
|
153
|
+
apcore.init_app(app, policy_path=policy)
|
|
148
154
|
|
|
149
155
|
from fastapi_apcore.engine.config import get_apcore_settings
|
|
150
156
|
from fastapi_apcore.engine.registry import get_executor, get_registry, get_metrics_collector
|
|
@@ -217,8 +223,11 @@ def create_cli() -> Any:
|
|
|
217
223
|
use_executor = bool(settings.middlewares or settings.acl_path or settings.executor_config)
|
|
218
224
|
registry_or_executor = get_executor() if use_executor else registry
|
|
219
225
|
|
|
220
|
-
|
|
221
|
-
|
|
226
|
+
# Diagnostics MUST go to stderr: under the stdio transport, stdout is the
|
|
227
|
+
# MCP JSON-RPC stream and any stray text corrupts it (strict clients like
|
|
228
|
+
# Claude Desktop reject the whole stream).
|
|
229
|
+
typer.echo(f"[fastapi-apcore] Starting MCP server '{name}' via {transport}...", err=True)
|
|
230
|
+
typer.echo(f"[fastapi-apcore] {registry.count} modules registered.", err=True)
|
|
222
231
|
|
|
223
232
|
try:
|
|
224
233
|
from apcore_mcp import serve as mcp_serve
|
|
@@ -339,6 +348,8 @@ def create_cli() -> Any:
|
|
|
339
348
|
max_age: int = typer.Option(None, "--max-age", help="Max age in seconds for completed tasks."),
|
|
340
349
|
) -> None:
|
|
341
350
|
"""Clean up old completed tasks."""
|
|
351
|
+
import asyncio
|
|
352
|
+
|
|
342
353
|
from fastapi_apcore.engine.config import get_apcore_settings
|
|
343
354
|
from fastapi_apcore.engine.tasks import get_task_manager
|
|
344
355
|
|
|
@@ -346,7 +357,8 @@ def create_cli() -> Any:
|
|
|
346
357
|
max_age = get_apcore_settings().task_cleanup_age
|
|
347
358
|
|
|
348
359
|
tm = get_task_manager()
|
|
349
|
-
|
|
360
|
+
# apcore >= 0.22 made AsyncTaskManager.cleanup() a coroutine.
|
|
361
|
+
count = asyncio.run(tm.cleanup(max_age_seconds=max_age))
|
|
350
362
|
typer.echo(f"Cleaned up {count} tasks.")
|
|
351
363
|
|
|
352
364
|
return cli
|
|
@@ -30,7 +30,9 @@ Usage::
|
|
|
30
30
|
from __future__ import annotations
|
|
31
31
|
|
|
32
32
|
import logging
|
|
33
|
+
import sys
|
|
33
34
|
import threading
|
|
35
|
+
import warnings
|
|
34
36
|
from typing import TYPE_CHECKING, Any
|
|
35
37
|
|
|
36
38
|
if TYPE_CHECKING:
|
|
@@ -71,6 +73,7 @@ class FastAPIApcore:
|
|
|
71
73
|
simplify_ids: bool = False,
|
|
72
74
|
include: str | None = None,
|
|
73
75
|
exclude: str | None = None,
|
|
76
|
+
policy_path: str | None = None,
|
|
74
77
|
) -> None:
|
|
75
78
|
"""Initialize fastapi-apcore with a FastAPI application.
|
|
76
79
|
|
|
@@ -115,6 +118,14 @@ class FastAPIApcore:
|
|
|
115
118
|
from fastapi_apcore.output import get_writer
|
|
116
119
|
|
|
117
120
|
scanned = self.scan(app, source=scan_source, simplify_ids=simplify_ids, include=include, exclude=exclude)
|
|
121
|
+
# Apply external governance policy (marks requires_approval /
|
|
122
|
+
# destructive) before registration so approval gating reflects the
|
|
123
|
+
# operator's decision — without modifying the target app's source.
|
|
124
|
+
effective_policy = policy_path or getattr(settings, "policy_path", None)
|
|
125
|
+
if scanned and effective_policy:
|
|
126
|
+
from fastapi_apcore.governance import apply_policy, load_policy
|
|
127
|
+
|
|
128
|
+
scanned = apply_policy(scanned, load_policy(effective_policy))
|
|
118
129
|
if scanned:
|
|
119
130
|
writer = get_writer(None) # RegistryWriter
|
|
120
131
|
writer.write(scanned, registry)
|
|
@@ -406,6 +417,33 @@ class FastAPIApcore:
|
|
|
406
417
|
**kwargs,
|
|
407
418
|
)
|
|
408
419
|
|
|
420
|
+
@staticmethod
|
|
421
|
+
def _apply_convention_modules(commands_dir: str, registry: Any) -> None:
|
|
422
|
+
"""Scan a commands directory and register convention modules into registry (§5.14).
|
|
423
|
+
|
|
424
|
+
Args:
|
|
425
|
+
commands_dir: Path to a directory of plain Python function files.
|
|
426
|
+
Must be a trusted, developer-controlled path — files are imported
|
|
427
|
+
and executed during scanning.
|
|
428
|
+
registry: The apcore Registry to register discovered modules into.
|
|
429
|
+
"""
|
|
430
|
+
try:
|
|
431
|
+
from apcore_toolkit.convention_scanner import ConventionScanner
|
|
432
|
+
from apcore_toolkit import RegistryWriter
|
|
433
|
+
|
|
434
|
+
conv_modules = ConventionScanner().scan(commands_dir)
|
|
435
|
+
if conv_modules:
|
|
436
|
+
RegistryWriter().write(conv_modules, registry)
|
|
437
|
+
logger.info(
|
|
438
|
+
"Convention scanner: registered %d modules from %s",
|
|
439
|
+
len(conv_modules),
|
|
440
|
+
commands_dir,
|
|
441
|
+
)
|
|
442
|
+
except ImportError:
|
|
443
|
+
logger.warning("apcore-toolkit not installed — convention module scanning unavailable")
|
|
444
|
+
except Exception as e:
|
|
445
|
+
logger.warning("Convention module scanning failed: %s", e)
|
|
446
|
+
|
|
409
447
|
def create_mcp_server(
|
|
410
448
|
self,
|
|
411
449
|
app: FastAPI | None = None,
|
|
@@ -416,6 +454,8 @@ class FastAPIApcore:
|
|
|
416
454
|
simplify_ids: bool = False,
|
|
417
455
|
include: str | None = None,
|
|
418
456
|
exclude: str | None = None,
|
|
457
|
+
binding_path: str | None = None,
|
|
458
|
+
commands_dir: str | None = None,
|
|
419
459
|
**serve_kwargs: Any,
|
|
420
460
|
) -> None:
|
|
421
461
|
"""Scan routes or discover modules, then start an MCP server.
|
|
@@ -446,6 +486,15 @@ class FastAPIApcore:
|
|
|
446
486
|
simplify_ids: Use simplified module IDs (function names only).
|
|
447
487
|
include: Regex pattern — only include matching module IDs.
|
|
448
488
|
exclude: Regex pattern — skip matching module IDs.
|
|
489
|
+
binding_path: Path to a ``.binding.yaml`` file or directory of
|
|
490
|
+
binding files. When provided, ``DisplayResolver`` applies
|
|
491
|
+
the ``display`` overlay (alias, description, guidance) to
|
|
492
|
+
scanned modules before they are registered (§5.13).
|
|
493
|
+
commands_dir: Path to a directory of plain Python function files.
|
|
494
|
+
When set, ``ConventionScanner`` from ``apcore-toolkit`` scans
|
|
495
|
+
for public functions and registers them as additional modules
|
|
496
|
+
alongside the scanned API routes (§5.14). Must be a trusted,
|
|
497
|
+
developer-controlled path — files are imported and executed.
|
|
449
498
|
**serve_kwargs: Passed directly to ``apcore_mcp.serve()``.
|
|
450
499
|
Common options: ``transport``, ``host``, ``port``, ``name``,
|
|
451
500
|
``authenticator``, ``require_auth``, ``approval_handler``,
|
|
@@ -460,6 +509,14 @@ class FastAPIApcore:
|
|
|
460
509
|
|
|
461
510
|
from apcore import Executor, Registry
|
|
462
511
|
|
|
512
|
+
if simplify_ids:
|
|
513
|
+
warnings.warn(
|
|
514
|
+
"simplify_ids=True is deprecated. Use binding_path with display.mcp.alias "
|
|
515
|
+
"in binding.yaml instead (§5.13 of the apcore PROTOCOL_SPEC).",
|
|
516
|
+
DeprecationWarning,
|
|
517
|
+
stacklevel=2,
|
|
518
|
+
)
|
|
519
|
+
|
|
463
520
|
registry = Registry(extensions_dir=extensions_dir) if extensions_dir else Registry()
|
|
464
521
|
|
|
465
522
|
# Discover modules from extensions_dir
|
|
@@ -477,11 +534,19 @@ class FastAPIApcore:
|
|
|
477
534
|
scanner = get_scanner(scan_source, simplify_ids=simplify_ids)
|
|
478
535
|
scanned = scanner.scan(app, include=include, exclude=exclude)
|
|
479
536
|
if scanned:
|
|
537
|
+
if binding_path is not None:
|
|
538
|
+
from apcore_toolkit.display import DisplayResolver
|
|
539
|
+
|
|
540
|
+
scanned = DisplayResolver().resolve(scanned, binding_path=binding_path)
|
|
480
541
|
writer = get_writer(None) # FastAPIRegistryWriter
|
|
481
542
|
writer.write(scanned, registry)
|
|
482
543
|
logger.info("Scanned and registered %d routes as MCP tools", len(scanned))
|
|
483
544
|
|
|
484
|
-
|
|
545
|
+
# Convention module discovery (§5.14)
|
|
546
|
+
if commands_dir is not None:
|
|
547
|
+
self._apply_convention_modules(commands_dir, registry)
|
|
548
|
+
|
|
549
|
+
if not scan and not extensions_dir and not commands_dir:
|
|
485
550
|
logger.warning(
|
|
486
551
|
"create_mcp_server called with scan=False and no extensions_dir — "
|
|
487
552
|
"MCP server will have no tools registered"
|
|
@@ -532,8 +597,12 @@ class FastAPIApcore:
|
|
|
532
597
|
scan_source: str = "openapi",
|
|
533
598
|
include: str | None = None,
|
|
534
599
|
exclude: str | None = None,
|
|
600
|
+
binding_path: str | None = None,
|
|
601
|
+
commands_dir: str | None = None,
|
|
535
602
|
help_text_max_length: int = 1000,
|
|
536
603
|
max_content_width: int | None = None,
|
|
604
|
+
docs_url: str | None = None,
|
|
605
|
+
verbose_help: bool = False,
|
|
537
606
|
) -> Any:
|
|
538
607
|
"""Create an apcore-cli Click group with all API routes as commands.
|
|
539
608
|
|
|
@@ -552,11 +621,28 @@ class FastAPIApcore:
|
|
|
552
621
|
scan_source: Scanner backend ('openapi' or 'native').
|
|
553
622
|
include: Regex pattern — only include matching module IDs.
|
|
554
623
|
exclude: Regex pattern — skip matching module IDs.
|
|
624
|
+
binding_path: Path to a ``.binding.yaml`` file or directory of
|
|
625
|
+
binding files. When provided, ``DisplayResolver`` applies
|
|
626
|
+
the ``display`` overlay (alias, description, guidance) to
|
|
627
|
+
scanned modules before CLI commands are built (§5.13).
|
|
628
|
+
commands_dir: Path to a directory of plain Python function files.
|
|
629
|
+
When set, ``ConventionScanner`` from ``apcore-toolkit`` scans
|
|
630
|
+
for public functions and registers them as additional modules
|
|
631
|
+
alongside the scanned API routes (§5.14). Must be a trusted,
|
|
632
|
+
developer-controlled path — files are imported and executed.
|
|
555
633
|
help_text_max_length: Max characters for CLI help text per
|
|
556
634
|
command. Defaults to 1000.
|
|
557
635
|
max_content_width: Maximum width for CLI help output. When set,
|
|
558
636
|
overrides Click's default (terminal width, max 80). Useful
|
|
559
637
|
when command names are long and truncate descriptions.
|
|
638
|
+
docs_url: Base URL for online documentation (e.g.
|
|
639
|
+
``"https://docs.example.com/cli"``). When set, the URL is
|
|
640
|
+
embedded in ``--help`` output and man pages generated via
|
|
641
|
+
``--help --man``.
|
|
642
|
+
verbose_help: When ``True``, show built-in apcore options
|
|
643
|
+
(``--input``, ``--yes``, ``--large-input``, ``--format``) in
|
|
644
|
+
``--help`` output. Defaults to ``False`` (hidden). Users can
|
|
645
|
+
also toggle this at runtime with the ``--verbose`` flag.
|
|
560
646
|
|
|
561
647
|
Returns:
|
|
562
648
|
A Click Group that can be invoked with ``cli(standalone_mode=True)``.
|
|
@@ -571,15 +657,15 @@ class FastAPIApcore:
|
|
|
571
657
|
app,
|
|
572
658
|
prog_name="myapp-cli",
|
|
573
659
|
base_url="http://localhost:8000",
|
|
574
|
-
|
|
660
|
+
docs_url="https://docs.example.com/cli",
|
|
575
661
|
)
|
|
576
662
|
cli(standalone_mode=True)
|
|
577
663
|
"""
|
|
578
664
|
try:
|
|
579
665
|
import click
|
|
580
|
-
from apcore_cli.cli import
|
|
666
|
+
from apcore_cli.cli import GroupedModuleGroup, set_docs_url, set_verbose_help
|
|
581
667
|
from apcore_cli.discovery import register_discovery_commands
|
|
582
|
-
from apcore_cli.shell import
|
|
668
|
+
from apcore_cli.shell import configure_man_help, register_completion_command
|
|
583
669
|
except ImportError:
|
|
584
670
|
raise ImportError(
|
|
585
671
|
"apcore-cli is required for create_cli(). " "Install with: pip install fastapi-apcore[cli]"
|
|
@@ -589,11 +675,36 @@ class FastAPIApcore:
|
|
|
589
675
|
from apcore_toolkit.output.http_proxy_writer import HTTPProxyRegistryWriter
|
|
590
676
|
from fastapi_apcore.scanners import get_scanner
|
|
591
677
|
|
|
678
|
+
# Apply apcore-cli 0.4.0 module-level settings before commands are built.
|
|
679
|
+
# Pre-parse sys.argv so --verbose also takes effect when passed alongside
|
|
680
|
+
# --help (Click processes --help before the group callback fires).
|
|
681
|
+
# Note: set_verbose_help / set_docs_url mutate apcore-cli module-level
|
|
682
|
+
# globals — each create_cli() call resets them to the supplied values.
|
|
683
|
+
# This is intentional: the last CLI created wins, matching apcore-cli's
|
|
684
|
+
# own create_cli() behaviour.
|
|
685
|
+
effective_verbose = verbose_help or "--verbose" in sys.argv[1:]
|
|
686
|
+
set_verbose_help(effective_verbose)
|
|
687
|
+
set_docs_url(docs_url) # Always set (None clears stale value from a prior call)
|
|
688
|
+
|
|
689
|
+
if simplify_ids:
|
|
690
|
+
warnings.warn(
|
|
691
|
+
"simplify_ids=True is deprecated. Use binding_path with display.cli.alias "
|
|
692
|
+
"in binding.yaml instead (§5.13 of the apcore PROTOCOL_SPEC).",
|
|
693
|
+
DeprecationWarning,
|
|
694
|
+
stacklevel=2,
|
|
695
|
+
)
|
|
696
|
+
|
|
592
697
|
# 1. Scan routes
|
|
593
698
|
scanner = get_scanner(scan_source, simplify_ids=simplify_ids)
|
|
594
699
|
modules = scanner.scan(app, include=include, exclude=exclude)
|
|
595
700
|
logger.info("Scanned %d API routes", len(modules))
|
|
596
701
|
|
|
702
|
+
# 1b. Apply display overlay from binding.yaml (§5.13)
|
|
703
|
+
if binding_path is not None:
|
|
704
|
+
from apcore_toolkit.display import DisplayResolver
|
|
705
|
+
|
|
706
|
+
modules = DisplayResolver().resolve(modules, binding_path=binding_path)
|
|
707
|
+
|
|
597
708
|
# 2. Register as HTTP proxy modules
|
|
598
709
|
registry = Registry()
|
|
599
710
|
writer = HTTPProxyRegistryWriter(
|
|
@@ -610,6 +721,10 @@ class FastAPIApcore:
|
|
|
610
721
|
else:
|
|
611
722
|
logger.info("Registered %d modules", registered)
|
|
612
723
|
|
|
724
|
+
# 2b. Convention module discovery (§5.14)
|
|
725
|
+
if commands_dir is not None:
|
|
726
|
+
self._apply_convention_modules(commands_dir, registry)
|
|
727
|
+
|
|
613
728
|
executor = Executor(registry)
|
|
614
729
|
|
|
615
730
|
# 3. Build Click group
|
|
@@ -617,20 +732,23 @@ class FastAPIApcore:
|
|
|
617
732
|
if max_content_width is not None:
|
|
618
733
|
ctx_settings["max_content_width"] = max_content_width
|
|
619
734
|
|
|
620
|
-
# Compute the effective help width for the command list.
|
|
621
|
-
# Click uses min(terminal_width, max_content_width), so when the
|
|
622
|
-
# terminal is narrow, long command names push the description column
|
|
623
|
-
# to zero and everything shows "...". We override format_commands
|
|
624
|
-
# to use the configured max_content_width directly.
|
|
625
735
|
effective_width = max_content_width
|
|
736
|
+
_cli_description = (
|
|
737
|
+
f"{prog_name} — CLI for {app.title or 'FastAPI'} API.\n\n"
|
|
738
|
+
"Tips:\n"
|
|
739
|
+
f" {prog_name} --help --verbose Show all options including built-in apcore flags\n"
|
|
740
|
+
f" {prog_name} --help --man Generate a full roff man page for all commands\n"
|
|
741
|
+
f" {prog_name} list List all available commands\n"
|
|
742
|
+
f" {prog_name} describe MODULE_ID Show schema and annotations for a command"
|
|
743
|
+
)
|
|
626
744
|
|
|
627
745
|
@click.group(
|
|
628
|
-
cls=
|
|
746
|
+
cls=GroupedModuleGroup,
|
|
629
747
|
registry=registry,
|
|
630
748
|
executor=executor,
|
|
631
749
|
help_text_max_length=help_text_max_length,
|
|
632
750
|
name=prog_name,
|
|
633
|
-
help=
|
|
751
|
+
help=_cli_description,
|
|
634
752
|
context_settings=ctx_settings,
|
|
635
753
|
)
|
|
636
754
|
@click.version_option(
|
|
@@ -646,13 +764,32 @@ class FastAPIApcore:
|
|
|
646
764
|
),
|
|
647
765
|
help="Log verbosity.",
|
|
648
766
|
)
|
|
649
|
-
|
|
767
|
+
@click.option(
|
|
768
|
+
"--verbose",
|
|
769
|
+
"verbose_flag",
|
|
770
|
+
is_flag=True,
|
|
771
|
+
default=False,
|
|
772
|
+
is_eager=True, # Process before non-eager params so subcommand --help sees it
|
|
773
|
+
help="Show all options in help output (including built-in apcore options).",
|
|
774
|
+
)
|
|
775
|
+
def cli(log_level: str | None = None, verbose_flag: bool = False) -> None:
|
|
650
776
|
if log_level is not None:
|
|
651
777
|
level = getattr(logging, log_level.upper(), logging.WARNING)
|
|
652
778
|
logging.getLogger().setLevel(level)
|
|
779
|
+
if verbose_flag:
|
|
780
|
+
set_verbose_help(True)
|
|
653
781
|
|
|
654
782
|
register_discovery_commands(cli, registry)
|
|
655
|
-
|
|
783
|
+
register_completion_command(cli, prog_name=prog_name)
|
|
784
|
+
|
|
785
|
+
# Enable --help --man to generate a full roff man page covering all commands.
|
|
786
|
+
configure_man_help(
|
|
787
|
+
cli,
|
|
788
|
+
prog_name=prog_name,
|
|
789
|
+
version=app.version or "0.0.0",
|
|
790
|
+
description=_cli_description,
|
|
791
|
+
docs_url=docs_url,
|
|
792
|
+
)
|
|
656
793
|
|
|
657
794
|
# Override format_commands to use effective_width so descriptions
|
|
658
795
|
# are not truncated when the terminal is narrower than the content.
|