reef-cli 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.
- reef_cli-0.1.0/PKG-INFO +24 -0
- reef_cli-0.1.0/README.md +10 -0
- reef_cli-0.1.0/pyproject.toml +25 -0
- reef_cli-0.1.0/src/reef_cli/__init__.py +1 -0
- reef_cli-0.1.0/src/reef_cli/cli.py +545 -0
reef_cli-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reef-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Command-line client for reef — shared, living memory for people and their AI assistants
|
|
5
|
+
Keywords: mcp,memory,cli,reef
|
|
6
|
+
Author: WOUTER DURNEZ
|
|
7
|
+
Author-email: WOUTER DURNEZ <wouter.durnez@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Requires-Dist: fastmcp>=2
|
|
10
|
+
Requires-Python: >=3.13
|
|
11
|
+
Project-URL: Homepage, https://reefwith.me
|
|
12
|
+
Project-URL: Repository, https://github.com/diepzee/rif
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# reef-cli
|
|
16
|
+
|
|
17
|
+
Command-line client for [reef](https://reefwith.me) — shared, living memory
|
|
18
|
+
for people and their AI assistants, reached over MCP.
|
|
19
|
+
|
|
20
|
+
uv tool install reef-cli
|
|
21
|
+
reef login
|
|
22
|
+
|
|
23
|
+
Every MCP tool is mirrored as a subcommand; `reef call <tool> '<json>'` is the
|
|
24
|
+
exact passthrough. JSON on stdout, always.
|
reef_cli-0.1.0/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# reef-cli
|
|
2
|
+
|
|
3
|
+
Command-line client for [reef](https://reefwith.me) — shared, living memory
|
|
4
|
+
for people and their AI assistants, reached over MCP.
|
|
5
|
+
|
|
6
|
+
uv tool install reef-cli
|
|
7
|
+
reef login
|
|
8
|
+
|
|
9
|
+
Every MCP tool is mirrored as a subcommand; `reef call <tool> '<json>'` is the
|
|
10
|
+
exact passthrough. JSON on stdout, always.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "reef-cli"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Command-line client for reef — shared, living memory for people and their AI assistants"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "WOUTER DURNEZ", email = "wouter.durnez@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
requires-python = ">=3.13"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"fastmcp>=2",
|
|
13
|
+
]
|
|
14
|
+
keywords = ["mcp", "memory", "cli", "reef"]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://reefwith.me"
|
|
18
|
+
Repository = "https://github.com/diepzee/rif"
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
reef = "reef_cli.cli:main"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["uv_build>=0.11.14,<0.12.0"]
|
|
25
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""reef-cli — command-line client for Reef's remote MCP server."""
|
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
"""Command-line client for Reef's remote MCP server.
|
|
2
|
+
|
|
3
|
+
The named commands are deliberately thin adapters over MCP tool calls. The
|
|
4
|
+
``call`` command is the escape hatch which preserves the exact MCP surface for
|
|
5
|
+
new tools and unusual argument shapes without waiting for a CLI release.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import asyncio
|
|
12
|
+
import base64
|
|
13
|
+
import json
|
|
14
|
+
import mimetypes
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
from collections.abc import Mapping, Sequence
|
|
19
|
+
from dataclasses import asdict, is_dataclass
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Any, SupportsFloat
|
|
22
|
+
|
|
23
|
+
from fastmcp import Client
|
|
24
|
+
from fastmcp.client.auth import OAuth
|
|
25
|
+
|
|
26
|
+
DEFAULT_MCP_URL = "https://reefwith.me/mcp"
|
|
27
|
+
DEFAULT_CONFIG_DIR = Path.home() / ".config" / "reef"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class CLIError(Exception):
|
|
31
|
+
"""A user-facing command-line error."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class JsonTokenStore:
|
|
35
|
+
"""Small persistent ``AsyncKeyValue`` store for FastMCP OAuth state.
|
|
36
|
+
|
|
37
|
+
FastMCP accepts any implementation of its async key-value protocol. A
|
|
38
|
+
single JSON file keeps the CLI dependency-light and portable. The parent
|
|
39
|
+
directory and file are user-only on POSIX systems, and writes replace the
|
|
40
|
+
file atomically so an interrupted login cannot leave half-written JSON.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, path: Path) -> None:
|
|
44
|
+
self.path = path
|
|
45
|
+
|
|
46
|
+
def _read(self) -> dict[str, dict[str, Any]]:
|
|
47
|
+
try:
|
|
48
|
+
raw = json.loads(self.path.read_text(encoding="utf-8"))
|
|
49
|
+
except FileNotFoundError:
|
|
50
|
+
return {}
|
|
51
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
52
|
+
raise CLIError(f"could not read OAuth state at {self.path}: {exc}") from exc
|
|
53
|
+
if not isinstance(raw, dict):
|
|
54
|
+
raise CLIError(f"OAuth state at {self.path} is not a JSON object")
|
|
55
|
+
return raw
|
|
56
|
+
|
|
57
|
+
def _write(self, data: dict[str, dict[str, Any]]) -> None:
|
|
58
|
+
try:
|
|
59
|
+
self.path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
60
|
+
if os.name == "posix":
|
|
61
|
+
self.path.parent.chmod(0o700)
|
|
62
|
+
temporary = self.path.with_suffix(self.path.suffix + ".tmp")
|
|
63
|
+
temporary.write_text(
|
|
64
|
+
json.dumps(data, separators=(",", ":")), encoding="utf-8"
|
|
65
|
+
)
|
|
66
|
+
if os.name == "posix":
|
|
67
|
+
temporary.chmod(0o600)
|
|
68
|
+
temporary.replace(self.path)
|
|
69
|
+
except OSError as exc:
|
|
70
|
+
raise CLIError(
|
|
71
|
+
f"could not write OAuth state at {self.path}: {exc}"
|
|
72
|
+
) from exc
|
|
73
|
+
|
|
74
|
+
@staticmethod
|
|
75
|
+
def _entry_key(key: str, collection: str | None) -> str:
|
|
76
|
+
return f"{collection or 'default'}\0{key}"
|
|
77
|
+
|
|
78
|
+
async def get(
|
|
79
|
+
self, key: str, *, collection: str | None = None
|
|
80
|
+
) -> dict[str, Any] | None:
|
|
81
|
+
data = self._read()
|
|
82
|
+
entry_key = self._entry_key(key, collection)
|
|
83
|
+
entry = data.get(entry_key)
|
|
84
|
+
if entry is None:
|
|
85
|
+
return None
|
|
86
|
+
expires_at = entry.get("expires_at")
|
|
87
|
+
if expires_at is not None and float(expires_at) <= time.time():
|
|
88
|
+
del data[entry_key]
|
|
89
|
+
self._write(data)
|
|
90
|
+
return None
|
|
91
|
+
value = entry.get("value")
|
|
92
|
+
return value if isinstance(value, dict) else None
|
|
93
|
+
|
|
94
|
+
async def ttl(
|
|
95
|
+
self, key: str, *, collection: str | None = None
|
|
96
|
+
) -> tuple[dict[str, Any] | None, float | None]:
|
|
97
|
+
value = await self.get(key, collection=collection)
|
|
98
|
+
if value is None:
|
|
99
|
+
return None, None
|
|
100
|
+
entry = self._read()[self._entry_key(key, collection)]
|
|
101
|
+
expires_at = entry.get("expires_at")
|
|
102
|
+
remaining = None if expires_at is None else max(0.0, expires_at - time.time())
|
|
103
|
+
return value, remaining
|
|
104
|
+
|
|
105
|
+
async def put(
|
|
106
|
+
self,
|
|
107
|
+
key: str,
|
|
108
|
+
value: Mapping[str, Any],
|
|
109
|
+
*,
|
|
110
|
+
collection: str | None = None,
|
|
111
|
+
ttl: SupportsFloat | None = None,
|
|
112
|
+
) -> None:
|
|
113
|
+
data = self._read()
|
|
114
|
+
data[self._entry_key(key, collection)] = {
|
|
115
|
+
"value": dict(value),
|
|
116
|
+
"expires_at": None if ttl is None else time.time() + float(ttl),
|
|
117
|
+
}
|
|
118
|
+
self._write(data)
|
|
119
|
+
|
|
120
|
+
async def delete(self, key: str, *, collection: str | None = None) -> bool:
|
|
121
|
+
data = self._read()
|
|
122
|
+
removed = data.pop(self._entry_key(key, collection), None) is not None
|
|
123
|
+
if removed:
|
|
124
|
+
self._write(data)
|
|
125
|
+
return removed
|
|
126
|
+
|
|
127
|
+
async def get_many(
|
|
128
|
+
self, keys: Sequence[str], *, collection: str | None = None
|
|
129
|
+
) -> list[dict[str, Any] | None]:
|
|
130
|
+
return [await self.get(key, collection=collection) for key in keys]
|
|
131
|
+
|
|
132
|
+
async def ttl_many(
|
|
133
|
+
self, keys: Sequence[str], *, collection: str | None = None
|
|
134
|
+
) -> list[tuple[dict[str, Any] | None, float | None]]:
|
|
135
|
+
return [await self.ttl(key, collection=collection) for key in keys]
|
|
136
|
+
|
|
137
|
+
async def put_many(
|
|
138
|
+
self,
|
|
139
|
+
keys: Sequence[str],
|
|
140
|
+
values: Sequence[Mapping[str, Any]],
|
|
141
|
+
*,
|
|
142
|
+
collection: str | None = None,
|
|
143
|
+
ttl: SupportsFloat | None = None,
|
|
144
|
+
) -> None:
|
|
145
|
+
if len(keys) != len(values):
|
|
146
|
+
raise ValueError("keys and values must have the same length")
|
|
147
|
+
for key, value in zip(keys, values, strict=True):
|
|
148
|
+
await self.put(key, value, collection=collection, ttl=ttl)
|
|
149
|
+
|
|
150
|
+
async def delete_many(
|
|
151
|
+
self, keys: Sequence[str], *, collection: str | None = None
|
|
152
|
+
) -> int:
|
|
153
|
+
removed = 0
|
|
154
|
+
for key in keys:
|
|
155
|
+
removed += await self.delete(key, collection=collection)
|
|
156
|
+
return removed
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _config_dir() -> Path:
|
|
160
|
+
override = os.environ.get("REEF_CONFIG_DIR")
|
|
161
|
+
if override:
|
|
162
|
+
return Path(override).expanduser()
|
|
163
|
+
xdg = os.environ.get("XDG_CONFIG_HOME")
|
|
164
|
+
if xdg:
|
|
165
|
+
return Path(xdg).expanduser() / "reef"
|
|
166
|
+
if os.name == "nt" and os.environ.get("APPDATA"):
|
|
167
|
+
return Path(os.environ["APPDATA"]) / "reef"
|
|
168
|
+
return DEFAULT_CONFIG_DIR
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _token_store() -> JsonTokenStore:
|
|
172
|
+
return JsonTokenStore(_config_dir() / "oauth.json")
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _text_source(value: str | None, file: str | None, label: str) -> str:
|
|
176
|
+
if value is not None:
|
|
177
|
+
return sys.stdin.read() if value == "-" else value
|
|
178
|
+
if file is None:
|
|
179
|
+
raise CLIError(f"one of --{label} or --{label}-file is required")
|
|
180
|
+
if file == "-":
|
|
181
|
+
return sys.stdin.read()
|
|
182
|
+
try:
|
|
183
|
+
return Path(file).read_text(encoding="utf-8")
|
|
184
|
+
except OSError as exc:
|
|
185
|
+
raise CLIError(f"could not read {label} file {file}: {exc}") from exc
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _json_source(source: str) -> Any:
|
|
189
|
+
if source == "-":
|
|
190
|
+
text = sys.stdin.read()
|
|
191
|
+
origin = "stdin"
|
|
192
|
+
elif source.startswith("@"):
|
|
193
|
+
origin = source[1:]
|
|
194
|
+
try:
|
|
195
|
+
text = Path(origin).read_text(encoding="utf-8")
|
|
196
|
+
except OSError as exc:
|
|
197
|
+
raise CLIError(f"could not read JSON file {origin}: {exc}") from exc
|
|
198
|
+
else:
|
|
199
|
+
text = source
|
|
200
|
+
origin = "argument"
|
|
201
|
+
try:
|
|
202
|
+
return json.loads(text)
|
|
203
|
+
except json.JSONDecodeError as exc:
|
|
204
|
+
raise CLIError(f"invalid JSON from {origin}: {exc}") from exc
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _add_text_source(parser: argparse.ArgumentParser, name: str) -> None:
|
|
208
|
+
group = parser.add_mutually_exclusive_group(required=True)
|
|
209
|
+
group.add_argument(
|
|
210
|
+
f"--{name}", help=f"{name.replace('-', ' ')} text; '-' reads stdin"
|
|
211
|
+
)
|
|
212
|
+
group.add_argument(
|
|
213
|
+
f"--{name}-file",
|
|
214
|
+
metavar="PATH",
|
|
215
|
+
help=f"read {name.replace('-', ' ')} from PATH",
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _tool_parser(
|
|
220
|
+
subparsers: argparse._SubParsersAction, name: str, help: str
|
|
221
|
+
) -> argparse.ArgumentParser:
|
|
222
|
+
parser = subparsers.add_parser(name.replace("_", "-"), help=help)
|
|
223
|
+
parser.set_defaults(tool=name)
|
|
224
|
+
return parser
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
228
|
+
"""Build the complete CLI parser."""
|
|
229
|
+
parser = argparse.ArgumentParser(
|
|
230
|
+
prog="reef", description="Read and write shared Reef memory over MCP."
|
|
231
|
+
)
|
|
232
|
+
parser.add_argument(
|
|
233
|
+
"--url",
|
|
234
|
+
default=os.environ.get("REEF_MCP_URL", DEFAULT_MCP_URL),
|
|
235
|
+
help="MCP endpoint (default: %(default)s; env: REEF_MCP_URL)",
|
|
236
|
+
)
|
|
237
|
+
parser.add_argument(
|
|
238
|
+
"--compact", action="store_true", help="emit compact rather than indented JSON"
|
|
239
|
+
)
|
|
240
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
241
|
+
|
|
242
|
+
sub.add_parser("login", help="sign in through MCP OAuth and cache the tokens")
|
|
243
|
+
sub.add_parser("logout", help="remove cached OAuth tokens for this endpoint")
|
|
244
|
+
sub.add_parser("tools", help="list the MCP server's current tool schemas")
|
|
245
|
+
call = sub.add_parser("call", help="call an exact MCP tool with a JSON object")
|
|
246
|
+
call.add_argument("tool_name")
|
|
247
|
+
call.add_argument(
|
|
248
|
+
"arguments",
|
|
249
|
+
nargs="?",
|
|
250
|
+
default="{}",
|
|
251
|
+
help="JSON object, @file, or '-' for stdin (default: {})",
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
_tool_parser(sub, "load_index", "load the body-free memory index")
|
|
255
|
+
read_pages = _tool_parser(sub, "read_pages", "read several pages from one space")
|
|
256
|
+
read_pages.add_argument("space")
|
|
257
|
+
read_pages.add_argument("paths", nargs="+")
|
|
258
|
+
_tool_parser(sub, "load_all_context", "load all page bodies for maintenance")
|
|
259
|
+
_tool_parser(sub, "get_operating_protocol", "load the protocol and persona")
|
|
260
|
+
read_page = _tool_parser(sub, "read_page", "read one page")
|
|
261
|
+
read_page.add_argument("space")
|
|
262
|
+
read_page.add_argument("path")
|
|
263
|
+
_tool_parser(sub, "list_spaces", "list spaces, members, ownership, and versions")
|
|
264
|
+
create_space = _tool_parser(sub, "create_space", "create a shared space")
|
|
265
|
+
create_space.add_argument("slug")
|
|
266
|
+
invite = _tool_parser(sub, "invite", "invite a person into a shared space")
|
|
267
|
+
invite.add_argument("space")
|
|
268
|
+
invite.add_argument("email")
|
|
269
|
+
invite.add_argument("--display-name")
|
|
270
|
+
invite_reef = _tool_parser(
|
|
271
|
+
sub, "invite_to_reef", "invite someone to Reef without sharing a space"
|
|
272
|
+
)
|
|
273
|
+
invite_reef.add_argument("email")
|
|
274
|
+
invite_reef.add_argument("--display-name")
|
|
275
|
+
remove = _tool_parser(sub, "remove_member", "remove a shared-space member")
|
|
276
|
+
remove.add_argument("space")
|
|
277
|
+
remove.add_argument("email")
|
|
278
|
+
leave = _tool_parser(sub, "leave_space", "leave a shared space, handing it on")
|
|
279
|
+
leave.add_argument("space")
|
|
280
|
+
delete = _tool_parser(
|
|
281
|
+
sub, "delete_space", "destroy a shared space you are alone in"
|
|
282
|
+
)
|
|
283
|
+
delete.add_argument("space")
|
|
284
|
+
remember = _tool_parser(sub, "remember", "append a fact to a space inbox")
|
|
285
|
+
remember.add_argument("fact", help="fact text; '-' reads stdin")
|
|
286
|
+
remember.add_argument("--space", default="personal")
|
|
287
|
+
|
|
288
|
+
write = _tool_parser(sub, "write_page", "create or replace one Markdown page")
|
|
289
|
+
write.add_argument("space")
|
|
290
|
+
write.add_argument("path")
|
|
291
|
+
_add_text_source(write, "body")
|
|
292
|
+
write.add_argument("--message", required=True)
|
|
293
|
+
write.add_argument("--title")
|
|
294
|
+
write.add_argument("--tag", dest="tags", action="append")
|
|
295
|
+
write.add_argument("--expected-version", type=int)
|
|
296
|
+
|
|
297
|
+
writes = _tool_parser(sub, "write_pages", "atomically write up to 20 pages")
|
|
298
|
+
writes.add_argument("space")
|
|
299
|
+
writes.add_argument("pages", help="JSON array, @file, or '-' for stdin")
|
|
300
|
+
writes.add_argument("--message", default="")
|
|
301
|
+
|
|
302
|
+
edit = _tool_parser(sub, "edit_page_section", "replace one exact page span")
|
|
303
|
+
edit.add_argument("space")
|
|
304
|
+
edit.add_argument("path")
|
|
305
|
+
_add_text_source(edit, "old-text")
|
|
306
|
+
_add_text_source(edit, "new-text")
|
|
307
|
+
edit.add_argument("--message", required=True)
|
|
308
|
+
edit.add_argument("--expected-version", type=int)
|
|
309
|
+
|
|
310
|
+
meta = _tool_parser(sub, "update_meta_page", "replace the personal persona page")
|
|
311
|
+
_add_text_source(meta, "body")
|
|
312
|
+
meta.add_argument("--message", required=True)
|
|
313
|
+
meta.add_argument(
|
|
314
|
+
"--confirm",
|
|
315
|
+
action="store_true",
|
|
316
|
+
help="confirm that the user agreed to this persona change",
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
prepare = _tool_parser(
|
|
320
|
+
sub, "prepare_to_share", "stage personal content for sharing"
|
|
321
|
+
)
|
|
322
|
+
prepare.add_argument("path")
|
|
323
|
+
prepare.add_argument("dest_space")
|
|
324
|
+
prepare.add_argument("--section")
|
|
325
|
+
prepare.add_argument("--section-file")
|
|
326
|
+
prepare.add_argument("--dest-path")
|
|
327
|
+
confirm = _tool_parser(sub, "confirm_share", "execute a staged share")
|
|
328
|
+
confirm.add_argument("nonce")
|
|
329
|
+
|
|
330
|
+
add_file = _tool_parser(
|
|
331
|
+
sub, "add_file", "upload a file with a searchable description"
|
|
332
|
+
)
|
|
333
|
+
add_file.add_argument("space")
|
|
334
|
+
add_file.add_argument("file", type=Path)
|
|
335
|
+
add_file.add_argument("--mime")
|
|
336
|
+
add_file.add_argument("--description", required=True)
|
|
337
|
+
add_file.add_argument("--page-path")
|
|
338
|
+
read_file = _tool_parser(sub, "read_file", "get file metadata and a signed URL")
|
|
339
|
+
read_file.add_argument("space")
|
|
340
|
+
read_file.add_argument("key")
|
|
341
|
+
delete_file = _tool_parser(sub, "delete_file", "permanently delete a stored file")
|
|
342
|
+
delete_file.add_argument("space")
|
|
343
|
+
delete_file.add_argument("key")
|
|
344
|
+
|
|
345
|
+
add_image = _tool_parser(
|
|
346
|
+
sub, "add_image", "compatibility alias for uploading an image"
|
|
347
|
+
)
|
|
348
|
+
add_image.add_argument("space")
|
|
349
|
+
add_image.add_argument("file", type=Path)
|
|
350
|
+
add_image.add_argument("--mime")
|
|
351
|
+
add_image.add_argument("--description", required=True)
|
|
352
|
+
add_image.add_argument("--page-path")
|
|
353
|
+
read_image = _tool_parser(sub, "read_image", "compatibility alias for read-file")
|
|
354
|
+
read_image.add_argument("space")
|
|
355
|
+
read_image.add_argument("key")
|
|
356
|
+
delete_image = _tool_parser(
|
|
357
|
+
sub, "delete_image", "compatibility alias for delete-file"
|
|
358
|
+
)
|
|
359
|
+
delete_image.add_argument("space")
|
|
360
|
+
delete_image.add_argument("key")
|
|
361
|
+
return parser
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _file_arguments(args: argparse.Namespace, *, image: bool = False) -> dict[str, Any]:
|
|
365
|
+
try:
|
|
366
|
+
data = args.file.read_bytes()
|
|
367
|
+
except OSError as exc:
|
|
368
|
+
raise CLIError(f"could not read file {args.file}: {exc}") from exc
|
|
369
|
+
mime = args.mime or mimetypes.guess_type(args.file.name)[0]
|
|
370
|
+
if mime is None:
|
|
371
|
+
mime = "application/octet-stream"
|
|
372
|
+
result = {
|
|
373
|
+
"space": args.space,
|
|
374
|
+
"data_base64": base64.b64encode(data).decode("ascii"),
|
|
375
|
+
"mime": mime,
|
|
376
|
+
"description": args.description,
|
|
377
|
+
"page_path": args.page_path,
|
|
378
|
+
}
|
|
379
|
+
if not image:
|
|
380
|
+
result["filename"] = args.file.name
|
|
381
|
+
return result
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def tool_call(args: argparse.Namespace) -> tuple[str, dict[str, Any]]:
|
|
385
|
+
"""Translate one parsed named command into its exact MCP call."""
|
|
386
|
+
tool = args.tool
|
|
387
|
+
if tool in {
|
|
388
|
+
"load_index",
|
|
389
|
+
"load_all_context",
|
|
390
|
+
"get_operating_protocol",
|
|
391
|
+
"list_spaces",
|
|
392
|
+
}:
|
|
393
|
+
return tool, {}
|
|
394
|
+
if tool == "read_pages":
|
|
395
|
+
return tool, {"space": args.space, "paths": args.paths}
|
|
396
|
+
if tool == "read_page":
|
|
397
|
+
return tool, {"space": args.space, "path": args.path}
|
|
398
|
+
if tool == "create_space":
|
|
399
|
+
return tool, {"slug": args.slug}
|
|
400
|
+
if tool in {"invite", "invite_to_reef"}:
|
|
401
|
+
payload = {"email": args.email, "display_name": args.display_name}
|
|
402
|
+
if tool == "invite":
|
|
403
|
+
payload["space"] = args.space
|
|
404
|
+
return tool, payload
|
|
405
|
+
if tool == "remove_member":
|
|
406
|
+
return tool, {"space": args.space, "email": args.email}
|
|
407
|
+
if tool in {"leave_space", "delete_space"}:
|
|
408
|
+
return tool, {"space": args.space}
|
|
409
|
+
if tool == "remember":
|
|
410
|
+
fact = sys.stdin.read() if args.fact == "-" else args.fact
|
|
411
|
+
return tool, {"fact": fact, "space": args.space}
|
|
412
|
+
if tool == "write_page":
|
|
413
|
+
return tool, {
|
|
414
|
+
"space": args.space,
|
|
415
|
+
"path": args.path,
|
|
416
|
+
"body": _text_source(args.body, args.body_file, "body"),
|
|
417
|
+
"message": args.message,
|
|
418
|
+
"title": args.title,
|
|
419
|
+
"tags": args.tags,
|
|
420
|
+
"expected_version": args.expected_version,
|
|
421
|
+
}
|
|
422
|
+
if tool == "write_pages":
|
|
423
|
+
pages = _json_source(args.pages)
|
|
424
|
+
if not isinstance(pages, list):
|
|
425
|
+
raise CLIError("write-pages input must be a JSON array")
|
|
426
|
+
return tool, {"space": args.space, "pages": pages, "message": args.message}
|
|
427
|
+
if tool == "edit_page_section":
|
|
428
|
+
return tool, {
|
|
429
|
+
"space": args.space,
|
|
430
|
+
"path": args.path,
|
|
431
|
+
"old_text": _text_source(args.old_text, args.old_text_file, "old-text"),
|
|
432
|
+
"new_text": _text_source(args.new_text, args.new_text_file, "new-text"),
|
|
433
|
+
"message": args.message,
|
|
434
|
+
"expected_version": args.expected_version,
|
|
435
|
+
}
|
|
436
|
+
if tool == "update_meta_page":
|
|
437
|
+
return tool, {
|
|
438
|
+
"space": "personal",
|
|
439
|
+
"path": "meta/persona.md",
|
|
440
|
+
"body": _text_source(args.body, args.body_file, "body"),
|
|
441
|
+
"message": args.message,
|
|
442
|
+
"confirm": args.confirm,
|
|
443
|
+
}
|
|
444
|
+
if tool == "prepare_to_share":
|
|
445
|
+
if args.section is not None and args.section_file is not None:
|
|
446
|
+
raise CLIError("use only one of --section and --section-file")
|
|
447
|
+
section = None
|
|
448
|
+
if args.section is not None or args.section_file is not None:
|
|
449
|
+
section = _text_source(args.section, args.section_file, "section")
|
|
450
|
+
return tool, {
|
|
451
|
+
"path": args.path,
|
|
452
|
+
"dest_space": args.dest_space,
|
|
453
|
+
"section": section,
|
|
454
|
+
"dest_path": args.dest_path,
|
|
455
|
+
}
|
|
456
|
+
if tool == "confirm_share":
|
|
457
|
+
return tool, {"nonce": args.nonce}
|
|
458
|
+
if tool == "add_file":
|
|
459
|
+
return tool, _file_arguments(args)
|
|
460
|
+
if tool == "add_image":
|
|
461
|
+
return tool, _file_arguments(args, image=True)
|
|
462
|
+
if tool in {"read_file", "delete_file", "read_image", "delete_image"}:
|
|
463
|
+
return tool, {"space": args.space, "key": args.key}
|
|
464
|
+
raise CLIError(f"unsupported command: {tool}")
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def _jsonable(value: Any) -> Any:
|
|
468
|
+
if hasattr(value, "model_dump"):
|
|
469
|
+
return _jsonable(value.model_dump(mode="json"))
|
|
470
|
+
if is_dataclass(value) and not isinstance(value, type):
|
|
471
|
+
return _jsonable(asdict(value))
|
|
472
|
+
if isinstance(value, Mapping):
|
|
473
|
+
return {str(key): _jsonable(item) for key, item in value.items()}
|
|
474
|
+
if isinstance(value, (list, tuple)):
|
|
475
|
+
return [_jsonable(item) for item in value]
|
|
476
|
+
if isinstance(value, (str, int, float, bool)) or value is None:
|
|
477
|
+
return value
|
|
478
|
+
return str(value)
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def _print_json(value: Any, *, compact: bool) -> None:
|
|
482
|
+
separators = (",", ":") if compact else None
|
|
483
|
+
print(
|
|
484
|
+
json.dumps(
|
|
485
|
+
_jsonable(value),
|
|
486
|
+
ensure_ascii=False,
|
|
487
|
+
indent=None if compact else 2,
|
|
488
|
+
separators=separators,
|
|
489
|
+
)
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
async def run(args: argparse.Namespace, *, client_class: type[Client] = Client) -> int:
|
|
494
|
+
"""Execute parsed arguments and return the intended process status."""
|
|
495
|
+
store = _token_store()
|
|
496
|
+
if args.command == "logout":
|
|
497
|
+
oauth = OAuth(mcp_url=args.url, client_name="Reef CLI", token_storage=store)
|
|
498
|
+
await oauth.token_storage_adapter.clear()
|
|
499
|
+
_print_json({"logged_out": True, "url": args.url}, compact=args.compact)
|
|
500
|
+
return 0
|
|
501
|
+
|
|
502
|
+
token = os.environ.get("REEF_ACCESS_TOKEN")
|
|
503
|
+
auth: Any
|
|
504
|
+
if args.command == "login" or not token:
|
|
505
|
+
auth = OAuth(mcp_url=args.url, client_name="Reef CLI", token_storage=store)
|
|
506
|
+
else:
|
|
507
|
+
auth = token
|
|
508
|
+
|
|
509
|
+
async with client_class(args.url, auth=auth) as client:
|
|
510
|
+
if args.command == "login":
|
|
511
|
+
await client.ping()
|
|
512
|
+
result: Any = {"logged_in": True, "url": args.url}
|
|
513
|
+
elif args.command == "tools":
|
|
514
|
+
result = await client.list_tools()
|
|
515
|
+
elif args.command == "call":
|
|
516
|
+
arguments = _json_source(args.arguments)
|
|
517
|
+
if not isinstance(arguments, dict):
|
|
518
|
+
raise CLIError("call arguments must be a JSON object")
|
|
519
|
+
result = await client.call_tool(args.tool_name, arguments)
|
|
520
|
+
else:
|
|
521
|
+
name, arguments = tool_call(args)
|
|
522
|
+
result = await client.call_tool(name, arguments)
|
|
523
|
+
|
|
524
|
+
serialised = _jsonable(result)
|
|
525
|
+
_print_json(serialised, compact=args.compact)
|
|
526
|
+
return 1 if isinstance(serialised, dict) and "error" in serialised else 0
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def main(argv: Sequence[str] | None = None) -> None:
|
|
530
|
+
"""CLI entry point."""
|
|
531
|
+
parser = build_parser()
|
|
532
|
+
args = parser.parse_args(argv)
|
|
533
|
+
try:
|
|
534
|
+
status = asyncio.run(run(args))
|
|
535
|
+
except CLIError as exc:
|
|
536
|
+
parser.exit(2, f"reef: {exc}\n")
|
|
537
|
+
except KeyboardInterrupt:
|
|
538
|
+
parser.exit(130, "reef: interrupted\n")
|
|
539
|
+
except Exception as exc: # noqa: BLE001 - CLI boundary turns failures into one line
|
|
540
|
+
parser.exit(1, f"reef: {exc}\n")
|
|
541
|
+
raise SystemExit(status)
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
if __name__ == "__main__":
|
|
545
|
+
main()
|