sieve-mcp-server 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.
- sieve_mcp_server-0.1.0/LICENSE +21 -0
- sieve_mcp_server-0.1.0/PKG-INFO +113 -0
- sieve_mcp_server-0.1.0/README.md +85 -0
- sieve_mcp_server-0.1.0/pyproject.toml +47 -0
- sieve_mcp_server-0.1.0/setup.cfg +4 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/__init__.py +4 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/__main__.py +6 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/cli.py +76 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/errors.py +43 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/isolation.py +110 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/paths.py +230 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/printconfig.py +51 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/protocol.py +353 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/selfcheck.py +307 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server/tools.py +481 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/PKG-INFO +113 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/SOURCES.txt +30 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/dependency_links.txt +1 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/entry_points.txt +2 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/requires.txt +5 -0
- sieve_mcp_server-0.1.0/sieve_mcp_server.egg-info/top_level.txt +1 -0
- sieve_mcp_server-0.1.0/tests/test_cli.py +72 -0
- sieve_mcp_server-0.1.0/tests/test_defaults.py +86 -0
- sieve_mcp_server-0.1.0/tests/test_determinism.py +124 -0
- sieve_mcp_server-0.1.0/tests/test_immutability.py +59 -0
- sieve_mcp_server-0.1.0/tests/test_package.py +163 -0
- sieve_mcp_server-0.1.0/tests/test_paths.py +120 -0
- sieve_mcp_server-0.1.0/tests/test_protocol.py +272 -0
- sieve_mcp_server-0.1.0/tests/test_public_text.py +173 -0
- sieve_mcp_server-0.1.0/tests/test_read_handoff.py +85 -0
- sieve_mcp_server-0.1.0/tests/test_responses.py +142 -0
- sieve_mcp_server-0.1.0/tests/test_secrecy.py +104 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kai IWASAKI
|
|
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,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sieve-mcp-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Sieve-MCP: the MCP server that hands local files to AI tools in a shareable form, over the sieve-tray engines
|
|
5
|
+
Author: Kai IWASAKI
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/neguseatama/sieve-mcp
|
|
8
|
+
Project-URL: Repository, https://github.com/neguseatama/sieve-mcp.git
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/neguseatama/sieve-mcp/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Security
|
|
19
|
+
Classifier: Topic :: Text Processing :: Filters
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: sieve-tray>=2.6.0
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
26
|
+
Requires-Dist: mcp>=2.3.0; extra == "test"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
**English** | [日本語](README.ja.md)
|
|
30
|
+
|
|
31
|
+
# Sieve-MCP v0.1.0 (sieve-mcp-server)
|
|
32
|
+
|
|
33
|
+
**The MCP server that hands local files to AI tools in a form ready to share**
|
|
34
|
+
|
|
35
|
+
Sieve-MCP is a small Model Context Protocol server for Claude Desktop,
|
|
36
|
+
Claude Code, and any other MCP client. It works as a lightweight wrapper
|
|
37
|
+
with the [Sieve Tray](https://github.com/neguseatama/sieve-tray) engines
|
|
38
|
+
built in: when the AI specifies a file or folder inside the folders you
|
|
39
|
+
allow, the reply includes what the material looks like and where the
|
|
40
|
+
shareable copy went — counts, status words, checksums, digests, and
|
|
41
|
+
relative paths. The package installs as `sieve-mcp-server`; the product
|
|
42
|
+
name is Sieve-MCP.
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
pip install sieve-mcp-server
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The only runtime dependency is `sieve-tray>=2.6.0` (installed
|
|
49
|
+
automatically, no extra needed). Requires Python >= 3.10.
|
|
50
|
+
|
|
51
|
+
## Configure
|
|
52
|
+
|
|
53
|
+
### Claude Desktop
|
|
54
|
+
|
|
55
|
+
Edit `claude_desktop_config.json` (the standard places: macOS
|
|
56
|
+
`~/Library/Application Support/Claude/`, Windows `%APPDATA%\Claude\`)
|
|
57
|
+
and add:
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"mcpServers": {
|
|
62
|
+
"sieve-mcp": {
|
|
63
|
+
"command": "sieve-mcp-server",
|
|
64
|
+
"args": ["--root", "/path/to/your/folder",
|
|
65
|
+
"--out", "/path/to/output"]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Claude Code
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
claude mcp add sieve-mcp -- sieve-mcp-server --root /path/to/your/folder --out /path/to/output
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`--root` names the folders the server may read (one or more); `--out`
|
|
78
|
+
names the folder every run writes into; the two must not contain each
|
|
79
|
+
other. `sieve-mcp-server --print-config --root ... --out ...` prints
|
|
80
|
+
this configuration from your own values, and
|
|
81
|
+
`sieve-mcp-server --self-check` runs the whole flow in one process and
|
|
82
|
+
exits 0 when everything matches.
|
|
83
|
+
|
|
84
|
+
## The tools
|
|
85
|
+
|
|
86
|
+
| Tool | What it does | What the reply carries |
|
|
87
|
+
| --- | --- | --- |
|
|
88
|
+
| `prepare_for_sharing` | Strips hidden characters and masks sensitive values into a fresh output folder (`handoff/` + `local/`) | Per-item status words, hidden-character kind counts, handoff file names and sizes, the record digest |
|
|
89
|
+
| `read_handoff` | Reads a UTF-8 text from a previous output's `handoff/` in slices | The text slice, the total length, the continuation offset |
|
|
90
|
+
| `observe_hidden_text` | Observes a file or a folder for hidden text, writing nothing | The check shape (`check_version: 1`): per-file status words and kind counts |
|
|
91
|
+
| `verify_record` | Re-checks a run record against a folder | `match` / `differs` / `not_verifiable` with the closed reason words |
|
|
92
|
+
| `remove_metadata` | Copies a JPEG, PNG, Office or PDF file with its metadata stripped | The eight metadata categories before and after, the re-observation, the output path |
|
|
93
|
+
| `create_file_list` | Lists a tree with per-file hashes and the whole-tree checksum | The file count, the overall checksum, the record path |
|
|
94
|
+
|
|
95
|
+
## How the answers stay safe
|
|
96
|
+
|
|
97
|
+
Every reply carries counts, status words, checksums and relative paths
|
|
98
|
+
only - the hidden text, the metadata values, the layer names and the
|
|
99
|
+
absolute paths never enter a reply, and everything the engines print
|
|
100
|
+
during a run is captured and dropped, so the protocol stream carries
|
|
101
|
+
the JSON messages alone. The server reads only inside the `--root`
|
|
102
|
+
folders (no `..`, no symbolic links, no special files), writes only
|
|
103
|
+
under `--out` into numbered folders that are never overwritten, leaves
|
|
104
|
+
the originals untouched, and skips the `.obsidian/` and `.trash/`
|
|
105
|
+
folders of a folder target by default.
|
|
106
|
+
|
|
107
|
+
## Development
|
|
108
|
+
|
|
109
|
+
- Test suite: `python -m pytest -v` (the protocol tests also ride the
|
|
110
|
+
official SDK; `pip install ".[test]"`).
|
|
111
|
+
- Public-text watch: `python tools/check_public_text.py`.
|
|
112
|
+
- The docs: `docs/tools.md` (the tool shapes) and `docs/protocol.md`
|
|
113
|
+
(the protocol versions and their sources).
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
**English** | [日本語](README.ja.md)
|
|
2
|
+
|
|
3
|
+
# Sieve-MCP v0.1.0 (sieve-mcp-server)
|
|
4
|
+
|
|
5
|
+
**The MCP server that hands local files to AI tools in a form ready to share**
|
|
6
|
+
|
|
7
|
+
Sieve-MCP is a small Model Context Protocol server for Claude Desktop,
|
|
8
|
+
Claude Code, and any other MCP client. It works as a lightweight wrapper
|
|
9
|
+
with the [Sieve Tray](https://github.com/neguseatama/sieve-tray) engines
|
|
10
|
+
built in: when the AI specifies a file or folder inside the folders you
|
|
11
|
+
allow, the reply includes what the material looks like and where the
|
|
12
|
+
shareable copy went — counts, status words, checksums, digests, and
|
|
13
|
+
relative paths. The package installs as `sieve-mcp-server`; the product
|
|
14
|
+
name is Sieve-MCP.
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
pip install sieve-mcp-server
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The only runtime dependency is `sieve-tray>=2.6.0` (installed
|
|
21
|
+
automatically, no extra needed). Requires Python >= 3.10.
|
|
22
|
+
|
|
23
|
+
## Configure
|
|
24
|
+
|
|
25
|
+
### Claude Desktop
|
|
26
|
+
|
|
27
|
+
Edit `claude_desktop_config.json` (the standard places: macOS
|
|
28
|
+
`~/Library/Application Support/Claude/`, Windows `%APPDATA%\Claude\`)
|
|
29
|
+
and add:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"mcpServers": {
|
|
34
|
+
"sieve-mcp": {
|
|
35
|
+
"command": "sieve-mcp-server",
|
|
36
|
+
"args": ["--root", "/path/to/your/folder",
|
|
37
|
+
"--out", "/path/to/output"]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Claude Code
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
claude mcp add sieve-mcp -- sieve-mcp-server --root /path/to/your/folder --out /path/to/output
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`--root` names the folders the server may read (one or more); `--out`
|
|
50
|
+
names the folder every run writes into; the two must not contain each
|
|
51
|
+
other. `sieve-mcp-server --print-config --root ... --out ...` prints
|
|
52
|
+
this configuration from your own values, and
|
|
53
|
+
`sieve-mcp-server --self-check` runs the whole flow in one process and
|
|
54
|
+
exits 0 when everything matches.
|
|
55
|
+
|
|
56
|
+
## The tools
|
|
57
|
+
|
|
58
|
+
| Tool | What it does | What the reply carries |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `prepare_for_sharing` | Strips hidden characters and masks sensitive values into a fresh output folder (`handoff/` + `local/`) | Per-item status words, hidden-character kind counts, handoff file names and sizes, the record digest |
|
|
61
|
+
| `read_handoff` | Reads a UTF-8 text from a previous output's `handoff/` in slices | The text slice, the total length, the continuation offset |
|
|
62
|
+
| `observe_hidden_text` | Observes a file or a folder for hidden text, writing nothing | The check shape (`check_version: 1`): per-file status words and kind counts |
|
|
63
|
+
| `verify_record` | Re-checks a run record against a folder | `match` / `differs` / `not_verifiable` with the closed reason words |
|
|
64
|
+
| `remove_metadata` | Copies a JPEG, PNG, Office or PDF file with its metadata stripped | The eight metadata categories before and after, the re-observation, the output path |
|
|
65
|
+
| `create_file_list` | Lists a tree with per-file hashes and the whole-tree checksum | The file count, the overall checksum, the record path |
|
|
66
|
+
|
|
67
|
+
## How the answers stay safe
|
|
68
|
+
|
|
69
|
+
Every reply carries counts, status words, checksums and relative paths
|
|
70
|
+
only - the hidden text, the metadata values, the layer names and the
|
|
71
|
+
absolute paths never enter a reply, and everything the engines print
|
|
72
|
+
during a run is captured and dropped, so the protocol stream carries
|
|
73
|
+
the JSON messages alone. The server reads only inside the `--root`
|
|
74
|
+
folders (no `..`, no symbolic links, no special files), writes only
|
|
75
|
+
under `--out` into numbered folders that are never overwritten, leaves
|
|
76
|
+
the originals untouched, and skips the `.obsidian/` and `.trash/`
|
|
77
|
+
folders of a folder target by default.
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
- Test suite: `python -m pytest -v` (the protocol tests also ride the
|
|
82
|
+
official SDK; `pip install ".[test]"`).
|
|
83
|
+
- Public-text watch: `python tools/check_public_text.py`.
|
|
84
|
+
- The docs: `docs/tools.md` (the tool shapes) and `docs/protocol.md`
|
|
85
|
+
(the protocol versions and their sources).
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sieve-mcp-server"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Sieve-MCP: the MCP server that hands local files to AI tools in a shareable form, over the sieve-tray engines"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Kai IWASAKI" }]
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
# the only runtime dependency: the Tray engines carry the observation,
|
|
14
|
+
# the Gate, the metadata clean, the manifest and the verify paths, and
|
|
15
|
+
# they run headless without any extra (the gui extra stays out)
|
|
16
|
+
dependencies = ["sieve-tray>=2.6.0"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Intended Audience :: Education",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Security",
|
|
27
|
+
"Topic :: Text Processing :: Filters",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
# the official protocol SDK rides the tests only - the server itself
|
|
32
|
+
# speaks the line-delimited JSON protocol with the standard library
|
|
33
|
+
test = ["pytest>=8.0", "mcp>=2.3.0"]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/neguseatama/sieve-mcp"
|
|
37
|
+
Repository = "https://github.com/neguseatama/sieve-mcp.git"
|
|
38
|
+
"Bug Tracker" = "https://github.com/neguseatama/sieve-mcp/issues"
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
sieve-mcp-server = "sieve_mcp_server.cli:main"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools]
|
|
44
|
+
packages = ["sieve_mcp_server"]
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.dynamic]
|
|
47
|
+
version = { attr = "sieve_mcp_server.__version__" }
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""The command line: the roots and the output folder, the limits, and
|
|
2
|
+
the two auxiliary commands (``--self-check`` and ``--print-config``)."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from . import __version__
|
|
8
|
+
from .paths import Spaces
|
|
9
|
+
from .protocol import serve
|
|
10
|
+
from .errors import StartupError
|
|
11
|
+
|
|
12
|
+
MAX_READ_CHARS_DEFAULT = 8000
|
|
13
|
+
MAX_READ_CHARS_CAP = 20000
|
|
14
|
+
MAX_FILE_BYTES_DEFAULT = 5 * 1024 * 1024
|
|
15
|
+
MAX_FILES_DEFAULT = 512
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
19
|
+
parser = argparse.ArgumentParser(
|
|
20
|
+
prog="sieve-mcp-server",
|
|
21
|
+
description="Sieve-MCP: hand local files to AI tools in a "
|
|
22
|
+
"shareable form over the sieve-tray engines.")
|
|
23
|
+
parser.add_argument("--root", action="append", default=[],
|
|
24
|
+
metavar="DIR",
|
|
25
|
+
help="an allowed data folder (one or more)")
|
|
26
|
+
parser.add_argument("--out", metavar="DIR",
|
|
27
|
+
help="the output folder (required)")
|
|
28
|
+
parser.add_argument("--max-read-chars", type=int,
|
|
29
|
+
default=MAX_READ_CHARS_DEFAULT, metavar="N",
|
|
30
|
+
help="the read_handoff slice cap "
|
|
31
|
+
"(default 8000, cap 20000)")
|
|
32
|
+
parser.add_argument("--max-file-bytes", type=int,
|
|
33
|
+
default=MAX_FILE_BYTES_DEFAULT, metavar="N",
|
|
34
|
+
help="the per-file byte cap (default 5 MiB)")
|
|
35
|
+
parser.add_argument("--max-files", type=int, default=MAX_FILES_DEFAULT,
|
|
36
|
+
metavar="N",
|
|
37
|
+
help="the per-run file count cap (default 512)")
|
|
38
|
+
parser.add_argument("--self-check", action="store_true",
|
|
39
|
+
help="run the in-process check (exit 0 / 1 / 2)")
|
|
40
|
+
parser.add_argument("--print-config", action="store_true",
|
|
41
|
+
help="print the client configuration from the "
|
|
42
|
+
"given --root / --out values")
|
|
43
|
+
return parser
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def main(argv=None) -> int:
|
|
47
|
+
parser = build_parser()
|
|
48
|
+
args = parser.parse_args(argv)
|
|
49
|
+
if args.self_check:
|
|
50
|
+
from .selfcheck import run_self_check
|
|
51
|
+
return run_self_check()
|
|
52
|
+
if args.print_config:
|
|
53
|
+
from .printconfig import print_config
|
|
54
|
+
if not args.root or not args.out:
|
|
55
|
+
parser.print_usage(sys.stderr)
|
|
56
|
+
return 2
|
|
57
|
+
return print_config(args.root, args.out)
|
|
58
|
+
if not args.root or not args.out:
|
|
59
|
+
parser.print_usage(sys.stderr)
|
|
60
|
+
return 2
|
|
61
|
+
try:
|
|
62
|
+
spaces = Spaces(args.root, args.out,
|
|
63
|
+
max_read_chars=args.max_read_chars,
|
|
64
|
+
max_file_bytes=args.max_file_bytes,
|
|
65
|
+
max_files=args.max_files)
|
|
66
|
+
except StartupError:
|
|
67
|
+
print("sieve-mcp-server: the startup options are invalid - the "
|
|
68
|
+
"roots and the output folder must be existing folders and "
|
|
69
|
+
"must not contain each other.", file=sys.stderr)
|
|
70
|
+
return 2
|
|
71
|
+
return serve(spaces)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
__all__ = ["main", "build_parser", "MAX_READ_CHARS_CAP",
|
|
75
|
+
"MAX_READ_CHARS_DEFAULT", "MAX_FILE_BYTES_DEFAULT",
|
|
76
|
+
"MAX_FILES_DEFAULT", "__version__"]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""The closed eight-word error vocabulary.
|
|
2
|
+
|
|
3
|
+
Every tool failure carries one word from ``CODES`` and one fixed short
|
|
4
|
+
sentence. The sentence never embeds an exception text, a path or a
|
|
5
|
+
file name - the raw exception stays inside the server.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
CODES = (
|
|
9
|
+
"path_not_allowed",
|
|
10
|
+
"not_found",
|
|
11
|
+
"not_a_regular_file",
|
|
12
|
+
"too_large",
|
|
13
|
+
"unsupported_type",
|
|
14
|
+
"invalid_arguments",
|
|
15
|
+
"run_failed",
|
|
16
|
+
"read_not_allowed",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
MESSAGES = {
|
|
20
|
+
"path_not_allowed": "The path is not allowed.",
|
|
21
|
+
"not_found": "The path does not exist.",
|
|
22
|
+
"not_a_regular_file": "The path is not a regular file.",
|
|
23
|
+
"too_large": "The file or the folder exceeds the size limits.",
|
|
24
|
+
"unsupported_type": "The file type is not supported.",
|
|
25
|
+
"invalid_arguments": "The tool arguments are invalid.",
|
|
26
|
+
"run_failed": "The engine run failed.",
|
|
27
|
+
"read_not_allowed": "Reading this file is not allowed.",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ToolError(Exception):
|
|
32
|
+
"""One tool failure with a closed word and a fixed sentence."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, code: str, message: str | None = None):
|
|
35
|
+
if code not in CODES:
|
|
36
|
+
code = "run_failed"
|
|
37
|
+
self.code = code
|
|
38
|
+
self.message = message or MESSAGES[code]
|
|
39
|
+
super().__init__(self.message)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class StartupError(Exception):
|
|
43
|
+
"""A startup violation (exit 2) with a fixed short sentence."""
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""The standard-output isolation around the engine runs.
|
|
2
|
+
|
|
3
|
+
From startup the protocol owns two things: a reserved duplicate of the
|
|
4
|
+
standard output's file descriptor (the only channel that ever carries a
|
|
5
|
+
protocol message) and the byte loop on the standard input. While an
|
|
6
|
+
engine runs, both the Python-level ``sys.stdout`` / ``sys.stderr`` and
|
|
7
|
+
the OS-level fd 1 / fd 2 are swapped away, so anything the engine
|
|
8
|
+
prints - through ``print()``, through ``sys.stdout.buffer``, or through
|
|
9
|
+
a raw ``os.write`` - lands in memory or in a scratch file and is never
|
|
10
|
+
emitted. The protocol channel keeps writing to its reserved descriptor
|
|
11
|
+
the whole time. Both layers are restored when the run ends.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import io
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import tempfile
|
|
18
|
+
from contextlib import contextmanager
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _BytesSink:
|
|
22
|
+
"""The buffer-level sink: accepts bytes, keeps them in memory."""
|
|
23
|
+
|
|
24
|
+
def __init__(self):
|
|
25
|
+
self._buf = bytearray()
|
|
26
|
+
|
|
27
|
+
def write(self, data) -> int:
|
|
28
|
+
if isinstance(data, str):
|
|
29
|
+
data = data.encode("utf-8", "replace")
|
|
30
|
+
self._buf += bytes(data)
|
|
31
|
+
return len(data)
|
|
32
|
+
|
|
33
|
+
def flush(self) -> None:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
def writable(self) -> bool:
|
|
37
|
+
return True
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _TextSink(io.TextIOBase):
|
|
41
|
+
"""The text-level sink: looks like a text stream (encoding, flush,
|
|
42
|
+
``reconfigure`` - the engines call it), writes into the byte sink."""
|
|
43
|
+
|
|
44
|
+
def __init__(self):
|
|
45
|
+
self._buffer = _BytesSink()
|
|
46
|
+
self._errors = "replace"
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def buffer(self):
|
|
50
|
+
return self._buffer
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def encoding(self):
|
|
54
|
+
return "utf-8"
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def errors(self):
|
|
58
|
+
return self._errors
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def name(self):
|
|
62
|
+
return "<sieve-mcp-engine-sink>"
|
|
63
|
+
|
|
64
|
+
def write(self, s) -> int:
|
|
65
|
+
return self._buffer.write(s)
|
|
66
|
+
|
|
67
|
+
def writelines(self, lines) -> None:
|
|
68
|
+
for line in lines:
|
|
69
|
+
self.write(line)
|
|
70
|
+
|
|
71
|
+
def flush(self) -> None:
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
def reconfigure(self, **_kw) -> None: # noqa: ANN003 - the engine's call
|
|
75
|
+
# the UTF-8 re-set some engines run on the text streams: the
|
|
76
|
+
# sink is already UTF-8, so the call is accepted and dropped
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
def isatty(self) -> bool:
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
def writable(self) -> bool:
|
|
83
|
+
return True
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@contextmanager
|
|
87
|
+
def isolated_stdio():
|
|
88
|
+
"""Swap away both standard-output layers while one engine run
|
|
89
|
+
executes; restore them afterwards, whatever the run did."""
|
|
90
|
+
saved_out, saved_err = sys.stdout, sys.stderr
|
|
91
|
+
out_sink, err_sink = _TextSink(), _TextSink()
|
|
92
|
+
with tempfile.TemporaryFile() as scratch_out, \
|
|
93
|
+
tempfile.TemporaryFile() as scratch_err:
|
|
94
|
+
saved_fd1, saved_fd2 = os.dup(1), os.dup(2)
|
|
95
|
+
try:
|
|
96
|
+
sys.stdout, sys.stderr = out_sink, err_sink
|
|
97
|
+
os.dup2(scratch_out.fileno(), 1)
|
|
98
|
+
os.dup2(scratch_err.fileno(), 2)
|
|
99
|
+
yield out_sink, err_sink
|
|
100
|
+
finally:
|
|
101
|
+
try:
|
|
102
|
+
sys.stdout.flush()
|
|
103
|
+
sys.stderr.flush()
|
|
104
|
+
except Exception: # noqa: BLE001 - the restore must run
|
|
105
|
+
pass
|
|
106
|
+
os.dup2(saved_fd1, 1)
|
|
107
|
+
os.dup2(saved_fd2, 2)
|
|
108
|
+
os.close(saved_fd1)
|
|
109
|
+
os.close(saved_fd2)
|
|
110
|
+
sys.stdout, sys.stderr = saved_out, saved_err
|