mitrity 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. mitrity-0.2.0/.gitignore +36 -0
  2. mitrity-0.2.0/LICENSE +202 -0
  3. mitrity-0.2.0/PKG-INFO +341 -0
  4. mitrity-0.2.0/README.md +300 -0
  5. mitrity-0.2.0/SECURITY.md +34 -0
  6. mitrity-0.2.0/pyproject.toml +105 -0
  7. mitrity-0.2.0/src/mitrity/__init__.py +20 -0
  8. mitrity-0.2.0/src/mitrity/admission/__init__.py +100 -0
  9. mitrity-0.2.0/src/mitrity/admission/_canonical.py +72 -0
  10. mitrity-0.2.0/src/mitrity/admission/_client.py +497 -0
  11. mitrity-0.2.0/src/mitrity/admission/_config.py +209 -0
  12. mitrity-0.2.0/src/mitrity/admission/_errors.py +48 -0
  13. mitrity-0.2.0/src/mitrity/admission/_types.py +235 -0
  14. mitrity-0.2.0/src/mitrity/claude_agent_sdk/__init__.py +24 -0
  15. mitrity-0.2.0/src/mitrity/claude_agent_sdk/_governor.py +513 -0
  16. mitrity-0.2.0/src/mitrity/crewai/__init__.py +24 -0
  17. mitrity-0.2.0/src/mitrity/crewai/_governed.py +333 -0
  18. mitrity-0.2.0/src/mitrity/langchain/__init__.py +24 -0
  19. mitrity-0.2.0/src/mitrity/langchain/_governed.py +373 -0
  20. mitrity-0.2.0/src/mitrity/openai_agents/__init__.py +34 -0
  21. mitrity-0.2.0/src/mitrity/openai_agents/_governed.py +1115 -0
  22. mitrity-0.2.0/src/mitrity/py.typed +0 -0
  23. mitrity-0.2.0/tests/__init__.py +0 -0
  24. mitrity-0.2.0/tests/conftest.py +28 -0
  25. mitrity-0.2.0/tests/fake_edge.py +253 -0
  26. mitrity-0.2.0/tests/test_canonical.py +36 -0
  27. mitrity-0.2.0/tests/test_claude_agent_sdk.py +468 -0
  28. mitrity-0.2.0/tests/test_client.py +258 -0
  29. mitrity-0.2.0/tests/test_config.py +140 -0
  30. mitrity-0.2.0/tests/test_crewai.py +230 -0
  31. mitrity-0.2.0/tests/test_decide.py +132 -0
  32. mitrity-0.2.0/tests/test_langchain.py +188 -0
  33. mitrity-0.2.0/tests/test_openai_agents.py +1001 -0
@@ -0,0 +1,36 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .eggs/
6
+ build/
7
+ dist/
8
+ .venv*/
9
+ venv/
10
+
11
+ # Tooling
12
+ .mypy_cache/
13
+ .ruff_cache/
14
+ .pytest_cache/
15
+ .coverage
16
+ coverage.xml
17
+ htmlcov/
18
+
19
+ # Editors and OS
20
+ .idea/
21
+ .vscode/
22
+ .DS_Store
23
+
24
+ # Agent worktrees
25
+ .claude/worktrees/
26
+
27
+ # Files the review workflows write into a checkout when run locally
28
+ /diff.txt
29
+ /diff_truncated.txt
30
+ /diff_nolock.txt
31
+ /review.md
32
+ /verdict.txt
33
+ /comment.md
34
+ /comment_truncated.md
35
+ /untrusted_input.md
36
+ /pr.json
mitrity-0.2.0/LICENSE ADDED
@@ -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 MITRITY AB
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.
mitrity-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,341 @@
1
+ Metadata-Version: 2.5
2
+ Name: mitrity
3
+ Version: 0.2.0
4
+ Summary: MITRITY governance adapter for Python agents: Claude Agent SDK, LangChain, OpenAI Agents SDK and CrewAI integrations for the MITRITY edge (admission API and gateway).
5
+ Project-URL: Homepage, https://mitrity.com
6
+ Project-URL: Source, https://github.com/mitrity-io/mitrity-python
7
+ Project-URL: Contract, https://github.com/mitrity-io/iag-specs/blob/main/sentinel/adapters.md
8
+ Author-email: MITRITY AB <hello@mitrity.com>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: agents,claude-agent-sdk,crewai,governance,langchain,mcp,mitrity,openai-agents
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Security
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.10
22
+ Requires-Dist: httpx<1,>=0.27
23
+ Provides-Extra: claude-agent-sdk
24
+ Requires-Dist: claude-agent-sdk>=0.2.157; extra == 'claude-agent-sdk'
25
+ Provides-Extra: crewai
26
+ Requires-Dist: crewai>=1.15; extra == 'crewai'
27
+ Provides-Extra: dev
28
+ Requires-Dist: anyio>=4.4; extra == 'dev'
29
+ Requires-Dist: claude-agent-sdk>=0.2.157; extra == 'dev'
30
+ Requires-Dist: crewai>=1.15; extra == 'dev'
31
+ Requires-Dist: langchain-core>=1.0; extra == 'dev'
32
+ Requires-Dist: mypy>=1.14; extra == 'dev'
33
+ Requires-Dist: openai-agents>=0.20; extra == 'dev'
34
+ Requires-Dist: pytest>=8.4; extra == 'dev'
35
+ Requires-Dist: ruff>=0.8; extra == 'dev'
36
+ Provides-Extra: langchain
37
+ Requires-Dist: langchain-core>=1.0; extra == 'langchain'
38
+ Provides-Extra: openai-agents
39
+ Requires-Dist: openai-agents>=0.20; extra == 'openai-agents'
40
+ Description-Content-Type: text/markdown
41
+
42
+ # mitrity
43
+
44
+ MITRITY governance adapter for Python agents: Claude Agent SDK, LangChain,
45
+ OpenAI Agents SDK and CrewAI integrations for the MITRITY edge (admission API
46
+ and gateway).
47
+
48
+ The MITRITY gateway governs what an agent asks it to do over MCP. It cannot
49
+ see what the agent's *framework* does on its own — the Agent SDK's `Bash`,
50
+ `Write`, `Edit` and `WebFetch`, a LangChain `ShellTool`. This package closes
51
+ that gap: every such call is admitted by the co-located edge **before it
52
+ runs**, with the same policy rules, command analysis, DLP and holds an MCP call
53
+ gets, and the same audit trail (`surface=agent_hook`). If the edge cannot be
54
+ reached, the call is denied — there is no fail-open mode.
55
+
56
+ Contract: [iag-specs/sentinel/adapters.md](https://github.com/mitrity-io/iag-specs/blob/main/sentinel/adapters.md)
57
+ (wire protocol: [sentinel/admission-api.md](https://github.com/mitrity-io/iag-specs/blob/main/sentinel/admission-api.md)).
58
+
59
+ ## Install
60
+
61
+ Until the first PyPI release, install from git at a pinned ref (a release tag
62
+ once one exists, a commit SHA until then):
63
+
64
+ ```bash
65
+ pip install "mitrity[claude-agent-sdk] @ git+https://github.com/mitrity-io/mitrity-python.git@<ref>"
66
+ pip install "mitrity[langchain] @ git+https://github.com/mitrity-io/mitrity-python.git@<ref>"
67
+ pip install "mitrity[openai-agents] @ git+https://github.com/mitrity-io/mitrity-python.git@<ref>"
68
+ pip install "mitrity[crewai] @ git+https://github.com/mitrity-io/mitrity-python.git@<ref>"
69
+ ```
70
+
71
+ Python ≥ 3.10. The only runtime dependency is `httpx`; the framework extras
72
+ pull in `claude-agent-sdk`, `langchain-core`, `openai-agents` and `crewai`
73
+ respectively.
74
+
75
+ ## Prerequisite: a co-located edge
76
+
77
+ The adapter talks to a `mitrity-gateway` (or `mitrity-mcp-sidecar`) running
78
+ next to the agent with an `admission` block:
79
+
80
+ ```yaml
81
+ admission:
82
+ enabled: true
83
+ listen_addr: "unix:/run/mitrity/admission.sock"
84
+ token_file: "/run/mitrity/admission.token"
85
+ ```
86
+
87
+ The adapter finds it through the same environment variables `mitrity-hook`
88
+ uses, with the same defaults:
89
+
90
+ | Variable | Default | Meaning |
91
+ | --- | --- | --- |
92
+ | `MITRITY_ADMISSION_ADDR` | `unix:/run/mitrity/admission.sock` (`127.0.0.1:8777` on Windows) | The edge's admission listener. Must be a Unix socket or loopback; anything else is refused. |
93
+ | `MITRITY_ADMISSION_TOKEN_FILE` | `/run/mitrity/admission.token` | The per-process token the edge writes at startup. |
94
+ | `MITRITY_HOOK_TIMEOUT` | `500ms` (max `30s`) | Deadline for one decision. |
95
+ | `MITRITY_HOOK_HOLD_TIMEOUT` | `540s` (max `570s`) | Longest to wait on a human approval; `0` disables waiting. |
96
+ | `MITRITY_HOOK_FAIL_MODE` | — | Ignored. Adapters have no fail-open mode. |
97
+
98
+ ## Claude Agent SDK
99
+
100
+ ```python
101
+ from claude_agent_sdk import query
102
+ from mitrity.claude_agent_sdk import governed_options
103
+
104
+ options = governed_options(
105
+ gateway={
106
+ "type": "stdio",
107
+ "command": "mitrity-gateway",
108
+ "args": ["--config", "/etc/mitrity/gateway.yaml"],
109
+ },
110
+ allowed_tools=["Bash", "Read", "Write", "mcp__mitrity"],
111
+ system_prompt="You are a careful engineering agent.",
112
+ )
113
+
114
+ async for message in query(prompt="Clean up the build directory", options=options):
115
+ print(message)
116
+ ```
117
+
118
+ What `governed_options()` does:
119
+
120
+ - Installs a `PreToolUse` hook over the execution-capable built-ins (`Bash`,
121
+ `Write`, `Edit`, `MultiEdit`, `NotebookEdit`, `WebFetch`, `WebSearch`) that
122
+ asks the edge before each call. An allow is silent, so your own
123
+ `allowed_tools` / `can_use_tool` flow still applies on top. A deny reaches
124
+ the model as `permissionDecision: "deny"` with the policy reason. A hold
125
+ waits for a human (up to `MITRITY_HOOK_HOLD_TIMEOUT`) and is a deny if nobody
126
+ approves.
127
+ - Pins `mcp_servers` to the gateway entry (plus anything you add, which is
128
+ reported as ungoverned), sets `strict_mcp_config=True` and
129
+ `setting_sources=[]` unless you override them.
130
+ - Attests the runtime's posture (`POST /v1/attest`) on the first prompt of
131
+ each session: which tools are hooked, which are not, other MCP servers,
132
+ permission mode, sandbox settings. The Python SDK has no `SessionStart`
133
+ callback hook, so the first `UserPromptSubmit` is the trigger.
134
+ - Keeps every other option you pass. Your own hooks run after MITRITY's; a
135
+ deny from any hook wins.
136
+
137
+ For statistics or the attestation object, build the `Governor` yourself:
138
+
139
+ ```python
140
+ from mitrity.claude_agent_sdk import Governor
141
+
142
+ governor = Governor(gateway=gateway_config)
143
+ options = governor.options(allowed_tools=["Bash"])
144
+ ...
145
+ print(governor.stats) # admitted / allowed / denied / held / unreachable / routed
146
+ print(governor.attestation()) # what the control plane was told
147
+ ```
148
+
149
+ ### Routed Bash (governed shell)
150
+
151
+ When the agent's policy sets `builtin_exec_routing: governed_shell`, an
152
+ allowed `Bash` comes back with `updated_input` rewriting the command to
153
+ `mitrity-hook exec <ticket>`. The adapter emits it as `updatedInput` with an
154
+ explicit allow, and the framework runs the relay instead of the model's
155
+ command; the gateway executes the judged bytes in its own sandbox. This needs
156
+ the `mitrity-hook` binary on `PATH` and a gateway with `exec.enabled: true`.
157
+
158
+ ### Two approval records per hold
159
+
160
+ The adapter asks twice for a held action: once without waiting (so an
161
+ unreachable edge is noticed in milliseconds), then again with the hold budget.
162
+ The edge creates an approval for each; resolve whichever is pending. The
163
+ stale one times out on its own. This matches `mitrity-hook`.
164
+
165
+ ## LangChain
166
+
167
+ ```python
168
+ from langchain_community.tools import ShellTool
169
+ from mitrity.langchain import govern_tools
170
+
171
+ tools = govern_tools([ShellTool(), my_write_tool], session_id=thread_id)
172
+ ```
173
+
174
+ `govern(tool)` returns a `BaseTool` with the same name, description and
175
+ schema. Its `_run` / `_arun` admit the call first; a deny raises
176
+ `ToolException` with the edge's reason (set `handle_tool_error=True` on the
177
+ tool if you want the reason fed back to the model as an error result rather
178
+ than raised), and an `updated_input` from the edge replaces the arguments
179
+ before the wrapped tool runs.
180
+
181
+ Two limits:
182
+
183
+ - **Coverage is what you hand it.** A tool that did not go through `govern`
184
+ is invisible to the adapter and to the attestation. `govern_tools` is the
185
+ boundary.
186
+ - **Argument names stay yours.** The edge parses commands from the keys
187
+ `command`, `cmd`, `script`, `args` and `argv`. LangChain's `ShellTool`
188
+ calls its argument `commands`, which today is DLP- and content-scanned but
189
+ not parsed as a command tree. The adapter does not rename it; that key
190
+ joins the edge's command family on the edge side.
191
+
192
+ The session id is, in order: the `session_id` you pass, the
193
+ `configurable.thread_id` of the run config, or one id per process.
194
+
195
+ ## OpenAI Agents SDK
196
+
197
+ ```python
198
+ from agents import Agent, Runner, ShellTool, function_tool
199
+ from mitrity.openai_agents import govern
200
+
201
+
202
+ @function_tool
203
+ def run_command(command: str) -> str:
204
+ """Run a shell command on the build host and return its output."""
205
+ ...
206
+
207
+
208
+ agent = govern(Agent(name="ops", tools=[run_command, ShellTool(executor=my_executor)]))
209
+ result = await Runner.run(agent, "clean up the build directory")
210
+ ```
211
+
212
+ `govern(agent)` returns a clone of the agent whose tools ask the edge before
213
+ they run; `govern_tools(tools)` does the same for a list. Each tool type is
214
+ blocked the way the SDK itself blocks that type:
215
+
216
+ - A **function tool** gets a *tool input guardrail*: a deny is
217
+ `reject_content` with the policy reason, which the SDK hands to the model
218
+ as the tool's output without running the tool. An allow is silent. An
219
+ `updated_input` from the edge is merged into the JSON arguments the
220
+ function receives.
221
+ - **`ShellTool`** and **`ApplyPatchTool`** go through the SDK's approval
222
+ flow: the adapter's `needs_approval` asks the edge, and on a deny its
223
+ `on_approval` rejects the call with the reason — as it rejects an approval
224
+ it cannot tie to a decision it made. Your own `needs_approval` and
225
+ `on_approval` still apply when MITRITY allows. The execution channel is
226
+ gated as well: the wrapped shell executor and the wrapped `apply_patch`
227
+ editor re-check the decision `needs_approval` made, and admit a call the
228
+ SDK runs without asking (an approved call on a resumed run); a deny there
229
+ reaches the model as the tool's failed output. The shell executor runs the
230
+ edge's rewritten `commands` when it sends them; an `apply_patch` rewrite
231
+ is a deny (the editor receives the operation alone, with nothing to apply
232
+ a rewrite to). A `ShellTool` whose `environment` is a hosted container
233
+ runs in OpenAI's cloud and passes through like the hosted tools below.
234
+ - **`LocalShellTool`** (deprecated upstream) has neither a guardrail nor an
235
+ approval, so a deny is returned as the executor's output with nothing run.
236
+ - **Hosted tools** (web search, file search, code interpreter, image
237
+ generation, hosted MCP) run in OpenAI's cloud and `ComputerTool` drives a
238
+ computer the SDK does not judge: they pass through untouched and are
239
+ reported as ungoverned in the attestation.
240
+
241
+ Judged bytes are the executed bytes. A decision is kept for the tool, the
242
+ SDK's `call_id` and a digest of the input it was made for; the invoker,
243
+ executor or editor reuses it only for that exact input, and an execution
244
+ whose input differs from what the edge judged raises `MitrityDenied` rather
245
+ than running under that decision.
246
+
247
+ MCP servers on the agent are not touched. Pass the MITRITY gateway server as
248
+ `gateway=` so it is not reported as ungoverned; every other server is. The
249
+ session id is, in order: the `session_id` you pass, `RunConfig.group_id`, or
250
+ one id per process. Handoff targets given as `Agent` objects are governed
251
+ too, once each — a specialist handing back to the triage agent, or two paths
252
+ to the same agent, resolve to one governed clone; a `Handoff` object built
253
+ with `handoff()` is left as is, so govern its agent before building it. A
254
+ tool type the adapter does not know passes through with a warning and is
255
+ reported as ungoverned.
256
+
257
+ ## CrewAI
258
+
259
+ ```python
260
+ from crewai import Agent
261
+ from mitrity.crewai import govern_tools
262
+
263
+ agent = Agent(role="ops", goal="...", backstory="...", tools=govern_tools([terminal, writer]))
264
+ ```
265
+
266
+ `govern(tool)` returns a `BaseTool` with the same name, description and
267
+ schema whose `_run` / `_arun` admit the call first. A deny is returned as a
268
+ `ToolFailure` carrying the policy reason — CrewAI's declared channel for a
269
+ tool that did not do what it was asked: the agent sees the reason, the
270
+ failure is recorded on the task output, and the agent's `tool_failure_policy`
271
+ decides whether to continue or abort. An `updated_input` from the edge
272
+ replaces the arguments before the wrapped tool runs; a rewrite of several
273
+ positional arguments must cover every one of them or the call is blocked.
274
+
275
+ A governed tool never caches: CrewAI's tool cache would replay an earlier
276
+ result for the same arguments without the edge judging the call again, so
277
+ the inner tool's `cache_function` is not copied and the governed one always
278
+ answers no. Govern a tool before its first run — the crew reads its cache by
279
+ tool name before the tool is reached, so a result an ungoverned run of the
280
+ same tool cached earlier would still be served.
281
+
282
+ Coverage is what you hand it. Tools CrewAI adds to an agent on its own — the
283
+ delegation tools, the code interpreter behind `allow_code_execution=True`,
284
+ MCP tools from `mcps` — never pass through `govern` and are invisible to the
285
+ adapter; on Linux hosts the MITRITY observer reports their executions after
286
+ the fact.
287
+
288
+ ## The wire client
289
+
290
+ Both integrations sit on `mitrity.admission.Client`, which you can use for
291
+ any framework:
292
+
293
+ ```python
294
+ from mitrity.admission import AdmitRequest, Client
295
+
296
+ client = Client() # discovers the edge from the environment
297
+ verdict = client.decide(
298
+ AdmitRequest(surface="custom", tool_name="Bash", tool_input={"command": "rm -rf build"})
299
+ )
300
+ if not verdict.allowed:
301
+ raise RuntimeError(verdict.reason)
302
+ ```
303
+
304
+ `decide()` never raises: it returns a `Verdict` whose `reason` is safe to
305
+ show the model and whose `error` is set when the deny is the adapter's own
306
+ (the edge could not be reached) rather than a policy decision. `admit()` is
307
+ the single round trip and raises an `AdmissionError` subclass on any failure.
308
+ Async variants: `decide_async`, `admit_async`, `attest_async`.
309
+
310
+ ## What is not governed
311
+
312
+ - Tools outside the hook matcher, or LangChain / CrewAI tools not passed to
313
+ `govern`. The attestation names them; the MITRITY dashboard shows the gap.
314
+ - Hosted tools of the OpenAI Agents SDK (they run in the vendor's cloud) and
315
+ its `ComputerTool`; reported as ungoverned in the attestation.
316
+ - The agent's own code: `subprocess.run` in your application never passes a
317
+ tool boundary. On Linux hosts the MITRITY observer reports it after the
318
+ fact.
319
+ - Removing the adapter. It is your code; the control plane detects the
320
+ missing attestation and the silent admission counters, it cannot prevent
321
+ the edit.
322
+
323
+ ## Development
324
+
325
+ ```bash
326
+ python -m venv .venv && . .venv/bin/activate
327
+ pip install -e ".[dev]"
328
+ ruff check . && ruff format --check . && mypy --strict && pytest
329
+ ```
330
+
331
+ Tests run against an in-process fake edge over a Unix socket; no MITRITY
332
+ account and no network are needed. The conformance tests are numbered after
333
+ the contract (`C1`–`C28`).
334
+
335
+ ## Security
336
+
337
+ See [SECURITY.md](SECURITY.md). Report vulnerabilities to soc@mitrity.com.
338
+
339
+ ## License
340
+
341
+ Apache-2.0. Copyright 2026 MITRITY AB.