Skip to main content

ADR-014: CE-MCP (Code Execution with MCP) Architecture

Statusโ€‹

Proposed

Dateโ€‹

2025-12-09

Decision Makersโ€‹

  • Architecture Team
  • AI Integration Team

Contextโ€‹

The MCP ADR Analysis Server has evolved into a comprehensive platform with 82 tools and 6,145 lines of prompt definitions. Analysis of the current implementation (see /tmp/ce_mcp_analysis.md) reveals significant token inefficiencies:

MetricCurrent StateImpact
Tools loaded per ListTools82 complete definitions~15K tokens per call
Prompt code loaded6,145 lines~28K tokens in memory
Per-analysis overhead9K-12K tokensContext assembly before LLM
AI call points121+ instancesSequential dependencies

Root Causes of Inefficiencyโ€‹

  1. Monolithic Tool Loading (src/index.ts:225-3170): All 82 tools with full inputSchema returned on every ListTools call

  2. Context Over-Assembly (src/index.ts:4383-4830): Sequential context building (knowledge โ†’ reflexion โ†’ base โ†’ environment) assembles 9,000+ tokens BEFORE any LLM call

  3. Intermediate Result Round-Trips: Pattern of LLM call โ†’ embed result in context โ†’ LLM call causes token multiplication (3,500 optimal โ†’ 10,500 actual)

  4. Eager Prompt Loading: All 10 prompt files imported when any tool uses prompts, only 10-15% utilized

Protocol Evolution Contextโ€‹

  • November 2024: MCP launched by Anthropic (direct tool-calling model)
  • 2025: CE-MCP paradigm introduced as recommended best practice
  • Key Shift: LLM role changes from step-by-step planner to holistic code generator

Decisionโ€‹

We will adopt the CE-MCP (Code Execution with MCP) architecture to address token inefficiencies through:

1. Progressive Tool Discoveryโ€‹

Replace monolithic tool loading with on-demand discovery:

Current: ListTools โ†’ 82 tools (15K tokens)
CE-MCP: ListTools โ†’ 20 meta-tools (5K tokens) + search_tools() function

Implementation approach:

  • Expose tools via file-based directory structure (./servers/{category}/{tool}/action.ts)
  • Return tool metadata catalog instead of full definitions
  • LLM requests specific tools on-demand via search_tools(category, query)

2. In-Sandbox Context Assemblyโ€‹

Shift context composition from tools to sandbox execution:

Current:
Tool assembles context โ†’ Tool calls LLM โ†’ LLM returns result โ†’ Tool embeds in new context

CE-MCP:
Tool returns composition directive โ†’ LLM orchestrates sandbox โ†’ Sandbox returns final result

Tools return composition directives:

{
"compose": {
"sections": [
{ "source": "knowledge_generation", "key": "knowledge" },
{ "source": "file_analysis", "key": "files" },
{ "source": "environment_analysis", "key": "environment" }
],
"template": "ecosystem_analysis_v2"
}
}

3. Lazy Prompt Loadingโ€‹

Implement prompt registry with on-demand loading:

Current: import * from './prompts/' โ†’ 28K tokens loaded
CE-MCP: Prompt catalog registered โ†’ load_prompt('adr_suggestion') โ†’ 500 tokens loaded

Prompt service architecture:

  • Register prompt catalog with metadata (line count, category, dependencies)
  • LLM requests specific prompts via load_prompt(name, section)
  • Cache loaded prompts for session duration

4. Sandbox Data Compositionโ€‹

Eliminate recursive tool calls by keeping intermediate data in sandbox:

Current:
analyzeProjectEcosystem()
โ†’ calls analyzeEnvironment() [tool call]
โ†’ embeds result in prompt [context bloat]
โ†’ sends to LLM

CE-MCP:
analyzeProjectEcosystem()
โ†’ returns sandbox operations
โ†’ LLM executes in sandbox
โ†’ intermediate results stay in sandbox memory
โ†’ only final summary returns to context

5. Stateful Tool Chainsโ€‹

Replace sequential LLM calls with state machine composition:

Current (rule generation):
AI Call 1: templates โ†’ AI Call 2: validation โ†’ AI Call 3: refinement

CE-MCP:
Return state machine definition
LLM executes transitions in sandbox
State passed through sandbox memory, not context

Implementation Prioritiesโ€‹

PriorityTargetCurrentAfterSavingsEffort
P1analyzeProjectEcosystem12K tokens4K tokens67%3-4 hours
P2Prompt service28K loaded1K on-demand96%6-8 hours
P3Dynamic tool discovery15K per call5K per call67%4-5 hours
P4Sandbox composition8K overhead2K overhead75%5-6 hours

Specific Code Locations for Refactoringโ€‹

High Priorityโ€‹

  1. analyzeProjectEcosystem main loop

    • File: src/index.ts:4383-4830
    • Issue: Sequential context assembly
    • Fix: Return composition directives
  2. Prompt module organization

    • Files: src/prompts/*.ts
    • Issue: Eager loading of 6,145 lines
    • Fix: Lazy-loading prompt registry
  3. Tool list in ListTools handler

    • File: src/index.ts:225-3170
    • Issue: 82 complete tools returned
    • Fix: Return metadata + dynamic discovery
  4. Tool invocation switch statement

    • File: src/index.ts:3209-3409
    • Issue: 82-case static routing
    • Fix: Dynamic tool dispatcher

Medium Priorityโ€‹

  1. Environment analysis recursion (src/index.ts:4556-4577)
  2. Knowledge context assembly (src/index.ts:4489-4519)
  3. Rule generation tool chain (src/tools/rule-generation-tool.ts)
  4. ADR suggestion enhancements (src/tools/adr-suggestion-tool.ts:95-200)

Consequencesโ€‹

Positiveโ€‹

  • 60-70% token reduction in average tool execution cost
  • Faster response times through fewer LLM roundtrips
  • Lower API costs aligned with usage patterns
  • Better composability with LLM-orchestrated tool chains
  • Improved maintainability without context embedding logic
  • Alignment with Anthropic's recommended MCP best practices

Negativeโ€‹

  • Significant refactoring effort (estimated 20-25 hours total)
  • Breaking changes to tool invocation patterns
  • Learning curve for new sandbox composition model
  • Testing complexity for state machine tool chains
  • Migration period where both patterns may coexist

Risksโ€‹

  • Sandbox security requires careful process isolation
  • State machine complexity may introduce debugging challenges
  • Backward compatibility with existing clients during transition

Compatibility with Existing ADRsโ€‹

ADRRelationshipNotes
ADR-001EvolvesSSE/JSON-RPC remains transport; MCP's role shifts to RPC interface for code-executing agents
ADR-002EvolvesLLM role shifts from step-by-step planner to holistic code generator
ADR-003CompatibleJSON storage, knowledge graph supports sandbox state management
ADR-010AlignsDAG executor already implements CE-MCP concepts (deterministic orchestration)
ADR-012AlignsFile-based YAML patterns match CE-MCP progressive discovery model
  • ADR-001: MCP Protocol Implementation Strategy (evolved by this ADR)
  • ADR-002: AI Integration and Advanced Prompting Strategy (evolved by this ADR)
  • ADR-010: Bootstrap Deployment Architecture (aligns with CE-MCP execution model)
  • ADR-012: Validated Patterns Framework (aligns with progressive discovery)

Referencesโ€‹

  • CE-MCP Refactoring Assessment: /tmp/ce_mcp_analysis.md
  • Anthropic MCP Documentation: Protocol evolution and best practices
  • Token optimization research: 2025 CE-MCP paradigm studies