cred-semantic-kernel 1.0.0__tar.gz → 2.0.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.
- {cred_semantic_kernel-1.0.0 → cred_semantic_kernel-2.0.0}/.gitignore +7 -0
- cred_semantic_kernel-2.0.0/LICENSE +160 -0
- cred_semantic_kernel-2.0.0/NOTICE +8 -0
- cred_semantic_kernel-2.0.0/PKG-INFO +102 -0
- cred_semantic_kernel-2.0.0/README.md +81 -0
- cred_semantic_kernel-2.0.0/cred_semantic_kernel/plugin.py +240 -0
- {cred_semantic_kernel-1.0.0 → cred_semantic_kernel-2.0.0}/pyproject.toml +9 -3
- cred_semantic_kernel-2.0.0/tests/test_plugin.py +413 -0
- cred_semantic_kernel-1.0.0/PKG-INFO +0 -76
- cred_semantic_kernel-1.0.0/README.md +0 -60
- cred_semantic_kernel-1.0.0/cred_semantic_kernel/plugin.py +0 -88
- cred_semantic_kernel-1.0.0/tests/test_plugin.py +0 -187
- {cred_semantic_kernel-1.0.0 → cred_semantic_kernel-2.0.0}/cred_semantic_kernel/__init__.py +0 -0
- {cred_semantic_kernel-1.0.0 → cred_semantic_kernel-2.0.0}/tests/__init__.py +0 -0
|
@@ -25,12 +25,18 @@ HANDOFF.md
|
|
|
25
25
|
VALIDATION.md
|
|
26
26
|
MARKETING-VALIDATION.md
|
|
27
27
|
FINAL-SUBMISSION-VALIDATION.md
|
|
28
|
+
|
|
29
|
+
# Standards-work registers and sweep reports are built from private mail.
|
|
30
|
+
# They live in the private cred-ninja/standards-private repo, never here.
|
|
31
|
+
reports/
|
|
32
|
+
|
|
28
33
|
.next/
|
|
29
34
|
out/
|
|
30
35
|
coverage/
|
|
31
36
|
*.tsbuildinfo
|
|
32
37
|
.turbo/
|
|
33
38
|
.DS_Store
|
|
39
|
+
packages/server/.test-*
|
|
34
40
|
*.pem
|
|
35
41
|
.env
|
|
36
42
|
.env.local
|
|
@@ -48,3 +54,4 @@ venv/
|
|
|
48
54
|
dist/
|
|
49
55
|
*.dist-info/
|
|
50
56
|
packages/sdk-python/.venv/
|
|
57
|
+
.claude/
|
|
@@ -0,0 +1,160 @@
|
|
|
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, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and
|
|
27
|
+
configuration files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object
|
|
31
|
+
code, generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
36
|
+
below).
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
39
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
40
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
41
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
42
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
43
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
44
|
+
|
|
45
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
46
|
+
version of the Work and any modifications or additions to that Work or
|
|
47
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
48
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
49
|
+
Entity authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal, or
|
|
51
|
+
written communication sent to the Licensor or its representatives, including
|
|
52
|
+
but not limited to communication on electronic mailing lists, source code
|
|
53
|
+
control systems, and issue tracking systems that are managed by, or on behalf
|
|
54
|
+
of, the Licensor for the purpose of discussing and improving the Work, but
|
|
55
|
+
excluding communication that is conspicuously marked or otherwise designated in
|
|
56
|
+
writing by the copyright owner as "Not a Contribution."
|
|
57
|
+
|
|
58
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
59
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
60
|
+
incorporated within the Work.
|
|
61
|
+
|
|
62
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
63
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
64
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
65
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
66
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
67
|
+
Object form.
|
|
68
|
+
|
|
69
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
70
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
71
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
|
72
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
|
73
|
+
and otherwise transfer the Work, where such license applies only to those
|
|
74
|
+
patent claims licensable by such Contributor that are necessarily infringed by
|
|
75
|
+
their Contribution(s) alone or by combination of their Contribution(s) with the
|
|
76
|
+
Work to which such Contribution(s) was submitted. If You institute patent
|
|
77
|
+
litigation against any entity (including a cross-claim or counterclaim in a
|
|
78
|
+
lawsuit) alleging that the Work or a Contribution incorporated within the Work
|
|
79
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
80
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
81
|
+
such litigation is filed.
|
|
82
|
+
|
|
83
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
84
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
85
|
+
Source or Object form, provided that You meet the following conditions:
|
|
86
|
+
|
|
87
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy
|
|
88
|
+
of this License; and
|
|
89
|
+
|
|
90
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
91
|
+
You changed the files; and
|
|
92
|
+
|
|
93
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
94
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
95
|
+
Source form of the Work, excluding those notices that do not pertain to any
|
|
96
|
+
part of the Derivative Works; and
|
|
97
|
+
|
|
98
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
99
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
100
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
101
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
102
|
+
following places: within a NOTICE text file distributed as part of the
|
|
103
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
104
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
105
|
+
Works, if and wherever such third-party notices normally appear. The contents
|
|
106
|
+
of the NOTICE file are for informational purposes only and do not modify the
|
|
107
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
108
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
109
|
+
provided that such additional attribution notices cannot be construed as
|
|
110
|
+
modifying the License.
|
|
111
|
+
|
|
112
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
113
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
114
|
+
distribution of Your modifications, or for any such Derivative Works as a
|
|
115
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise
|
|
116
|
+
complies with the conditions stated in this License.
|
|
117
|
+
|
|
118
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
119
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
120
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
121
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
122
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
123
|
+
executed with Licensor regarding such Contributions.
|
|
124
|
+
|
|
125
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
126
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
127
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
128
|
+
reproducing the content of the NOTICE file.
|
|
129
|
+
|
|
130
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
131
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
132
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
133
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
134
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
135
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
136
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
137
|
+
associated with Your exercise of permissions under this License.
|
|
138
|
+
|
|
139
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
140
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
141
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
142
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
143
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
144
|
+
character arising as a result of this License or out of the use or inability to
|
|
145
|
+
use the Work (including but not limited to damages for loss of goodwill, work
|
|
146
|
+
stoppage, computer failure or malfunction, or any and all other commercial
|
|
147
|
+
damages or losses), even if such Contributor has been advised of the
|
|
148
|
+
possibility of such damages.
|
|
149
|
+
|
|
150
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
151
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
152
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
153
|
+
and/or rights consistent with this License. However, in accepting such
|
|
154
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
155
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
|
156
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
|
157
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
158
|
+
accepting any such warranty or additional liability.
|
|
159
|
+
|
|
160
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Cred
|
|
2
|
+
Copyright 2026 Cred Ninja
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Cred Ninja and contributors.
|
|
5
|
+
|
|
6
|
+
Cred, Cred Ninja, and associated logos are trademarks or service marks of
|
|
7
|
+
Cred Ninja. The Apache License 2.0 does not grant permission to use the
|
|
8
|
+
Cred marks except as required for reasonable and customary attribution.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cred-semantic-kernel
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Cred Semantic Kernel integration — credential delegation plugin for Microsoft Semantic Kernel agents
|
|
5
|
+
Project-URL: Homepage, https://cred.ninja
|
|
6
|
+
Project-URL: Repository, https://github.com/cred-ninja/sdk
|
|
7
|
+
Project-URL: Documentation, https://cred.ninja/docs
|
|
8
|
+
Author-email: Cred <hello@cred.ninja>
|
|
9
|
+
License: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Keywords: ai-agents,cred,delegation,microsoft,oauth,semantic-kernel
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Requires-Dist: cred-auth<3.0.0,>=2.0.0
|
|
15
|
+
Requires-Dist: semantic-kernel>=1.0
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: hatchling; extra == 'dev'
|
|
18
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# cred-semantic-kernel
|
|
23
|
+
|
|
24
|
+
Microsoft Semantic Kernel integration for Cred. OAuth2 credential delegation for AI agents.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install cred-semantic-kernel
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import os
|
|
36
|
+
import semantic_kernel as sk
|
|
37
|
+
from cred_semantic_kernel import CredPlugin
|
|
38
|
+
from cred import AgentIdentity
|
|
39
|
+
|
|
40
|
+
# The agent's own did:key identity, created once with cred.generate_agent_identity()
|
|
41
|
+
# and persisted with identity.export(); load_agent_identity() stands for your own
|
|
42
|
+
# secret store lookup returning that dict.
|
|
43
|
+
identity = AgentIdentity.from_export(load_agent_identity())
|
|
44
|
+
|
|
45
|
+
kernel = sk.Kernel()
|
|
46
|
+
|
|
47
|
+
plugin = CredPlugin(
|
|
48
|
+
agent_token=os.environ["CRED_AGENT_TOKEN"],
|
|
49
|
+
base_url=os.environ["CRED_BASE_URL"],
|
|
50
|
+
user_id="user_123",
|
|
51
|
+
app_client_id="my_app_client_id",
|
|
52
|
+
token_format="handle",
|
|
53
|
+
agent_identity=identity,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
kernel.add_plugin(plugin, plugin_name="cred")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Plugin Functions
|
|
60
|
+
|
|
61
|
+
The `cred` plugin exposes two kernel functions:
|
|
62
|
+
|
|
63
|
+
### `delegate`
|
|
64
|
+
|
|
65
|
+
| Parameter | Type | Description |
|
|
66
|
+
|-----------|------|-------------|
|
|
67
|
+
| `service` | `string` | Service slug (e.g. `google`, `github`) |
|
|
68
|
+
| `scopes` | `string` | Comma-separated OAuth scopes to request |
|
|
69
|
+
|
|
70
|
+
`user_id` and `app_client_id` are pre-configured at construction time, not agent-controlled.
|
|
71
|
+
|
|
72
|
+
By default, `delegate` returns a legacy access token for backwards compatibility. Set `token_format="handle"` to return a brokered delegation id instead, then call `use` to make allowed service API calls without exposing provider access tokens.
|
|
73
|
+
|
|
74
|
+
`agent_identity` is required with `token_format="handle"`. The Cred server issues a Delegation Token (a DPoP-bound JWT) to the agent's own key and accepts it only with a DPoP proof signed by that key. The key stays in your process; it is never sent to the model or the server, and it is not a tool argument. The model only sees `delegation_id`: the token itself is kept in an in-process map (`delegation_id` to token and expiry), filled by the delegate tool and read by the use tool, so it never appears in tool output. The use tool raises a clear error if no `agent_identity` is configured or the `delegation_id` is unknown or expired (call the delegate tool again).
|
|
75
|
+
|
|
76
|
+
### `use`
|
|
77
|
+
|
|
78
|
+
| Parameter | Type | Description |
|
|
79
|
+
|-----------|------|-------------|
|
|
80
|
+
| `delegation_id` | `string` | Delegation id returned by `delegate` |
|
|
81
|
+
| `url` | `string` | Full HTTPS API URL |
|
|
82
|
+
| `method` | `string` | HTTP method |
|
|
83
|
+
| `body` | `string` | Optional JSON body, or empty string |
|
|
84
|
+
| `extra_headers` | `string` | Optional JSON object of service-specific headers, or empty string |
|
|
85
|
+
|
|
86
|
+
## Handling Consent
|
|
87
|
+
|
|
88
|
+
When the user hasn't connected the service, the `delegate` function raises `ConsentRequiredError`.
|
|
89
|
+
The error's `consent_url` attribute contains the URL to redirect the user.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from cred import ConsentRequiredError
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
result = plugin.delegate(service="google", scopes="calendar.readonly")
|
|
96
|
+
except ConsentRequiredError as e:
|
|
97
|
+
print(f"Redirect user to: {e.consent_url}")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Brokered Server Mode
|
|
101
|
+
|
|
102
|
+
Point `CRED_BASE_URL` at your self-hosted `@credninja/server` deployment and set `token_format="handle"` when you want the agent to receive delegation ids instead of provider access tokens.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# cred-semantic-kernel
|
|
2
|
+
|
|
3
|
+
Microsoft Semantic Kernel integration for Cred. OAuth2 credential delegation for AI agents.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install cred-semantic-kernel
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import os
|
|
15
|
+
import semantic_kernel as sk
|
|
16
|
+
from cred_semantic_kernel import CredPlugin
|
|
17
|
+
from cred import AgentIdentity
|
|
18
|
+
|
|
19
|
+
# The agent's own did:key identity, created once with cred.generate_agent_identity()
|
|
20
|
+
# and persisted with identity.export(); load_agent_identity() stands for your own
|
|
21
|
+
# secret store lookup returning that dict.
|
|
22
|
+
identity = AgentIdentity.from_export(load_agent_identity())
|
|
23
|
+
|
|
24
|
+
kernel = sk.Kernel()
|
|
25
|
+
|
|
26
|
+
plugin = CredPlugin(
|
|
27
|
+
agent_token=os.environ["CRED_AGENT_TOKEN"],
|
|
28
|
+
base_url=os.environ["CRED_BASE_URL"],
|
|
29
|
+
user_id="user_123",
|
|
30
|
+
app_client_id="my_app_client_id",
|
|
31
|
+
token_format="handle",
|
|
32
|
+
agent_identity=identity,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
kernel.add_plugin(plugin, plugin_name="cred")
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Plugin Functions
|
|
39
|
+
|
|
40
|
+
The `cred` plugin exposes two kernel functions:
|
|
41
|
+
|
|
42
|
+
### `delegate`
|
|
43
|
+
|
|
44
|
+
| Parameter | Type | Description |
|
|
45
|
+
|-----------|------|-------------|
|
|
46
|
+
| `service` | `string` | Service slug (e.g. `google`, `github`) |
|
|
47
|
+
| `scopes` | `string` | Comma-separated OAuth scopes to request |
|
|
48
|
+
|
|
49
|
+
`user_id` and `app_client_id` are pre-configured at construction time, not agent-controlled.
|
|
50
|
+
|
|
51
|
+
By default, `delegate` returns a legacy access token for backwards compatibility. Set `token_format="handle"` to return a brokered delegation id instead, then call `use` to make allowed service API calls without exposing provider access tokens.
|
|
52
|
+
|
|
53
|
+
`agent_identity` is required with `token_format="handle"`. The Cred server issues a Delegation Token (a DPoP-bound JWT) to the agent's own key and accepts it only with a DPoP proof signed by that key. The key stays in your process; it is never sent to the model or the server, and it is not a tool argument. The model only sees `delegation_id`: the token itself is kept in an in-process map (`delegation_id` to token and expiry), filled by the delegate tool and read by the use tool, so it never appears in tool output. The use tool raises a clear error if no `agent_identity` is configured or the `delegation_id` is unknown or expired (call the delegate tool again).
|
|
54
|
+
|
|
55
|
+
### `use`
|
|
56
|
+
|
|
57
|
+
| Parameter | Type | Description |
|
|
58
|
+
|-----------|------|-------------|
|
|
59
|
+
| `delegation_id` | `string` | Delegation id returned by `delegate` |
|
|
60
|
+
| `url` | `string` | Full HTTPS API URL |
|
|
61
|
+
| `method` | `string` | HTTP method |
|
|
62
|
+
| `body` | `string` | Optional JSON body, or empty string |
|
|
63
|
+
| `extra_headers` | `string` | Optional JSON object of service-specific headers, or empty string |
|
|
64
|
+
|
|
65
|
+
## Handling Consent
|
|
66
|
+
|
|
67
|
+
When the user hasn't connected the service, the `delegate` function raises `ConsentRequiredError`.
|
|
68
|
+
The error's `consent_url` attribute contains the URL to redirect the user.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from cred import ConsentRequiredError
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
result = plugin.delegate(service="google", scopes="calendar.readonly")
|
|
75
|
+
except ConsentRequiredError as e:
|
|
76
|
+
print(f"Redirect user to: {e.consent_url}")
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Brokered Server Mode
|
|
80
|
+
|
|
81
|
+
Point `CRED_BASE_URL` at your self-hosted `@credninja/server` deployment and set `token_format="handle"` when you want the agent to receive delegation ids instead of provider access tokens.
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"""Cred Semantic Kernel Plugin.
|
|
2
|
+
|
|
3
|
+
Provides a kernel function that delegates OAuth credentials for AI agents.
|
|
4
|
+
The plugin is pre-configured with agent identity and app context at construction.
|
|
5
|
+
At runtime, the agent provides ``service`` and ``scopes`` as function parameters.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import time
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from typing import TYPE_CHECKING, Annotated, Any, Optional
|
|
14
|
+
|
|
15
|
+
from semantic_kernel.functions import kernel_function
|
|
16
|
+
|
|
17
|
+
from cred import Cred
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from cred import DpopSigningIdentity
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _identity_kwargs(agent_identity: Any) -> dict[str, Any]:
|
|
24
|
+
"""``agent_identity=`` for the SDK call, only when one is configured."""
|
|
25
|
+
return {"agent_identity": agent_identity} if agent_identity is not None else {}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# ── Delegation token store ────────────────────────────────────────────────────
|
|
29
|
+
#
|
|
30
|
+
# The model only ever sees ``delegation_id``. The Delegation Token that
|
|
31
|
+
# ``cred_use`` needs stays in this in-process mapping (delegation_id ->
|
|
32
|
+
# (token, expiry)), populated by the delegate tool and read by the use tool.
|
|
33
|
+
|
|
34
|
+
_DELEGATION_TOKENS: dict[str, tuple[str, float, str, str, Optional[str]]] = {}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _agent_did(agent_identity: Any) -> str:
|
|
38
|
+
return getattr(agent_identity, "did", "") or ""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _remember_delegation_token(
|
|
42
|
+
delegation_id: str,
|
|
43
|
+
token: str,
|
|
44
|
+
expires_at: float,
|
|
45
|
+
*,
|
|
46
|
+
base_url: str,
|
|
47
|
+
agent_did: str,
|
|
48
|
+
user_id: Optional[str] = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
"""Store a token with the server, agent and user it was issued for."""
|
|
51
|
+
now = time.time()
|
|
52
|
+
for key in [k for k, entry in _DELEGATION_TOKENS.items() if entry[1] <= now]:
|
|
53
|
+
del _DELEGATION_TOKENS[key]
|
|
54
|
+
_DELEGATION_TOKENS[delegation_id] = (token, expires_at, base_url.rstrip("/"), agent_did, user_id)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _lookup_delegation_token(
|
|
58
|
+
delegation_id: str,
|
|
59
|
+
*,
|
|
60
|
+
base_url: str,
|
|
61
|
+
agent_did: str,
|
|
62
|
+
user_id: Optional[str] = None,
|
|
63
|
+
) -> str:
|
|
64
|
+
"""Return the token only to a tool configured for the same server, agent and user."""
|
|
65
|
+
entry = _DELEGATION_TOKENS.get(delegation_id)
|
|
66
|
+
if entry is None or entry[1] <= time.time():
|
|
67
|
+
_DELEGATION_TOKENS.pop(delegation_id, None)
|
|
68
|
+
raise ValueError("Unknown or expired delegation: call the delegate tool first")
|
|
69
|
+
_, _, entry_base_url, entry_agent_did, entry_user_id = entry
|
|
70
|
+
if (
|
|
71
|
+
entry_base_url != base_url.rstrip("/")
|
|
72
|
+
or entry_agent_did != agent_did
|
|
73
|
+
or (user_id is not None and entry_user_id != user_id)
|
|
74
|
+
):
|
|
75
|
+
raise ValueError("This delegation was issued for a different server, agent or user")
|
|
76
|
+
return entry[0]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _expiry_epoch(result: Any) -> float:
|
|
80
|
+
expires_at = getattr(result, "expires_at", None)
|
|
81
|
+
if isinstance(expires_at, datetime):
|
|
82
|
+
return expires_at.timestamp()
|
|
83
|
+
expires_in = getattr(result, "expires_in", 0)
|
|
84
|
+
return time.time() + (expires_in if isinstance(expires_in, (int, float)) else 0)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class CredPlugin:
|
|
88
|
+
"""Semantic Kernel plugin for Cred credential delegation.
|
|
89
|
+
|
|
90
|
+
Pre-configured at construction with the agent token, user ID, and
|
|
91
|
+
app client ID. Exposes a single ``delegate`` kernel function.
|
|
92
|
+
|
|
93
|
+
Example::
|
|
94
|
+
|
|
95
|
+
from cred_semantic_kernel import CredPlugin
|
|
96
|
+
import semantic_kernel as sk
|
|
97
|
+
|
|
98
|
+
kernel = sk.Kernel()
|
|
99
|
+
plugin = CredPlugin(
|
|
100
|
+
agent_token=os.environ["CRED_AGENT_TOKEN"],
|
|
101
|
+
user_id="user_123",
|
|
102
|
+
app_client_id="my_app",
|
|
103
|
+
base_url=os.environ["CRED_BASE_URL"],
|
|
104
|
+
token_format="handle",
|
|
105
|
+
agent_identity=identity, # an AgentIdentity; never shown to the model
|
|
106
|
+
)
|
|
107
|
+
kernel.add_plugin(plugin, plugin_name="cred")
|
|
108
|
+
|
|
109
|
+
Raises:
|
|
110
|
+
ConsentRequiredError: (from delegate at runtime) when the user hasn't
|
|
111
|
+
connected the requested service. The error message includes
|
|
112
|
+
``consent_url`` so the caller can redirect the user.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
def __init__(
|
|
116
|
+
self,
|
|
117
|
+
agent_token: str,
|
|
118
|
+
user_id: str,
|
|
119
|
+
app_client_id: str,
|
|
120
|
+
base_url: str,
|
|
121
|
+
token_format: str = "raw",
|
|
122
|
+
*,
|
|
123
|
+
agent_identity: Optional[DpopSigningIdentity] = None,
|
|
124
|
+
) -> None:
|
|
125
|
+
"""
|
|
126
|
+
Args:
|
|
127
|
+
agent_identity: The agent's own identity (an ``AgentIdentity`` from
|
|
128
|
+
``cred``). Brokered handles are bound to this key: the Cred server
|
|
129
|
+
requires a DPoP proof by it to issue a handle and to use one. The
|
|
130
|
+
key stays in your process; it is never sent to the model or the server.
|
|
131
|
+
"""
|
|
132
|
+
if token_format not in {"raw", "handle"}:
|
|
133
|
+
raise ValueError("token_format must be 'raw' or 'handle'")
|
|
134
|
+
self._cred = Cred(agent_token=agent_token, base_url=base_url)
|
|
135
|
+
self._base_url = base_url
|
|
136
|
+
self._user_id = user_id
|
|
137
|
+
self._app_client_id = app_client_id
|
|
138
|
+
self._token_format = token_format
|
|
139
|
+
# The agent's DPoP signing identity; never part of a kernel function's
|
|
140
|
+
# parameters or result.
|
|
141
|
+
self._agent_identity = agent_identity
|
|
142
|
+
|
|
143
|
+
@kernel_function(
|
|
144
|
+
name="delegate",
|
|
145
|
+
description=(
|
|
146
|
+
"Get delegated OAuth access for a third-party service "
|
|
147
|
+
"on behalf of the current user. "
|
|
148
|
+
"Returns JSON with either access_token or a brokered delegation_id, plus expiry, service, and scopes. "
|
|
149
|
+
"Raises an error with consent_url if the user hasn't connected the service."
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
def delegate(
|
|
153
|
+
self,
|
|
154
|
+
service: Annotated[str, "Service slug to get a token for (e.g. 'google', 'github', 'google-calendar')."],
|
|
155
|
+
scopes: Annotated[str, "Comma-separated OAuth scopes to request (e.g. 'calendar.readonly,calendar.events'). Pass empty string to use all consented scopes."],
|
|
156
|
+
) -> str:
|
|
157
|
+
"""Delegate credentials and return JSON result."""
|
|
158
|
+
scope_list: Optional[list[str]] = None
|
|
159
|
+
if scopes:
|
|
160
|
+
scope_list = [s.strip() for s in scopes.split(",") if s.strip()]
|
|
161
|
+
|
|
162
|
+
if self._token_format == "handle":
|
|
163
|
+
result = self._cred.delegate_handle(
|
|
164
|
+
service=service,
|
|
165
|
+
user_id=self._user_id,
|
|
166
|
+
app_client_id=self._app_client_id,
|
|
167
|
+
scopes=scope_list if scope_list else None,
|
|
168
|
+
**_identity_kwargs(self._agent_identity),
|
|
169
|
+
)
|
|
170
|
+
_remember_delegation_token(
|
|
171
|
+
result.delegation_id, result.token, _expiry_epoch(result),
|
|
172
|
+
base_url=self._base_url, agent_did=_agent_did(self._agent_identity), user_id=self._user_id,
|
|
173
|
+
)
|
|
174
|
+
return json.dumps({
|
|
175
|
+
"token_type": result.token_type,
|
|
176
|
+
"expires_in": result.expires_in,
|
|
177
|
+
"service": result.service,
|
|
178
|
+
"user_id": result.user_id,
|
|
179
|
+
"scopes": result.scopes,
|
|
180
|
+
"delegation_id": result.delegation_id,
|
|
181
|
+
"note": "Pass delegation_id to use to make authenticated API calls.",
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
result = self._cred.delegate(
|
|
185
|
+
service=service,
|
|
186
|
+
user_id=self._user_id,
|
|
187
|
+
app_client_id=self._app_client_id,
|
|
188
|
+
scopes=scope_list if scope_list else None,
|
|
189
|
+
)
|
|
190
|
+
return json.dumps({
|
|
191
|
+
"access_token": result.access_token,
|
|
192
|
+
"token_type": result.token_type,
|
|
193
|
+
"expires_in": result.expires_in,
|
|
194
|
+
"service": result.service,
|
|
195
|
+
"scopes": result.scopes,
|
|
196
|
+
"delegation_id": result.delegation_id,
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
@kernel_function(
|
|
200
|
+
name="use",
|
|
201
|
+
description=(
|
|
202
|
+
"Make an authenticated API call through Cred using a brokered delegation handle. "
|
|
203
|
+
"The provider access token stays on the Cred server."
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
def use(
|
|
207
|
+
self,
|
|
208
|
+
delegation_id: Annotated[str, "Brokered delegation handle returned by delegate."],
|
|
209
|
+
url: Annotated[str, "Full HTTPS API URL to call."],
|
|
210
|
+
method: Annotated[str, "HTTP method: GET, POST, PUT, PATCH, or DELETE."],
|
|
211
|
+
body: Annotated[str, "Optional JSON request body. Pass empty string when unused."] = "",
|
|
212
|
+
extra_headers: Annotated[str, "Optional JSON object of service-specific headers. Pass empty string when unused."] = "",
|
|
213
|
+
) -> str:
|
|
214
|
+
"""Broker an upstream API request and return JSON result."""
|
|
215
|
+
if self._agent_identity is None:
|
|
216
|
+
raise ValueError(
|
|
217
|
+
"use requires an agent_identity: the delegation token is bound to the agent's key. "
|
|
218
|
+
"Pass agent_identity= to CredPlugin.",
|
|
219
|
+
)
|
|
220
|
+
parsed_body = json.loads(body) if body else None
|
|
221
|
+
parsed_headers = json.loads(extra_headers) if extra_headers else None
|
|
222
|
+
token = _lookup_delegation_token(
|
|
223
|
+
delegation_id, base_url=self._base_url, agent_did=_agent_did(self._agent_identity), user_id=self._user_id,
|
|
224
|
+
)
|
|
225
|
+
result = self._cred.use(
|
|
226
|
+
token,
|
|
227
|
+
url=url,
|
|
228
|
+
method=method,
|
|
229
|
+
body=parsed_body,
|
|
230
|
+
extra_headers=parsed_headers,
|
|
231
|
+
agent_identity=self._agent_identity,
|
|
232
|
+
)
|
|
233
|
+
return json.dumps({
|
|
234
|
+
"status": result.status,
|
|
235
|
+
"ok": result.ok,
|
|
236
|
+
"content_type": result.content_type,
|
|
237
|
+
"body": result.body,
|
|
238
|
+
"truncated": result.truncated,
|
|
239
|
+
"truncated_at": result.truncated_at,
|
|
240
|
+
})
|
|
@@ -4,18 +4,24 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "cred-semantic-kernel"
|
|
7
|
-
version = "
|
|
7
|
+
version = "2.0.0"
|
|
8
8
|
description = "Cred Semantic Kernel integration — credential delegation plugin for Microsoft Semantic Kernel agents"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { text = "Apache-2.0" }
|
|
11
|
-
|
|
11
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
12
13
|
authors = [{ name = "Cred", email = "hello@cred.ninja" }]
|
|
13
14
|
keywords = ["cred", "semantic-kernel", "microsoft", "oauth", "delegation", "ai-agents"]
|
|
14
15
|
dependencies = [
|
|
15
|
-
"cred-auth>=0.
|
|
16
|
+
"cred-auth>=2.0.0,<3.0.0",
|
|
16
17
|
"semantic-kernel>=1.0",
|
|
17
18
|
]
|
|
18
19
|
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://cred.ninja"
|
|
22
|
+
Repository = "https://github.com/cred-ninja/sdk"
|
|
23
|
+
Documentation = "https://cred.ninja/docs"
|
|
24
|
+
|
|
19
25
|
[project.optional-dependencies]
|
|
20
26
|
dev = ["pytest>=7.0", "pytest-asyncio>=0.21", "hatchling"]
|
|
21
27
|
|