Files
DeepTutor/deeptutor/runtime/registry/tool_registry.py
Bingxi Zhao (Frank) 740ec413a0 release: v1.5.7
Per-account MCP Services and CLI Apps — the two ways a deployment plugs in
tools DeepTutor never shipped — plus the isolation that makes them safe on a
shared install.

- MCP Services: a 45-entry curated store of hosted servers each account
  installs for itself, OAuth 2.1 (PKCE + dynamic client registration) for the
  servers that require it, static keys held by reference and resolved in memory
  at connect time, stdio refused for self-service. Settings no longer
  configures MCP; /settings/mcp redirects to the store.
- CLI Apps: 101 tools snapshotted from the two CLI-Anything registries.
  Installs are admin-only, the registry's install_cmd is parsed into an argv we
  build rather than executed, npm install scripts are forced off, and 66
  first-party entries are pinned to one reviewed commit. Apps land in
  data/cli-apps, one environment each, mounted read-only into the runner.
- Credentials that authorise a person move to data/system, the one branch the
  exec sandbox never mounts: Codex tokens relocate themselves on first use
  (refusing to follow a symlink), per-user MCP configs and keys land beside
  them.
- Grants gain a cli_apps whitelist with the same deny-by-default posture as
  MCP tools; a per-turn registry view keeps one owner's provider tools out of
  the process-global registry and refuses an unauthorised tool at execute, not
  just in the manifest.
- The composer reports what filled this turn's context window, a gutter rail
  jumps back through long conversations, and the workspace collapses to a
  drawer on small screens.
- The composer keeps focus after sending and across tab switches (#720);
  released images carry provenance and SBOM attestations (#722).

README prose, all ten translated READMEs, and the docs-site pages that named
Settings → MCP are updated to match. Release notes:
assets/releases/ver1-5-7.md
2026-07-31 00:25:48 +08:00

154 lines
5.1 KiB
Python

"""
Tool Registry
=============
Central registry that discovers and manages all tools (built-in and plugin).
Provides lookup, listing, and OpenAI schema generation.
"""
from __future__ import annotations
import logging
from typing import Any
from deeptutor.core.tool_protocol import BaseTool, ToolDefinition, ToolPromptHints
from deeptutor.tools.builtin import BUILTIN_TOOL_TYPES, TOOL_ALIASES
from deeptutor.tools.prompting import compose_prompt_text
logger = logging.getLogger(__name__)
class ToolRegistry:
"""
Singleton-ish registry of all available tools.
Usage::
registry = ToolRegistry()
registry.load_builtins()
rag = registry.get("rag")
result = await rag.execute(query="hello")
"""
def __init__(self) -> None:
self._tools: dict[str, BaseTool] = {}
def register(self, tool: BaseTool) -> None:
name = tool.name
self._tools[name] = tool
logger.debug("Registered tool: %s", name)
def unregister(self, name: str) -> None:
"""Remove a tool (no-op when absent). Used by MCP reloads."""
self._tools.pop(name, None)
def deferred_tools(self) -> list[BaseTool]:
"""Tools flagged for progressive disclosure (see ``BaseTool.deferred``)."""
return [t for t in self._tools.values() if getattr(t, "deferred", False)]
def load_builtins(self) -> None:
"""Instantiate and register all built-in tools."""
for tool_type in BUILTIN_TOOL_TYPES:
try:
tool = tool_type()
except Exception:
logger.warning("Failed to instantiate built-in tool %s", tool_type, exc_info=True)
continue
if tool.name in self._tools:
continue
self.register(tool)
def _resolve_request(
self,
name: str,
kwargs: dict[str, Any] | None = None,
) -> tuple[str, dict[str, Any]]:
if name in self._tools:
return name, dict(kwargs or {})
resolved_name, default_kwargs = TOOL_ALIASES.get(name, (name, {}))
merged_kwargs = {**default_kwargs, **(kwargs or {})}
return resolved_name, merged_kwargs
def get(self, name: str) -> BaseTool | None:
resolved_name, _ = self._resolve_request(name)
return self._tools.get(resolved_name)
def list_tools(self) -> list[str]:
return list(self._tools.keys())
def get_enabled(self, names: list[str]) -> list[BaseTool]:
"""Return tool instances for the given names (skipping unknown)."""
enabled: list[BaseTool] = []
seen: set[str] = set()
for name in names:
tool = self.get(name)
if tool is None or tool.name in seen:
continue
enabled.append(tool)
seen.add(tool.name)
return enabled
def get_definitions(self, names: list[str] | None = None) -> list[ToolDefinition]:
"""Return definitions for *names* (or all if None)."""
tools = self._tools.values() if names is None else self.get_enabled(names)
return [t.get_definition() for t in tools]
def get_prompt_hints(
self,
names: list[str],
language: str = "en",
) -> list[tuple[str, ToolPromptHints]]:
"""Return prompt hints for the given tool names."""
entries: list[tuple[str, ToolPromptHints]] = []
for tool in self.get_enabled(names):
entries.append((tool.name, tool.get_prompt_hints(language=language)))
return entries
def build_prompt_text(
self,
names: list[str],
format: str = "list",
language: str = "en",
**opts: Any,
) -> str:
"""Compose prompt text for the given tools."""
return compose_prompt_text(
self.get_prompt_hints(names, language=language),
format=format,
language=language,
**opts,
)
def build_openai_schemas(self, names: list[str] | None = None) -> list[dict[str, Any]]:
"""Build OpenAI function-calling tool schemas."""
return [d.to_openai_schema() for d in self.get_definitions(names)]
async def execute(self, name: str, /, **kwargs: Any):
"""Resolve aliases, execute the tool, and return its ToolResult.
``name`` (the tool to run) is positional-only so it never collides
with a tool argument that happens to also be called ``name`` — e.g.
``read_skill(name=...)`` or an MCP tool whose schema declares a
``name`` parameter. All callers already pass the tool name
positionally.
"""
resolved_name, resolved_kwargs = self._resolve_request(name, kwargs)
tool = self._tools.get(resolved_name)
if tool is None:
raise KeyError(f"Unknown tool: {name}")
return await tool.execute(**resolved_kwargs)
_default_registry: ToolRegistry | None = None
def get_tool_registry() -> ToolRegistry:
"""Return the global ToolRegistry (creating & loading builtins on first call)."""
global _default_registry
if _default_registry is None:
_default_registry = ToolRegistry()
_default_registry.load_builtins()
return _default_registry