@yahyahammad/venastine 0.1.0

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.
Files changed (143) hide show
  1. package/.env.example +14 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +14 -0
  4. package/PRIVACY.md +66 -0
  5. package/README.md +878 -0
  6. package/SECURITY.md +88 -0
  7. package/THIRD_PARTY_NOTICES.md +100 -0
  8. package/agents/builtin/compactor.md +75 -0
  9. package/agents/builtin/explore.md +77 -0
  10. package/agents/builtin/grill-me.md +27 -0
  11. package/agents/builtin/initializer.md +94 -0
  12. package/agents/builtin/pipeline-reviewer.md +100 -0
  13. package/agents/builtin/plan.md +88 -0
  14. package/agents/builtin/review.md +74 -0
  15. package/agents/manager.py +195 -0
  16. package/agents/subagent_tool.py +215 -0
  17. package/agents/tui_commands.py +136 -0
  18. package/bin/venastine.mjs +382 -0
  19. package/config.py +1198 -0
  20. package/core/__init__.py +0 -0
  21. package/core/approval.py +115 -0
  22. package/core/client.py +1548 -0
  23. package/core/compaction.py +959 -0
  24. package/core/config_loader.py +1563 -0
  25. package/core/events.py +61 -0
  26. package/core/interaction.py +328 -0
  27. package/core/loop.py +1353 -0
  28. package/core/memory.py +370 -0
  29. package/core/model_windows.py +175 -0
  30. package/core/pipeline_models.py +176 -0
  31. package/core/reasoning/__init__.py +0 -0
  32. package/core/reasoning/authorization.py +130 -0
  33. package/core/reasoning/base.py +219 -0
  34. package/core/reasoning/confidence_scoring.py +468 -0
  35. package/core/reasoning/events.py +149 -0
  36. package/core/reasoning/json_retry.py +219 -0
  37. package/core/reasoning/orchestrator.py +1382 -0
  38. package/core/reasoning/output_writer.py +286 -0
  39. package/core/reasoning/payload_validation.py +398 -0
  40. package/core/reasoning/pipeline_storage.py +361 -0
  41. package/core/reasoning/review.py +615 -0
  42. package/core/reasoning/scholar.py +434 -0
  43. package/core/reasoning/source_corpus.py +327 -0
  44. package/core/reasoning/source_scoring.py +855 -0
  45. package/core/replay.py +167 -0
  46. package/core/session.py +153 -0
  47. package/core/workspace_trust.py +207 -0
  48. package/credentials.py +180 -0
  49. package/database.py +252 -0
  50. package/env_secrets.py +77 -0
  51. package/json_store.py +123 -0
  52. package/logging_setup.py +185 -0
  53. package/main.py +1959 -0
  54. package/mcp_client/__init__.py +15 -0
  55. package/mcp_client/client.py +499 -0
  56. package/mcp_client/config.py +379 -0
  57. package/mcp_client/registration.py +182 -0
  58. package/memories/manager.py +132 -0
  59. package/memories/tui_commands.py +232 -0
  60. package/package.json +73 -0
  61. package/project_init/config_files.py +278 -0
  62. package/project_init/doc_sets.py +330 -0
  63. package/project_init/generator.py +564 -0
  64. package/project_init/manifest.py +406 -0
  65. package/project_init/tui_commands.py +144 -0
  66. package/prompts/__init__.py +0 -0
  67. package/prompts/annotate.md +14 -0
  68. package/prompts/assumption_audit.md +27 -0
  69. package/prompts/claim_extraction.md +34 -0
  70. package/prompts/completeness.md +18 -0
  71. package/prompts/confidence_tiers.md +23 -0
  72. package/prompts/critic_pass.md +24 -0
  73. package/prompts/final_synthesis.md +13 -0
  74. package/prompts/initial_generation.md +14 -0
  75. package/prompts/preliminary_plan.md +23 -0
  76. package/prompts/revalidate.md +25 -0
  77. package/prompts/revise.md +15 -0
  78. package/prompts/source_grounding.md +66 -0
  79. package/prompts/system_prompts.py +262 -0
  80. package/prompts/universal_system_prompt +7 -0
  81. package/prompts/untrusted_content +1 -0
  82. package/providers.json.example +92 -0
  83. package/pyproject.toml +100 -0
  84. package/requirements.txt +72 -0
  85. package/safety/__init__.py +0 -0
  86. package/safety/policy_enforcement.py +682 -0
  87. package/security/__init__.py +0 -0
  88. package/security/capability.py +177 -0
  89. package/security/permissions.py +190 -0
  90. package/security/posture.py +302 -0
  91. package/security/protected_paths.py +309 -0
  92. package/security/sandbox.py +985 -0
  93. package/skills/builtin/crypto/cryptography-verification.md +66 -0
  94. package/skills/builtin/math/formal-specification.md +67 -0
  95. package/skills/builtin/math/numerical-methods.md +66 -0
  96. package/skills/builtin/math/proof-writing.md +61 -0
  97. package/skills/builtin/research/experiment-design.md +62 -0
  98. package/skills/builtin/research/literature-review.md +65 -0
  99. package/skills/builtin/research/reproducibility.md +55 -0
  100. package/skills/builtin/research/source-evaluation.md +70 -0
  101. package/skills/builtin/research/statistical-inference.md +63 -0
  102. package/skills/builtin/research/technical-writing.md +59 -0
  103. package/skills/builtin/security/cybersecurity-research.md +63 -0
  104. package/skills/builtin/software/code-review.md +61 -0
  105. package/skills/builtin/software/debugging.md +61 -0
  106. package/skills/builtin/software/test-design.md +67 -0
  107. package/skills/manager.py +138 -0
  108. package/skills/tui_commands.py +111 -0
  109. package/storage.py +1005 -0
  110. package/tools/base.py +305 -0
  111. package/tools/builtin/__init__.py +0 -0
  112. package/tools/builtin/_math_common.py +377 -0
  113. package/tools/builtin/_net_common.py +61 -0
  114. package/tools/builtin/arxiv.py +223 -0
  115. package/tools/builtin/ask_user.py +213 -0
  116. package/tools/builtin/discrete_math.py +134 -0
  117. package/tools/builtin/fetch_url.py +153 -0
  118. package/tools/builtin/file_ops.py +412 -0
  119. package/tools/builtin/geometry.py +148 -0
  120. package/tools/builtin/get_time.py +20 -0
  121. package/tools/builtin/linear_algebra.py +155 -0
  122. package/tools/builtin/load_skill.py +66 -0
  123. package/tools/builtin/logic.py +96 -0
  124. package/tools/builtin/pin.py +193 -0
  125. package/tools/builtin/probability_stats.py +228 -0
  126. package/tools/builtin/project_docs.py +405 -0
  127. package/tools/builtin/remember.py +158 -0
  128. package/tools/builtin/shell.py +461 -0
  129. package/tools/builtin/symbolic_math.py +119 -0
  130. package/tools/builtin/todo.py +147 -0
  131. package/tools/builtin/web_search.py +151 -0
  132. package/tools/context.py +136 -0
  133. package/tools/isolation.py +314 -0
  134. package/tools/registry.py +897 -0
  135. package/tui/app.py +3423 -0
  136. package/tui/app.tcss +458 -0
  137. package/tui/commands.py +61 -0
  138. package/tui/diffs.py +265 -0
  139. package/tui/preferences.py +182 -0
  140. package/tui/ravens.py +146 -0
  141. package/tui/screens.py +943 -0
  142. package/tui/themes.py +366 -0
  143. package/tui/widgets.py +1239 -0
@@ -0,0 +1,379 @@
1
+ """
2
+ mcp_client/config.py
3
+
4
+ Discovery and merge of `mcp.json`, plus the "seen this user-level server
5
+ before" store. ROADMAP_v2 §17.
6
+
7
+ Two locations, both resolved at CALL time (not import time) so tests can
8
+ redirect them and a moved config dir is picked up without a restart --
9
+ the same rule core/config_loader.py and core/workspace_trust.py follow:
10
+
11
+ user ~/.config/venastine/mcp.json -- you authored it
12
+ project <project>/.venastine/mcp.json -- trust-gated (D17)
13
+
14
+ THE PROJECT PATH IS `.venastine/mcp.json`, NOT `<project>/mcp.json`.
15
+ §17/D11 put it at the project root to match Claude Code's convention, but
16
+ workspace_trust hashes only `.venastine/`, and is_trusted() returns True
17
+ outright when that directory is absent. A root-level mcp.json would
18
+ therefore have been ungated and its edits would never have re-prompted --
19
+ in a file whose whole purpose is naming a local command to execute. Under
20
+ `.venastine/` the existing hash, trust prompt and file listing cover it
21
+ with no change to workspace_trust.py at all.
22
+
23
+ Validation is deliberately by CONNECTION, not by schema (§17 decision G):
24
+ unknown keys are ignored so a config pasted from Claude Desktop or Cursor
25
+ works, and an entry we cannot make sense of becomes that one server's
26
+ named connect failure rather than a startup error for everything else.
27
+ """
28
+
29
+ import hashlib
30
+ import json
31
+ import logging
32
+ import os
33
+ from dataclasses import dataclass, field
34
+ from typing import Any, Optional
35
+
36
+ from json_store import write_json_atomic
37
+
38
+ logger = logging.getLogger(__name__)
39
+
40
+ # Keys this harness actually reads. Anything else is ignored, not an
41
+ # error -- see the module docstring. Kept as data so the DEBUG log can
42
+ # name exactly what it skipped.
43
+ _KNOWN_SERVER_KEYS = frozenset({
44
+ "command", "args", "env", "cwd", # stdio
45
+ "type", "url", "headers", # http
46
+ "autoApprove", "auto_approve", # our extension (§17 D)
47
+ "disabled",
48
+ })
49
+
50
+ # Declared `type` values that mean streamable HTTP. `sse` is NOT one of
51
+ # them (#62): D4 promises SSE, the SDK v2 ships sse_client for it, and
52
+ # aliasing it here is how a server that speaks only SSE got offered a
53
+ # streamable handshake it could not answer.
54
+ _STREAMABLE_TYPES = frozenset({"http", "https", "streamable-http", "streamablehttp"})
55
+ _SSE_TYPES = frozenset({"sse"})
56
+
57
+
58
+ @dataclass
59
+ class ServerConfig:
60
+ """One entry from an mcp.json `mcpServers` object."""
61
+ name: str
62
+ tier: str # "user" | "project"
63
+ path: str # the file it came from
64
+ transport: str # "stdio" | "http" | "sse" | "unknown"
65
+ command: Optional[str] = None
66
+ args: list = field(default_factory=list)
67
+ env: dict = field(default_factory=dict)
68
+ cwd: Optional[str] = None
69
+ url: Optional[str] = None
70
+ headers: dict = field(default_factory=dict)
71
+ auto_approve: bool = False
72
+ disabled: bool = False
73
+ raw: dict = field(default_factory=dict)
74
+ error: Optional[str] = None # why transport == "unknown"
75
+
76
+ def describe(self) -> str:
77
+ """One line naming what this server will actually DO -- shown in
78
+ the trust prompt and the first-run warning, where 'informed' has
79
+ to mean seeing the command, not the server's nickname.
80
+
81
+ #60/F1: the fields that change the security posture ride along.
82
+ `autoApprove` decides whether any later prompt ever exists, so a
83
+ one-`y` acknowledgement that never named it consented blind to
84
+ unprompted execution in every future session including all ten
85
+ headless research passes. Env is disclosed by KEY NAME only --
86
+ printing values would put a credential on the terminal, which is
87
+ a different defect. The digest is computed from the raw entry
88
+ (entry_digest), never from this text, so improving the disclosure
89
+ cannot by itself re-ask an acknowledged server; F3's store bump
90
+ is what forces the one re-consent pass.
91
+
92
+ VENASTINE_MCP_ACK_FULL=1 (F2) appends the entry verbatim, for
93
+ debugging and audits. Read at call time, like every other state
94
+ path in this project: it is operator authority, deliberately not
95
+ a settings.json key, because settings.json's precedence lets a
96
+ project tier style a consent screen this flag renders.
97
+ """
98
+ if self.transport == "stdio":
99
+ argv = " ".join([self.command or ""] + [str(a) for a in self.args])
100
+ text = f"{self.name} [{self.tier}] runs: {argv.strip()}"
101
+ elif self.transport == "http":
102
+ text = f"{self.name} [{self.tier}] connects to: {self.url}"
103
+ elif self.transport == "sse":
104
+ text = f"{self.name} [{self.tier}] connects via SSE to: {self.url}"
105
+ else:
106
+ return f"{self.name} [{self.tier}] UNUSABLE: {self.error}"
107
+
108
+ notes = []
109
+ if self.auto_approve:
110
+ notes.append("AUTO-APPROVED: its tools will run without asking")
111
+ if self.cwd:
112
+ notes.append(f"working directory: {self.cwd}")
113
+ if self.disabled:
114
+ notes.append("disabled")
115
+ if self.env:
116
+ notes.append("env keys: " + ", ".join(sorted(self.env)))
117
+ if self.headers:
118
+ # Same class of surface as env keys, and MORE live since M19
119
+ # stopped discarding them: Authorization is where an http/sse
120
+ # server's credential actually rides (#60 owner follow-up).
121
+ notes.append("header keys: " + ", ".join(sorted(self.headers)))
122
+ lines = [text] + [f" {n}" for n in notes]
123
+
124
+ if os.environ.get("VENASTINE_MCP_ACK_FULL") == "1":
125
+ rendered = json.dumps(self.raw, indent=2, sort_keys=True,
126
+ default=str)
127
+ lines.append(" full entry:")
128
+ lines.extend(f" {l}" for l in rendered.splitlines())
129
+ return "\n".join(lines)
130
+
131
+
132
+ def user_config_path() -> str:
133
+ return os.path.expanduser("~/.config/venastine/mcp.json")
134
+
135
+
136
+ def project_config_path(project_path: str) -> str:
137
+ return os.path.join(os.path.realpath(project_path), ".venastine", "mcp.json")
138
+
139
+
140
+ def _flag(server: str, key: str, value: Any) -> bool:
141
+ """A JSON boolean, or False with a warning.
142
+
143
+ NOT bool(value): bool("false") is True, so `"autoApprove": "false"`
144
+ -- a plausible hand edit, and a shape other MCP hosts tolerate --
145
+ would turn the D28 approval gate OFF on a server the user was trying
146
+ to keep gated. Failing closed is the only safe direction for both
147
+ flags: an unexpectedly-gated server prompts, an unexpectedly-enabled
148
+ one runs third-party code unannounced.
149
+ """
150
+ if isinstance(value, bool):
151
+ return value
152
+ logger.warning(
153
+ "mcp.json server %r: %s must be true or false, got %r; using false.",
154
+ server, key, value)
155
+ return False
156
+
157
+
158
+ def _parse_entry(name: str, entry: Any, tier: str, path: str) -> ServerConfig:
159
+ """Turns one raw JSON value into a ServerConfig. Never raises: a bad
160
+ entry is returned with transport == 'unknown' and an error string, so
161
+ one malformed server cannot stop the others from connecting."""
162
+ if not isinstance(entry, dict):
163
+ return ServerConfig(name=name, tier=tier, path=path, transport="unknown",
164
+ error=f"expected an object, got {type(entry).__name__}")
165
+
166
+ unknown = set(entry) - _KNOWN_SERVER_KEYS
167
+ if unknown:
168
+ # DEBUG, not WARNING: configs shared with other MCP clients carry
169
+ # keys we don't implement, and making that noisy on every startup
170
+ # trains people to ignore the log.
171
+ logger.debug("mcp.json server %r: ignoring unrecognized keys %s",
172
+ name, sorted(unknown))
173
+
174
+ cfg = ServerConfig(
175
+ name=name, tier=tier, path=path, transport="unknown",
176
+ env=entry.get("env") or {},
177
+ cwd=entry.get("cwd"),
178
+ headers=entry.get("headers") or {},
179
+ # accept either spelling; ours is camelCase to match the file's
180
+ # existing style, but a snake_case one shouldn't silently no-op
181
+ auto_approve=_flag(
182
+ name, "autoApprove",
183
+ entry.get("autoApprove", entry.get("auto_approve", False))),
184
+ disabled=_flag(name, "disabled", entry.get("disabled", False)),
185
+ raw=entry,
186
+ )
187
+
188
+ declared = str(entry.get("type") or "").lower()
189
+ if entry.get("command"):
190
+ raw_args = entry.get("args") or []
191
+ if not isinstance(raw_args, list):
192
+ # list("--verbose") is ['-', '-', 'v', ...]. A natural hand
193
+ # edit therefore became a per-character argv and failed at
194
+ # connect with an opaque error naming none of the cause,
195
+ # defeating this module's rule that a bad entry becomes that
196
+ # one server's NAMED failure.
197
+ cfg.error = "'args' must be an array of strings"
198
+ return cfg
199
+ cfg.transport = "stdio"
200
+ cfg.command = str(entry["command"])
201
+ cfg.args = list(raw_args)
202
+ # SSE only by EXPLICIT declaration (F5): a bare "url" keeps meaning
203
+ # streamable HTTP, because that is what every config written before
204
+ # this branch existed assumed, and URL sniffing would be magic. The
205
+ # aliases below stay streamable; an unsupported declared type still
206
+ # refuses rather than falling through to HTTP (#61's M7).
207
+ elif entry.get("url") and declared in _SSE_TYPES:
208
+ cfg.transport = "sse"
209
+ cfg.url = str(entry["url"])
210
+ elif entry.get("url") and (not declared or declared in _STREAMABLE_TYPES):
211
+ cfg.transport = "http"
212
+ cfg.url = str(entry["url"])
213
+ elif entry.get("url"):
214
+ cfg.error = f"unsupported transport type {declared!r}"
215
+ else:
216
+ cfg.error = "no 'command' (stdio) and no 'url' (http)"
217
+ return cfg
218
+
219
+
220
+ def _read_file(path: str, tier: str) -> dict:
221
+ """Reads one mcp.json. A missing file is empty, not an error. A
222
+ corrupt one is reported and treated as empty -- consistent with G:
223
+ it must not take down a session over a file the user may not have
224
+ written, and the message names the file."""
225
+ if not os.path.exists(path):
226
+ return {}
227
+ try:
228
+ # utf-8-sig: mcp.json is commonly copied between hosts and hand
229
+ # edited, so a BOM is realistic and would otherwise be reported as
230
+ # invalid JSON at character 0.
231
+ with open(path, "r", encoding="utf-8-sig") as f:
232
+ data = json.load(f)
233
+ except (OSError, ValueError) as e:
234
+ logger.warning("Could not read %s-level mcp.json at %s: %s", tier, path, e)
235
+ return {}
236
+
237
+ servers = data.get("mcpServers") if isinstance(data, dict) else None
238
+ if servers is None and isinstance(data, dict):
239
+ # Tolerate a bare {"name": {...}} mapping; some tools write it.
240
+ servers = {k: v for k, v in data.items() if isinstance(v, dict)}
241
+ if not isinstance(servers, dict):
242
+ logger.warning("%s-level mcp.json at %s has no 'mcpServers' object; "
243
+ "ignoring it.", tier, path)
244
+ return {}
245
+
246
+ return {name: _parse_entry(name, entry, tier, path)
247
+ for name, entry in servers.items()}
248
+
249
+
250
+ def load_server_configs(project_path: str, trusted: bool) -> dict:
251
+ """All configured servers, keyed by name.
252
+
253
+ USER BEATS PROJECT on a name collision (§17 decision H), which is the
254
+ OPPOSITE of a plain "more specific wins" rule and deliberately so:
255
+ the user tier is what you authored, the project tier is what arrived
256
+ with a repo you cloned. A shadowed agent changes a prompt; a shadowed
257
+ MCP server changes which local command runs under a name you already
258
+ trust. core/config_loader.py's tier order was changed to match, so
259
+ agents, skills and MCP servers now all resolve the same way.
260
+
261
+ Non-colliding servers from BOTH tiers load -- precedence only ever
262
+ breaks same-name ties.
263
+
264
+ Project entries are omitted entirely when `trusted` is False. Absent,
265
+ not loaded-and-disabled: untrusted content should not be reachable by
266
+ a later bug (§14's rule, applied here).
267
+ """
268
+ merged: dict = {}
269
+
270
+ for cfg in _read_file(user_config_path(), "user").values():
271
+ merged[cfg.name] = cfg
272
+
273
+ if trusted:
274
+ for cfg in _read_file(project_config_path(project_path), "project").values():
275
+ if cfg.name in merged:
276
+ logger.warning(
277
+ "project-level MCP server %r at %s is shadowed by the "
278
+ "user-level definition at %s, which wins.",
279
+ cfg.name, cfg.path, merged[cfg.name].path,
280
+ )
281
+ continue
282
+ merged[cfg.name] = cfg
283
+
284
+ return merged
285
+
286
+
287
+ # --- first-run acknowledgement for user-level servers (§17 decision I) ----
288
+ #
289
+ # Project-level servers are covered by D17's trust prompt. User-level ones
290
+ # have no gate at all, on the assumption you authored them -- which is
291
+ # exactly wrong when an installer or another tool appended to the file.
292
+ # Same content-hash keying as workspace_trust, so an edited command
293
+ # re-asks rather than inheriting an old acknowledgement.
294
+
295
+ def known_servers_path() -> str:
296
+ return os.path.expanduser("~/.config/venastine/known_mcp_servers.json")
297
+
298
+
299
+ def entry_digest(cfg: ServerConfig) -> str:
300
+ """Hash of what the entry will DO. sort_keys so key order in the file
301
+ can't produce a different digest for identical configuration."""
302
+ return hashlib.sha256(
303
+ json.dumps(cfg.raw, sort_keys=True, default=str).encode("utf-8")
304
+ ).hexdigest()
305
+
306
+
307
+ # v2 (#60/F3): the acknowledgement prompt used to omit autoApprove -- the
308
+ # one field that decides whether any future prompt exists -- so consents
309
+ # recorded under v1 were given blind. The store carries a version; a
310
+ # legacy store's entries are treated as UNKNOWN (one re-ask each, under
311
+ # the improved disclosure) and a blind consent never survives the bump.
312
+ KNOWN_STORE_VERSION = 2
313
+
314
+
315
+ def _read_store() -> tuple:
316
+ """(name -> digest mapping, is_legacy). A missing file is empty and
317
+ current. An unreadable one fails CLOSED as empty-and-current: every
318
+ server then looks new and gets re-confirmed, which is the safe
319
+ direction. A readable store in the OLD flat shape is NOT corrupt --
320
+ it is valid v1, and comes back marked legacy."""
321
+ path = known_servers_path()
322
+ if not os.path.exists(path):
323
+ return {}, False
324
+ try:
325
+ with open(path, "r", encoding="utf-8-sig") as f:
326
+ data = json.load(f)
327
+ except (OSError, ValueError):
328
+ logger.warning("Could not read %s; re-confirming every user-level "
329
+ "MCP server.", path)
330
+ return {}, False
331
+ if not isinstance(data, dict):
332
+ logger.warning("%s is not an object; re-confirming every "
333
+ "user-level MCP server.", path)
334
+ return {}, False
335
+ if data.get("version") == KNOWN_STORE_VERSION \
336
+ and isinstance(data.get("servers"), dict):
337
+ return dict(data["servers"]), False
338
+ return {k: v for k, v in data.items()
339
+ if isinstance(k, str)}, True
340
+
341
+
342
+ def is_known(cfg: ServerConfig) -> bool:
343
+ """Whether this exact server entry has already been acknowledged.
344
+
345
+ Reads the store itself rather than taking one. It used to accept a
346
+ pre-read `store=` for callers batching several checks, and no caller
347
+ ever passed one -- so the branch was dead while the parameter went on
348
+ advertising an optimisation nobody could see was unused.
349
+ """
350
+ servers, legacy = _read_store()
351
+ # F3: a v1 consent was given under a prompt that never named the
352
+ # field it most needed to name. It does not count.
353
+ if legacy:
354
+ return False
355
+ return servers.get(cfg.name) == entry_digest(cfg)
356
+
357
+
358
+ def remember_server(cfg: ServerConfig) -> None:
359
+ path = known_servers_path()
360
+ servers, legacy = _read_store()
361
+ if legacy:
362
+ # Carrying the old digests forward would silently convert blind
363
+ # consents into disclosed ones for servers this session never
364
+ # re-asked. They start over; answering y here records THIS server
365
+ # only, and the others keep asking until they are answered.
366
+ servers = {}
367
+ servers[cfg.name] = entry_digest(cfg)
368
+ # ATOMIC since batch 45, and this was the odd one out. Its three
369
+ # sibling stores each wrote through a temp file and os.replace, and
370
+ # each spent four to eight docstring lines on why -- a
371
+ # truncate-then-write leaves the file empty for the length of the
372
+ # write and permanently damaged if the process dies inside it. This
373
+ # one used a bare open(path, "w") and said nothing, for a file that
374
+ # records CONSENT: the same class of data as the trust store, which
375
+ # is the sibling that argues hardest for the guarantee. Sharing the
376
+ # write is what stops a convention four comments long from having a
377
+ # fourth copy that does not follow it.
378
+ write_json_atomic(path, {"version": KNOWN_STORE_VERSION,
379
+ "servers": servers})
@@ -0,0 +1,182 @@
1
+ """
2
+ mcp_client/registration.py
3
+
4
+ Turns connected MCP servers' tools into ToolSpecs and puts them in the
5
+ registry at runtime (D15). ROADMAP_v2 §17.
6
+
7
+ Naming is `mcp__<server>__<tool>`, which is what makes the rest of the
8
+ system work without special cases:
9
+ * security.permissions._default_for_unknown_tool() allows the prefix,
10
+ * _default_for_unknown_approval() requires approval for it (§17 D),
11
+ * assert_permissions_declared() exempts it (D24),
12
+ * tui/ravens.py maps unknown tool names to a generic working state, so
13
+ MCP tools animate with no table edit.
14
+
15
+ Tool SCHEMAS are authored in Anthropic's native shape everywhere in this
16
+ project and translated per-provider at the boundary in core/client.py.
17
+ MCP's Tool is already almost that shape, so this is a rename rather than
18
+ a translation -- but it is still done HERE, once, rather than teaching
19
+ core/client.py about MCP types.
20
+ """
21
+
22
+ import logging
23
+
24
+ from security import permissions
25
+ from tools.base import BUDGET_IO, GRANT_ANYWHERE, ToolSpec
26
+
27
+ logger = logging.getLogger(__name__)
28
+
29
+ NAME_PREFIX = "mcp__"
30
+
31
+ # server name -> the exact tool names registered for it.
32
+ #
33
+ # THE PREFIX IS NOT A PARSER. `mcp__<server>__<tool>` cannot be taken
34
+ # apart again: neither `__` in a server name nor `__` in a tool name is
35
+ # illegal anywhere, and both occur. Two consequences, both silent:
36
+ #
37
+ # * teardown by prefix over-matches. Disconnecting server "fs" scans for
38
+ # "mcp__fs__", which also matches every tool of a still-connected
39
+ # server named "fs__legacy" -- unregistering live tools and dropping
40
+ # their approval defaults.
41
+ # * names collide. tool_name_for("a", "b__c") and
42
+ # tool_name_for("a__b", "c") are the same string, so registry.register
43
+ # silently overwrites: a tool the user believes is approval-gated on
44
+ # server `a` runs server `a__b`'s handler under a__b's autoApprove.
45
+ #
46
+ # A trusted project's .venastine/mcp.json can choose the colliding server
47
+ # name -- the D29 impersonation threat, one separator away.
48
+ #
49
+ # Recording what was actually registered fixes both without rejecting any
50
+ # mcp.json content, which decision G rules out: validation here is by
51
+ # CONNECTING, not by schema.
52
+ _registered: dict = {}
53
+
54
+
55
+ def tool_name_for(server_name: str, tool_name: str) -> str:
56
+ return f"{NAME_PREFIX}{server_name}__{tool_name}"
57
+
58
+
59
+ def schema_for(server_name: str, tool) -> dict:
60
+ """MCP Tool -> the Anthropic-native schema shape every TOOL_SCHEMA in
61
+ tools/builtin/ uses.
62
+
63
+ v2 spells these snake_case (.input_schema); v1 used .inputSchema. The
64
+ getattr fallback covers both rather than breaking on a pin move --
65
+ a stray empty schema here would be advertised to the model as a tool
66
+ that takes no arguments, which fails at call time instead of load
67
+ time and is the harder failure to trace.
68
+ """
69
+ input_schema = (
70
+ getattr(tool, "input_schema", None)
71
+ or getattr(tool, "inputSchema", None)
72
+ or {"type": "object", "properties": {}}
73
+ )
74
+ description = getattr(tool, "description", None) or ""
75
+ title = getattr(tool, "title", None)
76
+ if title and title not in description:
77
+ description = f"{title}. {description}".strip()
78
+ return {
79
+ "name": tool_name_for(server_name, tool.name),
80
+ "description": f"[MCP: {server_name}] {description}".strip(),
81
+ "input_schema": input_schema,
82
+ }
83
+
84
+
85
+ def _make_handler(client, server_name: str, tool_name: str):
86
+ """A genuinely plain synchronous function -- no bridging logic leaks
87
+ into the tool layer. client.call_tool() does the crossing (D2a) and
88
+ returns an already-normalized dict (D23)."""
89
+ def handler(params: dict) -> dict:
90
+ return client.call_tool(server_name, tool_name, params)
91
+ return handler
92
+
93
+
94
+ def register_all(client, registry) -> list:
95
+ """Register every tool from every connected server. Returns the tool
96
+ names registered, so a caller can report what became available."""
97
+ registered = []
98
+ for server_name in sorted(client.clients):
99
+ cfg = client.server_configs.get(server_name)
100
+ auto_approve = bool(getattr(cfg, "auto_approve", False))
101
+ try:
102
+ tools = client.list_tools(server_name)
103
+ except Exception as e:
104
+ logger.warning("Could not list tools for MCP server %r: %s",
105
+ server_name, e)
106
+ continue
107
+
108
+ for tool in tools:
109
+ name = tool_name_for(server_name, tool.name)
110
+ if name in registry._tools:
111
+ # Never overwrite. The colliding registration would replace
112
+ # BOTH the handler and (via set_dynamic_approval_default
113
+ # below) the approval policy of a tool already in place --
114
+ # so refusing loudly is the only outcome that can't be
115
+ # mistaken for the tool the user thinks they are calling.
116
+ logger.warning(
117
+ "MCP tool name collision: %r from server %r is already "
118
+ "registered; skipping it. Rename one of the servers or "
119
+ "tools -- '__' in a name makes these ambiguous.",
120
+ name, server_name)
121
+ continue
122
+ # Base approval default BEFORE registering, so there is no
123
+ # window in which the tool is callable without its policy set.
124
+ permissions.set_dynamic_approval_default(name, not auto_approve)
125
+ registry.register(ToolSpec(
126
+ name=name,
127
+ schema=schema_for(server_name, tool),
128
+ handler=_make_handler(client, server_name, tool.name),
129
+ # §25 R13. Stated here rather than left to
130
+ # registry.grant_policy's mcp__* fallback: this is the
131
+ # site that makes the decision, and MCP tools are the
132
+ # grant picker's real population -- R1's argument that
133
+ # the tool's own description is the whole of informed
134
+ # consent was written about exactly these, because MCP
135
+ # exposes no read-only/write metadata for anything to
136
+ # reason from. The fallback stays as documentation for
137
+ # any other dynamic registrant.
138
+ grant_policy=GRANT_ANYWHERE,
139
+ # H1. Exempt from assert_budget_declared for the reason
140
+ # R13 exempts them -- named at connection time, so no
141
+ # static declaration can exist -- and declared anyway,
142
+ # because these tools DO have a bound and it is worth
143
+ # naming which: client.call_tool wraps every call in the
144
+ # two-layer clock DEFAULT_CALL_TIMEOUT_S backstops. That
145
+ # is BUDGET_IO exactly, and it is the mechanism H2 was
146
+ # modelled on.
147
+ budget=BUDGET_IO,
148
+ ))
149
+ _registered.setdefault(server_name, []).append(name)
150
+ registered.append(name)
151
+
152
+ if auto_approve:
153
+ # WARNING, not INFO (F4): TranscriptLogHandler forwards
154
+ # WARNING+ into the TUI transcript, and on that path stderr is
155
+ # already closed -- at INFO this reached nothing but app.log,
156
+ # so the shell where approval prompts are MODALS was exactly
157
+ # the one never told some tools would not prompt. Fires every
158
+ # launch per auto-approved server, deliberately (#60): worst
159
+ # case it trains people past it, which equals today's silence;
160
+ # best case it is read once and someone catches a flag they
161
+ # did not knowingly set.
162
+ logger.warning("MCP server %r is auto-approved; its tools will "
163
+ "not prompt before running.", server_name)
164
+
165
+ logger.info("Registered %d MCP tool(s).", len(registered))
166
+ return registered
167
+
168
+
169
+ def unregister_all(registry, server_name: str = None) -> None:
170
+ """Remove MCP tools from the registry and drop their approval
171
+ defaults. Idempotent -- disconnect handling can run more than once
172
+ for the same server, which is why registry.unregister() is too.
173
+
174
+ Removes exactly what register_all() put in, by name. A prefix scan
175
+ would take a sibling server's tools with it whenever one server's
176
+ name is a `__`-prefix of another's (see _registered above).
177
+ """
178
+ servers = [server_name] if server_name is not None else list(_registered)
179
+ for server in servers:
180
+ for name in _registered.pop(server, []):
181
+ registry.unregister(name)
182
+ permissions.clear_dynamic_approval_defaults(name)