# DollhouseMCP Server Architecture **Version:** 2.0.0 **Last Updated:** April 2026 ## 1. Introduction ### 1.1. Project Overview The DollhouseMCP Server is an open-source, AI customization platform that provides personas, skills, templates, agents, memories, and ensembles for AI assistants through the Model Context Protocol (MCP). It is a TypeScript-based server that runs as a local service, extending the capabilities of AI assistants by allowing users to manage and interact with a portfolio of "elements." All element operations are exposed through the MCP-AQL (Agent Query Language) CRUDE interface, a unified protocol layer that consolidates 50+ discrete tools into 5 semantically grouped endpoints with built-in permission enforcement. This document provides a comprehensive overview of the server's architecture, components, and key workflows. ### 1.2. Purpose of this Document This document is intended to help developers understand the architecture of the DollhouseMCP Server. It provides a high-level overview of the system, as well as a detailed breakdown of its components. This document should enable developers to: * Understand the overall architecture and design principles. * Navigate the codebase and locate specific functionality. * Contribute to the project by fixing bugs or adding new features. ### 1.3. Target Audience This document is intended for software developers who are new to the DollhouseMCP Server project. It assumes a working knowledge of TypeScript, Node.js, and common software design patterns. ### 1.4. Terminology Glossary This section defines key terms used throughout the document to ensure consistent understanding: * **Element:** A self-contained unit of functionality (Persona, Skill, Template, Agent, Memory, Ensemble). Can refer to the data class, the file on disk, or the concept. * **Element Type:** Category of element (Personas, Skills, Templates, Agents, Memories, Ensembles). Represented by the `ElementType` enum. * **Ensemble:** A composite element that bundles multiple elements (personas, skills, templates, agents, memories) into a single activatable unit with coordinated policies. * **Element Manager:** Service class managing CRUD operations for a specific element type. All managers extend `BaseElementManager`. * **Handler:** Facade layer implementing the MCP tool interface. Handlers route tool calls to appropriate managers and services. * **MCP-AQL:** Model Context Protocol - Agent Query Language. The unified interface layer that consolidates all element operations into 5 CRUDE endpoints. See [MCP-AQL Architecture](mcp-aql/OVERVIEW.md). * **CRUDE:** Create, Read, Update, Delete, Execute. The five endpoint categories of MCP-AQL. Extends traditional CRUD with an Execute endpoint for agent lifecycle operations. * **Gatekeeper:** The permission enforcement layer that validates every MCP-AQL operation against active element policies. Implements a four-level permission model (AUTO_APPROVE, CONFIRM_SESSION, CONFIRM_SINGLE_USE, DENY). See [Gatekeeper Confirmation Model](../security/gatekeeper-confirmation-model.md). * **Autonomy Evaluator:** The component that scores whether an executing agent should continue autonomously or pause for human input after each step. * **Danger Zone:** Hard-block enforcement for high-risk operations. Danger Zone denials cannot be confirmed or bypassed by the LLM; the element must be deactivated or the policy changed. * **Activation Store:** Per-session persistence of element activation state. Survives MCP server restarts by writing activation records to `~/.dollhouse/state/activations-{sessionId}.json`. * **Portfolio:** The user's collection of local elements stored on the filesystem, located in `~/.dollhouse/portfolio/`. * **Collection:** The community-managed repository of elements hosted on GitHub at `DollhouseMCP/collection`. * **Service:** A reusable business logic component (e.g., `GitHubClient`, `ConfigManager`, `PortfolioManager`). * **MCP Tool:** An interface exposed to AI assistants via the Model Context Protocol. In v2, tools are exposed as MCP-AQL endpoints rather than discrete per-operation tools. * **Container:** The Dependency Injection container (`DollhouseContainer`) that manages service lifecycle and dependencies. * **Scope:** In dependency injection, defines instance lifetime—**Singleton** (one shared instance) or **Transient** (new instance per resolution). ## 2. High-Level Architecture ### 2.1. Architectural Style The DollhouseMCP Server follows a **modular monolith** architecture. The server is a single, deployable unit, but it is internally organized into a set of loosely coupled modules with well-defined responsibilities. The architecture employs dedicated responsibility domains for element management, collection services, portfolio operations, and MCP tool handling, ensuring maintainability and testability through clear separation of concerns. #### System Architecture Diagram ``` ┌─────────────────────────────────────────────────────────┐ │ MCP Protocol (stdio/JSON-RPC) │ └───────────────────────┬─────────────────────────────────┘ │ ┌────────▼────────────┐ │ DollhouseMCPServer │ │ Server Lifecycle │ └────────┬────────────┘ │ ┌──────────────┴──────────────────────────────────┐ │ MCP-AQL Layer (CRUDE Interface) │ ├─────────────────────────────────────────────────┤ │ mcp_aql_create │ mcp_aql_read │ │ mcp_aql_update │ mcp_aql_delete │ │ mcp_aql_execute │ (+ single unified mode) │ └────────┬────────────────────────────────────────┘ │ ┌────────▼────────────────────────────────────────┐ │ Security & Permission Layer │ ├─────────────────────────────────────────────────┤ │ Gatekeeper │ DangerZoneEnforcer │ │ Autonomy Evaluator │ ActivationStore │ └────────┬────────────────────────────────────────┘ │ ┌────────▼────────────────────────────────────────┐ │ Handler Layer (Facade Pattern) │ ├─────────────────────────────────────────────────┤ │ MCPAQLHandler │ ElementCRUDHandler │ │ CollectionHandler │ PortfolioHandler │ │ PersonaHandler │ EnhancedIndexHandler │ │ ConfigHandler │ ResourceHandler │ └────────┬────────────────────────────────────────┘ │ ┌────────▼────────────────────────────────────────┐ │ Element Management Layer │ ├─────────────────────────────────────────────────┤ │ PersonaManager │ PersonaElementManager │ │ SkillManager │ TemplateManager │ │ AgentManager │ MemoryManager │ │ EnsembleManager │ │ └────────┬────────────────────────────────────────┘ │ ┌────────▼────────────────────────────────────────┐ │ Service & Infrastructure Layer │ ├─────────────────────────────────────────────────┤ │ PortfolioManager │ UnifiedIndexMgr │ │ EnhancedIndexManager │ GitHubClient │ │ CollectionBrowser │ ConfigManager │ │ GitHubAuthManager │ SecurityMonitor │ └────────┬────────────────────────────────────────┘ │ ┌────────▼────────────────────────────────────────┐ │ Storage & External Services Layer │ ├─────────────────────────────────────────────────┤ │ ~/.dollhouse/portfolio/ │ GitHub API │ │ ~/.dollhouse/state/ │ Collection Cache │ │ ~/.dollhouse/pages/ │ Security Audit │ └─────────────────────────────────────────────────┘ ``` ### 2.2. Key Principles The architecture is guided by the following principles: * **Separation of Concerns (SoC):** The codebase is divided into distinct modules, each with a specific responsibility (e.g., element management, MCP tool handling, portfolio management). * **Dependency Injection (DI):** The server uses a DI container to manage the lifecycle of its components and to decouple them from each other. This makes the code more modular, testable, and maintainable. * **Single Responsibility Principle (SRP):** Each class and module has a single, well-defined responsibility. * **Design Patterns:** The system implements several industry-standard patterns (Template Method, Facade, Strategy, Repository) to ensure consistency and maintainability. ### 2.2.1. Architectural Patterns The system implements several industry-standard design patterns: * **Template Method Pattern** (`BaseElementManager`) * Defines the skeleton of element management operations in the base class * Subclasses override specific steps for element type-specific behavior * Eliminates code duplication across element managers * Example: The `load()` method provides a template with `parseMetadata()` as an overridable hook * **Facade Pattern** (Handlers) * Simplifies the complex subsystem of managers, services, and MCP protocol * Provides a single, unified interface for related operations * Handlers shield implementation details from the MCP layer * Example: `ElementCRUDHandler` wraps all element manager calls with a consistent interface * **Dependency Injection Pattern** (`DollhouseContainer`) * Decouples services from their dependencies * Centralizes service configuration and lifecycle management * Enables testability through mockable dependencies * Example: Handlers receive all dependencies via constructor injection * **Strategy Pattern** (Element-specific implementations) * Different strategies for parsing and validating different element types * Shared interface through `BaseElementManager` * Each element type implements its own parsing and validation strategy * Example: Persona vs Skill vs Template metadata parsing * **Repository Pattern** (`PortfolioManager`, `GitHubClient`) * Abstracts data source access (local filesystem, GitHub API) * Provides uniform interface for local and remote storage * Enables caching and optimization strategies transparently * Example: Fetch elements from cache or API without caller awareness * **Observer Pattern** (Security auditing) * Security monitors observe operations without intrusion * Non-intrusive security checks and logging * Audit trail generation for compliance * Example: `SecurityMonitor` tracks file access and validation events * **Singleton Pattern** (Selected services) * Managed by the DI container for lifecycle control * Single instance per process for shared state * Examples: `PortfolioManager`, `ConfigManager`, `GitHubAuthManager` ### 2.3. Technology Stack * **Language:** TypeScript * **Runtime:** Node.js * **Framework:** The server is built on top of the `@modelcontextprotocol/sdk`, which provides the core MCP server implementation. * **Key Libraries:** * `gray-matter`: For parsing YAML frontmatter from Markdown files. * `js-yaml`: For parsing YAML. * `madge`: For detecting circular dependencies. ## 3. Core Concepts ### 3.1. Elements Elements are the core building blocks of the DollhouseMCP ecosystem. Each element is a self-contained customization file — a markdown or YAML document that shapes the LLM's behavior, capabilities, or context in a specific structured way. The following element types are supported: * **Personas:** AI behavioral profiles that define the assistant's personality and communication style. Contain `instructions` (behavioral directives) as their primary field. * **Skills:** Discrete capabilities that the assistant can perform (e.g., code review, data analysis). * **Templates:** Reusable content structures with section format support (`