compshare-cli 0.4.3__py3-none-any.whl → 0.4.4__py3-none-any.whl
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.
- compshare_cli/__init__.py +1 -1
- compshare_cli/mcp_server.py +567 -0
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/METADATA +20 -1
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/RECORD +8 -7
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/entry_points.txt +1 -0
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/WHEEL +0 -0
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/licenses/LICENSE +0 -0
- {compshare_cli-0.4.3.dist-info → compshare_cli-0.4.4.dist-info}/top_level.txt +0 -0
compshare_cli/__init__.py
CHANGED
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple
|
|
10
|
+
|
|
11
|
+
from compshare_cli import __version__
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CommandKind(str, Enum):
|
|
15
|
+
READ = "read"
|
|
16
|
+
WRITE = "write"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
_READ_COMMANDS = {
|
|
20
|
+
("doctor",),
|
|
21
|
+
("instance", "search"),
|
|
22
|
+
("instance", "zones"),
|
|
23
|
+
("instance", "families"),
|
|
24
|
+
("instance", "list"),
|
|
25
|
+
("instance", "show"),
|
|
26
|
+
("instance", "wait"),
|
|
27
|
+
("instance", "price"),
|
|
28
|
+
("instance", "resize-price"),
|
|
29
|
+
("instance", "billing"),
|
|
30
|
+
("instance", "refund"),
|
|
31
|
+
("instance", "network"),
|
|
32
|
+
("instance", "models"),
|
|
33
|
+
("instance", "ports", "list"),
|
|
34
|
+
("instance", "schedule", "show"),
|
|
35
|
+
("instance", "software", "list"),
|
|
36
|
+
("instance", "template", "list"),
|
|
37
|
+
("instance", "template", "show"),
|
|
38
|
+
("instance", "template", "path"),
|
|
39
|
+
("instance", "job", "list"),
|
|
40
|
+
("instance", "job", "show"),
|
|
41
|
+
("instance", "job", "logs"),
|
|
42
|
+
("instance", "job", "wait"),
|
|
43
|
+
("image", "list"),
|
|
44
|
+
("image", "show"),
|
|
45
|
+
("image", "progress"),
|
|
46
|
+
("image", "shares"),
|
|
47
|
+
("image", "tags"),
|
|
48
|
+
("storage", "disk", "list"),
|
|
49
|
+
("storage", "disk", "price"),
|
|
50
|
+
("bandwidth", "list"),
|
|
51
|
+
("bandwidth", "instances"),
|
|
52
|
+
("minimax", "show"),
|
|
53
|
+
("minimax", "list"),
|
|
54
|
+
("minimax", "packages"),
|
|
55
|
+
("minimax", "points"),
|
|
56
|
+
("team", "list"),
|
|
57
|
+
("team", "joined"),
|
|
58
|
+
("team", "show"),
|
|
59
|
+
("team", "invite", "list"),
|
|
60
|
+
("team", "member", "list"),
|
|
61
|
+
("team", "billing", "list"),
|
|
62
|
+
("team", "billing", "summary"),
|
|
63
|
+
("team", "billing", "unpaid"),
|
|
64
|
+
("team", "billing", "products"),
|
|
65
|
+
("team", "audit"),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
_WRITE_COMMANDS = {
|
|
69
|
+
("feedback",),
|
|
70
|
+
("instance", "create"),
|
|
71
|
+
("instance", "start"),
|
|
72
|
+
("instance", "stop"),
|
|
73
|
+
("instance", "reboot"),
|
|
74
|
+
("instance", "delete"),
|
|
75
|
+
("instance", "rename"),
|
|
76
|
+
("instance", "password"),
|
|
77
|
+
("instance", "reinstall"),
|
|
78
|
+
("instance", "resize"),
|
|
79
|
+
("instance", "charge"),
|
|
80
|
+
("instance", "ssh"),
|
|
81
|
+
("instance", "cp"),
|
|
82
|
+
("instance", "ports", "update"),
|
|
83
|
+
("instance", "schedule", "set"),
|
|
84
|
+
("instance", "schedule", "extend"),
|
|
85
|
+
("instance", "schedule", "cancel"),
|
|
86
|
+
("instance", "template", "create"),
|
|
87
|
+
("instance", "template", "delete"),
|
|
88
|
+
("instance", "job", "submit"),
|
|
89
|
+
("instance", "job", "cancel"),
|
|
90
|
+
("instance", "job", "prune"),
|
|
91
|
+
("image", "create"),
|
|
92
|
+
("image", "update"),
|
|
93
|
+
("image", "delete"),
|
|
94
|
+
("image", "share"),
|
|
95
|
+
("image", "unshare"),
|
|
96
|
+
("image", "publish"),
|
|
97
|
+
("image", "favorite"),
|
|
98
|
+
("image", "unfavorite"),
|
|
99
|
+
("storage", "disk", "create"),
|
|
100
|
+
("storage", "disk", "attach"),
|
|
101
|
+
("storage", "disk", "detach"),
|
|
102
|
+
("storage", "disk", "resize"),
|
|
103
|
+
("storage", "disk", "delete"),
|
|
104
|
+
("storage", "us3", "attach"),
|
|
105
|
+
("bandwidth", "create"),
|
|
106
|
+
("bandwidth", "resize"),
|
|
107
|
+
("bandwidth", "delete"),
|
|
108
|
+
("bandwidth", "switch"),
|
|
109
|
+
("minimax", "create"),
|
|
110
|
+
("minimax", "cancel"),
|
|
111
|
+
("team", "create"),
|
|
112
|
+
("team", "update"),
|
|
113
|
+
("team", "delete"),
|
|
114
|
+
("team", "invite", "send"),
|
|
115
|
+
("team", "invite", "accept"),
|
|
116
|
+
("team", "invite", "reject"),
|
|
117
|
+
("team", "invite", "cancel"),
|
|
118
|
+
("team", "member", "rename"),
|
|
119
|
+
("team", "quota", "grant"),
|
|
120
|
+
("team", "quota", "reclaim"),
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_BLOCKED_COMMAND_PREFIXES = {
|
|
124
|
+
("config",),
|
|
125
|
+
("version",),
|
|
126
|
+
("instance", "scp"),
|
|
127
|
+
("instance", "upgrade-price"),
|
|
128
|
+
("team", "billing", "export"),
|
|
129
|
+
}
|
|
130
|
+
_DRY_RUN_COMMANDS = {
|
|
131
|
+
("instance", "create"),
|
|
132
|
+
("instance", "resize"),
|
|
133
|
+
("instance", "charge"),
|
|
134
|
+
("instance", "job", "prune"),
|
|
135
|
+
("bandwidth", "create"),
|
|
136
|
+
("bandwidth", "resize"),
|
|
137
|
+
("bandwidth", "delete"),
|
|
138
|
+
("minimax", "create"),
|
|
139
|
+
}
|
|
140
|
+
_PRINT_ONLY_COMMANDS = {("instance", "ssh"), ("instance", "cp")}
|
|
141
|
+
_FORBIDDEN_FLAGS = {
|
|
142
|
+
"--help",
|
|
143
|
+
"--install-completion",
|
|
144
|
+
"--json",
|
|
145
|
+
"--private-key",
|
|
146
|
+
"--profile",
|
|
147
|
+
"--public-key",
|
|
148
|
+
"--show-sensitive",
|
|
149
|
+
"--version",
|
|
150
|
+
"-h",
|
|
151
|
+
}
|
|
152
|
+
_COMPSHARE_ENVIRONMENT = {
|
|
153
|
+
"COMPSHARE_MINIMAX_API_KEY",
|
|
154
|
+
"COMPSHARE_MINIMAX_CALLBACK_TOKEN",
|
|
155
|
+
"COMPSHARE_PRIVATE_KEY",
|
|
156
|
+
"COMPSHARE_PUBLIC_KEY",
|
|
157
|
+
}
|
|
158
|
+
_CHILD_ENVIRONMENT = {
|
|
159
|
+
"ALL_PROXY",
|
|
160
|
+
"APPDATA",
|
|
161
|
+
"COMSPEC",
|
|
162
|
+
"HOME",
|
|
163
|
+
"HTTPS_PROXY",
|
|
164
|
+
"HTTP_PROXY",
|
|
165
|
+
"LANG",
|
|
166
|
+
"LC_ALL",
|
|
167
|
+
"LOCALAPPDATA",
|
|
168
|
+
"NO_PROXY",
|
|
169
|
+
"PATH",
|
|
170
|
+
"PATHEXT",
|
|
171
|
+
"SSH_AUTH_SOCK",
|
|
172
|
+
"SSL_CERT_DIR",
|
|
173
|
+
"SSL_CERT_FILE",
|
|
174
|
+
"SYSTEMROOT",
|
|
175
|
+
"TEMP",
|
|
176
|
+
"TMP",
|
|
177
|
+
"TMPDIR",
|
|
178
|
+
"USERPROFILE",
|
|
179
|
+
"WINDIR",
|
|
180
|
+
}
|
|
181
|
+
_MAX_ARGS = 128
|
|
182
|
+
_MAX_ARG_LENGTH = 8192
|
|
183
|
+
_MAX_OUTPUT_BYTES = 2 * 1024 * 1024
|
|
184
|
+
_MAX_TIMEOUT_SECONDS = 900
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class MCPBridgeError(RuntimeError):
|
|
188
|
+
"""Raised when a CLI request cannot be executed safely through MCP."""
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class CommandTimeout(MCPBridgeError):
|
|
192
|
+
"""Raised when a CLI subprocess exceeds the MCP call timeout."""
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _flag_set(args: Sequence[str]) -> set[str]:
|
|
196
|
+
flags = set()
|
|
197
|
+
for value in args:
|
|
198
|
+
if value == "--":
|
|
199
|
+
break
|
|
200
|
+
if value.startswith("-"):
|
|
201
|
+
flags.add(value.split("=", 1)[0])
|
|
202
|
+
return flags
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def validate_args(args: Sequence[str]) -> List[str]:
|
|
206
|
+
if isinstance(args, (str, bytes)) or not args:
|
|
207
|
+
raise MCPBridgeError("args must be a non-empty list of CompShare CLI arguments")
|
|
208
|
+
if len(args) > _MAX_ARGS:
|
|
209
|
+
raise MCPBridgeError(f"too many arguments; maximum is {_MAX_ARGS}")
|
|
210
|
+
normalized = []
|
|
211
|
+
before_separator = True
|
|
212
|
+
for value in args:
|
|
213
|
+
if not isinstance(value, str):
|
|
214
|
+
raise MCPBridgeError("every CLI argument must be a string")
|
|
215
|
+
if not value or "\x00" in value or len(value) > _MAX_ARG_LENGTH:
|
|
216
|
+
raise MCPBridgeError(
|
|
217
|
+
f"arguments must be non-empty, NUL-free, and at most {_MAX_ARG_LENGTH} characters"
|
|
218
|
+
)
|
|
219
|
+
if value == "--":
|
|
220
|
+
before_separator = False
|
|
221
|
+
elif before_separator and value.split("=", 1)[0] in _FORBIDDEN_FLAGS:
|
|
222
|
+
raise MCPBridgeError(
|
|
223
|
+
f"{value.split('=', 1)[0]} is managed by the MCP bridge and cannot be supplied"
|
|
224
|
+
)
|
|
225
|
+
normalized.append(value)
|
|
226
|
+
if normalized[0].startswith("-"):
|
|
227
|
+
raise MCPBridgeError("args must start with a CompShare command, not a global option")
|
|
228
|
+
return normalized
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _matching_command(args: Sequence[str]) -> Optional[Tuple[str, ...]]:
|
|
232
|
+
known = _READ_COMMANDS | _WRITE_COMMANDS | _BLOCKED_COMMAND_PREFIXES
|
|
233
|
+
for length in range(min(3, len(args)), 0, -1):
|
|
234
|
+
candidate = tuple(args[:length])
|
|
235
|
+
if candidate in known:
|
|
236
|
+
return candidate
|
|
237
|
+
return None
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def classify_command(args: Sequence[str]) -> CommandKind:
|
|
241
|
+
normalized = validate_args(args)
|
|
242
|
+
command = _matching_command(normalized)
|
|
243
|
+
if command is None:
|
|
244
|
+
shown = " ".join(normalized[:3])
|
|
245
|
+
raise MCPBridgeError(f"unsupported CompShare command: {shown}")
|
|
246
|
+
if command in _BLOCKED_COMMAND_PREFIXES:
|
|
247
|
+
raise MCPBridgeError(
|
|
248
|
+
f"{' '.join(command)} is managed outside this connector and cannot be called "
|
|
249
|
+
"through MCP"
|
|
250
|
+
)
|
|
251
|
+
flags = _flag_set(normalized)
|
|
252
|
+
if command == ("instance", "job", "logs") and flags & {"--follow", "-f"}:
|
|
253
|
+
raise MCPBridgeError("follow mode is not supported over MCP; request log slices instead")
|
|
254
|
+
if command == ("instance", "ssh") and "--print" not in flags and "--" not in normalized:
|
|
255
|
+
raise MCPBridgeError(
|
|
256
|
+
"interactive SSH is not supported over MCP; pass a remote command after --"
|
|
257
|
+
)
|
|
258
|
+
if command in _WRITE_COMMANDS:
|
|
259
|
+
if command in _DRY_RUN_COMMANDS and "--dry-run" in flags:
|
|
260
|
+
return CommandKind.READ
|
|
261
|
+
if command in _PRINT_ONLY_COMMANDS and "--print" in flags:
|
|
262
|
+
return CommandKind.READ
|
|
263
|
+
return CommandKind.WRITE
|
|
264
|
+
return CommandKind.READ
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def validate_help_path(command: Sequence[str]) -> List[str]:
|
|
268
|
+
if isinstance(command, (str, bytes)):
|
|
269
|
+
raise MCPBridgeError("command must be a list of zero to three command names")
|
|
270
|
+
if len(command) > 3:
|
|
271
|
+
raise MCPBridgeError("help accepts at most three command names")
|
|
272
|
+
normalized = []
|
|
273
|
+
for value in command:
|
|
274
|
+
if not isinstance(value, str) or not value or value.startswith("-") or "\x00" in value:
|
|
275
|
+
raise MCPBridgeError("help path entries must be plain command names")
|
|
276
|
+
normalized.append(value)
|
|
277
|
+
if normalized and normalized[0] in {"config", "version"}:
|
|
278
|
+
raise MCPBridgeError(f"{normalized[0]} is managed outside this connector")
|
|
279
|
+
return normalized
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _validate_timeout(timeout_seconds: int) -> int:
|
|
283
|
+
if isinstance(timeout_seconds, bool) or not 1 <= timeout_seconds <= _MAX_TIMEOUT_SECONDS:
|
|
284
|
+
raise MCPBridgeError(f"timeout_seconds must be between 1 and {_MAX_TIMEOUT_SECONDS}")
|
|
285
|
+
return timeout_seconds
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class CompShareRunner:
|
|
289
|
+
def __init__(
|
|
290
|
+
self,
|
|
291
|
+
*,
|
|
292
|
+
command_prefix: Optional[Sequence[str]] = None,
|
|
293
|
+
environ: Optional[Mapping[str, str]] = None,
|
|
294
|
+
) -> None:
|
|
295
|
+
source = dict(os.environ if environ is None else environ)
|
|
296
|
+
self._scratch = tempfile.TemporaryDirectory(prefix="compshare-mcp-")
|
|
297
|
+
self.command_prefix = list(command_prefix or (sys.executable, "-m", "compshare_cli"))
|
|
298
|
+
self.environment = {
|
|
299
|
+
key: value
|
|
300
|
+
for key, value in source.items()
|
|
301
|
+
if key in _CHILD_ENVIRONMENT or key in _COMPSHARE_ENVIRONMENT or key.startswith("LC_")
|
|
302
|
+
}
|
|
303
|
+
scratch = self._scratch.name
|
|
304
|
+
self.environment.update(
|
|
305
|
+
{
|
|
306
|
+
"COMPSHARE_CONFIG_FILE": os.path.join(scratch, "config.json"),
|
|
307
|
+
"COMPSHARE_SSH_CACHE_FILE": os.path.join(scratch, "ssh-cache.json"),
|
|
308
|
+
"COMPSHARE_TEMPLATE_FILE": os.path.join(scratch, "templates.json"),
|
|
309
|
+
}
|
|
310
|
+
)
|
|
311
|
+
self._secrets = tuple(
|
|
312
|
+
value for key in _COMPSHARE_ENVIRONMENT if (value := self.environment.get(key))
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
def close(self) -> None:
|
|
316
|
+
self._scratch.cleanup()
|
|
317
|
+
|
|
318
|
+
def status(self, timeout_seconds: int = 30) -> Dict[str, Any]:
|
|
319
|
+
timeout = _validate_timeout(timeout_seconds)
|
|
320
|
+
credential_status = {
|
|
321
|
+
"publicKey": bool(self.environment.get("COMPSHARE_PUBLIC_KEY")),
|
|
322
|
+
"privateKey": bool(self.environment.get("COMPSHARE_PRIVATE_KEY")),
|
|
323
|
+
"minimaxApiKey": bool(self.environment.get("COMPSHARE_MINIMAX_API_KEY")),
|
|
324
|
+
}
|
|
325
|
+
missing = [
|
|
326
|
+
name
|
|
327
|
+
for name, present in (
|
|
328
|
+
("COMPSHARE_PUBLIC_KEY", credential_status["publicKey"]),
|
|
329
|
+
("COMPSHARE_PRIVATE_KEY", credential_status["privateKey"]),
|
|
330
|
+
)
|
|
331
|
+
if not present
|
|
332
|
+
]
|
|
333
|
+
if missing:
|
|
334
|
+
return {
|
|
335
|
+
"ok": False,
|
|
336
|
+
"version": __version__,
|
|
337
|
+
"credentialMode": "workbuddy-token",
|
|
338
|
+
"credentials": credential_status,
|
|
339
|
+
"error": {
|
|
340
|
+
"kind": "authentication",
|
|
341
|
+
"message": "Update the Connector credentials: " + ", ".join(missing),
|
|
342
|
+
},
|
|
343
|
+
}
|
|
344
|
+
try:
|
|
345
|
+
api = self._invoke(("instance", "zones"), timeout)
|
|
346
|
+
except MCPBridgeError as exc:
|
|
347
|
+
return {
|
|
348
|
+
"ok": False,
|
|
349
|
+
"version": __version__,
|
|
350
|
+
"credentialMode": "workbuddy-token",
|
|
351
|
+
"credentials": credential_status,
|
|
352
|
+
"error": {"kind": "connection", "message": str(exc)},
|
|
353
|
+
}
|
|
354
|
+
result = self._result(api)
|
|
355
|
+
return {
|
|
356
|
+
"ok": result["ok"],
|
|
357
|
+
"version": __version__,
|
|
358
|
+
"credentialMode": "workbuddy-token",
|
|
359
|
+
"credentials": credential_status,
|
|
360
|
+
"api": result,
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
def help(self, command: Sequence[str], timeout_seconds: int = 10) -> Dict[str, Any]:
|
|
364
|
+
path = validate_help_path(command)
|
|
365
|
+
completed = self._invoke((*path, "--help"), _validate_timeout(timeout_seconds))
|
|
366
|
+
return self._result(completed)
|
|
367
|
+
|
|
368
|
+
def run(
|
|
369
|
+
self,
|
|
370
|
+
args: Sequence[str],
|
|
371
|
+
*,
|
|
372
|
+
expected_kind: CommandKind,
|
|
373
|
+
timeout_seconds: int,
|
|
374
|
+
) -> Dict[str, Any]:
|
|
375
|
+
normalized = validate_args(args)
|
|
376
|
+
actual_kind = classify_command(normalized)
|
|
377
|
+
if actual_kind is not expected_kind:
|
|
378
|
+
raise MCPBridgeError(
|
|
379
|
+
f"command is classified as {actual_kind.value}; use compshare_{actual_kind.value}"
|
|
380
|
+
)
|
|
381
|
+
timeout = _validate_timeout(timeout_seconds)
|
|
382
|
+
try:
|
|
383
|
+
completed = self._invoke(normalized, timeout)
|
|
384
|
+
except CommandTimeout as exc:
|
|
385
|
+
is_write = actual_kind is CommandKind.WRITE
|
|
386
|
+
return {
|
|
387
|
+
"ok": False,
|
|
388
|
+
"exitCode": None,
|
|
389
|
+
"outcome": "unknown" if is_write else "not-completed",
|
|
390
|
+
"retryable": not is_write,
|
|
391
|
+
"error": {
|
|
392
|
+
"kind": "timeout",
|
|
393
|
+
"message": str(exc),
|
|
394
|
+
"action": "check_existing" if is_write else "retry_later",
|
|
395
|
+
},
|
|
396
|
+
}
|
|
397
|
+
return self._result(completed)
|
|
398
|
+
|
|
399
|
+
def _invoke(
|
|
400
|
+
self, args: Sequence[str], timeout_seconds: int
|
|
401
|
+
) -> subprocess.CompletedProcess[str]:
|
|
402
|
+
try:
|
|
403
|
+
return subprocess.run(
|
|
404
|
+
[*self.command_prefix, "--json", *args],
|
|
405
|
+
capture_output=True,
|
|
406
|
+
text=True,
|
|
407
|
+
encoding="utf-8",
|
|
408
|
+
errors="replace",
|
|
409
|
+
timeout=timeout_seconds,
|
|
410
|
+
env=self.environment,
|
|
411
|
+
check=False,
|
|
412
|
+
)
|
|
413
|
+
except subprocess.TimeoutExpired as exc:
|
|
414
|
+
raise CommandTimeout(
|
|
415
|
+
f"CompShare command timed out after {timeout_seconds} seconds"
|
|
416
|
+
) from exc
|
|
417
|
+
except OSError as exc:
|
|
418
|
+
raise MCPBridgeError(f"failed to execute CompShare CLI: {exc}") from exc
|
|
419
|
+
|
|
420
|
+
def _result(self, completed: subprocess.CompletedProcess[str]) -> Dict[str, Any]:
|
|
421
|
+
raw = completed.stdout.strip()
|
|
422
|
+
if len(raw.encode("utf-8")) > _MAX_OUTPUT_BYTES:
|
|
423
|
+
raise MCPBridgeError("CompShare output exceeded 2 MiB; narrow the request")
|
|
424
|
+
for secret in self._secrets:
|
|
425
|
+
raw = raw.replace(secret, "[REDACTED]")
|
|
426
|
+
try:
|
|
427
|
+
payload = json.loads(raw)
|
|
428
|
+
except json.JSONDecodeError as exc:
|
|
429
|
+
error = completed.stderr.strip() or raw or "no output"
|
|
430
|
+
for secret in self._secrets:
|
|
431
|
+
error = error.replace(secret, "[REDACTED]")
|
|
432
|
+
raise MCPBridgeError(f"CompShare CLI returned invalid JSON: {error[:1000]}") from exc
|
|
433
|
+
if not isinstance(payload, dict):
|
|
434
|
+
raise MCPBridgeError("CompShare CLI returned a non-object JSON response")
|
|
435
|
+
return {
|
|
436
|
+
"ok": completed.returncode == 0 and payload.get("ok") is True,
|
|
437
|
+
"exitCode": completed.returncode,
|
|
438
|
+
"result": payload,
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def create_server(runner: Optional[CompShareRunner] = None):
|
|
443
|
+
try:
|
|
444
|
+
from mcp.server import MCPServer
|
|
445
|
+
from mcp.server.mcpserver.exceptions import ToolError
|
|
446
|
+
from mcp.types import ToolAnnotations
|
|
447
|
+
except ImportError as exc:
|
|
448
|
+
raise MCPBridgeError("MCP support is not installed; install compshare-cli[mcp]") from exc
|
|
449
|
+
|
|
450
|
+
bridge = runner or CompShareRunner()
|
|
451
|
+
server = MCPServer(
|
|
452
|
+
"CompShare",
|
|
453
|
+
version=__version__,
|
|
454
|
+
instructions=(
|
|
455
|
+
"Use status and help before running commands. Use the read tool for inspection and "
|
|
456
|
+
"dry runs. Use the write tool only after explicit user confirmation."
|
|
457
|
+
),
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
@server.tool(
|
|
461
|
+
title="检查 CompShare 连接状态",
|
|
462
|
+
annotations=ToolAnnotations(
|
|
463
|
+
readOnlyHint=True,
|
|
464
|
+
destructiveHint=False,
|
|
465
|
+
idempotentHint=True,
|
|
466
|
+
openWorldHint=True,
|
|
467
|
+
),
|
|
468
|
+
)
|
|
469
|
+
def compshare_status(timeout_seconds: int = 30) -> dict[str, Any]:
|
|
470
|
+
"""检查凭证注入、CLI 版本和 CompShare API 连通性,不执行写操作。"""
|
|
471
|
+
try:
|
|
472
|
+
return bridge.status(timeout_seconds)
|
|
473
|
+
except MCPBridgeError as exc:
|
|
474
|
+
raise ToolError(str(exc)) from exc
|
|
475
|
+
|
|
476
|
+
@server.tool(
|
|
477
|
+
title="查看 CompShare 命令帮助",
|
|
478
|
+
annotations=ToolAnnotations(
|
|
479
|
+
readOnlyHint=True,
|
|
480
|
+
destructiveHint=False,
|
|
481
|
+
idempotentHint=True,
|
|
482
|
+
openWorldHint=False,
|
|
483
|
+
),
|
|
484
|
+
)
|
|
485
|
+
def compshare_help(command: List[str], timeout_seconds: int = 10) -> dict[str, Any]:
|
|
486
|
+
"""读取当前 CLI 的结构化帮助;command 是不含 compshare 的零到三个命令名。"""
|
|
487
|
+
try:
|
|
488
|
+
return bridge.help(command, timeout_seconds)
|
|
489
|
+
except MCPBridgeError as exc:
|
|
490
|
+
raise ToolError(str(exc)) from exc
|
|
491
|
+
|
|
492
|
+
@server.tool(
|
|
493
|
+
title="执行 CompShare 只读命令",
|
|
494
|
+
annotations=ToolAnnotations(
|
|
495
|
+
readOnlyHint=True,
|
|
496
|
+
destructiveHint=False,
|
|
497
|
+
idempotentHint=True,
|
|
498
|
+
openWorldHint=True,
|
|
499
|
+
),
|
|
500
|
+
)
|
|
501
|
+
def compshare_read(args: List[str], timeout_seconds: int = 30) -> dict[str, Any]:
|
|
502
|
+
"""执行白名单内的只读、dry-run 或 print-only 命令。
|
|
503
|
+
|
|
504
|
+
args 不含开头的 compshare 和 --json,例如 ["instance", "list", "--all"]。
|
|
505
|
+
写命令会被拒绝并提示使用 compshare_write。
|
|
506
|
+
"""
|
|
507
|
+
try:
|
|
508
|
+
return bridge.run(
|
|
509
|
+
args,
|
|
510
|
+
expected_kind=CommandKind.READ,
|
|
511
|
+
timeout_seconds=timeout_seconds,
|
|
512
|
+
)
|
|
513
|
+
except MCPBridgeError as exc:
|
|
514
|
+
raise ToolError(str(exc)) from exc
|
|
515
|
+
|
|
516
|
+
@server.tool(
|
|
517
|
+
title="执行 CompShare 写命令",
|
|
518
|
+
annotations=ToolAnnotations(
|
|
519
|
+
readOnlyHint=False,
|
|
520
|
+
destructiveHint=True,
|
|
521
|
+
idempotentHint=False,
|
|
522
|
+
openWorldHint=True,
|
|
523
|
+
),
|
|
524
|
+
)
|
|
525
|
+
def compshare_write(
|
|
526
|
+
args: List[str],
|
|
527
|
+
user_confirmed: bool,
|
|
528
|
+
confirmation_summary: str,
|
|
529
|
+
timeout_seconds: int = 60,
|
|
530
|
+
) -> dict[str, Any]:
|
|
531
|
+
"""执行白名单内的资源写入、付费、远程命令或文件传输。
|
|
532
|
+
|
|
533
|
+
仅在用户看到本次准确对象、参数、费用和影响后明确同意时调用。
|
|
534
|
+
user_confirmed 必须为 true,confirmation_summary 必须具体记录本次确认。
|
|
535
|
+
写操作只执行一次;超时结果未知时先读取资源状态,禁止直接重试。
|
|
536
|
+
"""
|
|
537
|
+
try:
|
|
538
|
+
if not user_confirmed:
|
|
539
|
+
raise MCPBridgeError("explicit user confirmation is required")
|
|
540
|
+
summary = confirmation_summary.strip()
|
|
541
|
+
if len(summary) < 8:
|
|
542
|
+
raise MCPBridgeError(
|
|
543
|
+
"confirmation_summary must describe the specific confirmed operation"
|
|
544
|
+
)
|
|
545
|
+
result = bridge.run(
|
|
546
|
+
args,
|
|
547
|
+
expected_kind=CommandKind.WRITE,
|
|
548
|
+
timeout_seconds=timeout_seconds,
|
|
549
|
+
)
|
|
550
|
+
result["confirmationSummary"] = summary
|
|
551
|
+
return result
|
|
552
|
+
except MCPBridgeError as exc:
|
|
553
|
+
raise ToolError(str(exc)) from exc
|
|
554
|
+
|
|
555
|
+
return server
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def main() -> None:
|
|
559
|
+
try:
|
|
560
|
+
server = create_server()
|
|
561
|
+
except MCPBridgeError as exc:
|
|
562
|
+
raise SystemExit(str(exc)) from exc
|
|
563
|
+
server.run(transport="stdio")
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
if __name__ == "__main__":
|
|
567
|
+
main()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: compshare-cli
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.4
|
|
4
4
|
Summary: Command-line client for CompShare GPU compute and MiniMax H3
|
|
5
5
|
Author: CompShare
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -21,6 +21,8 @@ Provides-Extra: dev
|
|
|
21
21
|
Requires-Dist: pytest<10,>=8; extra == "dev"
|
|
22
22
|
Requires-Dist: pytest-cov<8,>=5; extra == "dev"
|
|
23
23
|
Requires-Dist: ruff<1,>=0.6; extra == "dev"
|
|
24
|
+
Provides-Extra: mcp
|
|
25
|
+
Requires-Dist: mcp<3,>=2.2; extra == "mcp"
|
|
24
26
|
Dynamic: license-file
|
|
25
27
|
|
|
26
28
|
# CompShare CLI
|
|
@@ -115,6 +117,23 @@ npx skills add compshare-cn/compshare-cli --skill compshare-cli
|
|
|
115
117
|
|
|
116
118
|
完整说明见 [CompShare CLI Skill](skills/compshare-cli/SKILL.md)。
|
|
117
119
|
|
|
120
|
+
## WorkBuddy Connector
|
|
121
|
+
|
|
122
|
+
仓库内置符合 WorkBuddy 规范的 `MCP + Skill` Token 连接器,提交目录为
|
|
123
|
+
[`connector/compshare`](connector/compshare)。用户在 WorkBuddy 表单中填写 API 公钥和
|
|
124
|
+
私钥后,连接器通过本地 stdio MCP 注入凭证,不需要 OAuth,也不会修改已有 CLI 配置。
|
|
125
|
+
|
|
126
|
+
WorkBuddy 配置固定使用 Python 3.11;本地开发需 Python 3.10 或更高版本,可安装 MCP 可选
|
|
127
|
+
依赖并启动服务:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
pip install -e '.[mcp]'
|
|
131
|
+
compshare-mcp
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
正式 Connector 使用 `uvx` 从 PyPI 安装固定版本,并将只读、预演和需要明确确认的写操作
|
|
135
|
+
分开暴露。MiniMax H3 使用表单中可选的独立模型 API Key。
|
|
136
|
+
|
|
118
137
|
## SDK
|
|
119
138
|
|
|
120
139
|
如需通过 SDK 集成 CompShare API,可参考独立的
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
compshare_cli/__init__.py,sha256=
|
|
1
|
+
compshare_cli/__init__.py,sha256=aSOaZyRZKx5IHghhjniZdvYA7XfoZ30l30biEhjm2dw,60
|
|
2
2
|
compshare_cli/__main__.py,sha256=ftkB0R9jLZejzr717131qktgfFhTe12noFeGqQsVZMc,74
|
|
3
3
|
compshare_cli/actions.py,sha256=DWIXXrUHru_mT6DNnrV5VhkfRhgyBGsu73uQk-7o8Mc,3633
|
|
4
4
|
compshare_cli/api.py,sha256=S4hUvkbG0l7qUqQaAf7trX29GteF1yVnOGgjcJj99jc,9286
|
|
@@ -9,6 +9,7 @@ compshare_cli/i18n.py,sha256=fEVoRRANTab7LavQvFtH-qd3J1_4MyL5K_zdmgG2hPA,57859
|
|
|
9
9
|
compshare_cli/insights.py,sha256=0ouShkU_8_XusQG8JttfZBW-cG6GD9YBpB_AbPS5tnU,4819
|
|
10
10
|
compshare_cli/instance_templates.py,sha256=UGDxQ5IChId4hTTDByKEVk594qswMj1r6LIuU9Qty7A,7894
|
|
11
11
|
compshare_cli/location.py,sha256=17hE_AGU3yKYEA5v-5W6HEU61l4K1JZDdgCpbGIM2KE,4324
|
|
12
|
+
compshare_cli/mcp_server.py,sha256=ttekPa49aAPtkge7AnHbDrcMhu5NtjzeHdRSS41FJrk,19260
|
|
12
13
|
compshare_cli/output.py,sha256=uvmmN3yT6jHkfMEo_GmzQTX8qMF2HHgcJ4aEKZcBBps,12525
|
|
13
14
|
compshare_cli/parsing.py,sha256=pn4W_lZY2FBhdUjHUDFAHvA_kgcc1ch6D-7uOfJZ68E,4578
|
|
14
15
|
compshare_cli/remote_jobs.py,sha256=soAPdpJLCG5Hus-ciqQ0f0vYm-4hgl_thz62RWR1wSA,18808
|
|
@@ -26,9 +27,9 @@ compshare_cli/commands/instance.py,sha256=ELAfNGpijeQmDozpYZgU-ZLMqnAtS01pjG-l5u
|
|
|
26
27
|
compshare_cli/commands/minimax.py,sha256=pbhCLARyMyaIdOGhJ4y47hotWXVTozWv9kQn3Ve8aPw,15364
|
|
27
28
|
compshare_cli/commands/storage.py,sha256=eUmX2KWx-aYr3ea6GVHVjffq0-58cjsEzINxLOrspyI,8975
|
|
28
29
|
compshare_cli/commands/team.py,sha256=weVCgFPOoDDl03LWmVGhXNR0BPkUvyOUwUHlxUOWyKY,23651
|
|
29
|
-
compshare_cli-0.4.
|
|
30
|
-
compshare_cli-0.4.
|
|
31
|
-
compshare_cli-0.4.
|
|
32
|
-
compshare_cli-0.4.
|
|
33
|
-
compshare_cli-0.4.
|
|
34
|
-
compshare_cli-0.4.
|
|
30
|
+
compshare_cli-0.4.4.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
31
|
+
compshare_cli-0.4.4.dist-info/METADATA,sha256=YjDf8RjEexvM8uJSQ6x8tKLP0336USUj5rzQahPGH6k,4816
|
|
32
|
+
compshare_cli-0.4.4.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
33
|
+
compshare_cli-0.4.4.dist-info/entry_points.txt,sha256=GGv28Bsp3CfBKbcoG6nr2ceqPWYR_XIk0HxxN02olsU,149
|
|
34
|
+
compshare_cli-0.4.4.dist-info/top_level.txt,sha256=ROBw509DOLNt7Z-xwrArjK066G7s4IH0jTcGiaKyMNw,14
|
|
35
|
+
compshare_cli-0.4.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|