feat(mesh): autostart MCP project from cwd in bunx decocms#3262
Open
feat(mesh): autostart MCP project from cwd in bunx decocms#3262
Conversation
When `bunx decocms` runs in a folder that contains an `mcp/` subfolder (or whose cwd itself looks like an MCP project), Studio now detects the project, runs `bun install` if needed, spawns its dev server, fetches its tools, registers it as an HTTP connection + Virtual MCP agent, and opens the user directly into a chat with that agent. Highlights: - Detection: `<cwd>/mcp` first, then cwd itself when shape matches (`api/main.*.ts` or `@decocms/runtime` / `@modelcontextprotocol/sdk` in deps). Resolves agent name from README H1 → `package.json#name` (scope stripped) → basename. - Lifecycle: picks a free port, spawns the project's `dev`/`start` script via the right package manager (lockfile-detected), pipes child stdout/stderr into the Ink TUI, races a healthcheck against child exit so crashes surface fast with the real reason. - Auto-install: runs `bun install` (or pnpm/yarn/npm install) when `node_modules` is missing. - Prompt: prefers an authored `prompt.md` / `AGENTS.md` / `CLAUDE.md` in the project root. Otherwise calls a small Anthropic model with a meta-prompt; falls back to a deterministic template when no provider key is configured or the call fails. - UI integration: tools that expose `_meta.ui.resourceUri` are auto-pinned as sidebar views, with the first one set as the default main view (`type: "ext-apps"`) so the chat opens with the agent's primary UI alongside. - Idempotency: deterministic IDs derived from `sha256(absPath)` so re-runs reuse the same connection + agent and just refresh the URL if the port changed. - Browser auto-opens the chat URL by default; suppress with `--no-open`. Suppress autostart entirely with `--no-autostart` or `DECOCMS_NO_AUTOSTART=1`. - Graceful shutdown kills any tracked autostart child processes. 23 unit tests cover detection (PMs, MCP-shape rules, prompt-file fallback, name resolution), agent registration (idempotency, URL refresh on port change), and prompt drafting (file > LLM > template fallbacks). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
🧪 BenchmarkShould we run the Virtual MCP strategy benchmark for this PR? React with 👍 to run the benchmark.
Benchmark will run on the next push after you react. |
Contributor
Release OptionsSuggested: Minor ( React with an emoji to override the release type:
Current version:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When users run
bunx decocmsfrom a folder that contains anmcp/subfolder (or whose cwd itself looks like an MCP project), Studio now detects the project, installs deps if needed, spawns its dev server, fetches its tools, creates a connection + Virtual MCP agent, and opens the browser straight into a chat with that agent.The agent's system prompt is taken from an authored
prompt.md/AGENTS.md/CLAUDE.mdin the project root when present; otherwise a small Anthropic model drafts one from the README + tool list, with a template fallback when no provider key is configured. Tools exposing_meta.ui.resourceUriare auto-pinned as sidebar views, with the first one set as the default main view alongside chat.Changes
New module —
apps/mesh/src/cli/autostart/detect.ts— finds the project, package manager (lockfile-based), and starter script. Resolves agent name from README H1 →package.json#name(scope stripped) → basename. Surfacesprompt.md/AGENTS.md/CLAUDE.mdif present.spawn.ts— picks a free port, runsbun/pnpm/yarn/npm installwhennode_modulesis missing, spawns the dev script withPORT=…, pipes child output into the Ink TUI, races healthcheck against child exit so crashes surface fast.prompt.ts— prefers authored prompt file → one-shot Anthropic call with project context → deterministic template fallback.register.ts— deterministic IDs fromsha256(absPath)(conn_auto_<12hex>/vir_auto_<12hex>); creates HTTP connection + Virtual MCP, refreshes URL on re-runs, persists pinnedViews + layout undermetadata.ui.index.ts— orchestrates the flow, builds chat URL<base>/<orgSlug>/<uuid>?virtualmcpid=<id>, opens browser by default.CLI wiring
--no-autostart,--no-open. Also respectsDECOCMS_NO_AUTOSTART=1.serve.tsreturns resolved port so cli.ts can pass it to autostart.autostartProjectpanel in the Ink TUI shows project name, status, chat URL.Demo
Run from a folder with an
mcp/subfolder:Test plan
bun test apps/mesh/src/cli/autostart/— 23 tests pass (detect, register idempotency, prompt fallbacks).bun run check— clean across all workspaces.bun run lint— 0 warnings, 0 errors.mcp/(custom MCP) — install runs, dev server boots, agent registered, chat URL renders.mcp/— silent no-op, normal Studio boot.Ctrl-Ckills both Studio and the spawned MCP cleanly.bunx decocmsin adecocms init-scaffolded folder and confirm the autostarted agent has functional tools + UI.Notes
🤖 Generated with Claude Code
Summary by cubic
Auto-starts a local MCP project when running
bunx decocms, then opens Studio directly into a chat with a pre-registered Virtual MCP agent. This streamlines local workflows by detecting the project, starting its dev server, wiring tools, and launching the chat automatically.New Features
<cwd>/mcpfirst, then cwd if it has MCP shape (api/main.*.tsor deps like@decocms/runtime,@modelcontextprotocol/sdk).node_modulesis missing, picks a free port, and runs thedev/startscript via the correct package manager; logs are piped into the TUI and crashes surface fast._meta.ui.resourceUri; sets the first as the default main view next to chat.prompt.md/AGENTS.md/CLAUDE.mdwhen present; otherwise drafts via a small model with a template fallback if no provider key is set.conn_auto_<hex>,vir_auto_<hex>); re-runs reuse IDs and refresh the URL if the port changes.Migration
bunx decocmsin a repo withmcp/will auto-open the agent chat.--no-autostartorDECOCMS_NO_AUTOSTART=1; skip opening the browser with--no-open.Written for commit e9ccedc. Summary will update on new commits.