pydantic-ai-apcore 0.1.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.
- pydantic_ai_apcore-0.1.0/.github/CODEOWNERS +3 -0
- pydantic_ai_apcore-0.1.0/.github/copilot-ignore +13 -0
- pydantic_ai_apcore-0.1.0/.github/workflows/ci.yml +76 -0
- pydantic_ai_apcore-0.1.0/.gitignore +18 -0
- pydantic_ai_apcore-0.1.0/.gitmessage +60 -0
- pydantic_ai_apcore-0.1.0/.pre-commit-config.yaml +27 -0
- pydantic_ai_apcore-0.1.0/PKG-INFO +358 -0
- pydantic_ai_apcore-0.1.0/README.md +330 -0
- pydantic_ai_apcore-0.1.0/examples/__init__.py +0 -0
- pydantic_ai_apcore-0.1.0/examples/acl_demo/README.md +98 -0
- pydantic_ai_apcore-0.1.0/examples/acl_demo/__init__.py +0 -0
- pydantic_ai_apcore-0.1.0/examples/acl_demo/acl.yaml +32 -0
- pydantic_ai_apcore-0.1.0/examples/acl_demo/app.py +137 -0
- pydantic_ai_apcore-0.1.0/examples/approval_demo/README.md +74 -0
- pydantic_ai_apcore-0.1.0/examples/approval_demo/__init__.py +0 -0
- pydantic_ai_apcore-0.1.0/examples/approval_demo/acl.yaml +15 -0
- pydantic_ai_apcore-0.1.0/examples/approval_demo/app.py +162 -0
- pydantic_ai_apcore-0.1.0/examples/scanner_demo/README.md +98 -0
- pydantic_ai_apcore-0.1.0/examples/scanner_demo/__init__.py +0 -0
- pydantic_ai_apcore-0.1.0/examples/scanner_demo/acl.yaml +14 -0
- pydantic_ai_apcore-0.1.0/examples/scanner_demo/app.py +179 -0
- pydantic_ai_apcore-0.1.0/pyproject.toml +74 -0
- pydantic_ai_apcore-0.1.0/src/pydantic_ai_apcore/__init__.py +28 -0
- pydantic_ai_apcore-0.1.0/src/pydantic_ai_apcore/scanner.py +228 -0
- pydantic_ai_apcore-0.1.0/src/pydantic_ai_apcore/surfaces.py +56 -0
- pydantic_ai_apcore-0.1.0/src/pydantic_ai_apcore/toolset.py +258 -0
- pydantic_ai_apcore-0.1.0/tests/test_acl_demo.py +83 -0
- pydantic_ai_apcore-0.1.0/tests/test_approval_demo.py +97 -0
- pydantic_ai_apcore-0.1.0/tests/test_scanner_demo.py +101 -0
- pydantic_ai_apcore-0.1.0/tests/test_toolset.py +606 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
permissions:
|
|
4
|
+
contents: read
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "main" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "main" ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
test:
|
|
14
|
+
name: Test on Python ${{ matrix.python-version }}
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
# container:
|
|
17
|
+
# image: python:${{ matrix.python-version }}
|
|
18
|
+
strategy:
|
|
19
|
+
fail-fast: false
|
|
20
|
+
matrix:
|
|
21
|
+
# Pin CI to a single, consistent Python version to reduce version-related
|
|
22
|
+
# test flakiness. Use 3.12 for CI stability; expand matrix later if desired.
|
|
23
|
+
python-version: ["3.12"]
|
|
24
|
+
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
27
|
+
|
|
28
|
+
- name: Set up Python
|
|
29
|
+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
|
30
|
+
with:
|
|
31
|
+
python-version: ${{ matrix.python-version }}
|
|
32
|
+
cache: pip
|
|
33
|
+
|
|
34
|
+
- name: Install dependencies
|
|
35
|
+
run: |
|
|
36
|
+
python -m pip install --upgrade pip
|
|
37
|
+
# If a constraints.txt file is present, use it to pin critical dependency versions.
|
|
38
|
+
if [ -f constraints.txt ]; then
|
|
39
|
+
pip install -e ".[dev]" -c constraints.txt
|
|
40
|
+
else
|
|
41
|
+
pip install -e ".[dev]"
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
- name: Lint with Ruff
|
|
45
|
+
run: |
|
|
46
|
+
ruff check src/ tests/ examples/
|
|
47
|
+
|
|
48
|
+
- name: Run tests
|
|
49
|
+
run: |
|
|
50
|
+
pytest
|
|
51
|
+
|
|
52
|
+
minimum-versions:
|
|
53
|
+
name: Test against declared dependency floors
|
|
54
|
+
runs-on: ubuntu-latest
|
|
55
|
+
|
|
56
|
+
steps:
|
|
57
|
+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
58
|
+
|
|
59
|
+
- name: Set up Python
|
|
60
|
+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
|
61
|
+
with:
|
|
62
|
+
python-version: "3.12"
|
|
63
|
+
cache: pip
|
|
64
|
+
|
|
65
|
+
- name: Install the lower bounds declared in pyproject.toml
|
|
66
|
+
run: |
|
|
67
|
+
python -m pip install --upgrade pip
|
|
68
|
+
# These must match the floors in pyproject.toml. A failure here means a
|
|
69
|
+
# declared minimum is no longer accurate -- raise the floor rather than
|
|
70
|
+
# relaxing this job.
|
|
71
|
+
pip install "apcore==0.26.0" "apcore-toolkit==0.10.0" "pydantic-ai-slim==1.32.0" pytest pytest-asyncio
|
|
72
|
+
pip install --no-deps -e .
|
|
73
|
+
|
|
74
|
+
- name: Run tests
|
|
75
|
+
run: |
|
|
76
|
+
pytest
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# <type>(<scope>): <subject>
|
|
2
|
+
#
|
|
3
|
+
# <body>
|
|
4
|
+
#
|
|
5
|
+
# <footer>
|
|
6
|
+
|
|
7
|
+
# Type must be one of the following:
|
|
8
|
+
# feat: A new feature
|
|
9
|
+
# fix: A bug fix
|
|
10
|
+
# docs: Documentation only changes
|
|
11
|
+
# style: Changes that do not affect the meaning of the code
|
|
12
|
+
# refactor: A code change that neither fixes a bug nor adds a feature
|
|
13
|
+
# perf: A code change that improves performance
|
|
14
|
+
# test: Adding missing tests or correcting existing tests
|
|
15
|
+
# chore: Changes to the build process or auxiliary tools
|
|
16
|
+
# ci: Changes to CI configuration files and scripts
|
|
17
|
+
|
|
18
|
+
# Scope is optional and can be anything specifying the place of the commit change.
|
|
19
|
+
# Examples: api, core, storage, cli, stdio, etc.
|
|
20
|
+
|
|
21
|
+
# Subject should be:
|
|
22
|
+
# - Use imperative, present tense: "change" not "changed" nor "changes"
|
|
23
|
+
# - Don't capitalize first letter
|
|
24
|
+
# - No dot (.) at the end
|
|
25
|
+
# - Maximum 72 characters
|
|
26
|
+
|
|
27
|
+
# Body should include:
|
|
28
|
+
# - Motivation for the change and contrast with previous behavior
|
|
29
|
+
# - What changed and why
|
|
30
|
+
# - Any breaking changes
|
|
31
|
+
|
|
32
|
+
# Footer should contain:
|
|
33
|
+
# - Breaking changes (start with BREAKING CHANGE:)
|
|
34
|
+
# - Issue references (Closes #123, Fixes #456)
|
|
35
|
+
|
|
36
|
+
# Examples:
|
|
37
|
+
# feat(stdio): add stdio executor for process execution
|
|
38
|
+
#
|
|
39
|
+
# Implement a new stdio executor that allows executing system commands
|
|
40
|
+
# and processes via stdin/stdout communication, similar to MCP stdio
|
|
41
|
+
# transport mode. This enables flexible task execution through shell
|
|
42
|
+
# commands and Python scripts.
|
|
43
|
+
#
|
|
44
|
+
# - Add StdioExecutor class with command execution support
|
|
45
|
+
# - Add system resource monitoring (CPU, memory, disk)
|
|
46
|
+
# - Support async process communication
|
|
47
|
+
# - Add comprehensive error handling and logging
|
|
48
|
+
#
|
|
49
|
+
# Closes #123
|
|
50
|
+
|
|
51
|
+
# refactor(core): extract shared types to core.types module
|
|
52
|
+
#
|
|
53
|
+
# Move common type definitions from various modules to a centralized
|
|
54
|
+
# core.types module to avoid circular dependencies and improve code
|
|
55
|
+
# organization.
|
|
56
|
+
#
|
|
57
|
+
# - Add TaskPreHook and TaskPostHook type aliases
|
|
58
|
+
# - Move TaskStatus enum to core.types
|
|
59
|
+
# - Update imports across affected modules
|
|
60
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
|
|
3
|
+
# apdev hooks
|
|
4
|
+
- repo: local
|
|
5
|
+
hooks:
|
|
6
|
+
- id: check-chars
|
|
7
|
+
name: apdev check-chars
|
|
8
|
+
entry: apdev check-chars
|
|
9
|
+
language: system
|
|
10
|
+
types_or: [text, python]
|
|
11
|
+
|
|
12
|
+
- id: check-imports
|
|
13
|
+
name: apdev check-imports
|
|
14
|
+
entry: apdev check-imports
|
|
15
|
+
language: system
|
|
16
|
+
pass_filenames: false
|
|
17
|
+
always_run: true
|
|
18
|
+
|
|
19
|
+
# Ruff linter and formatter
|
|
20
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
21
|
+
rev: v0.16.4
|
|
22
|
+
hooks:
|
|
23
|
+
- id: ruff
|
|
24
|
+
args: [--fix]
|
|
25
|
+
files: ^(src|tests|examples)/
|
|
26
|
+
- id: ruff-format
|
|
27
|
+
files: ^(src|tests|examples)/
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pydantic-ai-apcore
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: apcore integration for pydantic-ai: governed modules as agent tools, and agent tools as governed modules
|
|
5
|
+
Project-URL: Homepage, https://aiperceivable.com
|
|
6
|
+
Project-URL: Repository, https://github.com/aiperceivable/pydantic-ai-apcore
|
|
7
|
+
Project-URL: Documentation, https://github.com/aiperceivable/pydantic-ai-apcore#readme
|
|
8
|
+
Project-URL: Issues, https://github.com/aiperceivable/pydantic-ai-apcore/issues
|
|
9
|
+
Author-email: aiperceivable <team@aiperceivable.com>
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
Keywords: access-control,agent,ai-agent,apcore,governance,human-in-the-loop,integration,llm,mcp,pydantic-ai,scanner,tool-use,tools,toolset
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Requires-Dist: apcore-toolkit>=0.10.0
|
|
14
|
+
Requires-Dist: apcore>=0.26.0
|
|
15
|
+
Requires-Dist: pydantic-ai-slim>=1.32.0
|
|
16
|
+
Provides-Extra: all
|
|
17
|
+
Requires-Dist: apcore-cli>=0.10.3; extra == 'all'
|
|
18
|
+
Requires-Dist: apcore-mcp>=0.17.2; extra == 'all'
|
|
19
|
+
Provides-Extra: cli
|
|
20
|
+
Requires-Dist: apcore-cli>=0.10.3; extra == 'cli'
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: apdev[dev]>=0.2.1; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
25
|
+
Provides-Extra: mcp
|
|
26
|
+
Requires-Dist: apcore-mcp>=0.17.2; extra == 'mcp'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# pydantic-ai-apcore
|
|
30
|
+
|
|
31
|
+
[pydantic-ai](https://ai.pydantic.dev/) integration for [apcore](https://github.com/aiperceivable/apcore-python).
|
|
32
|
+
|
|
33
|
+
Works in both directions:
|
|
34
|
+
|
|
35
|
+
- **`ApcoreToolset`** — expose governed apcore modules to a pydantic-ai agent
|
|
36
|
+
- **`register_toolset()`** — register the tools an agent already has as apcore
|
|
37
|
+
modules, so they gain schema validation, ACL, approval gates, and audit, and
|
|
38
|
+
can then be served over MCP, CLI, or A2A without being rewritten
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- **AbstractToolset implementation** — drop-in toolset for pydantic-ai agents
|
|
43
|
+
- **Schema passthrough** — apcore `input_schema` mapped directly to `ToolDefinition.parameters_json_schema`
|
|
44
|
+
- **Annotation mapping** — `destructive`, `requires_approval`, `readonly`, `idempotent`, `open_world`, `streaming` annotations surfaced as tool metadata and description warnings
|
|
45
|
+
- **Include/exclude filtering** — glob-style patterns to select which apcore modules to expose
|
|
46
|
+
- **Approval gating** — modules with `requires_approval=True` emit `kind="unapproved"` for pydantic-ai's human-in-the-loop flow
|
|
47
|
+
- **Per-call caller identity** — resolve the calling principal from the agent run context so apcore ACL rules can tell agents apart
|
|
48
|
+
- **Provider-safe tool names** — dotted module IDs are translated to names the major model providers accept
|
|
49
|
+
- **Reverse registration** — existing pydantic-ai tools become governed apcore modules, ready for MCP/CLI/A2A
|
|
50
|
+
|
|
51
|
+
## Requirements
|
|
52
|
+
|
|
53
|
+
- Python >= 3.10
|
|
54
|
+
- apcore >= 0.26.0
|
|
55
|
+
- apcore-toolkit >= 0.10.0
|
|
56
|
+
- pydantic-ai-slim >= 1.32.0
|
|
57
|
+
|
|
58
|
+
Both lower bounds are the earliest versions the test suite actually passes on,
|
|
59
|
+
not the earliest version that happens to expose the APIs used here:
|
|
60
|
+
|
|
61
|
+
| | Verified | Fails on | Why |
|
|
62
|
+
|---|---|---|---|
|
|
63
|
+
| apcore | 0.26.0 – 0.27.0 | 0.12.0 | the code itself works from 0.13.0, where `cacheable` / `paginated` arrive; the effective floor is raised to 0.26.0 by `apcore-toolkit` |
|
|
64
|
+
| apcore-toolkit | 0.6.0 – 0.10.1 | — | floor kept at 0.10.0 to match the other `*-apcore` integrations, so installing several together cannot conflict |
|
|
65
|
+
| pydantic-ai-slim | 1.32.0 – 2.34.0 | 1.31.0 | earlier releases pin an OpenTelemetry API that no longer resolves |
|
|
66
|
+
|
|
67
|
+
`ToolDefinition.sequential` needs 1.0.10 and `kind="unapproved"` needs 1.0.0, so
|
|
68
|
+
the pydantic-ai floor is set by installability rather than by API surface.
|
|
69
|
+
Both major lines are verified: the test suite passes on 1.32.0, 1.66.0,
|
|
70
|
+
2.0.0, and 2.34.0.
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install pydantic-ai-apcore
|
|
76
|
+
|
|
77
|
+
# Optionally project registered modules onto other surfaces
|
|
78
|
+
pip install "pydantic-ai-apcore[mcp]" # serve them as an MCP server
|
|
79
|
+
pip install "pydantic-ai-apcore[cli]" # expose them on the command line
|
|
80
|
+
pip install "pydantic-ai-apcore[all]"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Quick Start
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from apcore import Registry, Executor, module
|
|
87
|
+
from pydantic_ai import Agent
|
|
88
|
+
|
|
89
|
+
from pydantic_ai_apcore import ApcoreToolset
|
|
90
|
+
|
|
91
|
+
# 1. Set up apcore registry and modules
|
|
92
|
+
registry = Registry()
|
|
93
|
+
executor = Executor(registry=registry)
|
|
94
|
+
|
|
95
|
+
@module(id="math.add", registry=registry)
|
|
96
|
+
def add(a: int, b: int) -> dict:
|
|
97
|
+
"""Add two numbers."""
|
|
98
|
+
return {"result": a + b}
|
|
99
|
+
|
|
100
|
+
# 2. Create agent with ApcoreToolset
|
|
101
|
+
agent = Agent(
|
|
102
|
+
"openai:gpt-4o",
|
|
103
|
+
toolsets=[ApcoreToolset(registry, executor)],
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# 3. Run
|
|
107
|
+
result = agent.run_sync("What is 3 + 5?")
|
|
108
|
+
print(result.output)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API
|
|
112
|
+
|
|
113
|
+
### `ApcoreToolset`
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
ApcoreToolset(
|
|
117
|
+
registry: Registry,
|
|
118
|
+
executor: Executor,
|
|
119
|
+
*,
|
|
120
|
+
identity: Identity | None = None,
|
|
121
|
+
identity_resolver: Callable[[RunContext[AgentDepsT]], Identity | None] | None = None,
|
|
122
|
+
include: list[str] | None = None,
|
|
123
|
+
exclude: list[str] | None = None,
|
|
124
|
+
toolset_id: str | None = None,
|
|
125
|
+
max_retries: int = 1,
|
|
126
|
+
sanitize_tool_names: bool = True,
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
| Parameter | Description |
|
|
131
|
+
|-----------|-------------|
|
|
132
|
+
| `registry` | apcore `Registry` containing discovered modules |
|
|
133
|
+
| `executor` | apcore `Executor` for running modules |
|
|
134
|
+
| `identity` | Fixed `Identity` used for every call — suitable when the whole agent runs as one principal |
|
|
135
|
+
| `identity_resolver` | Derives the `Identity` per call from the run context; takes precedence over `identity` |
|
|
136
|
+
| `include` | Glob-style patterns to include (e.g. `["api.*"]`) |
|
|
137
|
+
| `exclude` | Glob-style patterns to exclude (e.g. `["internal.*"]`) |
|
|
138
|
+
| `toolset_id` | Optional unique ID for this toolset instance |
|
|
139
|
+
| `max_retries` | Retries pydantic-ai allows per tool call |
|
|
140
|
+
| `sanitize_tool_names` | Translate dotted module IDs into provider-safe tool names |
|
|
141
|
+
|
|
142
|
+
### Caller identity and ACL
|
|
143
|
+
|
|
144
|
+
apcore matches ACL `callers` patterns against `Context.call_chain[-1]`, and
|
|
145
|
+
`conditions.roles` against the `Context` identity. This toolset writes the
|
|
146
|
+
resolved identity to **both**, so a rule like the following matches as written:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
ACLRule(
|
|
150
|
+
callers=["agent.*"],
|
|
151
|
+
targets=["crm.delete"],
|
|
152
|
+
effect="allow",
|
|
153
|
+
conditions={"roles": ["data_admin"]},
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Use `identity_resolver` when one toolset serves several principals — typically
|
|
158
|
+
reading whatever the surrounding application already knows from `ctx.deps`:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
@dataclass
|
|
162
|
+
class AgentIdentity:
|
|
163
|
+
caller_id: str
|
|
164
|
+
roles: tuple[str, ...]
|
|
165
|
+
|
|
166
|
+
toolset = ApcoreToolset(
|
|
167
|
+
registry, executor,
|
|
168
|
+
identity_resolver=lambda ctx: Identity(
|
|
169
|
+
id=ctx.deps.caller_id, type="ai", roles=ctx.deps.roles
|
|
170
|
+
),
|
|
171
|
+
)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
With neither `identity` nor `identity_resolver`, calls reach apcore without a
|
|
175
|
+
caller, and a default-deny ACL will reject them.
|
|
176
|
+
|
|
177
|
+
### Tool naming
|
|
178
|
+
|
|
179
|
+
apcore module IDs are dotted by convention (`executor.crm.read`), but the major
|
|
180
|
+
model providers accept only `[a-zA-Z0-9_-]` in a tool name. Names are therefore
|
|
181
|
+
translated (`executor_crm_read`) before being offered to a model, and the
|
|
182
|
+
original module ID travels in `ToolDefinition.metadata["apcore_module_id"]` so
|
|
183
|
+
execution still targets the right module. Two IDs that translate to the same
|
|
184
|
+
name get a numeric suffix. Pass `sanitize_tool_names=False` to offer IDs
|
|
185
|
+
verbatim.
|
|
186
|
+
|
|
187
|
+
### Filtering
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
# Only expose api.* modules, but not api.internal
|
|
191
|
+
toolset = ApcoreToolset(
|
|
192
|
+
registry, executor,
|
|
193
|
+
include=["api.*"],
|
|
194
|
+
exclude=["api.internal"],
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Exclude takes precedence over include.
|
|
199
|
+
|
|
200
|
+
### Annotation Handling
|
|
201
|
+
|
|
202
|
+
apcore `ModuleAnnotations` are mapped to pydantic-ai as follows:
|
|
203
|
+
|
|
204
|
+
| Annotation | Effect |
|
|
205
|
+
|------------|--------|
|
|
206
|
+
| `destructive=True` | Adds `WARNING: DESTRUCTIVE` to the description, and sets `sequential=True` so the call does not overlap with other tool calls |
|
|
207
|
+
| `requires_approval=True` | Sets `kind="unapproved"` + adds `REQUIRES APPROVAL` to description |
|
|
208
|
+
| `readonly`, `idempotent`, `open_world`, `streaming`, `cacheable`, `paginated` | Surfaced in `ToolDefinition.metadata` |
|
|
209
|
+
| `discoverable=False` | Excluded — `Registry.list()` omits these modules |
|
|
210
|
+
|
|
211
|
+
Other descriptor fields also reach the model: `documentation` and up to three
|
|
212
|
+
`examples` are appended to the description, and a `sunset_date` becomes a
|
|
213
|
+
`DEPRECATED` notice. `version` and `tags` travel in `metadata`.
|
|
214
|
+
|
|
215
|
+
### Registering existing tools
|
|
216
|
+
|
|
217
|
+
The reverse direction: take the tools an agent already has and put them behind
|
|
218
|
+
the apcore execution boundary.
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from apcore import Registry
|
|
222
|
+
from pydantic_ai.toolsets.function import FunctionToolset
|
|
223
|
+
from pydantic_ai_apcore import register_toolset
|
|
224
|
+
|
|
225
|
+
toolset = FunctionToolset()
|
|
226
|
+
|
|
227
|
+
def send_invoice(customer_id: str, amount: float) -> dict:
|
|
228
|
+
"""Send an invoice to a customer.
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
customer_id: Who to bill.
|
|
232
|
+
amount: How much, in USD.
|
|
233
|
+
"""
|
|
234
|
+
return {"customer_id": customer_id, "sent": True}
|
|
235
|
+
|
|
236
|
+
toolset.add_function(send_invoice, takes_ctx=False)
|
|
237
|
+
|
|
238
|
+
registry = Registry()
|
|
239
|
+
result = register_toolset(toolset, registry, prefix="billing.")
|
|
240
|
+
# result.registered -> ["billing.send_invoice"]
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
> `add_function(takes_ctx=...)` behaves identically on pydantic-ai 1.x and 2.x.
|
|
244
|
+
> The `.tool` decorator does not: 2.0 changed it to require a `RunContext` first
|
|
245
|
+
> parameter and added `.tool_plain` for functions that take none. Either
|
|
246
|
+
> decorator works if you only target one major version; `add_function` works on
|
|
247
|
+
> both.
|
|
248
|
+
|
|
249
|
+
Schemas are derived by apcore from each function's signature and docstring, the
|
|
250
|
+
same way a hand-written module would be, so the results are not second-class. A
|
|
251
|
+
tool's `requires_approval` flag carries over to the apcore annotation of the
|
|
252
|
+
same name — approval semantics survive the trip in both directions.
|
|
253
|
+
|
|
254
|
+
Tools that take a `RunContext` are **skipped**, and reported in
|
|
255
|
+
`result.skipped` with the reason. apcore invokes a module without a pydantic-ai
|
|
256
|
+
run context, and passing `None` would hand the function a broken context rather
|
|
257
|
+
than fail honestly.
|
|
258
|
+
|
|
259
|
+
#### Metadata gaps get flagged
|
|
260
|
+
|
|
261
|
+
pydantic-ai does not require parameter descriptions
|
|
262
|
+
(`require_parameter_descriptions` defaults to `False`), so a tool can reach a
|
|
263
|
+
model with nothing but parameter names. Scanning produces
|
|
264
|
+
`apcore_toolkit.ScannedModule` values whose `warnings` say where that happened:
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
for m in result.modules:
|
|
268
|
+
for w in m.warnings:
|
|
269
|
+
print(m.module_id, w)
|
|
270
|
+
# undocumented has no description for: x, y. A model sees only the parameter names.
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Pass `enhancers=` to fill the gaps before registration — for instance
|
|
274
|
+
`apcore_toolkit.AIEnhancer`, which uses a local SLM and is off unless
|
|
275
|
+
`APCORE_AI_ENABLED` is set:
|
|
276
|
+
|
|
277
|
+
```python
|
|
278
|
+
from apcore_toolkit import AIEnhancer
|
|
279
|
+
|
|
280
|
+
register_toolset(toolset, registry, enhancers=[AIEnhancer()])
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
#### Closures register on equal footing
|
|
284
|
+
|
|
285
|
+
Registration deliberately does **not** go through `apcore_toolkit.RegistryWriter`.
|
|
286
|
+
That writer resolves a callable from `ScannedModule.target` by import path,
|
|
287
|
+
which cannot reach a tool defined inside a function — a common shape in
|
|
288
|
+
pydantic-ai, where tools close over dependencies. The callables are held from
|
|
289
|
+
the scan instead, so such tools register and execute normally; their
|
|
290
|
+
`target` is still recorded, with a warning that it is not importable.
|
|
291
|
+
|
|
292
|
+
Once registered, they are ordinary apcore modules — anything that serves a
|
|
293
|
+
registry can serve them:
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
from pydantic_ai_apcore import serve_mcp # needs the [mcp] extra
|
|
297
|
+
|
|
298
|
+
serve_mcp(registry)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
`serve_mcp` / `serve_cli` are thin forwards to `apcore-mcp` and `apcore-cli`,
|
|
302
|
+
kept behind extras so the base install stays minimal. Calling one without its
|
|
303
|
+
extra installed raises an `ImportError` naming the command to fix it.
|
|
304
|
+
|
|
305
|
+
### Two approval checkpoints
|
|
306
|
+
|
|
307
|
+
`requires_approval` produces a tool the agent run suspends on, returning
|
|
308
|
+
`DeferredToolRequests`. That is pydantic-ai asking whether the model may make
|
|
309
|
+
the call at all. The apcore approval gate is separate and runs later, once the
|
|
310
|
+
sanctioned call reaches the execution pipeline — the two are in series, not
|
|
311
|
+
duplicated. See [`examples/governance_comparison.py`](examples/governance_comparison.py),
|
|
312
|
+
which demonstrates the ordering and runs offline without a model provider.
|
|
313
|
+
|
|
314
|
+
## Examples
|
|
315
|
+
|
|
316
|
+
Each demo is a self-contained directory with its own `README.md` and `acl.yaml`,
|
|
317
|
+
following the same layout as the other `*-apcore` integrations. All of them run
|
|
318
|
+
offline — `FunctionModel` drives the tool calls, so no model provider or API key
|
|
319
|
+
is involved.
|
|
320
|
+
|
|
321
|
+
| Demo | Shows |
|
|
322
|
+
|------|-------|
|
|
323
|
+
| [`acl_demo`](examples/acl_demo/) | ACL rules loaded from YAML via `APCORE_ACL_PATH`, and how `identity_resolver` gets the caller into the fields those rules match against. |
|
|
324
|
+
| [`approval_demo`](examples/approval_demo/) | The two approval checkpoints — pydantic-ai's and apcore's — and the order they fire in. |
|
|
325
|
+
| [`scanner_demo`](examples/scanner_demo/) | The reverse direction: existing agent tools registered as governed apcore modules, then denied by ACL and gated on approval. |
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
python examples/acl_demo/app.py
|
|
329
|
+
python examples/approval_demo/app.py
|
|
330
|
+
python examples/scanner_demo/app.py
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Each demo's ACL lives in its own `acl.yaml`, so the policy can be changed and
|
|
334
|
+
re-run without touching code:
|
|
335
|
+
|
|
336
|
+
```yaml
|
|
337
|
+
default_effect: deny
|
|
338
|
+
rules:
|
|
339
|
+
- description: Only data-admin agents may delete
|
|
340
|
+
callers: ["agent.*"] # matched against Context.call_chain[-1]
|
|
341
|
+
targets: ["crm.delete"] # module-id glob patterns
|
|
342
|
+
effect: allow
|
|
343
|
+
conditions:
|
|
344
|
+
roles: ["data_admin"]
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## Development
|
|
348
|
+
|
|
349
|
+
```bash
|
|
350
|
+
git clone https://github.com/aiperceivable/pydantic-ai-apcore.git
|
|
351
|
+
cd pydantic-ai-apcore
|
|
352
|
+
pip install -e ".[dev]"
|
|
353
|
+
pytest
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## License
|
|
357
|
+
|
|
358
|
+
Apache-2.0
|