keycardai-hermes 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.
Files changed (32) hide show
  1. keycardai_hermes-0.1.0/.gitignore +194 -0
  2. keycardai_hermes-0.1.0/LICENSE +9 -0
  3. keycardai_hermes-0.1.0/PKG-INFO +219 -0
  4. keycardai_hermes-0.1.0/README.md +184 -0
  5. keycardai_hermes-0.1.0/TESTING.md +79 -0
  6. keycardai_hermes-0.1.0/examples/profile/README.md +31 -0
  7. keycardai_hermes-0.1.0/examples/profile/config.yaml +33 -0
  8. keycardai_hermes-0.1.0/pyproject.toml +159 -0
  9. keycardai_hermes-0.1.0/src/keycardai/hermes/__init__.py +34 -0
  10. keycardai_hermes-0.1.0/src/keycardai/hermes/access.py +28 -0
  11. keycardai_hermes-0.1.0/src/keycardai/hermes/cli.py +155 -0
  12. keycardai_hermes-0.1.0/src/keycardai/hermes/grants.py +226 -0
  13. keycardai_hermes-0.1.0/src/keycardai/hermes/middleware.py +151 -0
  14. keycardai_hermes-0.1.0/src/keycardai/hermes/plugin.py +290 -0
  15. keycardai_hermes-0.1.0/src/keycardai/hermes/py.typed +0 -0
  16. keycardai_hermes-0.1.0/src/keycardai/hermes/relay.py +304 -0
  17. keycardai_hermes-0.1.0/src/keycardai/hermes/secret_source.py +102 -0
  18. keycardai_hermes-0.1.0/src/keycardai/hermes/settings.py +212 -0
  19. keycardai_hermes-0.1.0/src/keycardai/hermes/store.py +135 -0
  20. keycardai_hermes-0.1.0/src/keycardai/hermes/testing/__init__.py +11 -0
  21. keycardai_hermes-0.1.0/src/keycardai/hermes/testing/test_utils.py +87 -0
  22. keycardai_hermes-0.1.0/tests/__init__.py +0 -0
  23. keycardai_hermes-0.1.0/tests/conftest.py +112 -0
  24. keycardai_hermes-0.1.0/tests/hermes_fakes.py +244 -0
  25. keycardai_hermes-0.1.0/tests/test_cli.py +115 -0
  26. keycardai_hermes-0.1.0/tests/test_hardening.py +278 -0
  27. keycardai_hermes-0.1.0/tests/test_middleware.py +497 -0
  28. keycardai_hermes-0.1.0/tests/test_relay.py +210 -0
  29. keycardai_hermes-0.1.0/tests/test_secret_source.py +30 -0
  30. keycardai_hermes-0.1.0/tests/test_settings.py +143 -0
  31. keycardai_hermes-0.1.0/tests/test_testing_seam.py +13 -0
  32. keycardai_hermes-0.1.0/uv.lock +790 -0
@@ -0,0 +1,194 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ .venv-leg
126
+ env/
127
+ venv/
128
+ ENV/
129
+ env.bak/
130
+ venv.bak/
131
+
132
+ # Spyder project settings
133
+ .spyderproject
134
+ .spyproject
135
+
136
+ # Rope project settings
137
+ .ropeproject
138
+
139
+ # mkdocs documentation
140
+ /site
141
+
142
+ # mypy
143
+ .mypy_cache/
144
+ .dmypy.json
145
+ dmypy.json
146
+
147
+ # Pyre type checker
148
+ .pyre/
149
+
150
+ # pytype static type analyzer
151
+ .pytype/
152
+
153
+ # Cython debug symbols
154
+ cython_debug/
155
+
156
+ # PyCharm
157
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158
+ # be added to the global gitignore or merged into this project gitignore. For a PyCharm
159
+ # project, uncomment below!
160
+ #.idea/
161
+
162
+ # VS Code
163
+ .vscode/
164
+
165
+ # OS generated files
166
+ .DS_Store
167
+ .DS_Store?
168
+ ._*
169
+ .Spotlight-V100
170
+ .Trashes
171
+ ehthumbs.db
172
+ Thumbs.db
173
+
174
+ # Backup files
175
+ *~
176
+ *.bak
177
+ *.backup
178
+ *.old
179
+ *.orig
180
+
181
+ # Temporary files
182
+ *.tmp
183
+ *.temp
184
+
185
+ # IDE files
186
+ *.swp
187
+ *.swo
188
+ *~
189
+
190
+ # Local development
191
+ .local/
192
+
193
+ # Claude Code
194
+ CLAUDE.md
@@ -0,0 +1,9 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2026 Keycard Labs, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.5
2
+ Name: keycardai-hermes
3
+ Version: 0.1.0
4
+ Summary: Hermes Agent plugin for Keycard: delegated, per-tool-call access with brokered credentials
5
+ Project-URL: Homepage, https://github.com/keycardai/python-sdk
6
+ Project-URL: Repository, https://github.com/keycardai/python-sdk
7
+ Project-URL: Documentation, https://docs.keycardai.com
8
+ Project-URL: Issues, https://github.com/keycardai/python-sdk/issues
9
+ Author-email: Keycard <support@keycard.ai>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: agents,authentication,hermes-agent,keycard,oauth,token-exchange
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Internet :: WWW/HTTP :: Session
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Security
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Python: >=3.11
26
+ Requires-Dist: httpx>=0.27
27
+ Requires-Dist: keycardai-oauth>=0.36.0
28
+ Requires-Dist: pyyaml>=6.0
29
+ Provides-Extra: test
30
+ Requires-Dist: pytest-asyncio>=1.1.0; extra == 'test'
31
+ Requires-Dist: pytest-cov>=5.0; extra == 'test'
32
+ Requires-Dist: pytest>=8.4.1; extra == 'test'
33
+ Requires-Dist: types-pyyaml>=6.0; extra == 'test'
34
+ Description-Content-Type: text/markdown
35
+
36
+ # keycardai-hermes
37
+
38
+ Keycard plugin for [Hermes Agent](https://github.com/NousResearch/hermes-agent):
39
+ delegated, per-tool-call access with brokered credentials. Before each tool
40
+ runs, the plugin acquires a credential for the actor the turn acts as and hands
41
+ it to the tool as a non-throwing `AccessContext`; surfaces the tool-call
42
+ boundary does not reach (MCP servers, CLIs, scripts) get the same credentials
43
+ through a stdio MCP relay and `hermes keycard token`.
44
+
45
+ The contract is the [Hermes Agent integration spec](https://github.com/keycardai/keycard-sdk-spec/blob/main/specs/framework-integrations/hermes.md).
46
+ The package is experimental: it writes down what a reference deployment already
47
+ runs, and ships only what works without upstream Hermes changes.
48
+
49
+ ## Install
50
+
51
+ Hermes Agent is not on PyPI, so the plugin installs into Hermes's own
52
+ virtualenv, where `hermes_agent.plugins` entry points are discovered:
53
+
54
+ ```bash
55
+ # from the Hermes checkout, with its venv active
56
+ pip install keycardai-hermes
57
+ ```
58
+
59
+ Requires Python 3.11 (Hermes's floor) and `keycardai-oauth>=0.36.0`.
60
+
61
+ The plugin registers a `tool_execution` middleware, a secret source and the
62
+ `hermes keycard` command group. None of them needs a declared Hermes
63
+ capability, so there is no consent step.
64
+
65
+ ## Enable the plugin
66
+
67
+ One settings block per Hermes profile, in that profile's `config.yaml`:
68
+
69
+ ```yaml
70
+ plugins:
71
+ enabled:
72
+ - keycardai-hermes
73
+ entries:
74
+ keycardai-hermes:
75
+ settings:
76
+ zone_url: https://your-zone.keycard.cloud
77
+ application_credential:
78
+ type: client_secret
79
+ client_id: app_...
80
+ secret_name: KEYCARD_CLIENT_SECRET # read through Hermes's get_secret, from .env
81
+ sign_in_resource: urn:hermes:assistant # a resource the application owns
82
+ owner_actor: alice # the actor for CLI, TUI, cron and the relay
83
+ tool_resources:
84
+ calendar_search: [https://calendar.example.com]
85
+ # resources: [...] # granted for every tool, built-ins included
86
+ # request_scopes: ...
87
+ # default_pattern: on_behalf_of | impersonate | as_self
88
+ # actor_aliases: {athena: alice}
89
+ # actors: {alice: alice@example.com} # for impersonate and verify_sign_in
90
+ # secrets: {CALENDAR_KEY: https://calendar.example.com}
91
+ # block_without_sign_in: true
92
+ # sign_in_on_admission: false # answer a sender with no sign-in before the turn
93
+ # authorization_url: https://...
94
+ # verify_sign_in: false
95
+ ```
96
+
97
+ Put the secret in the profile's `.env` as `KEYCARD_CLIENT_SECRET=...`; the
98
+ settings file never holds it. A `workload_identity` block
99
+ (`{type: workload_identity, client_id: ..., token_file: ...}`) reads a
100
+ platform-projected token file instead.
101
+
102
+ `plugins.isolation: host` is not supported: the plugin refuses to load there,
103
+ including inside the plugin host child (`HERMES_PLUGIN_HOST_PROCESS=1`),
104
+ because a ContextVar set in the plugin host process cannot reach a tool.
105
+
106
+ ## Sign in an actor
107
+
108
+ ```bash
109
+ hermes keycard login --actor alice
110
+ ```
111
+
112
+ Registers a public client in the zone on first use (device code and refresh
113
+ token grants, no client authentication), runs the device flow for
114
+ `sign_in_resource`, and stores the sign-in under the actor in a 0600 file under
115
+ the profile's Hermes home. Without `--actor` the sign-in is stored under
116
+ `owner_actor`. With `verify_sign_in: true`, the zone's UserInfo must match
117
+ `actors[alice]` (its `sub`, or its `email` when the value is an email and the zone marks it
118
+ verified) or
119
+ nothing is stored. Each sign-in file is named by a readable prefix of the actor plus a digest of
120
+ the full id, so two actors never share one, and the directories are 0700.
121
+
122
+ In a gateway turn the actor is the sender (`HERMES_SESSION_ACTOR_ID`). A turn
123
+ with an empty actor acts as `owner_actor` unless Hermes's
124
+ `session_is_messaging_surface()` reports it as a messaging turn: API server,
125
+ CLI, TUI, cron and kanban turns use the owner, while a messaging turn with an
126
+ empty actor (Hermes clears it when a second sender's message joins a running
127
+ turn) gets no actor, never the owner's. Hermes counts any surface it doesn't
128
+ list as messaging, so an unknown surface gets no actor either. A tool call that needs a
129
+ sign-in and has none is answered with a tool result naming the login command,
130
+ and the tool does not run (`block_without_sign_in: false` records the error on
131
+ the context and runs the tool instead). With `sign_in_on_admission: true`, a
132
+ message from a sender with no sign-in is answered with the login command before
133
+ the turn starts, whether or not the turn would have called a tool.
134
+
135
+ ## Read the credential in a tool
136
+
137
+ ```python
138
+ from keycardai.hermes import get_access_context
139
+
140
+ def calendar_search(query: str) -> str:
141
+ access = get_access_context()
142
+ if access.has_resource_error("https://calendar.example.com"):
143
+ return f"calendar unavailable: {access.get_resource_error('https://calendar.example.com')['message']}"
144
+ token = access.access("https://calendar.example.com").access_token
145
+ ...
146
+ ```
147
+
148
+ Errors are data: a denied grant, an expired sign-in or a failed refresh is on
149
+ the `AccessContext`, never an exception out of the middleware. Outside a tool
150
+ call, `grant()` and `agrant()` install the same context:
151
+
152
+ ```python
153
+ from keycardai.hermes import grant
154
+
155
+ with grant(tool_name="calendar_search") as access:
156
+ ...
157
+ ```
158
+
159
+ ## A token for anything else
160
+
161
+ ```bash
162
+ hermes keycard token https://calendar.example.com # as owner_actor
163
+ hermes keycard token https://model.example.com --as-self # as the application
164
+ ```
165
+
166
+ Writes only the token to standard output and exits non-zero with the error on
167
+ standard error. It is the route for scripts and CLIs the terminal tool runs,
168
+ and the `key_cmd` of a model provider registered through
169
+ `providers.register_provider` (off unless configured). Note that the command
170
+ acts as `owner_actor` (or as the application) whichever sender's turn runs it,
171
+ and a token printed by the terminal tool lands in the session transcript; a
172
+ wrapper that writes it where the next command reads it keeps it out.
173
+
174
+ ## MCP servers behind a gateway: the relay
175
+
176
+ `keycardai-hermes-mcp` is a stdio MCP server that relays to a streamable HTTP
177
+ endpoint, attaching a token for the endpoint (or `--resource`) acquired as
178
+ `owner_actor`. It retries once on a 401 with a new token and once on a 404 by
179
+ replaying `initialize`; anything that still fails is a JSON-RPC error on that
180
+ request naming the resource. Requests run concurrently, so a slow tool call
181
+ holds up nothing else, and a reply may take up to five minutes. In the profile's `config.yaml`:
182
+
183
+ ```yaml
184
+ mcp_servers:
185
+ calendar:
186
+ command: keycardai-hermes-mcp
187
+ args: ["https://gateway.example.com/mcp/calendar"]
188
+ env:
189
+ KEYCARD_CLIENT_SECRET: "${KEYCARD_CLIENT_SECRET}"
190
+ ```
191
+
192
+ The relay runs outside Hermes, so it reads the application secret from the
193
+ environment variable named by `secret_name`. Hermes strips a stdio MCP server's
194
+ environment to a baseline, so the `env` block has to pass it through as above;
195
+ `HERMES_HOME` is set by Hermes to the profile's home, which is where the relay
196
+ reads the settings and the sign-in store. Like the token command, the relay
197
+ acts as `owner_actor`.
198
+
199
+ ## Static secrets
200
+
201
+ `secrets` maps names to resources; the plugin's secret source answers each
202
+ name with an as-itself token for its resource once per process, when Hermes
203
+ hydrates secret sources. Anything that rotates goes through the middleware,
204
+ the relay or the token command instead.
205
+
206
+ ## Testing
207
+
208
+ `keycardai.hermes.testing.mock_access_context` serves a synthetic context to a
209
+ tool without a zone:
210
+
211
+ ```python
212
+ from keycardai.hermes.testing import mock_access_context
213
+
214
+ with mock_access_context(resource_tokens={"https://calendar.example.com": "tok"}):
215
+ assert "tok" in calendar_search("standup")
216
+ ```
217
+
218
+ See `TESTING.md` for the test matrix against the spec and `examples/profile`
219
+ for a complete profile.
@@ -0,0 +1,184 @@
1
+ # keycardai-hermes
2
+
3
+ Keycard plugin for [Hermes Agent](https://github.com/NousResearch/hermes-agent):
4
+ delegated, per-tool-call access with brokered credentials. Before each tool
5
+ runs, the plugin acquires a credential for the actor the turn acts as and hands
6
+ it to the tool as a non-throwing `AccessContext`; surfaces the tool-call
7
+ boundary does not reach (MCP servers, CLIs, scripts) get the same credentials
8
+ through a stdio MCP relay and `hermes keycard token`.
9
+
10
+ The contract is the [Hermes Agent integration spec](https://github.com/keycardai/keycard-sdk-spec/blob/main/specs/framework-integrations/hermes.md).
11
+ The package is experimental: it writes down what a reference deployment already
12
+ runs, and ships only what works without upstream Hermes changes.
13
+
14
+ ## Install
15
+
16
+ Hermes Agent is not on PyPI, so the plugin installs into Hermes's own
17
+ virtualenv, where `hermes_agent.plugins` entry points are discovered:
18
+
19
+ ```bash
20
+ # from the Hermes checkout, with its venv active
21
+ pip install keycardai-hermes
22
+ ```
23
+
24
+ Requires Python 3.11 (Hermes's floor) and `keycardai-oauth>=0.36.0`.
25
+
26
+ The plugin registers a `tool_execution` middleware, a secret source and the
27
+ `hermes keycard` command group. None of them needs a declared Hermes
28
+ capability, so there is no consent step.
29
+
30
+ ## Enable the plugin
31
+
32
+ One settings block per Hermes profile, in that profile's `config.yaml`:
33
+
34
+ ```yaml
35
+ plugins:
36
+ enabled:
37
+ - keycardai-hermes
38
+ entries:
39
+ keycardai-hermes:
40
+ settings:
41
+ zone_url: https://your-zone.keycard.cloud
42
+ application_credential:
43
+ type: client_secret
44
+ client_id: app_...
45
+ secret_name: KEYCARD_CLIENT_SECRET # read through Hermes's get_secret, from .env
46
+ sign_in_resource: urn:hermes:assistant # a resource the application owns
47
+ owner_actor: alice # the actor for CLI, TUI, cron and the relay
48
+ tool_resources:
49
+ calendar_search: [https://calendar.example.com]
50
+ # resources: [...] # granted for every tool, built-ins included
51
+ # request_scopes: ...
52
+ # default_pattern: on_behalf_of | impersonate | as_self
53
+ # actor_aliases: {athena: alice}
54
+ # actors: {alice: alice@example.com} # for impersonate and verify_sign_in
55
+ # secrets: {CALENDAR_KEY: https://calendar.example.com}
56
+ # block_without_sign_in: true
57
+ # sign_in_on_admission: false # answer a sender with no sign-in before the turn
58
+ # authorization_url: https://...
59
+ # verify_sign_in: false
60
+ ```
61
+
62
+ Put the secret in the profile's `.env` as `KEYCARD_CLIENT_SECRET=...`; the
63
+ settings file never holds it. A `workload_identity` block
64
+ (`{type: workload_identity, client_id: ..., token_file: ...}`) reads a
65
+ platform-projected token file instead.
66
+
67
+ `plugins.isolation: host` is not supported: the plugin refuses to load there,
68
+ including inside the plugin host child (`HERMES_PLUGIN_HOST_PROCESS=1`),
69
+ because a ContextVar set in the plugin host process cannot reach a tool.
70
+
71
+ ## Sign in an actor
72
+
73
+ ```bash
74
+ hermes keycard login --actor alice
75
+ ```
76
+
77
+ Registers a public client in the zone on first use (device code and refresh
78
+ token grants, no client authentication), runs the device flow for
79
+ `sign_in_resource`, and stores the sign-in under the actor in a 0600 file under
80
+ the profile's Hermes home. Without `--actor` the sign-in is stored under
81
+ `owner_actor`. With `verify_sign_in: true`, the zone's UserInfo must match
82
+ `actors[alice]` (its `sub`, or its `email` when the value is an email and the zone marks it
83
+ verified) or
84
+ nothing is stored. Each sign-in file is named by a readable prefix of the actor plus a digest of
85
+ the full id, so two actors never share one, and the directories are 0700.
86
+
87
+ In a gateway turn the actor is the sender (`HERMES_SESSION_ACTOR_ID`). A turn
88
+ with an empty actor acts as `owner_actor` unless Hermes's
89
+ `session_is_messaging_surface()` reports it as a messaging turn: API server,
90
+ CLI, TUI, cron and kanban turns use the owner, while a messaging turn with an
91
+ empty actor (Hermes clears it when a second sender's message joins a running
92
+ turn) gets no actor, never the owner's. Hermes counts any surface it doesn't
93
+ list as messaging, so an unknown surface gets no actor either. A tool call that needs a
94
+ sign-in and has none is answered with a tool result naming the login command,
95
+ and the tool does not run (`block_without_sign_in: false` records the error on
96
+ the context and runs the tool instead). With `sign_in_on_admission: true`, a
97
+ message from a sender with no sign-in is answered with the login command before
98
+ the turn starts, whether or not the turn would have called a tool.
99
+
100
+ ## Read the credential in a tool
101
+
102
+ ```python
103
+ from keycardai.hermes import get_access_context
104
+
105
+ def calendar_search(query: str) -> str:
106
+ access = get_access_context()
107
+ if access.has_resource_error("https://calendar.example.com"):
108
+ return f"calendar unavailable: {access.get_resource_error('https://calendar.example.com')['message']}"
109
+ token = access.access("https://calendar.example.com").access_token
110
+ ...
111
+ ```
112
+
113
+ Errors are data: a denied grant, an expired sign-in or a failed refresh is on
114
+ the `AccessContext`, never an exception out of the middleware. Outside a tool
115
+ call, `grant()` and `agrant()` install the same context:
116
+
117
+ ```python
118
+ from keycardai.hermes import grant
119
+
120
+ with grant(tool_name="calendar_search") as access:
121
+ ...
122
+ ```
123
+
124
+ ## A token for anything else
125
+
126
+ ```bash
127
+ hermes keycard token https://calendar.example.com # as owner_actor
128
+ hermes keycard token https://model.example.com --as-self # as the application
129
+ ```
130
+
131
+ Writes only the token to standard output and exits non-zero with the error on
132
+ standard error. It is the route for scripts and CLIs the terminal tool runs,
133
+ and the `key_cmd` of a model provider registered through
134
+ `providers.register_provider` (off unless configured). Note that the command
135
+ acts as `owner_actor` (or as the application) whichever sender's turn runs it,
136
+ and a token printed by the terminal tool lands in the session transcript; a
137
+ wrapper that writes it where the next command reads it keeps it out.
138
+
139
+ ## MCP servers behind a gateway: the relay
140
+
141
+ `keycardai-hermes-mcp` is a stdio MCP server that relays to a streamable HTTP
142
+ endpoint, attaching a token for the endpoint (or `--resource`) acquired as
143
+ `owner_actor`. It retries once on a 401 with a new token and once on a 404 by
144
+ replaying `initialize`; anything that still fails is a JSON-RPC error on that
145
+ request naming the resource. Requests run concurrently, so a slow tool call
146
+ holds up nothing else, and a reply may take up to five minutes. In the profile's `config.yaml`:
147
+
148
+ ```yaml
149
+ mcp_servers:
150
+ calendar:
151
+ command: keycardai-hermes-mcp
152
+ args: ["https://gateway.example.com/mcp/calendar"]
153
+ env:
154
+ KEYCARD_CLIENT_SECRET: "${KEYCARD_CLIENT_SECRET}"
155
+ ```
156
+
157
+ The relay runs outside Hermes, so it reads the application secret from the
158
+ environment variable named by `secret_name`. Hermes strips a stdio MCP server's
159
+ environment to a baseline, so the `env` block has to pass it through as above;
160
+ `HERMES_HOME` is set by Hermes to the profile's home, which is where the relay
161
+ reads the settings and the sign-in store. Like the token command, the relay
162
+ acts as `owner_actor`.
163
+
164
+ ## Static secrets
165
+
166
+ `secrets` maps names to resources; the plugin's secret source answers each
167
+ name with an as-itself token for its resource once per process, when Hermes
168
+ hydrates secret sources. Anything that rotates goes through the middleware,
169
+ the relay or the token command instead.
170
+
171
+ ## Testing
172
+
173
+ `keycardai.hermes.testing.mock_access_context` serves a synthetic context to a
174
+ tool without a zone:
175
+
176
+ ```python
177
+ from keycardai.hermes.testing import mock_access_context
178
+
179
+ with mock_access_context(resource_tokens={"https://calendar.example.com": "tok"}):
180
+ assert "tok" in calendar_search("standup")
181
+ ```
182
+
183
+ See `TESTING.md` for the test matrix against the spec and `examples/profile`
184
+ for a complete profile.
@@ -0,0 +1,79 @@
1
+ # Test matrix
2
+
3
+ Each row of the spec's unit table
4
+ ([hermes.md](https://github.com/keycardai/keycard-sdk-spec/blob/main/specs/framework-integrations/hermes.md),
5
+ tracked in [SDK-133](https://linear.app/keycardlabs/issue/SDK-133)) is covered
6
+ by a test named or commented with its row number. Run the suite with
7
+ `just test-package hermes`, or directly:
8
+
9
+ ```bash
10
+ cd packages/hermes && uv run --extra test pytest tests/ -v
11
+ ```
12
+
13
+ Every test is hermetic. `hermes-agent` is not on PyPI, so `tests/hermes_fakes.py`
14
+ holds fakes mirroring the v0.23.2 signatures (commit `ae2a81cc`): `PluginContext`
15
+ (`register_middleware`, `register_secret_source`, `register_cli_command`,
16
+ `register_hook`, `get_config`), the `tool_execution` middleware chain with its
17
+ skip-and-continue on a raising callback, and `get_session_env` over per-turn
18
+ ContextVars. The OAuth client is a stub; the relay runs over pipes against
19
+ `httpx.MockTransport`. Re-verify the fakes whenever the pinned Hermes version
20
+ moves.
21
+
22
+ ## Unit
23
+
24
+ | Row | Where |
25
+ |---|---|
26
+ | 1 | `test_middleware.py::test_row_1_missing_sign_in_blocks_with_the_login_command` |
27
+ | 2 | `test_row_2_engine_failure_returns_a_tool_result_and_never_raises` |
28
+ | 3 | `test_row_3_actor_id_is_used_and_user_id_never_read` |
29
+ | 4 | `test_row_4_cli_with_no_session_vars_uses_owner_actor` |
30
+ | 5 | `test_row_5_no_session_vars_and_no_owner_actor` |
31
+ | 6 | `test_row_6_mcp_toolset_acquires_nothing` |
32
+ | 7 | `test_row_7_host_isolation_refuses_to_load` |
33
+ | 8 | `test_secret_source.py::test_row_8_fetch_acquires_each_name_once` |
34
+ | 9 | `test_cli.py::test_row_9_login_registers_once_runs_the_device_flow_and_stores_0600` |
35
+ | 10 | `test_cli.py::test_row_10_verify_sign_in_mismatch_stores_nothing_and_names_neither_account` |
36
+ | 11 | `test_row_11_refresh_rejected_with_invalid_grant_routes_to_sign_in` |
37
+ | 12 | `test_row_12_concurrent_turns_exchange_their_own_actors_token` |
38
+ | 13 | `test_row_13_sync_threadpool_and_async_tools_see_the_context` |
39
+ | 14 | `test_row_14_denied_grant_with_authorization_url_blocks` |
40
+ | 15 | `test_row_15_as_self_with_denied_grant_records_and_runs` |
41
+ | 16 | `test_row_16_grant_and_agrant_outside_a_run` |
42
+ | 17 | `test_row_17_post_gateway_admission_asks_for_sign_in` (with `sign_in_on_admission: true`) |
43
+ | 18 | `test_row_18_near_expiry_refreshes_as_the_public_client_then_exchanges` |
44
+ | 19 | `test_row_19_rotated_refresh_token_is_stored_by_rename_at_0600` |
45
+ | 20 | `test_row_20_two_threads_share_one_refresh`, `test_row_20_two_processes_share_one_refresh` |
46
+ | 21 | `test_row_21_other_refresh_failures_are_per_resource_and_leave_the_store` (network error and 5xx) |
47
+ | 22 | `test_cli.py::test_row_22_login_without_a_refresh_token_stores_nothing` |
48
+ | 23 | `test_cli.py::test_row_23_login_defaults_to_owner_actor_and_reuses_the_client` |
49
+ | 24 | `test_row_24_impersonate_without_an_actors_entry` |
50
+ | 25 | `test_cli.py::test_row_25_token_writes_only_the_token_then_fails_cleanly` |
51
+ | 26 | `test_row_26_misconfiguration_names_the_setting_and_registers_nothing`, `test_row_26_missing_secret_names_the_setting` |
52
+ | 27 | `test_row_27_empty_actor_is_owner_actor` |
53
+ | 28 | `test_row_28_actor_aliases_map_an_a2a_peer` |
54
+ | 29 | `test_row_29_tool_with_no_resources_runs_with_an_empty_context` |
55
+ | 30 | `test_relay.py::test_row_30_401_acquires_a_new_token_and_retries_once` |
56
+ | 31 | `test_relay.py::test_row_31_404_replays_initialize_and_retries_on_the_new_session` |
57
+ | 32 | `test_relay.py::test_row_32_batch_notification_and_sse` |
58
+ | 33 | `test_cli.py::test_row_33_token_as_self_uses_client_credentials_and_reads_no_sign_in` |
59
+ | 34 | `test_relay.py::test_row_34_missing_secret_env_fails_every_request_without_a_token_request` |
60
+ | 35 | `test_row_35_resources_cover_built_in_tools` |
61
+
62
+ Rows in `tests/test_middleware.py` unless noted. `tests/test_settings.py` covers
63
+ settings validation and the Hermes default seams; `tests/test_testing_seam.py`
64
+ covers `keycardai.hermes.testing`.
65
+
66
+ ## Integration
67
+
68
+ None against a live zone, as the spec says: the framework is not on PyPI and
69
+ the sign-in is interactive.
70
+
71
+ Beyond the spec rows, `tests/test_hardening.py` covers: distinct actors never
72
+ sharing a sign-in file, private store directories, the plugin host child
73
+ counting as host isolation, `grant()` resolving its profile by Hermes home, the
74
+ relay surviving an unexpected error and answering a parse error, `login`
75
+ saving under the sign-in lock, UserInfo email matching, secret source
76
+ warnings, and the relay's SSE line splitting, session replay and inline
77
+ notifications. Rows 44 (an API server turn with an empty actor uses
78
+ `owner_actor`) and 45 (an unknown surface with an empty actor is
79
+ `missing_identity`) are in `tests/test_middleware.py`.