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 @@
"""Interns MCP — cheap LLM interns for Claude Code."""

View File

@@ -0,0 +1,43 @@
"""OpenAI-compatible HTTP client, persistent per-endpoint."""
from __future__ import annotations
from typing import Any
from openai import OpenAI
# Bound every LLM call. Without this the SDK default (600s) lets a stalled
# endpoint hang the worker thread that ran the tool; enough such threads exhaust
# FastMCP's pool and wedge the whole stdio server. 90s is well above a healthy
# deepseek-v4-flash response yet short enough to fail fast and free the thread.
DEFAULT_REQUEST_TIMEOUT = 90.0
def _resolve_timeout(endpoint_cfg: dict[str, Any]) -> float:
"""Per-endpoint request timeout in seconds; falls back to the bounded default."""
return float(endpoint_cfg.get("request_timeout", DEFAULT_REQUEST_TIMEOUT))
def make_client(endpoint_cfg: dict[str, Any], api_key: str) -> OpenAI:
"""Create a persistent OpenAI client for an endpoint."""
kwargs: dict[str, Any] = {
"base_url": endpoint_cfg["base_url"],
"api_key": api_key,
"timeout": _resolve_timeout(endpoint_cfg),
}
defaults = endpoint_cfg.get("request_defaults", {})
if "extra_body" in defaults:
kwargs["default_query"] = {}
return OpenAI(**kwargs)
class ClientPool:
"""Manages persistent HTTP clients per endpoint."""
def __init__(self) -> None:
self._clients: dict[str, OpenAI] = {}
def get(self, name: str, endpoint_cfg: dict[str, Any], api_key: str) -> OpenAI:
if name not in self._clients:
self._clients[name] = make_client(endpoint_cfg, api_key)
return self._clients[name]

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,
},
)

View File

@@ -0,0 +1,38 @@
"""Load intern catalog from config.yaml."""
from __future__ import annotations
from pathlib import Path
from typing import Any
import yaml
from .interns.base import Intern
from .interns.bulk_text_read import BulkTextRead
from .interns.grep_audit import GrepAudit
from .interns.repo_read import RepoRead
from .interns.transcript_distill import TranscriptDistill
# Explicit registry — MVP choice (see design spec open questions).
INTERN_CLASSES: dict[str, type[Intern]] = {
"bulk_text_read": BulkTextRead,
"grep_audit": GrepAudit,
"repo_read": RepoRead,
"transcript_distill": TranscriptDistill,
}
def load_config(config_path: Path) -> dict[str, Any]:
with open(config_path, encoding="utf-8") as f:
return yaml.safe_load(f)
def load_interns(config: dict[str, Any], client: Any = None, safety: Any = None) -> list[Intern]:
"""Instantiate interns from config, wiring shared client and safety."""
interns = []
for intern_id, intern_cfg in config.get("interns", {}).items():
cls = INTERN_CLASSES.get(intern_id)
if cls is None:
continue
interns.append(cls(config=intern_cfg, client=client, safety=safety))
return interns

View File

@@ -0,0 +1,65 @@
"""Always-ask path matcher — safety enforcement for intern calls."""
from __future__ import annotations
from pathlib import PurePath
# Patterns that always require explicit user approval before sending to an intern.
ALWAYS_ASK_PATTERNS: list[str] = [
"**/.env",
"**/.env.*",
"**/secrets/**",
"**/projects-secrets/**",
"**/credentials*",
"**/*.key",
"**/*.pem",
"**/.ssh/**",
"**/.aws/credentials",
"**/.aws/config",
"**/.netrc",
"**/.npmrc",
"**/.pypirc",
]
ALWAYS_ASK_REASON = (
"Path matched always-ask pattern. "
"Sending this file to an external endpoint requires explicit approval."
)
def check_paths(paths: list[str], extra_patterns: list[str] | None = None) -> list[dict]:
"""Check each path against always-ask patterns.
Returns list of blocked entries: [{"path": ..., "pattern": ..., "reason": ...}].
Empty list = all paths clear.
"""
patterns = list(ALWAYS_ASK_PATTERNS)
if extra_patterns:
patterns.extend(extra_patterns)
blocked = []
for p in paths:
pp = PurePath(p)
for pattern in patterns:
if pp.match(pattern):
blocked.append({"path": p, "pattern": pattern, "reason": ALWAYS_ASK_REASON})
break
return blocked
def always_ask_globs() -> list[str]:
"""Return glob patterns for always-ask paths, suitable for --ignore flags.
Used by tool-wrapping interns (e.g. repo_read) to inject --ignore flags
into subprocess calls, preventing repomix from packaging sensitive files
even if they exist inside the target directory.
"""
# Convert PurePath-style patterns to glob patterns repomix understands.
# Strip leading **/ since repomix --ignore uses gitignore-style patterns.
globs = []
for p in ALWAYS_ASK_PATTERNS:
if p.startswith("**/"):
globs.append(p[3:])
else:
globs.append(p)
return globs

View File

@@ -0,0 +1,114 @@
"""FastMCP stdio server — MCP entry point for interns."""
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
from dotenv import load_dotenv
from fastmcp import FastMCP
from .client import ClientPool
from .registry import load_config, load_interns
from .safety import check_paths
# Resolve config path relative to .common root; secrets live outside the git tree.
_COMMON_ROOT = Path(__file__).resolve().parents[3] # lib/interns-mcp/interns_mcp/ → .common/
CONFIG_PATH = _COMMON_ROOT / "config" / "interns" / "config.yaml"
SECRETS_PATH = Path(
os.environ.get("INTERNS_SECRETS_PATH")
or (Path.home() / ".config" / "projects-secrets" / "interns.env")
)
mcp = FastMCP("interns")
def _get_api_key(endpoint_name: str, endpoints: dict[str, Any]) -> str:
env_var = endpoints[endpoint_name]["api_key_env"]
key = os.environ.get(env_var, "")
if not key:
raise RuntimeError(f"Missing API key: set {env_var} in {SECRETS_PATH}")
return key
def _build_extra_body(endpoint_cfg: dict[str, Any]) -> dict[str, Any]:
return endpoint_cfg.get("request_defaults", {}).get("extra_body", {})
def register_tool(intern: Any, client_pool: ClientPool, endpoints: dict[str, Any]) -> None:
"""Register a single intern as an MCP tool."""
@mcp.tool(name=intern.id, description=intern.description)
def intern_tool(paths: list[str], question: str, max_tokens: int | None = None) -> dict:
if intern.safety:
blocked = check_paths(paths)
if blocked:
return {"blocked": True, "path": blocked[0]["path"], "pattern": blocked[0]["pattern"], "reason": blocked[0]["reason"]}
# Wire endpoint client if intern needs one.
if intern.endpoint and intern.client is None:
ep_cfg = endpoints[intern.endpoint]
api_key = _get_api_key(intern.endpoint, endpoints)
intern.client = client_pool.get(intern.endpoint, ep_cfg, api_key)
kwargs: dict[str, Any] = {}
if intern.endpoint and intern.endpoint in endpoints:
kwargs["extra_body"] = _build_extra_body(endpoints[intern.endpoint])
if max_tokens is not None:
kwargs["max_tokens"] = max_tokens
result = intern.run(paths, question, **kwargs)
if isinstance(result, dict) and result.get("blocked"):
return result
return {"text": result.text, "usage": result.usage}
def register_grep_audit_tool(intern: Any) -> None:
"""Register grep_audit with its distinct (paths, patterns, output, case_sensitive) shape.
Deterministic: no LLM client wiring path. Safety gate still applies — server is
the single trust boundary regardless of whether the intern hits an endpoint.
"""
@mcp.tool(name=intern.id, description=intern.description)
def grep_audit_tool(
paths: list[str],
patterns: list[Any],
output: str = "table",
case_sensitive: bool = True,
) -> dict:
if intern.safety:
blocked = check_paths(paths)
if blocked:
return {"blocked": True, "path": blocked[0]["path"], "pattern": blocked[0]["pattern"], "reason": blocked[0]["reason"]}
result = intern.run(
paths=paths,
patterns=patterns,
output=output,
case_sensitive=case_sensitive,
)
return {"text": result.text, "usage": result.usage}
def main() -> None:
load_dotenv(SECRETS_PATH)
config = load_config(CONFIG_PATH)
endpoints = config.get("endpoints", {})
pool = ClientPool()
from . import safety as _safety_mod
interns = load_interns(config, safety=_safety_mod, client=None)
for intern in interns:
if intern.id == "grep_audit":
register_grep_audit_tool(intern)
else:
register_tool(intern, pool, endpoints)
mcp.run(transport="stdio")
if __name__ == "__main__":
main()