integrity-killswitch 0.3.0__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.
- agent_killswitch/__init__.py +63 -0
- agent_killswitch/__main__.py +10 -0
- agent_killswitch/ancestry.py +767 -0
- agent_killswitch/cloud.py +477 -0
- agent_killswitch/core.py +1214 -0
- agent_killswitch/hooks.py +748 -0
- agent_killswitch/mcp_server.py +551 -0
- agent_killswitch/network.py +1442 -0
- agent_killswitch/web.py +426 -0
- integrity_killswitch-0.3.0.dist-info/METADATA +356 -0
- integrity_killswitch-0.3.0.dist-info/RECORD +15 -0
- integrity_killswitch-0.3.0.dist-info/WHEEL +5 -0
- integrity_killswitch-0.3.0.dist-info/entry_points.txt +2 -0
- integrity_killswitch-0.3.0.dist-info/licenses/LICENSE +21 -0
- integrity_killswitch-0.3.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"""Zero-dependency stdlib JSON-RPC 2.0 stdio MCP server for agent-killswitch.
|
|
2
|
+
|
|
3
|
+
Security invariant: TIGHTEN-ONLY over MCP.
|
|
4
|
+
Exposes read and tighten operations only (status, check, panic, vendor-off, scope-off).
|
|
5
|
+
Restore/weakening operations (resume, on, scope-on, net-restore, thaw, uninstall-hooks)
|
|
6
|
+
are NEVER exposed and are explicitly refused.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import json
|
|
13
|
+
import sys
|
|
14
|
+
from typing import Any, TextIO
|
|
15
|
+
|
|
16
|
+
from . import ancestry, core
|
|
17
|
+
|
|
18
|
+
JSONRPC_VERSION = "2.0"
|
|
19
|
+
DEFAULT_PROTOCOL_VERSION = "2024-11-05"
|
|
20
|
+
SERVER_NAME = "agent-killswitch"
|
|
21
|
+
|
|
22
|
+
PARSE_ERROR = -32700
|
|
23
|
+
INVALID_REQUEST = -32600
|
|
24
|
+
METHOD_NOT_FOUND = -32601
|
|
25
|
+
INVALID_PARAMS = -32602
|
|
26
|
+
INTERNAL_ERROR = -32603
|
|
27
|
+
|
|
28
|
+
FORBIDDEN_RESTORE_TOOLS: frozenset[str] = frozenset(
|
|
29
|
+
{
|
|
30
|
+
"resume",
|
|
31
|
+
"killswitch_resume",
|
|
32
|
+
"on",
|
|
33
|
+
"killswitch_on",
|
|
34
|
+
"vendor_on",
|
|
35
|
+
"vendor-on",
|
|
36
|
+
"killswitch_vendor_on",
|
|
37
|
+
"scope_on",
|
|
38
|
+
"scope-on",
|
|
39
|
+
"killswitch_scope_on",
|
|
40
|
+
"net_restore",
|
|
41
|
+
"net-restore",
|
|
42
|
+
"killswitch_net_restore",
|
|
43
|
+
"thaw",
|
|
44
|
+
"killswitch_thaw",
|
|
45
|
+
"uninstall_hooks",
|
|
46
|
+
"uninstall-hooks",
|
|
47
|
+
"killswitch_uninstall_hooks",
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
TOOLS: tuple[dict[str, Any], ...] = (
|
|
52
|
+
{
|
|
53
|
+
"name": "killswitch_status",
|
|
54
|
+
"description": (
|
|
55
|
+
"Read-only status of the local agent-killswitch: master panic flag, disabled vendors, "
|
|
56
|
+
"disabled action scopes, state path, and OS process-ancestry caller classification."
|
|
57
|
+
),
|
|
58
|
+
"inputSchema": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"properties": {},
|
|
61
|
+
"additionalProperties": False,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"name": "killswitch_check",
|
|
66
|
+
"description": (
|
|
67
|
+
"Evaluate whether an AI agent vendor and action scope are currently allowed under "
|
|
68
|
+
"local and cached killswitch policy. Returns {allowed, exit_code, reason, ...} without crashing."
|
|
69
|
+
),
|
|
70
|
+
"inputSchema": {
|
|
71
|
+
"type": "object",
|
|
72
|
+
"properties": {
|
|
73
|
+
"vendor": {
|
|
74
|
+
"type": "string",
|
|
75
|
+
"description": (
|
|
76
|
+
"Optional vendor hint (e.g. claude, codex, cursor, antigravity). "
|
|
77
|
+
"Cannot override an OS-detected LLM vendor or spoof owner."
|
|
78
|
+
),
|
|
79
|
+
},
|
|
80
|
+
"action": {
|
|
81
|
+
"type": "string",
|
|
82
|
+
"description": (
|
|
83
|
+
"Optional action scope to check (e.g. deploy, ssh, secrets, git_push, db_write). "
|
|
84
|
+
"Defaults to 'any'."
|
|
85
|
+
),
|
|
86
|
+
},
|
|
87
|
+
"target": {
|
|
88
|
+
"type": "string",
|
|
89
|
+
"description": "Optional target label recorded in audit logs if blocked.",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
"additionalProperties": False,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"name": "killswitch_panic",
|
|
97
|
+
"description": (
|
|
98
|
+
"TIGHTEN-ONLY emergency stop: immediately activates Master Panic to block all AI agents. "
|
|
99
|
+
"Cannot be undone via MCP; restore requires the human owner via interactive CLI or Web UI."
|
|
100
|
+
),
|
|
101
|
+
"inputSchema": {
|
|
102
|
+
"type": "object",
|
|
103
|
+
"properties": {
|
|
104
|
+
"reason": {
|
|
105
|
+
"type": "string",
|
|
106
|
+
"minLength": 1,
|
|
107
|
+
"description": "Required reason explaining why Master Panic is being activated.",
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
"required": ["reason"],
|
|
111
|
+
"additionalProperties": False,
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"name": "killswitch_vendor_off",
|
|
116
|
+
"description": (
|
|
117
|
+
"TIGHTEN-ONLY: immediately disables access for a single AI agent vendor "
|
|
118
|
+
"(e.g. claude, codex, cursor, antigravity, gemini). Re-enabling is owner-only outside MCP."
|
|
119
|
+
),
|
|
120
|
+
"inputSchema": {
|
|
121
|
+
"type": "object",
|
|
122
|
+
"properties": {
|
|
123
|
+
"vendor": {
|
|
124
|
+
"type": "string",
|
|
125
|
+
"minLength": 1,
|
|
126
|
+
"description": "Vendor ID to disable (lowercase alphanumeric, hyphen, underscore).",
|
|
127
|
+
},
|
|
128
|
+
"reason": {
|
|
129
|
+
"type": "string",
|
|
130
|
+
"description": "Optional audit reason for disabling this vendor.",
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
"required": ["vendor"],
|
|
134
|
+
"additionalProperties": False,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"name": "killswitch_scope_off",
|
|
139
|
+
"description": (
|
|
140
|
+
"TIGHTEN-ONLY: immediately disables an action scope (e.g. deploy, ssh, secrets, git_push, db_write) "
|
|
141
|
+
"for all AI agents. Re-enabling is owner-only outside MCP."
|
|
142
|
+
),
|
|
143
|
+
"inputSchema": {
|
|
144
|
+
"type": "object",
|
|
145
|
+
"properties": {
|
|
146
|
+
"scope": {
|
|
147
|
+
"type": "string",
|
|
148
|
+
"minLength": 1,
|
|
149
|
+
"description": "Action scope name to disable (e.g. deploy, ssh, secrets, git_push, db_write).",
|
|
150
|
+
},
|
|
151
|
+
"reason": {
|
|
152
|
+
"type": "string",
|
|
153
|
+
"description": "Optional audit reason for disabling this scope.",
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
"required": ["scope"],
|
|
157
|
+
"additionalProperties": False,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
ALLOWED_TOOL_NAMES: frozenset[str] = frozenset(tool["name"] for tool in TOOLS)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _server_version() -> str:
|
|
166
|
+
from . import __version__
|
|
167
|
+
|
|
168
|
+
return __version__
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _jsonrpc_result(req_id: Any, result: dict[str, Any]) -> dict[str, Any]:
|
|
172
|
+
return {"jsonrpc": JSONRPC_VERSION, "id": req_id, "result": result}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _jsonrpc_error(req_id: Any, code: int, message: str, data: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
176
|
+
err: dict[str, Any] = {"code": code, "message": message}
|
|
177
|
+
if data is not None:
|
|
178
|
+
err["data"] = data
|
|
179
|
+
return {"jsonrpc": JSONRPC_VERSION, "id": req_id, "error": err}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _tool_result(payload: dict[str, Any], *, is_error: bool = False) -> dict[str, Any]:
|
|
183
|
+
return {
|
|
184
|
+
"content": [{"type": "text", "text": json.dumps(payload, ensure_ascii=False)}],
|
|
185
|
+
"isError": is_error,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _is_restore_attempt(tool_name: str) -> bool:
|
|
190
|
+
norm = tool_name.strip().lower()
|
|
191
|
+
if norm in FORBIDDEN_RESTORE_TOOLS:
|
|
192
|
+
return True
|
|
193
|
+
restore_tokens = ("resume", "restore", "thaw", "uninstall", "_on", "-on", "enable")
|
|
194
|
+
return any(tok in norm for tok in restore_tokens)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class MCPServer:
|
|
198
|
+
"""Stateless JSON-RPC 2.0 MCP server exposing read + tighten-only killswitch tools."""
|
|
199
|
+
|
|
200
|
+
def __init__(self, state_override: str | None = None) -> None:
|
|
201
|
+
self.state_override = state_override
|
|
202
|
+
|
|
203
|
+
def _tool_status(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
204
|
+
if arguments:
|
|
205
|
+
return _tool_result(
|
|
206
|
+
{"ok": False, "error": "killswitch_status does not accept arguments"},
|
|
207
|
+
is_error=True,
|
|
208
|
+
)
|
|
209
|
+
path = core.state_path(self.state_override)
|
|
210
|
+
state = core.load_state(path, create_if_missing=False)
|
|
211
|
+
caller = ancestry.detect_vendor()
|
|
212
|
+
ancestors = ancestry.get_process_ancestor_names()
|
|
213
|
+
vendors = state.get("vendors", {})
|
|
214
|
+
scopes = state.get("scopes", {})
|
|
215
|
+
disabled_vendors = sorted(
|
|
216
|
+
name for name, info in vendors.items() if not isinstance(info, dict) or not info.get("enabled", True)
|
|
217
|
+
)
|
|
218
|
+
disabled_scopes = sorted(name for name, enabled in scopes.items() if not enabled)
|
|
219
|
+
return _tool_result(
|
|
220
|
+
{
|
|
221
|
+
"master_panic": bool(state.get("master_panic", False)),
|
|
222
|
+
"disabled_vendors": disabled_vendors,
|
|
223
|
+
"disabled_scopes": disabled_scopes,
|
|
224
|
+
"state_path": str(path),
|
|
225
|
+
"updated_at": state.get("updated_at"),
|
|
226
|
+
"updated_by": state.get("updated_by"),
|
|
227
|
+
"note": state.get("note", ""),
|
|
228
|
+
"caller": caller,
|
|
229
|
+
"ancestors": ancestors,
|
|
230
|
+
"vendors": vendors,
|
|
231
|
+
"scopes": scopes,
|
|
232
|
+
"policy": "tighten-only",
|
|
233
|
+
}
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
def _tool_check(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
237
|
+
vendor_arg = arguments.get("vendor")
|
|
238
|
+
action_arg = arguments.get("action")
|
|
239
|
+
target_arg = arguments.get("target")
|
|
240
|
+
if vendor_arg is not None and not isinstance(vendor_arg, str):
|
|
241
|
+
return _tool_result({"ok": False, "error": "'vendor' must be a string"}, is_error=True)
|
|
242
|
+
if action_arg is not None and not isinstance(action_arg, str):
|
|
243
|
+
return _tool_result({"ok": False, "error": "'action' must be a string"}, is_error=True)
|
|
244
|
+
if target_arg is not None and not isinstance(target_arg, str):
|
|
245
|
+
return _tool_result({"ok": False, "error": "'target' must be a string"}, is_error=True)
|
|
246
|
+
|
|
247
|
+
claimed = vendor_arg.strip().lower() if vendor_arg and vendor_arg.strip() else None
|
|
248
|
+
if claimed == "owner":
|
|
249
|
+
claimed = None
|
|
250
|
+
action = action_arg.strip().lower() if action_arg and action_arg.strip() else "any"
|
|
251
|
+
target = target_arg.strip() if target_arg else ""
|
|
252
|
+
path = core.state_path(self.state_override)
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
effective_vendor = core.resolve_effective_vendor(claimed)
|
|
256
|
+
allowed, reason = core.is_allowed(
|
|
257
|
+
vendor=claimed,
|
|
258
|
+
action=action,
|
|
259
|
+
target=target,
|
|
260
|
+
state_file=path,
|
|
261
|
+
)
|
|
262
|
+
state = core.load_state(path, create_if_missing=False)
|
|
263
|
+
master_panic = bool(state.get("master_panic", False))
|
|
264
|
+
except (OSError, ValueError, RuntimeError) as exc:
|
|
265
|
+
effective_vendor = ancestry.detect_vendor()
|
|
266
|
+
allowed = False
|
|
267
|
+
reason = f"KILLSWITCH ERROR: {type(exc).__name__}: {exc}"
|
|
268
|
+
master_panic = True
|
|
269
|
+
core.log_audit_event(
|
|
270
|
+
"check_error",
|
|
271
|
+
{"action": action, "target": target, "reason": reason, "via": "mcp"},
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
return _tool_result(
|
|
275
|
+
{
|
|
276
|
+
"allowed": allowed,
|
|
277
|
+
"exit_code": core.EXIT_ALLOWED if allowed else core.EXIT_REFUSED,
|
|
278
|
+
"reason": reason,
|
|
279
|
+
"vendor": effective_vendor,
|
|
280
|
+
"action": action,
|
|
281
|
+
"target": target,
|
|
282
|
+
"master_panic": master_panic,
|
|
283
|
+
"state_path": str(path),
|
|
284
|
+
}
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
def _tool_panic(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
288
|
+
reason_raw = arguments.get("reason")
|
|
289
|
+
if not isinstance(reason_raw, str) or not reason_raw.strip():
|
|
290
|
+
return _tool_result(
|
|
291
|
+
{"ok": False, "error": "killswitch_panic requires a non-empty string 'reason'"},
|
|
292
|
+
is_error=True,
|
|
293
|
+
)
|
|
294
|
+
reason = reason_raw.strip()
|
|
295
|
+
path = core.state_path(self.state_override)
|
|
296
|
+
actor = ancestry.detect_vendor()
|
|
297
|
+
|
|
298
|
+
def _apply_panic(state: dict[str, Any]) -> None:
|
|
299
|
+
state["master_panic"] = True
|
|
300
|
+
state["note"] = reason
|
|
301
|
+
|
|
302
|
+
core.mutate_state(path, _apply_panic, actor=f"{actor}-mcp-panic")
|
|
303
|
+
core.log_audit_event("panic_triggered", {"reason": reason, "actor": actor, "via": "mcp"})
|
|
304
|
+
return _tool_result(
|
|
305
|
+
{
|
|
306
|
+
"ok": True,
|
|
307
|
+
"action": "panic",
|
|
308
|
+
"master_panic": True,
|
|
309
|
+
"reason": reason,
|
|
310
|
+
"actor": actor,
|
|
311
|
+
"state_path": str(path),
|
|
312
|
+
"note": (
|
|
313
|
+
"Master Panic activated. MCP is tighten-only; restore requires the human owner "
|
|
314
|
+
"via interactive CLI (`agent-killswitch resume`) or localhost Web UI."
|
|
315
|
+
),
|
|
316
|
+
}
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
def _tool_vendor_off(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
320
|
+
vendor_raw = arguments.get("vendor")
|
|
321
|
+
reason_raw = arguments.get("reason")
|
|
322
|
+
if not isinstance(vendor_raw, str) or not vendor_raw.strip():
|
|
323
|
+
return _tool_result(
|
|
324
|
+
{"ok": False, "error": "killswitch_vendor_off requires a non-empty string 'vendor'"},
|
|
325
|
+
is_error=True,
|
|
326
|
+
)
|
|
327
|
+
if reason_raw is not None and not isinstance(reason_raw, str):
|
|
328
|
+
return _tool_result({"ok": False, "error": "'reason' must be a string"}, is_error=True)
|
|
329
|
+
|
|
330
|
+
vendor = vendor_raw.strip().lower()
|
|
331
|
+
if vendor == "owner" or not core.VENDOR_NAME_RE.fullmatch(vendor):
|
|
332
|
+
return _tool_result({"ok": False, "error": f"Invalid vendor name {vendor!r}"}, is_error=True)
|
|
333
|
+
|
|
334
|
+
actor = ancestry.detect_vendor()
|
|
335
|
+
reason = reason_raw.strip() if reason_raw and reason_raw.strip() else f"Turned OFF via MCP by {actor}"
|
|
336
|
+
path = core.state_path(self.state_override)
|
|
337
|
+
|
|
338
|
+
def _apply_vendor_off(state: dict[str, Any]) -> None:
|
|
339
|
+
vendors = state.setdefault("vendors", {})
|
|
340
|
+
if vendor not in vendors or not isinstance(vendors[vendor], dict):
|
|
341
|
+
vendors[vendor] = {
|
|
342
|
+
"title": vendor.capitalize(),
|
|
343
|
+
"enabled": False,
|
|
344
|
+
"reason": reason,
|
|
345
|
+
"updated_at": core.utc_now(),
|
|
346
|
+
}
|
|
347
|
+
else:
|
|
348
|
+
vendors[vendor]["enabled"] = False
|
|
349
|
+
vendors[vendor]["reason"] = reason
|
|
350
|
+
vendors[vendor]["updated_at"] = core.utc_now()
|
|
351
|
+
|
|
352
|
+
core.mutate_state(path, _apply_vendor_off, actor=f"{actor}-mcp")
|
|
353
|
+
core.log_audit_event("vendor_disable", {"vendor": vendor, "reason": reason, "actor": actor, "via": "mcp"})
|
|
354
|
+
return _tool_result(
|
|
355
|
+
{
|
|
356
|
+
"ok": True,
|
|
357
|
+
"action": "vendor_off",
|
|
358
|
+
"vendor": vendor,
|
|
359
|
+
"enabled": False,
|
|
360
|
+
"reason": reason,
|
|
361
|
+
"actor": actor,
|
|
362
|
+
"state_path": str(path),
|
|
363
|
+
}
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
def _tool_scope_off(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
367
|
+
scope_raw = arguments.get("scope")
|
|
368
|
+
reason_raw = arguments.get("reason")
|
|
369
|
+
if not isinstance(scope_raw, str) or not scope_raw.strip():
|
|
370
|
+
return _tool_result(
|
|
371
|
+
{"ok": False, "error": "killswitch_scope_off requires a non-empty string 'scope'"},
|
|
372
|
+
is_error=True,
|
|
373
|
+
)
|
|
374
|
+
if reason_raw is not None and not isinstance(reason_raw, str):
|
|
375
|
+
return _tool_result({"ok": False, "error": "'reason' must be a string"}, is_error=True)
|
|
376
|
+
|
|
377
|
+
scope = scope_raw.strip().lower()
|
|
378
|
+
if not core.VENDOR_NAME_RE.fullmatch(scope):
|
|
379
|
+
return _tool_result({"ok": False, "error": f"Invalid scope name {scope!r}"}, is_error=True)
|
|
380
|
+
|
|
381
|
+
actor = ancestry.detect_vendor()
|
|
382
|
+
reason = reason_raw.strip() if reason_raw and reason_raw.strip() else f"Scope disabled via MCP by {actor}"
|
|
383
|
+
path = core.state_path(self.state_override)
|
|
384
|
+
|
|
385
|
+
def _apply_scope_off(state: dict[str, Any]) -> None:
|
|
386
|
+
state.setdefault("scopes", {})[scope] = False
|
|
387
|
+
|
|
388
|
+
core.mutate_state(path, _apply_scope_off, actor=f"{actor}-mcp")
|
|
389
|
+
core.log_audit_event(
|
|
390
|
+
"scope_toggle",
|
|
391
|
+
{"scope": scope, "enabled": False, "reason": reason, "actor": actor, "via": "mcp"},
|
|
392
|
+
)
|
|
393
|
+
return _tool_result(
|
|
394
|
+
{
|
|
395
|
+
"ok": True,
|
|
396
|
+
"action": "scope_off",
|
|
397
|
+
"scope": scope,
|
|
398
|
+
"enabled": False,
|
|
399
|
+
"reason": reason,
|
|
400
|
+
"actor": actor,
|
|
401
|
+
"state_path": str(path),
|
|
402
|
+
}
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
def _dispatch_tool(self, name: str, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
406
|
+
if name == "killswitch_status":
|
|
407
|
+
return self._tool_status(arguments)
|
|
408
|
+
if name == "killswitch_check":
|
|
409
|
+
return self._tool_check(arguments)
|
|
410
|
+
if name == "killswitch_panic":
|
|
411
|
+
return self._tool_panic(arguments)
|
|
412
|
+
if name == "killswitch_vendor_off":
|
|
413
|
+
return self._tool_vendor_off(arguments)
|
|
414
|
+
if name == "killswitch_scope_off":
|
|
415
|
+
return self._tool_scope_off(arguments)
|
|
416
|
+
|
|
417
|
+
actor = ancestry.detect_vendor()
|
|
418
|
+
core.log_audit_event("mcp_tool_refused", {"tool": name, "actor": actor, "policy": "tighten-only"})
|
|
419
|
+
if _is_restore_attempt(name):
|
|
420
|
+
return _tool_result(
|
|
421
|
+
{
|
|
422
|
+
"ok": False,
|
|
423
|
+
"policy": "tighten-only",
|
|
424
|
+
"error": (
|
|
425
|
+
f"REFUSED: tool {name!r} is disabled under the tighten-only MCP security policy. "
|
|
426
|
+
"Restore/unblock actions (resume, on, scope-on, net-restore, thaw, uninstall-hooks) "
|
|
427
|
+
"are owner-only via interactive CLI or the localhost Web UI."
|
|
428
|
+
),
|
|
429
|
+
},
|
|
430
|
+
is_error=True,
|
|
431
|
+
)
|
|
432
|
+
return _tool_result(
|
|
433
|
+
{
|
|
434
|
+
"ok": False,
|
|
435
|
+
"policy": "tighten-only",
|
|
436
|
+
"error": (
|
|
437
|
+
f"REFUSED: unknown tool {name!r}. This MCP server enforces a tighten-only policy "
|
|
438
|
+
f"and only exposes: {', '.join(sorted(ALLOWED_TOOL_NAMES))}."
|
|
439
|
+
),
|
|
440
|
+
},
|
|
441
|
+
is_error=True,
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
def handle_request(self, request: dict[str, Any]) -> dict[str, Any] | None:
|
|
445
|
+
has_id = "id" in request
|
|
446
|
+
req_id = request.get("id")
|
|
447
|
+
method = request.get("method")
|
|
448
|
+
if not isinstance(method, str) or not method:
|
|
449
|
+
return _jsonrpc_error(req_id if has_id else None, INVALID_REQUEST, "Invalid Request: missing method")
|
|
450
|
+
|
|
451
|
+
params = request.get("params")
|
|
452
|
+
if params is None:
|
|
453
|
+
params = {}
|
|
454
|
+
elif not isinstance(params, dict):
|
|
455
|
+
if not has_id:
|
|
456
|
+
return None
|
|
457
|
+
return _jsonrpc_error(req_id, INVALID_PARAMS, "Invalid params: expected JSON object")
|
|
458
|
+
|
|
459
|
+
if method == "notifications/initialized" or method.startswith("notifications/"):
|
|
460
|
+
if not has_id:
|
|
461
|
+
return None
|
|
462
|
+
return _jsonrpc_result(req_id, {})
|
|
463
|
+
|
|
464
|
+
if method == "initialize":
|
|
465
|
+
raw_proto = params.get("protocolVersion")
|
|
466
|
+
proto = raw_proto.strip() if isinstance(raw_proto, str) and raw_proto.strip() else DEFAULT_PROTOCOL_VERSION
|
|
467
|
+
result = {
|
|
468
|
+
"protocolVersion": proto,
|
|
469
|
+
"capabilities": {"tools": {}},
|
|
470
|
+
"serverInfo": {"name": SERVER_NAME, "version": _server_version()},
|
|
471
|
+
}
|
|
472
|
+
return _jsonrpc_result(req_id, result) if has_id else None
|
|
473
|
+
|
|
474
|
+
if method == "ping":
|
|
475
|
+
return _jsonrpc_result(req_id, {}) if has_id else None
|
|
476
|
+
|
|
477
|
+
if method == "tools/list":
|
|
478
|
+
return _jsonrpc_result(req_id, {"tools": list(TOOLS)}) if has_id else None
|
|
479
|
+
|
|
480
|
+
if method == "tools/call":
|
|
481
|
+
tool_name = params.get("name")
|
|
482
|
+
if not isinstance(tool_name, str) or not tool_name.strip():
|
|
483
|
+
if not has_id:
|
|
484
|
+
return None
|
|
485
|
+
return _jsonrpc_error(req_id, INVALID_PARAMS, "Invalid params: tools/call requires string 'name'")
|
|
486
|
+
tool_args = params.get("arguments")
|
|
487
|
+
if tool_args is None:
|
|
488
|
+
tool_args = {}
|
|
489
|
+
elif not isinstance(tool_args, dict):
|
|
490
|
+
if not has_id:
|
|
491
|
+
return None
|
|
492
|
+
return _jsonrpc_error(req_id, INVALID_PARAMS, "Invalid params: 'arguments' must be an object")
|
|
493
|
+
try:
|
|
494
|
+
tool_res = self._dispatch_tool(tool_name.strip(), tool_args)
|
|
495
|
+
except (OSError, ValueError, RuntimeError, TimeoutError) as exc:
|
|
496
|
+
tool_res = _tool_result(
|
|
497
|
+
{"ok": False, "error": f"{type(exc).__name__}: {exc}"},
|
|
498
|
+
is_error=True,
|
|
499
|
+
)
|
|
500
|
+
return _jsonrpc_result(req_id, tool_res) if has_id else None
|
|
501
|
+
|
|
502
|
+
if not has_id:
|
|
503
|
+
return None
|
|
504
|
+
return _jsonrpc_error(req_id, METHOD_NOT_FOUND, f"Method not found: {method}")
|
|
505
|
+
|
|
506
|
+
def handle_line(self, raw_line: str) -> dict[str, Any] | None:
|
|
507
|
+
text = raw_line.strip()
|
|
508
|
+
if not text:
|
|
509
|
+
return None
|
|
510
|
+
try:
|
|
511
|
+
decoded = json.loads(text)
|
|
512
|
+
except json.JSONDecodeError as exc:
|
|
513
|
+
return _jsonrpc_error(None, PARSE_ERROR, f"Parse error: {exc.msg}")
|
|
514
|
+
if not isinstance(decoded, dict):
|
|
515
|
+
return _jsonrpc_error(None, INVALID_REQUEST, "Invalid Request: expected JSON object")
|
|
516
|
+
return self.handle_request(decoded)
|
|
517
|
+
|
|
518
|
+
def serve_stdio(self, stdin: TextIO | None = None, stdout: TextIO | None = None) -> int:
|
|
519
|
+
in_stream = stdin if stdin is not None else sys.stdin
|
|
520
|
+
out_stream = stdout if stdout is not None else sys.stdout
|
|
521
|
+
for raw_line in in_stream:
|
|
522
|
+
response = self.handle_line(raw_line)
|
|
523
|
+
if response is not None:
|
|
524
|
+
out_stream.write(json.dumps(response, ensure_ascii=False, separators=(",", ":")) + "\n")
|
|
525
|
+
out_stream.flush()
|
|
526
|
+
return 0
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def serve_stdio(
|
|
530
|
+
state_override: str | None = None,
|
|
531
|
+
*,
|
|
532
|
+
stdin: TextIO | None = None,
|
|
533
|
+
stdout: TextIO | None = None,
|
|
534
|
+
) -> int:
|
|
535
|
+
"""Runs the tighten-only MCP server over newline-delimited JSON-RPC 2.0 stdio."""
|
|
536
|
+
server = MCPServer(state_override=state_override)
|
|
537
|
+
return server.serve_stdio(stdin=stdin, stdout=stdout)
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
def main(argv: list[str] | None = None) -> int:
|
|
541
|
+
parser = argparse.ArgumentParser(
|
|
542
|
+
prog="agent-killswitch-mcp",
|
|
543
|
+
description="Zero-dependency tighten-only MCP server (JSON-RPC 2.0 over stdio) for agent-killswitch.",
|
|
544
|
+
)
|
|
545
|
+
parser.add_argument("--state", help="Override state JSON path (default: ~/.agent-killswitch/state.json)")
|
|
546
|
+
args = parser.parse_args(argv)
|
|
547
|
+
return serve_stdio(state_override=args.state)
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
if __name__ == "__main__":
|
|
551
|
+
sys.exit(main())
|