Implement a production-grade "MCP as code" solution for Claude Code using the mcp-dynamic-orchestrator skill so that:
- MCP servers are declared only in
skills/mcp-dynamic-orchestrator/mcp.registry.json. - Claude discovers MCPs and their purpose via small meta-tools, not hundreds of direct tools.
- All real usage flows through generated TypeScript/JS clients and a secure code executor.
- Expose only three tools via
.claude-plugin/plugin.json:list_mcp_capabilities: Discover configured MCPs.describe_mcp: Inspect a specific MCP (summary, tools, optional schemas).execute_mcp_code: Run TS/JS usingmcp-clients/<id>to call MCPs.
- Keep
SKILL.mdminimal and model-optimized:- Document the discover → describe → code → execute workflow.
- Document
mcp.registry.jsonas the only config surface. - Document empty-registry behavior and file path.
- Implement an MCP client manager:
- Reads each server’s
command,args,env,transportfrom registry. - Lazily spawns/connects MCP servers (stdio/http) per
id. - Caches clients per
id. - Provides:
listTools(id)for metadata.callTool(id, tool, args)for execution.
- Reads each server’s
- Best practices:
- Add timeouts, retries, structured errors.
- No secrets in code; read from env/secure config.
- Clean up MCP processes; prevent zombie processes.
getToolMetadata(id):- On first call, use MCP client to fetch tools and schemas.
- Cache compact entries:
name,description, schema excerpts.
describe_mcp:summary: registry only.tools: names + 1-line descriptions from metadata.schema: bounded subset of schemas for selected tools.
getClientModuleMap(allowedIds):- For each allowed MCP, generate virtual modules:
mcp-clients/index.tsexporting MCP namespaces.mcp-clients/<id>/index.ts+ per-tool modules.
- Each tool module:
- Calls
callMcpTool(id, tool, args). - Includes JSDoc from schemas and registry ("Use when" hints).
- Uses strong TS types where feasible;
anyfallback when needed.
- Calls
- For each allowed MCP, generate virtual modules:
- Implement sandboxed runtime:
- Input:
language,files,entrypoint,allowedMcpIds,maxRuntimeMs,maxLogs. - Build
mergedFiles= user files + generatedmcp-clients/*+runtimeshim. - Execute in an isolated environment with:
- No general outbound network.
- No unrestricted filesystem.
- Only MCP access via
callMcpTool.
- Return
{ logs, result, errors? }.
- Input:
- Claude Code integration:
- Use the repo’s existing Node/Bun runtime or a controlled worker process.
- Enforce CPU/time/memory limits.
- Disallow dynamic requires outside
mcp-clients/*and safe stdlib.
- Use registry fields:
visibility: default vs opt_in vs experimental.sensitivity: low/medium/high.
- Enforcement:
list_mcp_capabilitiesdefaults tovisibility: ["default"].execute_mcp_codeonly loads opt-in MCPs if explicitly requested.callMcpToolchecks MCP id against allowed lists; apply per-MCP timeouts & rate limits.
- Unit tests:
- Registry parsing & validation.
searchCapabilitiesscoring & filters.describe_mcpbehavior for all detail levels.- Codegen layout for a mocked MCP server.
- Integration tests:
- Run simple MCP servers (e.g.,
time,grep-mcp) locally. - Verify: discover → describe → code → execute flow works end-to-end.
- Run simple MCP servers (e.g.,
- Manual Claude Code scenarios:
- Example: call Cloudflare MCP via code to fetch Workers KV docs.
- Confirm Claude never sees raw MCP tools, only the orchestrator interface.
- Keep
orchestrator.tsandmcp.registry.jsonframework-agnostic. - For other agents (OpenAI, Cloudflare Agents), expose the same 3-tool API and reuse the orchestrator.