Skip to content

feat(sandbox): materialize org tool catalog into the workspace - #4445

Merged
pedrofrxncx merged 3 commits into
mainfrom
feat/sandbox-typegen
Jul 11, 2026
Merged

pedrofrxncx merged 3 commits into
mainfrom
feat/sandbox-typegen

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Lets a sandboxed coding agent discover and script against org/user tools from the filesystem. The daemon writes a JSON Schema tool catalog under <repo>/.deco/tools/ — one file per tool ({ name, description, inputSchema, outputSchema }) — sourced from the run's Virtual MCP endpoint the daemon already receives. Pairs with the @decocms/typegen CLI/client (an agent can read the catalog, or run typegen for a typed client).

How it works

  • tools-catalog.ts — connects to the run's Virtual MCP endpoint via the MCP SDK, lists tools, and emits one raw JSON Schema file per tool. Deliberately no TypeScript codegen, so it pulls no prettier / json-schema-to-typescript into the daemon bundle. The MCP SDK is already in the bundle (via @decocms/harness), so bundle size is unchanged (3.20 MB before and after).
  • POST /_sandbox/tools/sync (token-gated) — body { url, headers }; syncs the catalog on demand. Returns { count, tools }.
  • Auto-sync on dispatch — the dispatch handler already receives the per-run mcp endpoint; it now fires the sync fire-and-forget (non-blocking, errors logged) so every run populates the catalog without a separate caller. Wired via an optional onDispatchMcp dep, keeping dispatch.ts decoupled from the fs.

Why this shape

  • mcp reaches only the in-process harness / dispatch path, not the dev-server/exec env — so dispatch is the correct seam, not buildDevEnv.
  • Raw JSON catalog (not the typed client) keeps the daemon lean; the typed client is a heavier, optional step an agent runs itself via @decocms/typegen.

Testing

  • Unit (bun test): toolCatalogFiles — one file per tool, optional fields omitted, filename sanitization (slashes → _, dots kept, safePath clamps regardless).
  • E2E (daemon.tools.e2e.test.ts, black-box): stands up a stub Virtual MCP over HTTP, points the spawned daemon at it, and asserts the catalog files land in the workspace with correct content; plus 400 (malformed body), 502 (unreachable endpoint), 401 (missing token).

bun test for the daemon suite: tools 7 pass, dispatch e2e 8 pass, core daemon e2e 103 pass. bun run check, bun run fmt, bun run lint (no new warnings) clean.

Follow-ups (not here)

  • Inject STUDIO_* env into the agent's shell so deco call / the generated client run flagless (needs the x-org-id header story sorted for the typegen CLI).
  • Optionally gate/curate which tools land in the catalog.

Summary by cubic

Materializes org/user tools into the sandbox workspace as a JSON Schema catalog under .deco/tools so agents can discover and script tools from disk. Auto-syncs on each run from the Virtual MCP and exposes a token-gated endpoint for on-demand sync.

  • New Features

    • Writes one JSON file per tool to .deco/tools with { name, description?, inputSchema (defaults to { type: "object" }), outputSchema? }; filenames are sanitized.
    • Adds POST /_sandbox/tools/sync (body { url, headers }) to fetch via MCP; returns { count, tools } and surfaces 400/401/502/500 errors.
    • Auto-syncs during dispatch using the per-run MCP endpoint (fire-and-forget; non-blocking).
    • Uses @modelcontextprotocol/sdk to list tools; keeps output as raw JSON Schema (no TS codegen), pairs with @decocms/typegen if a typed client is needed.
    • Adds a black-box e2e proving discovery via glob/grep (email tools; validates ARCHIVE_EMAIL input).
  • Bug Fixes

    • Prunes stale *.json on each sync and paginates via the MCP cursor so large orgs aren’t truncated.
    • Ensures collision-safe filenames; distinct tools that sanitize to the same name get a -2, -3, etc. suffix.
    • Excludes /.deco/tools/ via .git/info/exclude so shutdown git add -A never commits the catalog.
    • Coalesces auto-sync with a minimum interval to avoid per-run re-list/write races; upstream MCP failures return 502, local write failures return 500.

Written for commit 5d1c8e2. Summary will update on new commits.

Review in cubic

Adds a daemon capability to write a JSON Schema tool catalog under
<repo>/.deco/tools/ so a sandboxed agent can discover and script against
org/user tools from disk (pairs with the @decocms/typegen CLI/client).

- tools-catalog.ts: fetch tools from the run's Virtual MCP endpoint via
  the MCP SDK and emit one raw JSON Schema file per tool (no TS codegen,
  so no prettier/json-schema-to-typescript in the daemon bundle)
- route POST /_sandbox/tools/sync (token-gated) for on-demand sync
- dispatch auto-fires the sync (fire-and-forget) from the per-run mcp
  endpoint it already receives, so every run populates the catalog
- unit tests (pure file emission) + black-box e2e (stub MCP over HTTP,
  spawned daemon writes the catalog into the workspace)
@pedrofrxncx
pedrofrxncx force-pushed the feat/sandbox-typegen branch from ee2ab62 to 4fccfd9 Compare July 11, 2026 00:33
- prune stale *.json on each sync so renamed/removed tools don't linger
- follow the MCP pagination cursor so large orgs aren't truncated
- collision-safe filenames so distinct tools can't clobber each other
- exclude /.deco/tools/ via .git/info/exclude so shutdown git add -A won't
  commit the catalog (extracted shared ensureGitExclude, reused by repo-link)
- coalesce + min-interval the dispatch auto-sync (no per-run re-list/write race)
- split fetch/write so upstream failures are 502 and local write failures 500
Adds a black-box daemon e2e that proves the whole point of the catalog:
a coding agent handed "archive all my emails" discovers which org tools
to script against straight from disk, via the same glob/grep/bash routes
its shell uses. Extends the stub MCP with LIST_EMAILS/ARCHIVE_EMAIL and
asserts glob enumerates the catalog, POSIX grep narrows to the email
tools (LIST_CUSTOMERS excluded), and ARCHIVE_EMAIL's inputSchema exposes
the emailId contract.
@pedrofrxncx
pedrofrxncx merged commit 9bb315a into main Jul 11, 2026
14 checks passed
@pedrofrxncx
pedrofrxncx deleted the feat/sandbox-typegen branch July 11, 2026 01:10
decocms Bot pushed a commit that referenced this pull request Jul 11, 2026
PR: #4445 feat(sandbox): materialize org tool catalog into the workspace
Bump type: minor

- @decocms/sandbox (packages/sandbox/package.json): 1.13.1 -> 1.14.0

Deploy-Scope: both
tlgimenes pushed a commit that referenced this pull request Jul 16, 2026
…TS.md (#4475)

Mined ~300 merged PRs for the delta between the first PR proposal and the
work added before merge. The dominant shape: a first draft ships the happy
path, then a hardening pass (often a different engineer who takes over the
branch and merges — the vibecoder→engineer handoff) adds the same categories
every time. Encoded those 9 categories as an actionable first-pass checklist,
each citing a real PR (#4008, #4230, #4365, #4409, #4426, #4445, #4357, #4355,
#4449, #4350, #4373, #4416, #4446, #4461, #4134, #4469).

Covers: variant/edge-case handling, tenant/permission scoping, concurrency &
silent-data-loss, test-per-fix + invert-bug-tests + real-PG, dead-code/knip,
lifecycle symmetry + state reset, kill-switch/GC/git-exclude for risky infra,
input validation vs over-engineering, compile-time type-safety.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant