protonlite-mcp 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 William Wilamo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,174 @@
1
+ Metadata-Version: 2.4
2
+ Name: protonlite-mcp
3
+ Version: 0.2.0
4
+ Summary: MCP server for Proton Mail, Calendar, Contacts and Drive -- no Bridge, no paid plan
5
+ Author: William Wilamo
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Maytrella007/protonlite-mcp
8
+ Project-URL: Issues, https://github.com/Maytrella007/protonlite-mcp/issues
9
+ Project-URL: Library, https://github.com/Maytrella007/protonlite
10
+ Keywords: mcp,mcp-server,model-context-protocol,proton,protonmail,proton-mail,proton-drive,proton-calendar,email,claude,cursor,agent
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Communications :: Email
15
+ Classifier: Topic :: Security :: Cryptography
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: protonlite<1,>=0.2
20
+ Requires-Dist: mcp<2,>=1.2
21
+ Dynamic: license-file
22
+
23
+ <!-- mcp-name: io.github.Maytrella007/protonlite-mcp -->
24
+
25
+ # protonlite-mcp
26
+
27
+ An **MCP server** for **Proton Mail** (ProtonMail), Proton Calendar, Proton Contacts and
28
+ Proton Drive. Point Claude Code, Claude Desktop, Cursor or any other
29
+ [MCP](https://modelcontextprotocol.io) client at it and the agent can read and send your
30
+ mail, check your agenda, look up contacts and work with your files -- with **no Proton Mail
31
+ Bridge and no paid plan**.
32
+
33
+ It is the server half of [protonlite](https://github.com/Maytrella007/protonlite), which does
34
+ the Proton API and the end-to-end crypto (ported from Proton's own MIT-licensed Go
35
+ libraries). This package is the MCP wiring: the tools, their descriptions, and the policy
36
+ that governs sending.
37
+
38
+ > **Unofficial.** Not affiliated with, endorsed by, or supported by Proton AG. "Proton",
39
+ > "Proton Mail", "Proton Calendar" and "Proton Drive" are trademarks of Proton AG. It talks
40
+ > to a private, undocumented API that can change at any time, and using it may be subject to
41
+ > Proton's terms of service. Use it at your own risk, on accounts you own.
42
+
43
+ ## Install
44
+
45
+ ```bash
46
+ pip install protonlite-mcp
47
+ ```
48
+
49
+ Requires Python 3.10+. The `protonlite` library comes along as a dependency. To run it
50
+ without installing anything, use `uvx protonlite-mcp`.
51
+
52
+ ## Sign in once
53
+
54
+ The server never takes a password. A person signs in from a terminal, which saves a session
55
+ file, and the server reuses that file:
56
+
57
+ ```bash
58
+ protonlite login --user you@proton.me # prompts for the password with getpass
59
+ ```
60
+
61
+ Proton usually asks a human to complete a verification page (hCaptcha) on a server login;
62
+ the CLI prints that URL so you can finish it in a browser. Two-factor (TOTP) is supported.
63
+ The login prints where it saved the session; that path is what you hand the server in
64
+ `PROTONLITE_SESSION`.
65
+
66
+ ## Run it
67
+
68
+ ```bash
69
+ PROTONLITE_SESSION=~/you@proton.me.json protonlite-mcp
70
+ ```
71
+
72
+ The bare command speaks MCP over stdio, which is what a client launches. `python -m
73
+ protonlite_mcp` and `uvx protonlite-mcp` run the same server.
74
+
75
+ ### Claude Code
76
+
77
+ ```bash
78
+ claude mcp add proton -e PROTONLITE_SESSION=/home/you/you@proton.me.json -- protonlite-mcp
79
+ ```
80
+
81
+ ### Claude Desktop, Cursor and other `mcpServers` configs
82
+
83
+ ```json
84
+ {
85
+ "mcpServers": {
86
+ "proton": {
87
+ "command": "protonlite-mcp",
88
+ "env": { "PROTONLITE_SESSION": "/home/you/you@proton.me.json" }
89
+ }
90
+ }
91
+ }
92
+ ```
93
+
94
+ With `uvx` instead of a local install, use `"command": "uvx"` and
95
+ `"args": ["protonlite-mcp"]`.
96
+
97
+ ## The 18 tools
98
+
99
+ | Area | Tools |
100
+ |---|---|
101
+ | Mail | `list_messages`, `read_message`, `search_messages`, `send_message` |
102
+ | Calendar | `calendar` |
103
+ | Contacts | `contacts` |
104
+ | Drive | `drive_list`, `drive_download`, `drive_upload`, `drive_mkdir`, `drive_move`, `drive_rename`, `drive_trash`, `drive_share_link`, `drive_unshare_link` |
105
+ | Send policy | `email_policy_status`, `email_allow`, `set_send_confirmation` |
106
+
107
+ `send_message` sends plain text or HTML and can attach local files, for example one the agent
108
+ just fetched with `drive_download`. `drive_share_link` returns a public link whose URL is the
109
+ whole secret (the password rides in the fragment, so it never reaches Proton's servers):
110
+ anyone holding that URL can open the file, so it is meant for files you intend to share, and
111
+ `drive_unshare_link` revokes it.
112
+
113
+ Everything other people wrote -- message bodies, calendar titles, contact notes, file names
114
+ -- comes back wrapped as untrusted data, because an MCP client is a language model and that
115
+ text must never be read as instructions.
116
+
117
+ ## Send policy
118
+
119
+ Sending is the one action that reaches other people, so `send_message` is governed by a
120
+ recipient allowlist and an optional "confirm before sending" rule. There are two layers.
121
+
122
+ **What the agent can change** (convenient, and therefore advisory):
123
+
124
+ - `email_policy_status` -- show the allowlist, any hard ceiling, and whether confirmation is on.
125
+ - `email_allow` -- set the allowlist: addresses or globs (`*@company.com`) to restrict
126
+ sending, `["*"]` to allow anyone, `[]` to block every send.
127
+ - `set_send_confirmation` -- the soft two-step rule: the first `send_message` sends nothing
128
+ and asks for approval, and a second call with `confirm=true` sends.
129
+
130
+ **What only a person can change**, out of band, which no tool can loosen:
131
+
132
+ ```bash
133
+ protonlite-mcp policy show # print the effective policy
134
+ protonlite-mcp policy ceiling '*@your-company.com' # a ceiling email_allow cannot exceed
135
+ protonlite-mcp policy require-confirm on # the agent can no longer send at all
136
+ protonlite-mcp policy require-confirm off # lift it
137
+ ```
138
+
139
+ - **Recipient ceiling** -- `protonlite-mcp policy ceiling <addrs>`, or the
140
+ `PROTON_ALLOWED_RECIPIENTS` env var (comma-separated addresses or globs). A send to anyone
141
+ outside it is refused whatever the agent does.
142
+ - **Hard confirmation** -- `protonlite-mcp policy require-confirm on`, or
143
+ `PROTON_REQUIRE_SEND_CONFIRMATION=1`, turns sending off for the agent entirely: the
144
+ `confirm` argument cannot lift it, and a person sends from a terminal with `protonlite
145
+ send`.
146
+
147
+ The hard half lives in a `locked` section of the policy file that only the CLI writes
148
+ (`~/.config/protonlite/mcp_policy.json`, or `PROTONLITE_MCP_POLICY`). `email_policy_status`
149
+ always reports both layers and the exact command to change each one, so the agent can tell a
150
+ person what to run instead of guessing.
151
+
152
+ Run the server in a client that asks you to approve each tool call: `send_message` and
153
+ `drive_upload` read local files and can send them out. For a send gate an agent can never
154
+ reach -- a fail-closed human-approval hook on every send -- use
155
+ [hermes-proton](https://github.com/Maytrella007/hermes-proton).
156
+
157
+ ## Container
158
+
159
+ ```bash
160
+ docker build -t protonlite-mcp .
161
+ docker run -i --rm -e PROTONLITE_SESSION=/session.json -v ~/you@proton.me.json:/session.json:ro protonlite-mcp
162
+ ```
163
+
164
+ ## Related
165
+
166
+ - [protonlite](https://github.com/Maytrella007/protonlite) -- the Python library and CLI this
167
+ server is built on (Mail, Calendar, Contacts, Drive, and the login).
168
+ - [hermes-proton](https://github.com/Maytrella007/hermes-proton) -- the same engine as a
169
+ [Hermes Agent](https://github.com/NousResearch/hermes-agent) plugin, with a human-approval
170
+ gate on every send.
171
+
172
+ ## License
173
+
174
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,152 @@
1
+ <!-- mcp-name: io.github.Maytrella007/protonlite-mcp -->
2
+
3
+ # protonlite-mcp
4
+
5
+ An **MCP server** for **Proton Mail** (ProtonMail), Proton Calendar, Proton Contacts and
6
+ Proton Drive. Point Claude Code, Claude Desktop, Cursor or any other
7
+ [MCP](https://modelcontextprotocol.io) client at it and the agent can read and send your
8
+ mail, check your agenda, look up contacts and work with your files -- with **no Proton Mail
9
+ Bridge and no paid plan**.
10
+
11
+ It is the server half of [protonlite](https://github.com/Maytrella007/protonlite), which does
12
+ the Proton API and the end-to-end crypto (ported from Proton's own MIT-licensed Go
13
+ libraries). This package is the MCP wiring: the tools, their descriptions, and the policy
14
+ that governs sending.
15
+
16
+ > **Unofficial.** Not affiliated with, endorsed by, or supported by Proton AG. "Proton",
17
+ > "Proton Mail", "Proton Calendar" and "Proton Drive" are trademarks of Proton AG. It talks
18
+ > to a private, undocumented API that can change at any time, and using it may be subject to
19
+ > Proton's terms of service. Use it at your own risk, on accounts you own.
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ pip install protonlite-mcp
25
+ ```
26
+
27
+ Requires Python 3.10+. The `protonlite` library comes along as a dependency. To run it
28
+ without installing anything, use `uvx protonlite-mcp`.
29
+
30
+ ## Sign in once
31
+
32
+ The server never takes a password. A person signs in from a terminal, which saves a session
33
+ file, and the server reuses that file:
34
+
35
+ ```bash
36
+ protonlite login --user you@proton.me # prompts for the password with getpass
37
+ ```
38
+
39
+ Proton usually asks a human to complete a verification page (hCaptcha) on a server login;
40
+ the CLI prints that URL so you can finish it in a browser. Two-factor (TOTP) is supported.
41
+ The login prints where it saved the session; that path is what you hand the server in
42
+ `PROTONLITE_SESSION`.
43
+
44
+ ## Run it
45
+
46
+ ```bash
47
+ PROTONLITE_SESSION=~/you@proton.me.json protonlite-mcp
48
+ ```
49
+
50
+ The bare command speaks MCP over stdio, which is what a client launches. `python -m
51
+ protonlite_mcp` and `uvx protonlite-mcp` run the same server.
52
+
53
+ ### Claude Code
54
+
55
+ ```bash
56
+ claude mcp add proton -e PROTONLITE_SESSION=/home/you/you@proton.me.json -- protonlite-mcp
57
+ ```
58
+
59
+ ### Claude Desktop, Cursor and other `mcpServers` configs
60
+
61
+ ```json
62
+ {
63
+ "mcpServers": {
64
+ "proton": {
65
+ "command": "protonlite-mcp",
66
+ "env": { "PROTONLITE_SESSION": "/home/you/you@proton.me.json" }
67
+ }
68
+ }
69
+ }
70
+ ```
71
+
72
+ With `uvx` instead of a local install, use `"command": "uvx"` and
73
+ `"args": ["protonlite-mcp"]`.
74
+
75
+ ## The 18 tools
76
+
77
+ | Area | Tools |
78
+ |---|---|
79
+ | Mail | `list_messages`, `read_message`, `search_messages`, `send_message` |
80
+ | Calendar | `calendar` |
81
+ | Contacts | `contacts` |
82
+ | Drive | `drive_list`, `drive_download`, `drive_upload`, `drive_mkdir`, `drive_move`, `drive_rename`, `drive_trash`, `drive_share_link`, `drive_unshare_link` |
83
+ | Send policy | `email_policy_status`, `email_allow`, `set_send_confirmation` |
84
+
85
+ `send_message` sends plain text or HTML and can attach local files, for example one the agent
86
+ just fetched with `drive_download`. `drive_share_link` returns a public link whose URL is the
87
+ whole secret (the password rides in the fragment, so it never reaches Proton's servers):
88
+ anyone holding that URL can open the file, so it is meant for files you intend to share, and
89
+ `drive_unshare_link` revokes it.
90
+
91
+ Everything other people wrote -- message bodies, calendar titles, contact notes, file names
92
+ -- comes back wrapped as untrusted data, because an MCP client is a language model and that
93
+ text must never be read as instructions.
94
+
95
+ ## Send policy
96
+
97
+ Sending is the one action that reaches other people, so `send_message` is governed by a
98
+ recipient allowlist and an optional "confirm before sending" rule. There are two layers.
99
+
100
+ **What the agent can change** (convenient, and therefore advisory):
101
+
102
+ - `email_policy_status` -- show the allowlist, any hard ceiling, and whether confirmation is on.
103
+ - `email_allow` -- set the allowlist: addresses or globs (`*@company.com`) to restrict
104
+ sending, `["*"]` to allow anyone, `[]` to block every send.
105
+ - `set_send_confirmation` -- the soft two-step rule: the first `send_message` sends nothing
106
+ and asks for approval, and a second call with `confirm=true` sends.
107
+
108
+ **What only a person can change**, out of band, which no tool can loosen:
109
+
110
+ ```bash
111
+ protonlite-mcp policy show # print the effective policy
112
+ protonlite-mcp policy ceiling '*@your-company.com' # a ceiling email_allow cannot exceed
113
+ protonlite-mcp policy require-confirm on # the agent can no longer send at all
114
+ protonlite-mcp policy require-confirm off # lift it
115
+ ```
116
+
117
+ - **Recipient ceiling** -- `protonlite-mcp policy ceiling <addrs>`, or the
118
+ `PROTON_ALLOWED_RECIPIENTS` env var (comma-separated addresses or globs). A send to anyone
119
+ outside it is refused whatever the agent does.
120
+ - **Hard confirmation** -- `protonlite-mcp policy require-confirm on`, or
121
+ `PROTON_REQUIRE_SEND_CONFIRMATION=1`, turns sending off for the agent entirely: the
122
+ `confirm` argument cannot lift it, and a person sends from a terminal with `protonlite
123
+ send`.
124
+
125
+ The hard half lives in a `locked` section of the policy file that only the CLI writes
126
+ (`~/.config/protonlite/mcp_policy.json`, or `PROTONLITE_MCP_POLICY`). `email_policy_status`
127
+ always reports both layers and the exact command to change each one, so the agent can tell a
128
+ person what to run instead of guessing.
129
+
130
+ Run the server in a client that asks you to approve each tool call: `send_message` and
131
+ `drive_upload` read local files and can send them out. For a send gate an agent can never
132
+ reach -- a fail-closed human-approval hook on every send -- use
133
+ [hermes-proton](https://github.com/Maytrella007/hermes-proton).
134
+
135
+ ## Container
136
+
137
+ ```bash
138
+ docker build -t protonlite-mcp .
139
+ docker run -i --rm -e PROTONLITE_SESSION=/session.json -v ~/you@proton.me.json:/session.json:ro protonlite-mcp
140
+ ```
141
+
142
+ ## Related
143
+
144
+ - [protonlite](https://github.com/Maytrella007/protonlite) -- the Python library and CLI this
145
+ server is built on (Mail, Calendar, Contacts, Drive, and the login).
146
+ - [hermes-proton](https://github.com/Maytrella007/hermes-proton) -- the same engine as a
147
+ [Hermes Agent](https://github.com/NousResearch/hermes-agent) plugin, with a human-approval
148
+ gate on every send.
149
+
150
+ ## License
151
+
152
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,12 @@
1
+ """An MCP server that exposes a Proton account (Mail, Calendar, Contacts, Drive)
2
+ to any MCP client, built on the `protonlite <https://pypi.org/project/protonlite/>`_
3
+ library.
4
+
5
+ Install with ``pip install protonlite-mcp``; the library comes along as a
6
+ dependency. Sign in once with ``protonlite login``, then point the client at
7
+ ``protonlite-mcp``.
8
+ """
9
+
10
+ __version__ = "0.2.0"
11
+
12
+ __all__ = ["__version__"]
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
@@ -0,0 +1,124 @@
1
+ """Command line for the Proton MCP server: ``protonlite-mcp [command]``.
2
+
3
+ With no command it speaks MCP over stdio, which is what an MCP client launches,
4
+ so ``protonlite-mcp`` and ``uvx protonlite-mcp`` are enough to run the server.
5
+
6
+ The ``policy`` commands are the other half: they write the parts of the send
7
+ policy that no tool can change, so they are meant for a person at a terminal.
8
+ The agent-facing half of the same policy lives in the ``email_policy_status``,
9
+ ``email_allow`` and ``set_send_confirmation`` tools.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import argparse
15
+ import sys
16
+
17
+
18
+ def _cmd_serve(_args: argparse.Namespace | None = None) -> int:
19
+ # The mcp SDK is imported here rather than at module level so `policy`
20
+ # keeps working if the SDK is missing or broken.
21
+ from .server import main as serve
22
+
23
+ serve()
24
+ return 0
25
+
26
+
27
+ def _print_policy() -> int:
28
+ # Print the same status text the agent sees from email_policy_status, so a
29
+ # person and the model never read two different descriptions of the policy.
30
+ from . import tools
31
+
32
+ print(tools.email_policy_status())
33
+ return 0
34
+
35
+
36
+ def _cmd_policy_show(_args: argparse.Namespace) -> int:
37
+ return _print_policy()
38
+
39
+
40
+ def _cmd_policy_allow(args: argparse.Namespace) -> int:
41
+ from . import policy
42
+
43
+ policy.set_allowed(args.recipients)
44
+ return _print_policy()
45
+
46
+
47
+ def _cmd_policy_ceiling(args: argparse.Namespace) -> int:
48
+ from . import policy
49
+
50
+ if args.clear:
51
+ policy.set_locked(ceiling=None)
52
+ elif args.recipients:
53
+ policy.set_locked(ceiling=args.recipients)
54
+ else:
55
+ print("give one or more addresses/globs, or --clear to remove the ceiling", file=sys.stderr)
56
+ return 1
57
+ return _print_policy()
58
+
59
+
60
+ def _cmd_policy_require_confirm(args: argparse.Namespace) -> int:
61
+ from . import policy
62
+
63
+ policy.set_locked(require_confirmation=(args.state == "on"))
64
+ return _print_policy()
65
+
66
+
67
+ def build_parser() -> argparse.ArgumentParser:
68
+ parser = argparse.ArgumentParser(
69
+ prog="protonlite-mcp",
70
+ description="Run the Proton MCP server over stdio, or manage its send policy.",
71
+ )
72
+ sub = parser.add_subparsers(dest="command")
73
+
74
+ serve = sub.add_parser("serve", help="speak MCP over stdio (the default with no command)")
75
+ serve.set_defaults(func=_cmd_serve)
76
+
77
+ policy_p = sub.add_parser("policy", help="manage the send policy (allowlist and confirmation)")
78
+ psub = policy_p.add_subparsers(dest="policy_command", required=True)
79
+
80
+ p_show = psub.add_parser("show", help="print the current send policy")
81
+ p_show.set_defaults(func=_cmd_policy_show)
82
+
83
+ p_allow = psub.add_parser(
84
+ "allow",
85
+ help="set the recipient allowlist (addresses or globs; '*' opens it, none blocks all)",
86
+ )
87
+ p_allow.add_argument("recipients", nargs="*", help="addresses or globs; omit to block all sends")
88
+ p_allow.set_defaults(func=_cmd_policy_allow)
89
+
90
+ p_ceiling = psub.add_parser(
91
+ "ceiling",
92
+ help="set a HARD recipient ceiling the agent cannot loosen (--clear removes it)",
93
+ )
94
+ p_ceiling.add_argument("recipients", nargs="*", help="addresses or globs the agent may never exceed")
95
+ p_ceiling.add_argument("--clear", action="store_true", help="remove the hard ceiling")
96
+ p_ceiling.set_defaults(func=_cmd_policy_ceiling)
97
+
98
+ p_rc = psub.add_parser(
99
+ "require-confirm",
100
+ help="on: the agent can no longer send (a person sends with `protonlite send`); off: lift it",
101
+ )
102
+ p_rc.add_argument("state", choices=["on", "off"])
103
+ p_rc.set_defaults(func=_cmd_policy_require_confirm)
104
+
105
+ return parser
106
+
107
+
108
+ def main(argv: list[str] | None = None) -> int:
109
+ args = list(sys.argv[1:] if argv is None else argv)
110
+ # An MCP client starts the bare command and expects stdio, so no arguments
111
+ # means serve. Anything else goes through the parser, which keeps --help.
112
+ if not args:
113
+ return _cmd_serve()
114
+ parsed = build_parser().parse_args(args)
115
+ if not hasattr(parsed, "func"):
116
+ return _cmd_serve()
117
+ try:
118
+ return int(parsed.func(parsed))
119
+ except KeyboardInterrupt:
120
+ return 130
121
+
122
+
123
+ if __name__ == "__main__":
124
+ raise SystemExit(main())
@@ -0,0 +1,154 @@
1
+ """The send policy the MCP server enforces: a recipient allowlist and a
2
+ "confirm before sending" rule.
3
+
4
+ The policy is kept in a small JSON file so it survives across runs. Two
5
+ environment variables act as a hard ceiling a human sets out of band, which the
6
+ tools cannot loosen:
7
+
8
+ - ``PROTON_ALLOWED_RECIPIENTS`` -- comma-separated addresses or globs. When set,
9
+ a send is refused unless every recipient matches, no matter what the mutable
10
+ allowlist says, and the allowlist tool cannot open sending to everyone.
11
+ - ``PROTON_REQUIRE_SEND_CONFIRMATION`` -- when truthy, confirmation is forced on
12
+ and the tool cannot turn it off.
13
+
14
+ Without those env vars the tools are freely mutable: convenient, but an agent
15
+ can change them, so they are advisory rather than a hard boundary. The env vars
16
+ are what make the boundary hard.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import fnmatch
22
+ import json
23
+ import os
24
+ from pathlib import Path
25
+
26
+ _DEFAULT_PATH = Path.home() / ".config" / "protonlite" / "mcp_policy.json"
27
+ _TRUTHY = {"1", "true", "yes", "on"}
28
+ _UNSET = object()
29
+
30
+
31
+ def _path() -> Path:
32
+ return Path(os.environ.get("PROTONLITE_MCP_POLICY") or _DEFAULT_PATH)
33
+
34
+
35
+ def env_ceiling() -> list[str] | None:
36
+ """The hard recipient ceiling from the env, or None when unset."""
37
+ raw = os.environ.get("PROTON_ALLOWED_RECIPIENTS", "").strip()
38
+ if not raw:
39
+ return None
40
+ return [p.strip() for p in raw.split(",") if p.strip()]
41
+
42
+
43
+ def env_forces_confirmation() -> bool:
44
+ return os.environ.get("PROTON_REQUIRE_SEND_CONFIRMATION", "").strip().lower() in _TRUTHY
45
+
46
+
47
+ def load() -> dict:
48
+ """Load the policy, filling defaults from the env on first use."""
49
+ path = _path()
50
+ data: dict = {}
51
+ if path.exists():
52
+ try:
53
+ data = json.loads(path.read_text())
54
+ except (ValueError, OSError):
55
+ data = {}
56
+ if "allowed" not in data:
57
+ ceiling = env_ceiling()
58
+ # Default to open so send keeps working when no policy is set; the env
59
+ # ceiling, if any, still applies on top at send time.
60
+ data["allowed"] = list(ceiling) if ceiling is not None else ["*"]
61
+ if "require_confirmation" not in data:
62
+ data["require_confirmation"] = env_forces_confirmation()
63
+ return data
64
+
65
+
66
+ def save(data: dict) -> None:
67
+ path = _path()
68
+ path.parent.mkdir(parents=True, exist_ok=True)
69
+ path.write_text(json.dumps(data, indent=2))
70
+
71
+
72
+ def _matches(addr: str, patterns: list[str]) -> bool:
73
+ a = addr.strip().lower()
74
+ return any(fnmatch.fnmatch(a, pat.strip().lower()) for pat in patterns if pat.strip())
75
+
76
+
77
+ def is_open(allowed: list[str]) -> bool:
78
+ return "*" in [p.strip() for p in allowed]
79
+
80
+
81
+ def locked_ceiling(data: dict) -> list[str] | None:
82
+ """The hard recipient ceiling set from the CLI (the `locked` section), or None."""
83
+ ceiling = (data.get("locked") or {}).get("ceiling")
84
+ return ceiling or None
85
+
86
+
87
+ def locked_requires_confirmation(data: dict) -> bool:
88
+ return bool((data.get("locked") or {}).get("require_confirmation"))
89
+
90
+
91
+ def classify_blocked(recipients: list[str], data: dict) -> tuple[list[str], list[str]]:
92
+ """Split the blocked recipients by why they are blocked: those outside a hard
93
+ ceiling (the env var or the CLI-set `locked` ceiling, which no tool can lift)
94
+ and those only off the mutable allowlist (which `email_allow` can permit)."""
95
+ env_c = env_ceiling()
96
+ lock_c = locked_ceiling(data)
97
+ allowed = data.get("allowed", ["*"])
98
+ ceiling_blocked, allow_blocked = [], []
99
+ for r in recipients:
100
+ if not r.strip():
101
+ continue
102
+ if env_c is not None and not _matches(r, env_c):
103
+ ceiling_blocked.append(r)
104
+ elif lock_c is not None and not _matches(r, lock_c):
105
+ ceiling_blocked.append(r)
106
+ elif not _matches(r, allowed):
107
+ allow_blocked.append(r)
108
+ return ceiling_blocked, allow_blocked
109
+
110
+
111
+ def disallowed(recipients: list[str], data: dict) -> list[str]:
112
+ """Every recipient that may NOT be emailed (ceiling or allowlist), as one list."""
113
+ ceiling_blocked, allow_blocked = classify_blocked(recipients, data)
114
+ return ceiling_blocked + allow_blocked
115
+
116
+
117
+ def soft_confirmation(data: dict) -> bool:
118
+ """The mutable confirm rule an agent can toggle (two-step send)."""
119
+ return bool(data.get("require_confirmation"))
120
+
121
+
122
+ def hard_confirmation(data: dict) -> bool:
123
+ """Confirmation enforced out of band (env var or the CLI-set `locked`
124
+ section). When this is on, the agent cannot send at all: a person sends from
125
+ the CLI. An agent cannot bypass it with the `confirm` argument."""
126
+ return env_forces_confirmation() or locked_requires_confirmation(data)
127
+
128
+
129
+ # -- setters the CLI uses to write the human-only parts of the policy ----------
130
+
131
+
132
+ def set_allowed(recipients: list[str]) -> dict:
133
+ data = load()
134
+ data["allowed"] = [r.strip() for r in recipients if r.strip()]
135
+ save(data)
136
+ return data
137
+
138
+
139
+ def set_soft_confirmation(required: bool) -> dict:
140
+ data = load()
141
+ data["require_confirmation"] = bool(required)
142
+ save(data)
143
+ return data
144
+
145
+
146
+ def set_locked(*, ceiling=_UNSET, require_confirmation=_UNSET) -> dict:
147
+ data = load()
148
+ locked = data.setdefault("locked", {})
149
+ if ceiling is not _UNSET:
150
+ locked["ceiling"] = [c.strip() for c in ceiling if c.strip()] if ceiling else None
151
+ if require_confirmation is not _UNSET:
152
+ locked["require_confirmation"] = bool(require_confirmation)
153
+ save(data)
154
+ return data