feat(lib): bundle MCP servers from .common/lib into factory/lib/

Copies source (no node_modules, dist, .tasks, .wiki, __pycache__) for:
- projects-meta-mcp v2.25.0 (TypeScript/Node)
- wiki-graph v0.3.1 (TypeScript/Node)
- interns-mcp v0.3.3 (Python/FastMCP)

.gitignore: exclude lib build artefacts (node_modules, dist, .venv, __pycache__, *.pyc)
bootstrap.ps1: add MCP build step — npm install+build for TS servers, venv+pip for Python

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 13:17:12 +03:00
parent 9eeae13e72
commit ca669d96e1
116 changed files with 25331 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
"""Base intern protocol and response types."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
@dataclass
class InternResponse:
text: str
usage: dict[str, Any] = field(default_factory=dict)
@dataclass
class BlockedByPolicy:
path: str
pattern: str
reason: str
@dataclass
class BudgetExceededError:
tokens: int
limit: int
top_files: list[str] = field(default_factory=list)
hint: str = "use compress=True or narrow paths"
class Intern:
id: str = ""
description: str = ""
endpoint: str | None = None
model: str | None = None
max_tokens: int = 4096
temperature: float = 0.2
system_prompt: str = ""
context_budget: int = 120000
def __init__(self, config: dict[str, Any] | None = None, client: Any = None, safety: Any = None):
self.client = client
self.safety = safety
if config:
self._apply_config(config)
def _apply_config(self, config: dict[str, Any]) -> None:
for k, v in config.items():
if hasattr(self, k):
setattr(self, k, v)
def run(self, paths: list[str], question: str, **kwargs: Any) -> InternResponse | BlockedByPolicy | BudgetExceededError:
raise NotImplementedError

View File

@@ -0,0 +1,46 @@
"""Bulk text read intern — read N files and answer a focused question."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from .base import BlockedByPolicy, Intern, InternResponse
class BulkTextRead(Intern):
id = "bulk_text_read"
description = "Read N files and answer a focused question. Returns concise summary."
def run(self, paths: list[str], question: str, **kwargs: Any) -> InternResponse | BlockedByPolicy:
contents: list[str] = []
for p in paths:
text = Path(p).read_text(encoding="utf-8", errors="replace")
contents.append(f"--- {p} ---\n{text}")
combined = "\n\n".join(contents)
prompt = f"Files:\n{combined}\n\nQuestion: {question}"
if self.client is None:
return InternResponse(text="No client configured", usage={})
extra_body = kwargs.get("extra_body", {})
resp = self.client.chat.completions.create(
model=self.model or "deepseek-v4-flash",
messages=[
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt},
],
max_tokens=kwargs.get("max_tokens", self.max_tokens),
temperature=self.temperature,
extra_body=extra_body if extra_body else None,
)
choice = resp.choices[0]
return InternResponse(
text=choice.message.content or "",
usage={
"tokens_in": resp.usage.prompt_tokens if resp.usage else 0,
"tokens_out": resp.usage.completion_tokens if resp.usage else 0,
},
)

View File

@@ -0,0 +1,104 @@
"""Grep audit intern — deterministic matrix of N paths x M patterns. No LLM call."""
from __future__ import annotations
import json
import re
from pathlib import Path
from typing import Any, Callable, Literal
from .base import Intern, InternResponse
class GrepAudit(Intern):
id = "grep_audit"
description = (
"Deterministic grep matrix over N paths x M patterns (substring or regex). "
"No LLM call, no endpoint cost. Returns markdown table (default) or JSON."
)
def run(
self,
paths: list[str],
patterns: list[str | dict[str, Any]],
output: Literal["table", "json"] = "table",
case_sensitive: bool = True,
**_kwargs: Any,
) -> InternResponse:
compiled = _compile_patterns(patterns, case_sensitive)
rows: list[dict[str, Any]] = []
matches_total = 0
for p in paths:
try:
text = Path(p).read_text(encoding="utf-8", errors="replace")
except (FileNotFoundError, PermissionError, IsADirectoryError, OSError) as exc:
rows.append(
{
"path": p,
"matches": {c["name"]: None for c in compiled},
"error": str(exc),
}
)
continue
row_matches: dict[str, bool] = {}
for c in compiled:
hit = bool(c["matcher"](text))
row_matches[c["name"]] = hit
if hit:
matches_total += 1
rows.append({"path": p, "matches": row_matches})
if output == "json":
rendered = json.dumps({"rows": rows}, ensure_ascii=False, indent=2)
else:
rendered = _render_table(rows, [c["name"] for c in compiled])
return InternResponse(
text=rendered,
usage={
"files_scanned": len(paths),
"patterns_evaluated": len(compiled),
"matches_total": matches_total,
},
)
def _compile_patterns(
patterns: list[str | dict[str, Any]], case_sensitive: bool
) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
for p in patterns:
if isinstance(p, str):
out.append({"name": p, "matcher": _substring_matcher(p, case_sensitive)})
continue
name = p.get("name", p["pattern"])
if p.get("regex"):
flags = 0 if case_sensitive else re.IGNORECASE
compiled = re.compile(p["pattern"], flags)
out.append({"name": name, "matcher": compiled.search})
else:
out.append(
{"name": name, "matcher": _substring_matcher(p["pattern"], case_sensitive)}
)
return out
def _substring_matcher(needle: str, case_sensitive: bool) -> Callable[[str], bool]:
if case_sensitive:
return lambda text: needle in text
lowered = needle.lower()
return lambda text: lowered in text.lower()
def _render_table(rows: list[dict[str, Any]], names: list[str]) -> str:
header = "| Path | " + " | ".join(names) + " |"
sep = "|" + "---|" * (len(names) + 1)
lines = [header, sep]
for row in rows:
cells: list[str] = []
for n in names:
v = row["matches"].get(n)
cells.append("⚠️" if v is None else ("" if v else ""))
lines.append(f"| `{row['path']}` | " + " | ".join(cells) + " |")
return "\n".join(lines)

View File

@@ -0,0 +1,164 @@
"""Repo read intern — pack repo with repomix, answer question with LLM."""
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any
from .base import BudgetExceededError, Intern, InternResponse
from ..safety import always_ask_globs
# Conservative token estimate: chars/4 overestimates for repomix XML (tag overhead),
# so BudgetExceededError fires early rather than late. Real enforcement is on the
# endpoint side — this is a client-side safety net, not a precision counter.
CHARS_PER_TOKEN = 4
def _estimate_tokens(text: str) -> int:
return len(text) // CHARS_PER_TOKEN
def _top_files_by_size(packed: str, n: int = 5) -> list[str]:
"""Return the n largest files in repomix XML output, by block content size.
Parses each ``<file path="X">...</file>`` block and measures the byte length
of its content, so BudgetExceededError.top_files points the user at the real
overflow culprits — not the first files in document order.
"""
sizes: list[tuple[int, str]] = []
pos = 0
while True:
open_idx = packed.find('<file ', pos)
if open_idx == -1:
break
close_idx = packed.find('</file>', open_idx)
if close_idx == -1:
break
path_start = packed.find('path="', open_idx) + 6
path_end = packed.find('"', path_start)
path = packed[path_start:path_end]
# Content sits between the opening tag's '>' and the closing '</file>'.
content_start = packed.find('>', open_idx) + 1
content_size = close_idx - content_start
sizes.append((content_size, path))
pos = close_idx + len('</file>')
sizes.sort(key=lambda x: x[0], reverse=True)
return [path for _, path in sizes[:n]]
class RepoRead(Intern):
id = "repo_read"
description = "Pack directory/repo with repomix and answer a question. Cheap repo comprehension."
def run(
self,
paths: list[str],
question: str,
compress: bool = False,
**kwargs: Any,
) -> InternResponse | BudgetExceededError:
if not paths:
return InternResponse(text="No paths provided", usage={})
# Build repomix args: pass all paths, not just first.
ignore_globs = always_ask_globs()
# Resolve npx via PATH. On Windows, subprocess.run(shell=False) does not
# expand PATHEXT, so passing bare "npx" fails to find npx.cmd; shutil.which
# finds the .cmd (or .exe / shell wrapper on POSIX) and returns a full path
# that CreateProcess / execve can dispatch directly.
npx = shutil.which("npx") or shutil.which("npx.cmd")
if npx is None:
return InternResponse(text="npx not found - install Node.js", usage={})
# Use mkstemp to avoid Windows file-sharing issues with NamedTemporaryFile.
# os.close(fd) releases the handle before repomix writes to the path.
fd, tmp_path = tempfile.mkstemp(suffix=".xml")
os.close(fd)
err_fd, err_path = tempfile.mkstemp(suffix=".stderr")
os.close(err_fd)
try:
args = [
npx, "-y", "repomix@latest",
*paths,
"--output", tmp_path,
"--style", "xml",
]
if compress:
args.append("--compress")
for glob in ignore_globs:
args += ["--ignore", glob]
try:
# Redirect repomix's stdio to FILES/DEVNULL, never PIPES. capture_output
# spawns reader threads that communicate() joins; if any process in the
# child tree (an orphaned `node` grandchild, or — under concurrent launches
# on Windows — a sibling subprocess that inherited the pipe handle) keeps a
# pipe write-end open, that join blocks FOREVER, past the timeout, wedging
# the worker thread. repomix's real output already goes to --output, so its
# stdout is unused; stderr is captured to a file for error reporting only.
with open(err_path, "w", encoding="utf-8", errors="replace") as err_f:
subprocess.run(
args,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=err_f,
timeout=120,
check=True,
)
packed_text = Path(tmp_path).read_text()
except subprocess.TimeoutExpired:
return InternResponse(text="Repomix timed out (120s)", usage={})
except subprocess.CalledProcessError:
err = Path(err_path).read_text(encoding="utf-8", errors="replace")
return InternResponse(text=f"Repomix failed: {err[:500]}", usage={})
except FileNotFoundError:
return InternResponse(text="npx not found - install Node.js", usage={})
finally:
# Always clean up the temp files, even on error.
for _p in (tmp_path, err_path):
try:
os.unlink(_p)
except OSError:
pass
# Budget check before sending to LLM.
tokens = _estimate_tokens(packed_text)
if tokens > self.context_budget:
return BudgetExceededError(
tokens=tokens,
limit=self.context_budget,
top_files=_top_files_by_size(packed_text),
)
prompt = f"# Packed repo\n\n{packed_text}\n\n# Question\n\n{question}"
if self.client is None:
return InternResponse(text="No client configured", usage={})
extra_body = kwargs.get("extra_body", {})
resp = self.client.chat.completions.create(
model=self.model or "deepseek-v4-flash",
messages=[
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt},
],
max_tokens=kwargs.get("max_tokens", self.max_tokens),
temperature=self.temperature,
extra_body=extra_body if extra_body else None,
)
choice = resp.choices[0]
return InternResponse(
text=choice.message.content or "",
usage={
"tokens_in": resp.usage.prompt_tokens if resp.usage else 0,
"tokens_out": resp.usage.completion_tokens if resp.usage else 0,
},
)

View File

@@ -0,0 +1,46 @@
"""Transcript distill intern — compress session transcript into structured action-list."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from .base import BlockedByPolicy, Intern, InternResponse
class TranscriptDistill(Intern):
id = "transcript_distill"
description = "Compress a session transcript/log into a structured action-list."
def run(self, paths: list[str], question: str = "Extract action items, decisions, and unresolved questions.", **kwargs: Any) -> InternResponse | BlockedByPolicy:
contents: list[str] = []
for p in paths:
text = Path(p).read_text(encoding="utf-8", errors="replace")
contents.append(f"--- {p} ---\n{text}")
combined = "\n\n".join(contents)
prompt = f"Transcript:\n{combined}\n\nTask: {question}"
if self.client is None:
return InternResponse(text="No client configured", usage={})
extra_body = kwargs.get("extra_body", {})
resp = self.client.chat.completions.create(
model=self.model or "deepseek-v4-flash",
messages=[
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt},
],
max_tokens=kwargs.get("max_tokens", self.max_tokens),
temperature=self.temperature,
extra_body=extra_body if extra_body else None,
)
choice = resp.choices[0]
return InternResponse(
text=choice.message.content or "",
usage={
"tokens_in": resp.usage.prompt_tokens if resp.usage else 0,
"tokens_out": resp.usage.completion_tokens if resp.usage else 0,
},
)