shieldaai-agent-guard 0.1.0.dev0__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.
- shieldaai_agent_guard-0.1.0.dev0/LICENSE +202 -0
- shieldaai_agent_guard-0.1.0.dev0/PKG-INFO +201 -0
- shieldaai_agent_guard-0.1.0.dev0/README.md +182 -0
- shieldaai_agent_guard-0.1.0.dev0/pyproject.toml +49 -0
- shieldaai_agent_guard-0.1.0.dev0/pyproject.toml.orig +37 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/__init__.py +51 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/_client.py +641 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/_errors.py +74 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/crewai.py +84 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/langchain.py +203 -0
- shieldaai_agent_guard-0.1.0.dev0/src/shieldaai/agent_guard/py.typed +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 ShieldaAI contributors
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shieldaai-agent-guard
|
|
3
|
+
Version: 0.1.0.dev0
|
|
4
|
+
Summary: Python client for the Agent Guard decision server: ask before every AI agent tool call, run it, report back. LangChain, LangGraph and CrewAI helpers. No runtime dependencies.
|
|
5
|
+
Keywords: ai-agents,security,authorization,tool-calling,langchain,langgraph,crewai,human-in-the-loop
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Security
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Project-URL: Homepage, https://github.com/ShieldaAI/agent-guard/tree/main/clients/python
|
|
16
|
+
Project-URL: Documentation, https://github.com/ShieldaAI/agent-guard/blob/main/docs/decide-api.md
|
|
17
|
+
Project-URL: Issues, https://github.com/ShieldaAI/agent-guard/issues
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# shieldaai-agent-guard
|
|
21
|
+
|
|
22
|
+
> **Preview.** New in Agent Guard 1.9.0. The HTTP API it speaks may still change in a minor
|
|
23
|
+
> release, and it has no production users yet. Please
|
|
24
|
+
> [open an issue](https://github.com/ShieldaAI/agent-guard/issues) if something does not fit.
|
|
25
|
+
|
|
26
|
+
Python client for the Agent Guard decision server. Before each tool call, your agent asks the
|
|
27
|
+
server whether the call may run. On yes, it runs the tool itself with the arguments the server
|
|
28
|
+
checked, then reports the outcome. The server applies Agent Guard's checks: grants per task and
|
|
29
|
+
resource, allowed recipients, sensitive and untrusted data, secrets in external calls, approvals
|
|
30
|
+
by a person, call and cost budgets, and your policies.
|
|
31
|
+
|
|
32
|
+
No runtime dependencies. Python 3.10 or later. Helpers for LangChain, LangGraph and CrewAI.
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
pip install shieldaai-agent-guard
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Start the server
|
|
39
|
+
|
|
40
|
+
The server is `agent-guard-decide`, from the npm package `@shieldaai/agent-guard-adapters`
|
|
41
|
+
(Node.js 22 or later). It reads a JSON file that lists your tools, and you get its admin token
|
|
42
|
+
from the environment:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
export AGENT_GUARD_ADMIN_TOKEN="$(npx -y -p @shieldaai/agent-guard-adapters@1.9.0 agent-guard-decide --new-token)"
|
|
46
|
+
npx -y -p @shieldaai/agent-guard-adapters@1.9.0 agent-guard-decide --config decide.json
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"version": 1,
|
|
52
|
+
"tools": [
|
|
53
|
+
{
|
|
54
|
+
"name": "ticket_read",
|
|
55
|
+
"description": "Read one support ticket",
|
|
56
|
+
"inputSchema": {
|
|
57
|
+
"type": "object",
|
|
58
|
+
"properties": { "id": { "type": "string", "pattern": "^[0-9]+$" } },
|
|
59
|
+
"required": ["id"],
|
|
60
|
+
"additionalProperties": false
|
|
61
|
+
},
|
|
62
|
+
"resource": "ticket:{id}",
|
|
63
|
+
"readsUntrusted": true
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"name": "ticket_close",
|
|
67
|
+
"description": "Close one support ticket",
|
|
68
|
+
"inputSchema": { "type": "object", "properties": { "id": { "type": "string" } }, "required": ["id"] },
|
|
69
|
+
"resource": "ticket:{id}",
|
|
70
|
+
"requiresApproval": true
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`resource` names what the call touches, built from its arguments. A task may only call a tool
|
|
77
|
+
on the resources it was granted. Every field is described in
|
|
78
|
+
[the API reference](https://github.com/ShieldaAI/agent-guard/blob/main/docs/decide-api.md#configuration).
|
|
79
|
+
|
|
80
|
+
## Create a task, then guard the tools
|
|
81
|
+
|
|
82
|
+
Your backend creates one task per job, for the user it signed in, and gives the agent only the
|
|
83
|
+
task's token:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from shieldaai.agent_guard import AdminClient, Grant, TaskClient
|
|
87
|
+
|
|
88
|
+
admin = AdminClient("http://127.0.0.1:8787", admin_token) # your backend only
|
|
89
|
+
task = admin.create_task(
|
|
90
|
+
principal_id=user.id, # from your own sign-in, never from the model
|
|
91
|
+
tenant_id=user.tenant_id,
|
|
92
|
+
grants=[Grant("ticket_read", ["ticket:42"]), Grant("ticket_close", ["ticket:42"])],
|
|
93
|
+
max_calls=20,
|
|
94
|
+
max_concurrent=4, # LangGraph runs one message's tool calls in parallel
|
|
95
|
+
ttl_seconds=600,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
guard = TaskClient("http://127.0.0.1:8787", task.token) # the agent side
|
|
99
|
+
|
|
100
|
+
@guard.tool()
|
|
101
|
+
def ticket_read(id: str) -> dict:
|
|
102
|
+
return tickets.get(id)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`ticket_read(id="42")` asks the server, runs the function and reports the outcome.
|
|
106
|
+
`ticket_read(id="7")` raises `ToolDenied`, because the task was granted ticket 42 only;
|
|
107
|
+
`str(error)` is a message written for the model. If the function raises, the failure is
|
|
108
|
+
reported and the exception propagates.
|
|
109
|
+
|
|
110
|
+
A tool that needs approval raises `ApprovalPending` (with `action_id`). Calling it again with
|
|
111
|
+
the same arguments does not queue a second request: it raises `ApprovalPending` for the same
|
|
112
|
+
action until a person approves it, and then runs it. Pass `wait_for_approval=120` to wait in the
|
|
113
|
+
call instead.
|
|
114
|
+
|
|
115
|
+
- `max_concurrent` (default 1) is how many calls may run at once. Set it to at least the number
|
|
116
|
+
of tool calls a model may make in one turn, or the extra ones are denied with
|
|
117
|
+
`CONCURRENCY_EXCEEDED`. The server lets one task token have 8 requests in progress (a call
|
|
118
|
+
waiting for approval counts); raise `server.maxActiveRequestsPerCredential` in its
|
|
119
|
+
configuration for more.
|
|
120
|
+
- The guard sees every argument by name, defaults included (except a default of `None`, which is
|
|
121
|
+
not sent), so the schema must list every parameter. The function runs with the values it
|
|
122
|
+
checked after a JSON round trip: a tuple arrives as a list, a date as a string.
|
|
123
|
+
- `@guard.tool` and `@guard.tool("name")` both work, on `def` and `async def`. For a method, call
|
|
124
|
+
`guard.run("name", args, call)` inside it.
|
|
125
|
+
- `cost=lambda result: ...` reports what the call cost. A cost that fails or is out of range is
|
|
126
|
+
logged and the call is charged its maximum.
|
|
127
|
+
|
|
128
|
+
## LangChain and LangGraph
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from shieldaai.agent_guard.langchain import guard_tools
|
|
132
|
+
from langgraph.prebuilt import create_react_agent
|
|
133
|
+
|
|
134
|
+
agent = create_react_agent(model, guard_tools(guard, [ticket_read, ticket_close]))
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Each guarded tool keeps its name, description and schema. A denial reaches the model as the
|
|
138
|
+
tool's error message, so the agent reads why instead of stopping. Arguments the tool marks as
|
|
139
|
+
injected (`InjectedToolArg`, `InjectedState`, `InjectedToolCallId`) are filled in by your code
|
|
140
|
+
or LangGraph, so they are not sent to the guard and it does not check them; every other
|
|
141
|
+
argument is. A tool that returns a LangGraph `Command` returns it unchanged. `config_entry(tool,
|
|
142
|
+
resource="ticket:{id}")` builds the server configuration entry from a LangChain tool, so the
|
|
143
|
+
server checks the same schema the model is shown.
|
|
144
|
+
|
|
145
|
+
## CrewAI
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from shieldaai.agent_guard.crewai import guard_tools
|
|
149
|
+
|
|
150
|
+
agent = Agent(role="Support", goal="...", backstory="...", tools=guard_tools(guard, [ReadTicket()]))
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The guarded copies never write to CrewAI's result cache: a cached answer would skip the check
|
|
154
|
+
and the budget. CrewAI also reads that cache by tool name for the whole crew, so create the crew
|
|
155
|
+
with `cache=False`.
|
|
156
|
+
|
|
157
|
+
## Approvals
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
for item in admin.pending(tenant_id=reviewer.tenant_id):
|
|
161
|
+
if item.state == "pending": # "approved" ones wait for the agent to collect them
|
|
162
|
+
# Show item.tool, item.args and item.resource_id to the reviewer, then:
|
|
163
|
+
admin.approve(item.action_id, approver_id=reviewer.id) # or admin.reject(item.action_id)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The agent collects the approval by calling the tool again with the same arguments, by calling
|
|
167
|
+
`guard.wait(action_id, timeout=...)`, or in the call itself when the decorator was given
|
|
168
|
+
`wait_for_approval`.
|
|
169
|
+
|
|
170
|
+
## Connections
|
|
171
|
+
|
|
172
|
+
Each call through the decorator, `run` or the framework helpers sends a fresh `callId` with its
|
|
173
|
+
decision. If the answer is lost to a timeout or a dropped connection, the client asks once more
|
|
174
|
+
with the same ID and gets the first answer back, so a retry never starts a second call.
|
|
175
|
+
|
|
176
|
+
The client never follows a redirect and ignores proxy settings such as `HTTP_PROXY`, so the
|
|
177
|
+
token goes only to the URL you gave. Plain `http://` is accepted for this machine
|
|
178
|
+
(`localhost`, `127.0.0.1`); for any other host use `https://`, or pass
|
|
179
|
+
`allow_insecure_http=True` on a network you trust.
|
|
180
|
+
|
|
181
|
+
## What it does not do
|
|
182
|
+
|
|
183
|
+
- **It cannot stop a call that is already running.** Revoking a task stops new decisions. A
|
|
184
|
+
call that was allowed and has started finishes on your side.
|
|
185
|
+
- **It never sees results.** It records what you report. If no report arrives within the
|
|
186
|
+
server's lease (60 seconds by default), it records the call as failed with an unknown effect
|
|
187
|
+
and charges its full cost.
|
|
188
|
+
- **Resource templates cannot look up who owns a resource.** Grant the exact resources a task
|
|
189
|
+
needs. When the owner has to be checked, write the tool in JavaScript with its own `resolve`
|
|
190
|
+
function ([guard library](https://github.com/ShieldaAI/agent-guard/tree/main/packages/core)).
|
|
191
|
+
- **It does not detect prompt injection.** It limits what a misled agent can do.
|
|
192
|
+
|
|
193
|
+
## API
|
|
194
|
+
|
|
195
|
+
| | |
|
|
196
|
+
| --- | --- |
|
|
197
|
+
| `TaskClient(url, token)` | `decide(tool, args)` returns `Allow`, `Deny` or `NeedsApproval` (the HTTP API's `allow`, `deny` and `require_approval`). Also `wait(action_id, timeout)`, `report(action_id, "succeeded" \| "failed", actual_cost)`, `status()`, `run(tool, args, call)`, `arun(...)` and the `tool()` decorator |
|
|
198
|
+
| `AdminClient(url, admin_token)` | `create_task(...)`, `revoke_task(task_id)`, `task_status(task_id)`, `pending(task_id=..., tenant_id=...)`, `approve(action_id, approver_id=...)` (returns when the approval expires, in clock milliseconds), `reject(action_id)` |
|
|
199
|
+
| Exceptions | `ToolDenied` (nothing ran), `ApprovalPending` (nothing ran yet), `ServiceError` (the server refused the request or could not be reached), all subclasses of `GuardError` |
|
|
200
|
+
|
|
201
|
+
Licensed under Apache-2.0.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# shieldaai-agent-guard
|
|
2
|
+
|
|
3
|
+
> **Preview.** New in Agent Guard 1.9.0. The HTTP API it speaks may still change in a minor
|
|
4
|
+
> release, and it has no production users yet. Please
|
|
5
|
+
> [open an issue](https://github.com/ShieldaAI/agent-guard/issues) if something does not fit.
|
|
6
|
+
|
|
7
|
+
Python client for the Agent Guard decision server. Before each tool call, your agent asks the
|
|
8
|
+
server whether the call may run. On yes, it runs the tool itself with the arguments the server
|
|
9
|
+
checked, then reports the outcome. The server applies Agent Guard's checks: grants per task and
|
|
10
|
+
resource, allowed recipients, sensitive and untrusted data, secrets in external calls, approvals
|
|
11
|
+
by a person, call and cost budgets, and your policies.
|
|
12
|
+
|
|
13
|
+
No runtime dependencies. Python 3.10 or later. Helpers for LangChain, LangGraph and CrewAI.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pip install shieldaai-agent-guard
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Start the server
|
|
20
|
+
|
|
21
|
+
The server is `agent-guard-decide`, from the npm package `@shieldaai/agent-guard-adapters`
|
|
22
|
+
(Node.js 22 or later). It reads a JSON file that lists your tools, and you get its admin token
|
|
23
|
+
from the environment:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
export AGENT_GUARD_ADMIN_TOKEN="$(npx -y -p @shieldaai/agent-guard-adapters@1.9.0 agent-guard-decide --new-token)"
|
|
27
|
+
npx -y -p @shieldaai/agent-guard-adapters@1.9.0 agent-guard-decide --config decide.json
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"version": 1,
|
|
33
|
+
"tools": [
|
|
34
|
+
{
|
|
35
|
+
"name": "ticket_read",
|
|
36
|
+
"description": "Read one support ticket",
|
|
37
|
+
"inputSchema": {
|
|
38
|
+
"type": "object",
|
|
39
|
+
"properties": { "id": { "type": "string", "pattern": "^[0-9]+$" } },
|
|
40
|
+
"required": ["id"],
|
|
41
|
+
"additionalProperties": false
|
|
42
|
+
},
|
|
43
|
+
"resource": "ticket:{id}",
|
|
44
|
+
"readsUntrusted": true
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"name": "ticket_close",
|
|
48
|
+
"description": "Close one support ticket",
|
|
49
|
+
"inputSchema": { "type": "object", "properties": { "id": { "type": "string" } }, "required": ["id"] },
|
|
50
|
+
"resource": "ticket:{id}",
|
|
51
|
+
"requiresApproval": true
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`resource` names what the call touches, built from its arguments. A task may only call a tool
|
|
58
|
+
on the resources it was granted. Every field is described in
|
|
59
|
+
[the API reference](https://github.com/ShieldaAI/agent-guard/blob/main/docs/decide-api.md#configuration).
|
|
60
|
+
|
|
61
|
+
## Create a task, then guard the tools
|
|
62
|
+
|
|
63
|
+
Your backend creates one task per job, for the user it signed in, and gives the agent only the
|
|
64
|
+
task's token:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from shieldaai.agent_guard import AdminClient, Grant, TaskClient
|
|
68
|
+
|
|
69
|
+
admin = AdminClient("http://127.0.0.1:8787", admin_token) # your backend only
|
|
70
|
+
task = admin.create_task(
|
|
71
|
+
principal_id=user.id, # from your own sign-in, never from the model
|
|
72
|
+
tenant_id=user.tenant_id,
|
|
73
|
+
grants=[Grant("ticket_read", ["ticket:42"]), Grant("ticket_close", ["ticket:42"])],
|
|
74
|
+
max_calls=20,
|
|
75
|
+
max_concurrent=4, # LangGraph runs one message's tool calls in parallel
|
|
76
|
+
ttl_seconds=600,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
guard = TaskClient("http://127.0.0.1:8787", task.token) # the agent side
|
|
80
|
+
|
|
81
|
+
@guard.tool()
|
|
82
|
+
def ticket_read(id: str) -> dict:
|
|
83
|
+
return tickets.get(id)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`ticket_read(id="42")` asks the server, runs the function and reports the outcome.
|
|
87
|
+
`ticket_read(id="7")` raises `ToolDenied`, because the task was granted ticket 42 only;
|
|
88
|
+
`str(error)` is a message written for the model. If the function raises, the failure is
|
|
89
|
+
reported and the exception propagates.
|
|
90
|
+
|
|
91
|
+
A tool that needs approval raises `ApprovalPending` (with `action_id`). Calling it again with
|
|
92
|
+
the same arguments does not queue a second request: it raises `ApprovalPending` for the same
|
|
93
|
+
action until a person approves it, and then runs it. Pass `wait_for_approval=120` to wait in the
|
|
94
|
+
call instead.
|
|
95
|
+
|
|
96
|
+
- `max_concurrent` (default 1) is how many calls may run at once. Set it to at least the number
|
|
97
|
+
of tool calls a model may make in one turn, or the extra ones are denied with
|
|
98
|
+
`CONCURRENCY_EXCEEDED`. The server lets one task token have 8 requests in progress (a call
|
|
99
|
+
waiting for approval counts); raise `server.maxActiveRequestsPerCredential` in its
|
|
100
|
+
configuration for more.
|
|
101
|
+
- The guard sees every argument by name, defaults included (except a default of `None`, which is
|
|
102
|
+
not sent), so the schema must list every parameter. The function runs with the values it
|
|
103
|
+
checked after a JSON round trip: a tuple arrives as a list, a date as a string.
|
|
104
|
+
- `@guard.tool` and `@guard.tool("name")` both work, on `def` and `async def`. For a method, call
|
|
105
|
+
`guard.run("name", args, call)` inside it.
|
|
106
|
+
- `cost=lambda result: ...` reports what the call cost. A cost that fails or is out of range is
|
|
107
|
+
logged and the call is charged its maximum.
|
|
108
|
+
|
|
109
|
+
## LangChain and LangGraph
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from shieldaai.agent_guard.langchain import guard_tools
|
|
113
|
+
from langgraph.prebuilt import create_react_agent
|
|
114
|
+
|
|
115
|
+
agent = create_react_agent(model, guard_tools(guard, [ticket_read, ticket_close]))
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Each guarded tool keeps its name, description and schema. A denial reaches the model as the
|
|
119
|
+
tool's error message, so the agent reads why instead of stopping. Arguments the tool marks as
|
|
120
|
+
injected (`InjectedToolArg`, `InjectedState`, `InjectedToolCallId`) are filled in by your code
|
|
121
|
+
or LangGraph, so they are not sent to the guard and it does not check them; every other
|
|
122
|
+
argument is. A tool that returns a LangGraph `Command` returns it unchanged. `config_entry(tool,
|
|
123
|
+
resource="ticket:{id}")` builds the server configuration entry from a LangChain tool, so the
|
|
124
|
+
server checks the same schema the model is shown.
|
|
125
|
+
|
|
126
|
+
## CrewAI
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from shieldaai.agent_guard.crewai import guard_tools
|
|
130
|
+
|
|
131
|
+
agent = Agent(role="Support", goal="...", backstory="...", tools=guard_tools(guard, [ReadTicket()]))
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The guarded copies never write to CrewAI's result cache: a cached answer would skip the check
|
|
135
|
+
and the budget. CrewAI also reads that cache by tool name for the whole crew, so create the crew
|
|
136
|
+
with `cache=False`.
|
|
137
|
+
|
|
138
|
+
## Approvals
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
for item in admin.pending(tenant_id=reviewer.tenant_id):
|
|
142
|
+
if item.state == "pending": # "approved" ones wait for the agent to collect them
|
|
143
|
+
# Show item.tool, item.args and item.resource_id to the reviewer, then:
|
|
144
|
+
admin.approve(item.action_id, approver_id=reviewer.id) # or admin.reject(item.action_id)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The agent collects the approval by calling the tool again with the same arguments, by calling
|
|
148
|
+
`guard.wait(action_id, timeout=...)`, or in the call itself when the decorator was given
|
|
149
|
+
`wait_for_approval`.
|
|
150
|
+
|
|
151
|
+
## Connections
|
|
152
|
+
|
|
153
|
+
Each call through the decorator, `run` or the framework helpers sends a fresh `callId` with its
|
|
154
|
+
decision. If the answer is lost to a timeout or a dropped connection, the client asks once more
|
|
155
|
+
with the same ID and gets the first answer back, so a retry never starts a second call.
|
|
156
|
+
|
|
157
|
+
The client never follows a redirect and ignores proxy settings such as `HTTP_PROXY`, so the
|
|
158
|
+
token goes only to the URL you gave. Plain `http://` is accepted for this machine
|
|
159
|
+
(`localhost`, `127.0.0.1`); for any other host use `https://`, or pass
|
|
160
|
+
`allow_insecure_http=True` on a network you trust.
|
|
161
|
+
|
|
162
|
+
## What it does not do
|
|
163
|
+
|
|
164
|
+
- **It cannot stop a call that is already running.** Revoking a task stops new decisions. A
|
|
165
|
+
call that was allowed and has started finishes on your side.
|
|
166
|
+
- **It never sees results.** It records what you report. If no report arrives within the
|
|
167
|
+
server's lease (60 seconds by default), it records the call as failed with an unknown effect
|
|
168
|
+
and charges its full cost.
|
|
169
|
+
- **Resource templates cannot look up who owns a resource.** Grant the exact resources a task
|
|
170
|
+
needs. When the owner has to be checked, write the tool in JavaScript with its own `resolve`
|
|
171
|
+
function ([guard library](https://github.com/ShieldaAI/agent-guard/tree/main/packages/core)).
|
|
172
|
+
- **It does not detect prompt injection.** It limits what a misled agent can do.
|
|
173
|
+
|
|
174
|
+
## API
|
|
175
|
+
|
|
176
|
+
| | |
|
|
177
|
+
| --- | --- |
|
|
178
|
+
| `TaskClient(url, token)` | `decide(tool, args)` returns `Allow`, `Deny` or `NeedsApproval` (the HTTP API's `allow`, `deny` and `require_approval`). Also `wait(action_id, timeout)`, `report(action_id, "succeeded" \| "failed", actual_cost)`, `status()`, `run(tool, args, call)`, `arun(...)` and the `tool()` decorator |
|
|
179
|
+
| `AdminClient(url, admin_token)` | `create_task(...)`, `revoke_task(task_id)`, `task_status(task_id)`, `pending(task_id=..., tenant_id=...)`, `approve(action_id, approver_id=...)` (returns when the approval expires, in clock milliseconds), `reject(action_id)` |
|
|
180
|
+
| Exceptions | `ToolDenied` (nothing ran), `ApprovalPending` (nothing ran yet), `ServiceError` (the server refused the request or could not be reached), all subclasses of `GuardError` |
|
|
181
|
+
|
|
182
|
+
Licensed under Apache-2.0.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shieldaai-agent-guard"
|
|
7
|
+
version = "0.1.0.dev0"
|
|
8
|
+
description = "Python client for the Agent Guard decision server: ask before every AI agent tool call, run it, report back. LangChain, LangGraph and CrewAI helpers. No runtime dependencies."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
dependencies = []
|
|
14
|
+
keywords = [
|
|
15
|
+
"ai-agents",
|
|
16
|
+
"security",
|
|
17
|
+
"authorization",
|
|
18
|
+
"tool-calling",
|
|
19
|
+
"langchain",
|
|
20
|
+
"langgraph",
|
|
21
|
+
"crewai",
|
|
22
|
+
"human-in-the-loop",
|
|
23
|
+
]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 3 - Alpha",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Topic :: Security",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/ShieldaAI/agent-guard/tree/main/clients/python"
|
|
35
|
+
Documentation = "https://github.com/ShieldaAI/agent-guard/blob/main/docs/decide-api.md"
|
|
36
|
+
Issues = "https://github.com/ShieldaAI/agent-guard/issues"
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = ["pytest>=8"]
|
|
40
|
+
frameworks = [
|
|
41
|
+
"langchain-core>=0.3",
|
|
42
|
+
"crewai>=0.100",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.uv.build-backend]
|
|
46
|
+
module-name = "shieldaai.agent_guard"
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shieldaai-agent-guard"
|
|
7
|
+
version = "0.1.0.dev0"
|
|
8
|
+
description = "Python client for the Agent Guard decision server: ask before every AI agent tool call, run it, report back. LangChain, LangGraph and CrewAI helpers. No runtime dependencies."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
dependencies = []
|
|
14
|
+
keywords = ["ai-agents", "security", "authorization", "tool-calling", "langchain", "langgraph", "crewai", "human-in-the-loop"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Topic :: Security",
|
|
21
|
+
"Typing :: Typed",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/ShieldaAI/agent-guard/tree/main/clients/python"
|
|
26
|
+
Documentation = "https://github.com/ShieldaAI/agent-guard/blob/main/docs/decide-api.md"
|
|
27
|
+
Issues = "https://github.com/ShieldaAI/agent-guard/issues"
|
|
28
|
+
|
|
29
|
+
[dependency-groups]
|
|
30
|
+
dev = ["pytest>=8"]
|
|
31
|
+
frameworks = ["langchain-core>=0.3", "crewai>=0.100"]
|
|
32
|
+
|
|
33
|
+
[tool.uv.build-backend]
|
|
34
|
+
module-name = "shieldaai.agent_guard"
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
testpaths = ["tests"]
|