trustgate-sdk 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.
- trustgate_sdk-0.1.0/.gitignore +15 -0
- trustgate_sdk-0.1.0/LICENSE +201 -0
- trustgate_sdk-0.1.0/PKG-INFO +160 -0
- trustgate_sdk-0.1.0/README.md +132 -0
- trustgate_sdk-0.1.0/pyproject.toml +59 -0
- trustgate_sdk-0.1.0/src/trustgate/__init__.py +95 -0
- trustgate_sdk-0.1.0/src/trustgate/agent.py +208 -0
- trustgate_sdk-0.1.0/src/trustgate/client.py +180 -0
- trustgate_sdk-0.1.0/src/trustgate/config.py +51 -0
- trustgate_sdk-0.1.0/src/trustgate/connections.py +100 -0
- trustgate_sdk-0.1.0/src/trustgate/errors.py +180 -0
- trustgate_sdk-0.1.0/src/trustgate/formats.py +390 -0
- trustgate_sdk-0.1.0/src/trustgate/mcp.py +179 -0
- trustgate_sdk-0.1.0/src/trustgate/py.typed +0 -0
- trustgate_sdk-0.1.0/src/trustgate/schema.py +185 -0
- trustgate_sdk-0.1.0/src/trustgate/transport.py +113 -0
- trustgate_sdk-0.1.0/src/trustgate/types.py +116 -0
- trustgate_sdk-0.1.0/src/trustgate/whoami.py +184 -0
- trustgate_sdk-0.1.0/tests/__init__.py +0 -0
- trustgate_sdk-0.1.0/tests/fake_gateway.py +164 -0
- trustgate_sdk-0.1.0/tests/test_connect.py +320 -0
- trustgate_sdk-0.1.0/tests/test_schema.py +137 -0
- trustgate_sdk-0.1.0/tests/test_toolkit.py +300 -0
- trustgate_sdk-0.1.0/uv.lock +184 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
node_modules/
|
|
2
|
+
dist/
|
|
3
|
+
*.tsbuildinfo
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.egg-info/
|
|
6
|
+
.venv/
|
|
7
|
+
.pytest_cache/
|
|
8
|
+
.coverage
|
|
9
|
+
|
|
10
|
+
# Filled in from the .env.example next to it; holds real keys.
|
|
11
|
+
.env
|
|
12
|
+
# uv's resolved lock for the examples: they are a demo, not a shipped artifact,
|
|
13
|
+
# and pinning them would go stale against the SDK they build from source.
|
|
14
|
+
examples/python/uv.lock
|
|
15
|
+
examples/typescript/package-lock.json
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and do
|
|
117
|
+
not modify the License. You may add Your own attribution notices
|
|
118
|
+
within Derivative Works that You distribute, alongside or as an
|
|
119
|
+
addendum to the NOTICE text from the Work, provided that such
|
|
120
|
+
additional attribution notices cannot be construed as modifying
|
|
121
|
+
the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 NeuralTrust
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: trustgate-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: TrustGate SDK - governed tools and models for agents, over the MCP and LLM planes of a TrustGate gateway.
|
|
5
|
+
Project-URL: Homepage, https://github.com/NeuralTrust/trustgate-sdk
|
|
6
|
+
Project-URL: Documentation, https://docs.neuraltrust.ai/trustgate/mcp/connect
|
|
7
|
+
Project-URL: Issues, https://github.com/NeuralTrust/trustgate-sdk/issues
|
|
8
|
+
Author: NeuralTrust
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agents,gateway,llm,mcp,model-context-protocol,neuraltrust,trustgate
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
26
|
+
Requires-Dist: ruff>=0.15; extra == 'dev'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# trustgate-sdk
|
|
30
|
+
|
|
31
|
+
The TrustGate SDK for Python. 3.10+, standard library only.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install trustgate-sdk
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Setup
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from trustgate import TrustGate, ToolFormat
|
|
41
|
+
|
|
42
|
+
tg = TrustGate() # TRUSTGATE_URL + TRUSTGATE_API_KEY
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Two values, or none if they are in the environment. The applications behind
|
|
46
|
+
the key are asked for: `tg.identity()` asks the gateway once and remembers it.
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
identity = tg.identity()
|
|
50
|
+
# identity.key -> KeyInfo(name="prod", expires_at=None) # None means never
|
|
51
|
+
# KeyConsumer(slug="support-agent", type="MCP", url="https://…/support-agent/mcp",
|
|
52
|
+
# upstreams=[KeyUpstream(server="Notion", account="shared", connected=True)])
|
|
53
|
+
# KeyConsumer(slug="support-llm", type="LLM", url="https://…/support-llm/v1")
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`key.expires_at` is the 401 you would otherwise meet mid-run, and `upstreams`
|
|
57
|
+
is the refusal you would otherwise meet on the first tool call — both answered
|
|
58
|
+
before anything starts.
|
|
59
|
+
|
|
60
|
+
`mcp_consumer` and `llm_consumer` (or `TRUSTGATE_MCP_CONSUMER` /
|
|
61
|
+
`TRUSTGATE_LLM_CONSUMER`) are only needed when a key reaches two applications on
|
|
62
|
+
the same plane — the SDK names them and refuses rather than guessing.
|
|
63
|
+
|
|
64
|
+
## An agent that acts as the application
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
agent = tg.connect(requires=["search"])
|
|
68
|
+
|
|
69
|
+
agent.mcp # url + headers for a framework
|
|
70
|
+
tg.llm() # base_url for OpenAI, anthropic_base_url for Anthropic
|
|
71
|
+
toolkit = agent.toolkit(ToolFormat.OPENAI_RESPONSES)
|
|
72
|
+
agent.call_tool("search", {"query": "runbook"})
|
|
73
|
+
agent.refresh() # re-read the toolkit
|
|
74
|
+
agent.connections # its own upstream accounts
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
A batch that must not stop halfway:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
try:
|
|
81
|
+
agent = tg.connect(requires=["search"])
|
|
82
|
+
except UpstreamNotConnectedError as error:
|
|
83
|
+
sys.exit(str(error)) # names each server and who fixes it
|
|
84
|
+
|
|
85
|
+
for row in rows:
|
|
86
|
+
agent.call_tool("search", {"query": row.query})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The gateway serves a tool under the server it came from - Notion's `search` as
|
|
90
|
+
`notion_search` - so two servers with a `search` stay apart. That prefix is the
|
|
91
|
+
gateway's, so `requires` and `call_tool` take the name the server itself gave the
|
|
92
|
+
tool and add it; naming a tool two of your servers serve is the one case they
|
|
93
|
+
ask instead.
|
|
94
|
+
|
|
95
|
+
## An agent acting for one of its users
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
alice = tg.for_end_user("user_123")
|
|
99
|
+
# or, from an agent you already have - no round trip, same toolkit:
|
|
100
|
+
bob = agent.for_end_user("user_456")
|
|
101
|
+
|
|
102
|
+
alice.connections()
|
|
103
|
+
alice.connect_link("com.notion/mcp")
|
|
104
|
+
alice.toolkit(ToolFormat.ANTHROPIC_MESSAGES)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Both handles work on the same application and the same key: who a call runs as
|
|
108
|
+
comes from the call, not from anything configured on the application. The name is
|
|
109
|
+
yours to choose and the gateway namespaces it, so two applications naming
|
|
110
|
+
`user_123` never reach the same account.
|
|
111
|
+
|
|
112
|
+
## The model call, with tools
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
toolkit = agent.toolkit(ToolFormat.OPENAI_RESPONSES, strict=True)
|
|
116
|
+
for warning in toolkit.warnings:
|
|
117
|
+
log.warning("%s could not be strict: %s", warning.tool, warning.reason)
|
|
118
|
+
|
|
119
|
+
response = client.responses.create(model="gpt-5.2", tools=toolkit.tools, input=messages)
|
|
120
|
+
while any(item.type == "function_call" for item in response.output):
|
|
121
|
+
response = client.responses.create(
|
|
122
|
+
model="gpt-5.2",
|
|
123
|
+
tools=toolkit.tools,
|
|
124
|
+
previous_response_id=response.id,
|
|
125
|
+
input=toolkit.execute(response.output),
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`execute()` reads the provider's own objects as well as plain dicts, so the
|
|
130
|
+
response from the OpenAI or Anthropic SDK can be passed straight in.
|
|
131
|
+
|
|
132
|
+
## Your own HTTP client
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
tg = TrustGate(..., transport=MyTransport())
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`Transport` is a one-method protocol. The default is `UrllibTransport`; the
|
|
139
|
+
tests use a fake one. It is also where a retry policy or a proxy belongs.
|
|
140
|
+
|
|
141
|
+
## Errors
|
|
142
|
+
|
|
143
|
+
| Class | When |
|
|
144
|
+
|---|---|
|
|
145
|
+
| `MissingToolsError` | `requires` names a tool the application does not serve |
|
|
146
|
+
| `UpstreamNotConnectedError` | a server the application calls has no account behind it; `servers` names them and the message says who connects it |
|
|
147
|
+
| `ConsentRequiredError` | an end user has not connected; carries `connect_url` |
|
|
148
|
+
| `PolicyBlockedError` | a gateway policy refused the call |
|
|
149
|
+
| `ToolNotFoundError` | the tool left the toolkit under a running agent |
|
|
150
|
+
| `PlaneUnavailableError` | the key reaches no application on that plane |
|
|
151
|
+
| `AuthenticationError`, `RateLimitedError`, `ServiceUnavailableError`, `TrustGateServerError` | as named |
|
|
152
|
+
|
|
153
|
+
## Development
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
uv sync --extra dev
|
|
157
|
+
uv run pytest
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
[CONTRIBUTING.md](../CONTRIBUTING.md) has every check CI runs.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# trustgate-sdk
|
|
2
|
+
|
|
3
|
+
The TrustGate SDK for Python. 3.10+, standard library only.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install trustgate-sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Setup
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from trustgate import TrustGate, ToolFormat
|
|
13
|
+
|
|
14
|
+
tg = TrustGate() # TRUSTGATE_URL + TRUSTGATE_API_KEY
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Two values, or none if they are in the environment. The applications behind
|
|
18
|
+
the key are asked for: `tg.identity()` asks the gateway once and remembers it.
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
identity = tg.identity()
|
|
22
|
+
# identity.key -> KeyInfo(name="prod", expires_at=None) # None means never
|
|
23
|
+
# KeyConsumer(slug="support-agent", type="MCP", url="https://…/support-agent/mcp",
|
|
24
|
+
# upstreams=[KeyUpstream(server="Notion", account="shared", connected=True)])
|
|
25
|
+
# KeyConsumer(slug="support-llm", type="LLM", url="https://…/support-llm/v1")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`key.expires_at` is the 401 you would otherwise meet mid-run, and `upstreams`
|
|
29
|
+
is the refusal you would otherwise meet on the first tool call — both answered
|
|
30
|
+
before anything starts.
|
|
31
|
+
|
|
32
|
+
`mcp_consumer` and `llm_consumer` (or `TRUSTGATE_MCP_CONSUMER` /
|
|
33
|
+
`TRUSTGATE_LLM_CONSUMER`) are only needed when a key reaches two applications on
|
|
34
|
+
the same plane — the SDK names them and refuses rather than guessing.
|
|
35
|
+
|
|
36
|
+
## An agent that acts as the application
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
agent = tg.connect(requires=["search"])
|
|
40
|
+
|
|
41
|
+
agent.mcp # url + headers for a framework
|
|
42
|
+
tg.llm() # base_url for OpenAI, anthropic_base_url for Anthropic
|
|
43
|
+
toolkit = agent.toolkit(ToolFormat.OPENAI_RESPONSES)
|
|
44
|
+
agent.call_tool("search", {"query": "runbook"})
|
|
45
|
+
agent.refresh() # re-read the toolkit
|
|
46
|
+
agent.connections # its own upstream accounts
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A batch that must not stop halfway:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
try:
|
|
53
|
+
agent = tg.connect(requires=["search"])
|
|
54
|
+
except UpstreamNotConnectedError as error:
|
|
55
|
+
sys.exit(str(error)) # names each server and who fixes it
|
|
56
|
+
|
|
57
|
+
for row in rows:
|
|
58
|
+
agent.call_tool("search", {"query": row.query})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The gateway serves a tool under the server it came from - Notion's `search` as
|
|
62
|
+
`notion_search` - so two servers with a `search` stay apart. That prefix is the
|
|
63
|
+
gateway's, so `requires` and `call_tool` take the name the server itself gave the
|
|
64
|
+
tool and add it; naming a tool two of your servers serve is the one case they
|
|
65
|
+
ask instead.
|
|
66
|
+
|
|
67
|
+
## An agent acting for one of its users
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
alice = tg.for_end_user("user_123")
|
|
71
|
+
# or, from an agent you already have - no round trip, same toolkit:
|
|
72
|
+
bob = agent.for_end_user("user_456")
|
|
73
|
+
|
|
74
|
+
alice.connections()
|
|
75
|
+
alice.connect_link("com.notion/mcp")
|
|
76
|
+
alice.toolkit(ToolFormat.ANTHROPIC_MESSAGES)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Both handles work on the same application and the same key: who a call runs as
|
|
80
|
+
comes from the call, not from anything configured on the application. The name is
|
|
81
|
+
yours to choose and the gateway namespaces it, so two applications naming
|
|
82
|
+
`user_123` never reach the same account.
|
|
83
|
+
|
|
84
|
+
## The model call, with tools
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
toolkit = agent.toolkit(ToolFormat.OPENAI_RESPONSES, strict=True)
|
|
88
|
+
for warning in toolkit.warnings:
|
|
89
|
+
log.warning("%s could not be strict: %s", warning.tool, warning.reason)
|
|
90
|
+
|
|
91
|
+
response = client.responses.create(model="gpt-5.2", tools=toolkit.tools, input=messages)
|
|
92
|
+
while any(item.type == "function_call" for item in response.output):
|
|
93
|
+
response = client.responses.create(
|
|
94
|
+
model="gpt-5.2",
|
|
95
|
+
tools=toolkit.tools,
|
|
96
|
+
previous_response_id=response.id,
|
|
97
|
+
input=toolkit.execute(response.output),
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`execute()` reads the provider's own objects as well as plain dicts, so the
|
|
102
|
+
response from the OpenAI or Anthropic SDK can be passed straight in.
|
|
103
|
+
|
|
104
|
+
## Your own HTTP client
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
tg = TrustGate(..., transport=MyTransport())
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`Transport` is a one-method protocol. The default is `UrllibTransport`; the
|
|
111
|
+
tests use a fake one. It is also where a retry policy or a proxy belongs.
|
|
112
|
+
|
|
113
|
+
## Errors
|
|
114
|
+
|
|
115
|
+
| Class | When |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `MissingToolsError` | `requires` names a tool the application does not serve |
|
|
118
|
+
| `UpstreamNotConnectedError` | a server the application calls has no account behind it; `servers` names them and the message says who connects it |
|
|
119
|
+
| `ConsentRequiredError` | an end user has not connected; carries `connect_url` |
|
|
120
|
+
| `PolicyBlockedError` | a gateway policy refused the call |
|
|
121
|
+
| `ToolNotFoundError` | the tool left the toolkit under a running agent |
|
|
122
|
+
| `PlaneUnavailableError` | the key reaches no application on that plane |
|
|
123
|
+
| `AuthenticationError`, `RateLimitedError`, `ServiceUnavailableError`, `TrustGateServerError` | as named |
|
|
124
|
+
|
|
125
|
+
## Development
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
uv sync --extra dev
|
|
129
|
+
uv run pytest
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
[CONTRIBUTING.md](../CONTRIBUTING.md) has every check CI runs.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "trustgate-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "TrustGate SDK - governed tools and models for agents, over the MCP and LLM planes of a TrustGate gateway."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "NeuralTrust" }]
|
|
14
|
+
keywords = ["mcp", "model-context-protocol", "agents", "llm", "gateway", "trustgate", "neuraltrust"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Software Development :: Libraries",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
dependencies = []
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/NeuralTrust/trustgate-sdk"
|
|
32
|
+
Documentation = "https://docs.neuraltrust.ai/trustgate/mcp/connect"
|
|
33
|
+
Issues = "https://github.com/NeuralTrust/trustgate-sdk/issues"
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
dev = ["pytest>=8", "ruff>=0.15"]
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.wheel]
|
|
39
|
+
packages = ["src/trustgate"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
43
|
+
pythonpath = ["src"]
|
|
44
|
+
|
|
45
|
+
[tool.ruff]
|
|
46
|
+
line-length = 100
|
|
47
|
+
target-version = "py310"
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint]
|
|
50
|
+
# pyflakes and pycodestyle errors, import order, and the upgrades a 3.10 floor
|
|
51
|
+
# allows. Nothing opinionated beyond that: the checks are here to catch bugs.
|
|
52
|
+
select = ["E", "F", "W", "I", "UP", "B"]
|
|
53
|
+
# The formatter owns line length; a long string or URL is not a bug.
|
|
54
|
+
ignore = ["E501"]
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint.isort]
|
|
57
|
+
# The examples share one local helper module; keep it apart from the SDK import.
|
|
58
|
+
known-first-party = ["trustgate"]
|
|
59
|
+
known-local-folder = ["_config"]
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""TrustGate SDK - governed tools and models for agents.
|
|
2
|
+
|
|
3
|
+
An admin creates the consumer and decides what it may reach; this package
|
|
4
|
+
points an agent at it. What it adds on top of a plain HTTP call is the part
|
|
5
|
+
that is easy to get wrong: which actor a call speaks as, whether the tools the
|
|
6
|
+
agent was written around are still on its toolkit, and what to do when an
|
|
7
|
+
upstream account is not connected.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .agent import Agent, EndUserAgent, Toolkit
|
|
11
|
+
from .client import LLMEndpoint, TrustGate
|
|
12
|
+
from .config import API_KEY_HEADER, END_USER_HEADER
|
|
13
|
+
from .errors import (
|
|
14
|
+
AuthenticationError,
|
|
15
|
+
ConsentRequiredError,
|
|
16
|
+
InvalidRequestError,
|
|
17
|
+
MissingToolsError,
|
|
18
|
+
PlaneUnavailableError,
|
|
19
|
+
PolicyBlockedError,
|
|
20
|
+
RateLimitedError,
|
|
21
|
+
ServiceUnavailableError,
|
|
22
|
+
ToolNotFoundError,
|
|
23
|
+
TrustGateError,
|
|
24
|
+
TrustGateServerError,
|
|
25
|
+
UpstreamNotConnectedError,
|
|
26
|
+
)
|
|
27
|
+
from .formats import ConversionWarning, adapter_for, result_to_text
|
|
28
|
+
from .mcp import MCPTransport
|
|
29
|
+
from .schema import StrictResult, inline_refs, strip_injected_nulls, to_strict
|
|
30
|
+
from .transport import Response, Transport, UrllibTransport
|
|
31
|
+
from .types import (
|
|
32
|
+
Actor,
|
|
33
|
+
Connection,
|
|
34
|
+
ConnectLink,
|
|
35
|
+
Endpoint,
|
|
36
|
+
GatewayTool,
|
|
37
|
+
ToolCall,
|
|
38
|
+
ToolFormat,
|
|
39
|
+
)
|
|
40
|
+
from .whoami import (
|
|
41
|
+
KeyConsumer,
|
|
42
|
+
KeyIdentity,
|
|
43
|
+
KeyInfo,
|
|
44
|
+
KeyUpstream,
|
|
45
|
+
select_consumer,
|
|
46
|
+
who_am_i,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
__version__ = "0.1.0"
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
"Actor",
|
|
53
|
+
"Agent",
|
|
54
|
+
"API_KEY_HEADER",
|
|
55
|
+
"AuthenticationError",
|
|
56
|
+
"ConnectLink",
|
|
57
|
+
"Connection",
|
|
58
|
+
"ConsentRequiredError",
|
|
59
|
+
"ConversionWarning",
|
|
60
|
+
"END_USER_HEADER",
|
|
61
|
+
"Endpoint",
|
|
62
|
+
"EndUserAgent",
|
|
63
|
+
"GatewayTool",
|
|
64
|
+
"KeyConsumer",
|
|
65
|
+
"KeyIdentity",
|
|
66
|
+
"KeyInfo",
|
|
67
|
+
"KeyUpstream",
|
|
68
|
+
"InvalidRequestError",
|
|
69
|
+
"LLMEndpoint",
|
|
70
|
+
"MCPTransport",
|
|
71
|
+
"MissingToolsError",
|
|
72
|
+
"PlaneUnavailableError",
|
|
73
|
+
"PolicyBlockedError",
|
|
74
|
+
"RateLimitedError",
|
|
75
|
+
"Response",
|
|
76
|
+
"ServiceUnavailableError",
|
|
77
|
+
"StrictResult",
|
|
78
|
+
"ToolCall",
|
|
79
|
+
"ToolFormat",
|
|
80
|
+
"ToolNotFoundError",
|
|
81
|
+
"Toolkit",
|
|
82
|
+
"Transport",
|
|
83
|
+
"TrustGate",
|
|
84
|
+
"TrustGateError",
|
|
85
|
+
"TrustGateServerError",
|
|
86
|
+
"UpstreamNotConnectedError",
|
|
87
|
+
"UrllibTransport",
|
|
88
|
+
"adapter_for",
|
|
89
|
+
"inline_refs",
|
|
90
|
+
"result_to_text",
|
|
91
|
+
"select_consumer",
|
|
92
|
+
"strip_injected_nulls",
|
|
93
|
+
"to_strict",
|
|
94
|
+
"who_am_i",
|
|
95
|
+
]
|