keywright-router 0.0.1__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.
- keywright_router-0.0.1/.gitignore +48 -0
- keywright_router-0.0.1/LICENSE +202 -0
- keywright_router-0.0.1/NOTICE +5 -0
- keywright_router-0.0.1/PKG-INFO +140 -0
- keywright_router-0.0.1/README.md +120 -0
- keywright_router-0.0.1/THIRD_PARTY_NOTICES +55 -0
- keywright_router-0.0.1/keywright_router/__init__.py +9 -0
- keywright_router-0.0.1/keywright_router/__main__.py +83 -0
- keywright_router-0.0.1/keywright_router/access.py +114 -0
- keywright_router-0.0.1/keywright_router/config.py +241 -0
- keywright_router-0.0.1/keywright_router/kw.py +99 -0
- keywright_router-0.0.1/keywright_router/router.py +331 -0
- keywright_router-0.0.1/keywright_router/rules.py +124 -0
- keywright_router-0.0.1/keywright_router/state.py +67 -0
- keywright_router-0.0.1/pyproject.toml +36 -0
- keywright_router-0.0.1/rules.example.yaml +79 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Local configuration and credentials
|
|
2
|
+
.env
|
|
3
|
+
.env.*
|
|
4
|
+
*.env
|
|
5
|
+
*.env.*
|
|
6
|
+
!**/.env.example
|
|
7
|
+
!**/*.env.example
|
|
8
|
+
*.pem
|
|
9
|
+
*.key
|
|
10
|
+
*.p12
|
|
11
|
+
*.pfx
|
|
12
|
+
id_rsa*
|
|
13
|
+
id_ed25519*
|
|
14
|
+
# Runtime and local environments
|
|
15
|
+
.venv/
|
|
16
|
+
venv/
|
|
17
|
+
data/
|
|
18
|
+
*.db
|
|
19
|
+
*.db-*
|
|
20
|
+
*.sqlite*
|
|
21
|
+
*.log
|
|
22
|
+
__pycache__/
|
|
23
|
+
*.py[cod]
|
|
24
|
+
.pytest_cache/
|
|
25
|
+
.ruff_cache/
|
|
26
|
+
.coverage
|
|
27
|
+
htmlcov/
|
|
28
|
+
# Build and local tooling
|
|
29
|
+
dist/
|
|
30
|
+
build/
|
|
31
|
+
*.egg-info/
|
|
32
|
+
.idea/
|
|
33
|
+
.vscode/
|
|
34
|
+
.DS_Store
|
|
35
|
+
# Operational captures belong outside the source repository
|
|
36
|
+
reports/
|
|
37
|
+
artifacts/
|
|
38
|
+
backups/
|
|
39
|
+
*.tar.gz
|
|
40
|
+
*.zip
|
|
41
|
+
|
|
42
|
+
# Generated release material outputs (never source inputs)
|
|
43
|
+
/inventory.json
|
|
44
|
+
/materials/
|
|
45
|
+
/source-link.Dockerfile
|
|
46
|
+
/release-input/
|
|
47
|
+
/release-assets/
|
|
48
|
+
/release-state.json
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: keywright-router
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Keywright router: routes incoming mail with boolean rules, acting only through Keywright.
|
|
5
|
+
Project-URL: Repository, https://github.com/johnrcarty/keywright
|
|
6
|
+
Project-URL: Issues, https://github.com/johnrcarty/keywright/issues
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
License-File: NOTICE
|
|
10
|
+
License-File: THIRD_PARTY_NOTICES
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Requires-Dist: httpx>=0.27
|
|
13
|
+
Requires-Dist: keywright<0.1,>=0.0.1
|
|
14
|
+
Requires-Dist: pyyaml>=6
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
17
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
18
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# keywright-router
|
|
22
|
+
|
|
23
|
+
Acts on mail that reaches the agents' mailbox. It is an ordinary Keywright agent (`mail-router`):
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
Mail provider ⇄ email-mcp ⇄ Keywright ⇄ router ──(label / draft only)──> Keywright /llm ⇄ LiteLLM
|
|
27
|
+
└──(actions)──> Keywright ⇄ email-mcp / ha-mcp / ...
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- **No secrets.** The mailbox password is only in `email-mcp`, the provider keys are only in LiteLLM.
|
|
31
|
+
The router holds a Keywright token, so revoking `mail-router` stops it completely.
|
|
32
|
+
- **Routing is boolean.** Rules in `rules.yaml` test sender, recipients, subject, body and attachments.
|
|
33
|
+
The model is asked only when a rule reaches a `label:` test (one call per message, answer
|
|
34
|
+
restricted to the configured labels) or an action drafts text with `llm:`.
|
|
35
|
+
- **Mail never chooses where things go.** `send`/`forward` addresses and `mcp` tool names are
|
|
36
|
+
literals in the rules. Message text reaches a call only through `{{placeholders}}` in free
|
|
37
|
+
text. The one exception is `reply`, which answers the sender, and Keywright's recipient rules
|
|
38
|
+
gate that like any other send. The router never replies to bounces or robots.
|
|
39
|
+
- **Every action is a Keywright call.** A call the agent may not make without asking puts a
|
|
40
|
+
prompt on the phone. The message's job waits (checked with `access_status`, never re-sent),
|
|
41
|
+
resumes with the identical call once approved, and gives up after `pending_hours`.
|
|
42
|
+
- **Leaves your mail alone unless a rule claims it.** Handled mail is marked read and filed in
|
|
43
|
+
`done_folder`, or in the folder a rule's `move` names. Mail no rule claimed stays unread in the
|
|
44
|
+
inbox. Only mail that arrives after the router's first start is considered.
|
|
45
|
+
|
|
46
|
+
Rules reference: see [rules.example.yaml](rules.example.yaml). Commands:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
keywright-router check # validate the rules file
|
|
50
|
+
keywright-router dry-run # route today's unread mail and print the calls, making none
|
|
51
|
+
keywright-router run # the container's command
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Setting it up (once)
|
|
55
|
+
|
|
56
|
+
1. **Mailbox.** Fill in a protected `email.env` file on the broker host (0600; template
|
|
57
|
+
`deploy/stack/email.env.example`): address, IMAP/SMTP hosts, password, recipient allowlist.
|
|
58
|
+
2. **Keywright resource `email`** (Resources → add MCP server):
|
|
59
|
+
URL `http://email-mcp:9557/mcp`, default **approval**,
|
|
60
|
+
argument keys `account_name, recipients, cc, bcc, attachments, include_attachments`.
|
|
61
|
+
Each key present in a call adds a path (`.../send_email/recipients=a@b.c`) and every one
|
|
62
|
+
must be allowed, so a recipient that is not allowed asks even when the rest of the call is.
|
|
63
|
+
Standing rules for every agent:
|
|
64
|
+
- deny `mcp/email/tools/delete_emails`, `mcp/email/tools/download_attachment`, `mcp/email/tools/set_email_tags`
|
|
65
|
+
- deny `mcp/email/tools/send_email/attachments=*` and `mcp/email/tools/forward_email/include_attachments=True`
|
|
66
|
+
unless an agent needs them
|
|
67
|
+
3. **Agent `mail-router`** (Agents → add). Put its token in `router.env` as `KW_TOKEN=kw_...`.
|
|
68
|
+
No rules by hand: on its first start the router works out what its rules need (the mailbox
|
|
69
|
+
reads, each fixed `mcp` call, each `forward`/`send` recipient), qualified exactly the way
|
|
70
|
+
Keywright qualifies the calls, and asks for all of it in **one** request. Open it under
|
|
71
|
+
Requests and choose **Always allow**. It does not poll while that request waits. Replies,
|
|
72
|
+
values a rule fills from the message, and model access are left out on purpose and ask per
|
|
73
|
+
call (a model needs a rule in the UI: `llm/<gateway>/model/<model>`). Preview the list with
|
|
74
|
+
`keywright-router access`; after editing the rules, restart the router and it asks only for
|
|
75
|
+
what is new.
|
|
76
|
+
4. **Rules.** Edit `./rules.yaml` (seeded from the example).
|
|
77
|
+
Set `llm.gateway` to the name of the LLM gateway resource.
|
|
78
|
+
5. **Start.** Deploy your chosen email MCP service separately. Install the router from
|
|
79
|
+
source with `uv pip install ./client ./router`, then run
|
|
80
|
+
`keywright-router dry-run --config ./rules.yaml` before enabling
|
|
81
|
+
`keywright-router run --config ./rules.yaml`. Supply `KW_URL` and `KW_TOKEN` through
|
|
82
|
+
the environment. This optional integration is not part of the minimal Compose stack.
|
|
83
|
+
|
|
84
|
+
`email-mcp` also enforces its own recipient floor (`MCP_EMAIL_SERVER_ALLOWED_RECIPIENTS` in
|
|
85
|
+
`email.env`). Nothing leaves for an address outside it, whoever approved it.
|
|
86
|
+
|
|
87
|
+
## Web inbox and PDF delivery
|
|
88
|
+
|
|
89
|
+
Keywright's **Mail** screen (`/ui/mail`) adds a searchable inbox, reading pane, PDF downloads,
|
|
90
|
+
email rules, and local processing tags: Unprocessed, Queued, Processing, Waiting for approval,
|
|
91
|
+
Completed, and Failed. Tags are stored in Keywright, independently of the mailbox's read flag;
|
|
92
|
+
opening a message does not mark it read. All indexed mail retains delivery history after a
|
|
93
|
+
message leaves the watched folder. The cache indexes up to 10,000 messages in the selected
|
|
94
|
+
mailbox and renders message bodies as escaped text, without loading remote email images.
|
|
95
|
+
|
|
96
|
+
This worker runs inside Keywright and is disabled until configured in **Mail → Connection**.
|
|
97
|
+
Select the existing email resource, account `agents`, mailbox `INBOX`, and an enabled agent
|
|
98
|
+
(normally `mail-router`). Mailbox reads, attachment reads, and mutations go through that agent's
|
|
99
|
+
existing broker permissions and audit log. Pending approvals park a delivery; a denial or
|
|
100
|
+
failure produces a Failed tag and a manual Retry action. Run a single Keywright server worker,
|
|
101
|
+
as in the shipped stack; its in-process lock serializes mailbox sync and delivery operations.
|
|
102
|
+
|
|
103
|
+
The UI rules are separate from the existing YAML rules. Pause overlapping YAML rules (or stop
|
|
104
|
+
the legacy router container once its jobs have finished) before enabling the new worker. The
|
|
105
|
+
first sync only indexes existing mail. Subsequently discovered mail can run automatically;
|
|
106
|
+
existing messages require **Run rules**. Editing a rule does not re-run old messages. Rules use
|
|
107
|
+
sender address patterns and subject substrings, with the first enabled match winning in priority
|
|
108
|
+
order. Actions currently include PDF delivery to Budget, marking read, and moving to a folder.
|
|
109
|
+
|
|
110
|
+
For PDF delivery:
|
|
111
|
+
|
|
112
|
+
1. Deploy the budget app's document intake feature and create a **document delivery token** under
|
|
113
|
+
**Settings → Document inbox**. This is separate from its Home Assistant summary token.
|
|
114
|
+
2. In **Mail → Connection**, enter the budget app's direct server URL and delivery token. The
|
|
115
|
+
endpoint is fixed to `/api/integrations/documents`. Do not use a Home Assistant ingress URL.
|
|
116
|
+
3. On email-mcp 1.9.1, set `MCP_EMAIL_SERVER_ENABLE_ATTACHMENT_CONTENT=true` in `email.env` and
|
|
117
|
+
recreate that service. For managed email-server configurations, enable the corresponding
|
|
118
|
+
attachment-content policy instead. This does not enable filesystem attachment downloads.
|
|
119
|
+
4. Allow `get_attachment_content` for the chosen agent/account, along with metadata/body reads and
|
|
120
|
+
marking read. A deny on `download_attachment` can remain: the UI uses embedded binary resources.
|
|
121
|
+
5. Add an electric-bill rule with **Send PDFs to Budget**. Test it on one existing message before
|
|
122
|
+
enabling automatic processing.
|
|
123
|
+
|
|
124
|
+
Each PDF is transferred byte-for-byte, without parsing or changing budget amounts. The worker
|
|
125
|
+
accepts PDFs up to 5 MiB each (within email-mcp's 8 MiB serialized-result ceiling). Budget confirms
|
|
126
|
+
the SHA-256 checksum, and Keywright stores the receipt before continuing. Partial delivery retries
|
|
127
|
+
skip confirmed files; Budget also deduplicates a delivery if the response was lost. Mail is marked
|
|
128
|
+
read only after all PDFs are confirmed. Completed mail stays in the inbox. Destination changes do
|
|
129
|
+
not silently redirect an existing job: restore its original destination before retrying it.
|
|
130
|
+
|
|
131
|
+
The budget token stays in Keywright's protected settings database and is never rendered back
|
|
132
|
+
into the form or included in audit details. Back up that database as a credential-bearing file.
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
Keywright is licensed under [Apache-2.0](LICENSE). Copyright 2026 Keywright
|
|
137
|
+
contributors. See [NOTICE](NOTICE) and [third-party notices](THIRD_PARTY_NOTICES).
|
|
138
|
+
Commercial use and proprietary extensions are permitted under the license's terms.
|
|
139
|
+
Unless explicitly stated otherwise, contributions intentionally submitted for
|
|
140
|
+
inclusion in Keywright are provided under Apache-2.0.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# keywright-router
|
|
2
|
+
|
|
3
|
+
Acts on mail that reaches the agents' mailbox. It is an ordinary Keywright agent (`mail-router`):
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
Mail provider ⇄ email-mcp ⇄ Keywright ⇄ router ──(label / draft only)──> Keywright /llm ⇄ LiteLLM
|
|
7
|
+
└──(actions)──> Keywright ⇄ email-mcp / ha-mcp / ...
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- **No secrets.** The mailbox password is only in `email-mcp`, the provider keys are only in LiteLLM.
|
|
11
|
+
The router holds a Keywright token, so revoking `mail-router` stops it completely.
|
|
12
|
+
- **Routing is boolean.** Rules in `rules.yaml` test sender, recipients, subject, body and attachments.
|
|
13
|
+
The model is asked only when a rule reaches a `label:` test (one call per message, answer
|
|
14
|
+
restricted to the configured labels) or an action drafts text with `llm:`.
|
|
15
|
+
- **Mail never chooses where things go.** `send`/`forward` addresses and `mcp` tool names are
|
|
16
|
+
literals in the rules. Message text reaches a call only through `{{placeholders}}` in free
|
|
17
|
+
text. The one exception is `reply`, which answers the sender, and Keywright's recipient rules
|
|
18
|
+
gate that like any other send. The router never replies to bounces or robots.
|
|
19
|
+
- **Every action is a Keywright call.** A call the agent may not make without asking puts a
|
|
20
|
+
prompt on the phone. The message's job waits (checked with `access_status`, never re-sent),
|
|
21
|
+
resumes with the identical call once approved, and gives up after `pending_hours`.
|
|
22
|
+
- **Leaves your mail alone unless a rule claims it.** Handled mail is marked read and filed in
|
|
23
|
+
`done_folder`, or in the folder a rule's `move` names. Mail no rule claimed stays unread in the
|
|
24
|
+
inbox. Only mail that arrives after the router's first start is considered.
|
|
25
|
+
|
|
26
|
+
Rules reference: see [rules.example.yaml](rules.example.yaml). Commands:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
keywright-router check # validate the rules file
|
|
30
|
+
keywright-router dry-run # route today's unread mail and print the calls, making none
|
|
31
|
+
keywright-router run # the container's command
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Setting it up (once)
|
|
35
|
+
|
|
36
|
+
1. **Mailbox.** Fill in a protected `email.env` file on the broker host (0600; template
|
|
37
|
+
`deploy/stack/email.env.example`): address, IMAP/SMTP hosts, password, recipient allowlist.
|
|
38
|
+
2. **Keywright resource `email`** (Resources → add MCP server):
|
|
39
|
+
URL `http://email-mcp:9557/mcp`, default **approval**,
|
|
40
|
+
argument keys `account_name, recipients, cc, bcc, attachments, include_attachments`.
|
|
41
|
+
Each key present in a call adds a path (`.../send_email/recipients=a@b.c`) and every one
|
|
42
|
+
must be allowed, so a recipient that is not allowed asks even when the rest of the call is.
|
|
43
|
+
Standing rules for every agent:
|
|
44
|
+
- deny `mcp/email/tools/delete_emails`, `mcp/email/tools/download_attachment`, `mcp/email/tools/set_email_tags`
|
|
45
|
+
- deny `mcp/email/tools/send_email/attachments=*` and `mcp/email/tools/forward_email/include_attachments=True`
|
|
46
|
+
unless an agent needs them
|
|
47
|
+
3. **Agent `mail-router`** (Agents → add). Put its token in `router.env` as `KW_TOKEN=kw_...`.
|
|
48
|
+
No rules by hand: on its first start the router works out what its rules need (the mailbox
|
|
49
|
+
reads, each fixed `mcp` call, each `forward`/`send` recipient), qualified exactly the way
|
|
50
|
+
Keywright qualifies the calls, and asks for all of it in **one** request. Open it under
|
|
51
|
+
Requests and choose **Always allow**. It does not poll while that request waits. Replies,
|
|
52
|
+
values a rule fills from the message, and model access are left out on purpose and ask per
|
|
53
|
+
call (a model needs a rule in the UI: `llm/<gateway>/model/<model>`). Preview the list with
|
|
54
|
+
`keywright-router access`; after editing the rules, restart the router and it asks only for
|
|
55
|
+
what is new.
|
|
56
|
+
4. **Rules.** Edit `./rules.yaml` (seeded from the example).
|
|
57
|
+
Set `llm.gateway` to the name of the LLM gateway resource.
|
|
58
|
+
5. **Start.** Deploy your chosen email MCP service separately. Install the router from
|
|
59
|
+
source with `uv pip install ./client ./router`, then run
|
|
60
|
+
`keywright-router dry-run --config ./rules.yaml` before enabling
|
|
61
|
+
`keywright-router run --config ./rules.yaml`. Supply `KW_URL` and `KW_TOKEN` through
|
|
62
|
+
the environment. This optional integration is not part of the minimal Compose stack.
|
|
63
|
+
|
|
64
|
+
`email-mcp` also enforces its own recipient floor (`MCP_EMAIL_SERVER_ALLOWED_RECIPIENTS` in
|
|
65
|
+
`email.env`). Nothing leaves for an address outside it, whoever approved it.
|
|
66
|
+
|
|
67
|
+
## Web inbox and PDF delivery
|
|
68
|
+
|
|
69
|
+
Keywright's **Mail** screen (`/ui/mail`) adds a searchable inbox, reading pane, PDF downloads,
|
|
70
|
+
email rules, and local processing tags: Unprocessed, Queued, Processing, Waiting for approval,
|
|
71
|
+
Completed, and Failed. Tags are stored in Keywright, independently of the mailbox's read flag;
|
|
72
|
+
opening a message does not mark it read. All indexed mail retains delivery history after a
|
|
73
|
+
message leaves the watched folder. The cache indexes up to 10,000 messages in the selected
|
|
74
|
+
mailbox and renders message bodies as escaped text, without loading remote email images.
|
|
75
|
+
|
|
76
|
+
This worker runs inside Keywright and is disabled until configured in **Mail → Connection**.
|
|
77
|
+
Select the existing email resource, account `agents`, mailbox `INBOX`, and an enabled agent
|
|
78
|
+
(normally `mail-router`). Mailbox reads, attachment reads, and mutations go through that agent's
|
|
79
|
+
existing broker permissions and audit log. Pending approvals park a delivery; a denial or
|
|
80
|
+
failure produces a Failed tag and a manual Retry action. Run a single Keywright server worker,
|
|
81
|
+
as in the shipped stack; its in-process lock serializes mailbox sync and delivery operations.
|
|
82
|
+
|
|
83
|
+
The UI rules are separate from the existing YAML rules. Pause overlapping YAML rules (or stop
|
|
84
|
+
the legacy router container once its jobs have finished) before enabling the new worker. The
|
|
85
|
+
first sync only indexes existing mail. Subsequently discovered mail can run automatically;
|
|
86
|
+
existing messages require **Run rules**. Editing a rule does not re-run old messages. Rules use
|
|
87
|
+
sender address patterns and subject substrings, with the first enabled match winning in priority
|
|
88
|
+
order. Actions currently include PDF delivery to Budget, marking read, and moving to a folder.
|
|
89
|
+
|
|
90
|
+
For PDF delivery:
|
|
91
|
+
|
|
92
|
+
1. Deploy the budget app's document intake feature and create a **document delivery token** under
|
|
93
|
+
**Settings → Document inbox**. This is separate from its Home Assistant summary token.
|
|
94
|
+
2. In **Mail → Connection**, enter the budget app's direct server URL and delivery token. The
|
|
95
|
+
endpoint is fixed to `/api/integrations/documents`. Do not use a Home Assistant ingress URL.
|
|
96
|
+
3. On email-mcp 1.9.1, set `MCP_EMAIL_SERVER_ENABLE_ATTACHMENT_CONTENT=true` in `email.env` and
|
|
97
|
+
recreate that service. For managed email-server configurations, enable the corresponding
|
|
98
|
+
attachment-content policy instead. This does not enable filesystem attachment downloads.
|
|
99
|
+
4. Allow `get_attachment_content` for the chosen agent/account, along with metadata/body reads and
|
|
100
|
+
marking read. A deny on `download_attachment` can remain: the UI uses embedded binary resources.
|
|
101
|
+
5. Add an electric-bill rule with **Send PDFs to Budget**. Test it on one existing message before
|
|
102
|
+
enabling automatic processing.
|
|
103
|
+
|
|
104
|
+
Each PDF is transferred byte-for-byte, without parsing or changing budget amounts. The worker
|
|
105
|
+
accepts PDFs up to 5 MiB each (within email-mcp's 8 MiB serialized-result ceiling). Budget confirms
|
|
106
|
+
the SHA-256 checksum, and Keywright stores the receipt before continuing. Partial delivery retries
|
|
107
|
+
skip confirmed files; Budget also deduplicates a delivery if the response was lost. Mail is marked
|
|
108
|
+
read only after all PDFs are confirmed. Completed mail stays in the inbox. Destination changes do
|
|
109
|
+
not silently redirect an existing job: restore its original destination before retrying it.
|
|
110
|
+
|
|
111
|
+
The budget token stays in Keywright's protected settings database and is never rendered back
|
|
112
|
+
into the form or included in audit details. Back up that database as a credential-bearing file.
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
Keywright is licensed under [Apache-2.0](LICENSE). Copyright 2026 Keywright
|
|
117
|
+
contributors. See [NOTICE](NOTICE) and [third-party notices](THIRD_PARTY_NOTICES).
|
|
118
|
+
Commercial use and proprietary extensions are permitted under the license's terms.
|
|
119
|
+
Unless explicitly stated otherwise, contributions intentionally submitted for
|
|
120
|
+
inclusion in Keywright are provided under Apache-2.0.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Third-party components
|
|
2
|
+
======================
|
|
3
|
+
|
|
4
|
+
Apache-2.0 applies to Keywright's own code and documentation. Dependencies,
|
|
5
|
+
container base images, optional services and embedding models retain their own
|
|
6
|
+
licenses. This file is a guide to those components, not a replacement for their
|
|
7
|
+
license texts or a complete inventory of a particular image.
|
|
8
|
+
|
|
9
|
+
Python dependencies
|
|
10
|
+
-------------------
|
|
11
|
+
Keywright distributions declare dependencies rather than vendoring them. pip
|
|
12
|
+
installs their license files alongside their package metadata, typically under
|
|
13
|
+
/usr/local/lib/python3.13/site-packages/<distribution>.dist-info/ in the supplied
|
|
14
|
+
images. Preserve those files when redistributing an installation or image.
|
|
15
|
+
The pyproject.toml files declare the dependency ranges; resolved versions may
|
|
16
|
+
change between builds.
|
|
17
|
+
|
|
18
|
+
AsyncSSH is used as an unmodified library under EPL-2.0. Its installed asyncssh/
|
|
19
|
+
directory contains the Python source, including upstream copyright headers;
|
|
20
|
+
its distribution metadata includes the Eclipse Public License text. Source and
|
|
21
|
+
release history: https://github.com/ronf/asyncssh
|
|
22
|
+
|
|
23
|
+
Certifi uses MPL-2.0 for its certificate bundle. The installed certifi/ directory
|
|
24
|
+
contains the bundle and Python source, and its distribution metadata includes
|
|
25
|
+
the license. Source and release history: https://github.com/certifi/python-certifi
|
|
26
|
+
|
|
27
|
+
Container operating-system packages
|
|
28
|
+
-----------------------------------
|
|
29
|
+
The images use the official Python slim image and Debian packages. License and
|
|
30
|
+
copyright notices are retained under /usr/share/doc/<package>/copyright and in
|
|
31
|
+
Python's installation. Debian source packages are available through the Debian
|
|
32
|
+
archive and https://snapshot.debian.org/ for the corresponding package version.
|
|
33
|
+
Some system tools have copyleft licenses; redistributors must satisfy their
|
|
34
|
+
source-distribution requirements as well as retaining notices. The release workflow verifies exact installed versions against a reviewed source
|
|
35
|
+
lock and publishes matching source materials before publishing the application
|
|
36
|
+
image. The image label io.keywright.source-image identifies the matching source
|
|
37
|
+
image in the same registry package; retain it while distributing the application.
|
|
38
|
+
Version-tag builds also attach source and notice assets to a draft GitHub Release. A Keywright image license label describes the
|
|
39
|
+
Keywright application, not every component in the image.
|
|
40
|
+
|
|
41
|
+
Optional services
|
|
42
|
+
-----------------
|
|
43
|
+
PostgreSQL, pgvector, LiteLLM and any embedding server/model are separate services
|
|
44
|
+
with their own upstream terms. Check the particular image and model version you
|
|
45
|
+
deploy or redistribute. Keywright's license does not relicense those services.
|
|
46
|
+
|
|
47
|
+
Supplemental native notices and source coverage
|
|
48
|
+
---------------------------------------------
|
|
49
|
+
Prebuilt images include supplemental upstream notices under
|
|
50
|
+
/usr/share/keywright/licenses/LICENSES.txt and a verified dependency inventory.
|
|
51
|
+
The source lock and notice provenance are maintained under third_party/ in the
|
|
52
|
+
repository. This covers bundled native libraries in addition to installed package
|
|
53
|
+
notices, including libffi, libuv, OpenSSL and Rust components. See
|
|
54
|
+
https://github.com/johnrcarty/keywright/blob/main/third_party/README.md for obtaining
|
|
55
|
+
and verifying the matching source archive without running the source image.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Keywright router: watches a mailbox through Keywright and acts on what arrives.
|
|
2
|
+
|
|
3
|
+
The router is an ordinary Keywright agent. It holds no mail password and no provider key:
|
|
4
|
+
it reads and sends mail through the brokered `email` MCP server, asks the LLM gateway only
|
|
5
|
+
when a rule needs a label or a draft, and every call it makes is checked against the rules
|
|
6
|
+
the human set for its token. Routing itself is plain boolean logic over the message.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__version__ = "0.0.1"
|