Aleph: MCP Server for Recursive Language Models

repository·main·Indexed 42 days ago

https://github.com/hmbown/aleph

An MCP server and skill for Recursive Language Models (RLMs) that enables LLMs to reason over large codebases, logs, and data. It maintains state—including search indexes, code execution, and evidence—in a persistent Python process outside the prompt window to prevent context exhaustion. Aleph provides tools for context loading, navigation, computation via Python/JS/TS REPLs, and recursive reasoning, with support for Claude Code, Cursor, and local models via llama.cpp.

Tokens
35.8K
Snippets
92
Records
134
Agent score
52%

What's inside Aleph

  1. Configure Aleph workspace auto-detection

    main

    If --workspace-root is not explicitly provided, Aleph follows this priority order to determine the workspace:

    1. The ALEPH_WORKSPACE_ROOT environment variable.
    2. The current working directory (PWD), falling back to INIT_CWD.
    3. A walk up from os.getcwd() to the nearest .git root.
  2. Understand Aleph's RLM Operating Modes

    main

    Aleph operates in three distinct modes based on how it interacts with Recursive Language Model (RLM) principles. Understanding these helps you choose the right tool for your workflow:

    1. Core RLM Mode: This is the paper-aligned, closed-loop execution mode. It uses the iterative root loop and REPL-style symbolic interaction.
      • Key Commands: aleph run, alef, or calling Aleph.complete(...) via the API.
    2. RLM Infrastructure Mode: This mode acts as an MCP (Model Context Protocol) external memory server. It is designed for tool-driven orchestration where a host assistant (like Claude or ChatGPT) drives the interaction.
    3. Platform Extensions: These are features that extend Aleph beyond canonical RLM research, such as swarm workflows, remote MCP orchestration, and workspace/action tooling.
  3. Manage Checkpointing and Resumability

    main

    The integration supports stateful conversations and resumability:

    • Automatic Checkpointing: build_rlm_default_graph compiles with checkpointing if a checkpointer is provided in AlephRLMConfig or if enable_checkpointing=True (which defaults to LangGraph MemorySaver).
    • Resuming Threads: Pass a thread_id to invoke_rlm(...) to resume the same thread state across multiple calls, which is essential for debugging and multi-turn interactions.
  4. Understand Aleph sub-query context delivery modes

    main

    Aleph uses two distinct modes to provide context to sub-agents during sub-queries. Choosing the right mode depends on your context size and the complexity of the query.

    1. Shared-session MCP (share_session=true):

      • How it works: Aleph starts a streamable HTTP server (default: http://127.0.0.1:8765/mcp) that exposes MCP tools like search_context, peek_context, and exec_python. The sub-query backend is configured to connect to this server.
      • Best for: Large contexts and exploratory queries where the sub-agent needs to interactively search or run code against the parent's in-memory context.
      • Requirement: The sub-query backend CLI must support per-session MCP configuration.
    2. Prompt-embedded (share_session=false):

      • How it works: The parent slices the context and pastes it directly into the sub-agent's prompt.
      • Best for: Small contexts and simple, direct questions where the answer is likely contained within a single slice.
      • Limitation: The sub-agent cannot explore beyond the provided slice and is limited by context truncation (default max_context_chars is 20,000).
  5. Control sub_aleph recursion depth

    main

    The sub_aleph tool runs a full Aleph loop inside another Aleph run. You can control the maximum allowed recursion depth using the ALEPH_MAX_DEPTH environment variable.

    • Default: 2 levels.
    • Example: Setting ALEPH_MAX_DEPTH=3 allows one additional nested layer.
    export ALEPH_MAX_DEPTH=3
  6. Enable Shared Sub-Query Sessions (Live Sandbox)

    main

    To allow CLI sub-agents spawned via sub_query to access the same live Aleph session (including tools, contexts, and sandbox state), enable streamable HTTP sharing. This spins up a local HTTP endpoint that the nested CLI points to automatically.

    Key configuration options for shared sessions:

    • --sub-query-backend: Set to codex for shared sessions.
    • --sub-query-share-session: Set to true.
    • --sub-query-codex-mode: Set to mcp to allow Aleph to talk to codex mcp-server instead of codex exec.
    • --sub-query-codex-model: Specify the model (e.g., gpt-5.4).
    • --sub-query-codex-reasoning-effort: Specify effort (e.g., low).

    Note: Tools are exposed under the server name aleph_shared to avoid conflicts with existing aleph entries.

    {
      "mcpServers": {
        "aleph": {
          "command": "aleph",
          "args": [
            "--enable-actions",
            "--workspace-mode", "any",
            "--tool-docs", "concise",
            "--sub-query-backend", "codex",
            "--sub-query-share-session", "true",
            "--sub-query-timeout", "300",
            "--sub-query-codex-mode", "mcp",
            "--sub-query-codex-model", "gpt-5.4",
            "--sub-query-codex-reasoning-effort", "low"
          ]
        }
      }
    }
  7. Understand the /aleph External Memory Workflow

    main

    The /aleph skill is an external memory workflow designed for handling large local data (codebases, large logs, etc.) without overwhelming the LLM prompt. The core principle is to keep whole contexts out of the prompt, returning only focused slices or compact derived results.

    The workflow follows a 5-Phase Loop:

    1. Load: Use the appropriate entry point (load_workspace_manifest for repos, load_file for single files, or load_context for inline content).
    2. Orient: Locate relevant information using search_context, peek_context, semantic_search, chunk_context, or rg_search.
    3. Compute: Perform analysis using exec_python or built-in helpers (search, peek, lines, etc.). Store results in variables and retrieve them via get_variable.
    4. Recurse: Use sub-querying tools (sub_query, sub_query_batch, sub_query_map, sub_aleph) to handle complex, multi-step reasoning.
    5. Converge: Stabilize the answer using evaluate_progress, summarize_so_far, or finalize.
  8. Configure the sub_query tool backend

    main

    The sub_query tool enables recursive reasoning by spawning independent sub-agents. You can configure which backend these agents use via the ALEPH_SUB_QUERY_BACKEND environment variable or CLI flags.

    Available Backends:

    • codex: The recommended first-class backend for nested MCP sub-queries and shared-session recursion.
    • api: A fallback for OpenAI-compatible endpoints.
    • claude: A stable fallback for general sub-queries.
    • gemini: An experimental override with live Aleph MCP access.
    • kimi: An experimental override.
    • auto: The default mode. It attempts to use codex if installed, otherwise falls back to api.

    Selection Precedence:

    1. Programmatic configuration (SubQueryConfig or configure).
    2. ALEPH_SUB_QUERY_BACKEND environment variable.
    3. Auto-detection (codex -> api).
    export ALEPH_SUB_QUERY_BACKEND=api      # Force API backend
    export ALEPH_SUB_QUERY_BACKEND=claude   # Force Claude CLI
    export ALEPH_SUB_QUERY_BACKEND=codex    # Force Codex CLI
    export ALEPH_SUB_QUERY_BACKEND=gemini   # Force Gemini CLI
    export ALEPH_SUB_QUERY_BACKEND=kimi     # Force Kimi CLI
    export ALEPH_SUB_QUERY_BACKEND=auto     # Return to auto selection
  9. Avoid Aleph Anti-Patterns

    main

    To maintain effective RLM performance and avoid prompt bloating, avoid these common mistakes:

    • Incorrect Entry Points: Do not use read_file for large files or repos; use load_workspace_manifest instead.
    • Context Dumping: Do not paste raw content into the prompt; use search or compute instead.
    • Invalid Variable Access: Never attempt to use get_variable("ctx") as it is explicitly blocked.
    • Misunderstanding Output: Do not assume exec_python only returns print() output; it returns a full feedback object (stdout, stderr, etc.).
    • Hardcoding Backends: Do not pin specific sub-query backends in the plugin wrapper; keep the configuration portable.
  10. How Aleph recursion depths work

    main

    Aleph uses a depth-based strategy to handle different data sizes and complexities. You can request a specific depth using the syntax /aleph N file where N is the depth.

    DepthStrategyDescription
    1Direct AnalysisNo sub-agents. Uses search, peek, or exec_python directly. Best for < 200K chars.
    2Parallel Fan-outUses sub_query_batch or sub_query_map over chunks. Best for 200K-2M chars.
    3Recursive (RLM^2)Sub-agents use exec_python and sub_query internally. Best for 2M+ chars.
    4Deep Recursion (RLM^3)Sub-agents spawn their own sub-agents. Best for massive datasets.

    Important: For depth 3 or higher, you must increase timeouts using configure(sub_query_timeout=300, sandbox_timeout=300) to prevent execution failures.

    # Example of configuring timeouts for deep recursion
    configure(sub_query_timeout=300, sandbox_timeout=300)
  11. Understand Aleph context isolation and MCP response bounds

    main

    Aleph implements context isolation to prevent large in-memory sessions from overwhelming the model's context window via Model Context Protocol (MCP) tool responses.

    Key Constraints:

    • Server-side Context: The raw session context (ctx) is kept server-side. Accessing it via get_variable("ctx") is explicitly blocked.
    • Response Caps: MCP tool responses are capped by default at 10_000 characters (max_tool_response_chars). If a response exceeds this, it is appended with the truncation marker ... [TRUNCATED].
    • Tool Bounding: Tools used for querying or navigating context—such as peek_context, search_context, semantic_search, and diff_contexts—are bounded at the MCP boundary to ensure they do not return massive payloads.
    • Sub-query Context: When performing sub-query context embedding, the process is explicit and bounded by max_context_chars (defaulting to 20_000).
  12. Choose between Python and JS/TS REPLs

    main

    Aleph provides different execution environments (REPLs) depending on your analysis needs:

    Use exec_python for:

    • General-purpose analysis and orchestration: It is the primary control layer and the default REPL.
    • Full Aleph surface area: Access to all Aleph helper functions and the recipe DSL.
    • Python ecosystem: Using numeric/symbolic stacks like numpy, scipy, sympy, networkx, etc.
    • Recipe execution: Running existing recipes via run_recipe_code.
    • Synchronous sub-queries: Using sub_query(...) or sub_aleph(...) synchronously.

    Use exec_javascript or exec_typescript for:

    • JS/TS-heavy repositories: When the target code is naturally JavaScript or TypeScript.
    • Persistent Node state: Maintaining state across calls in a Node.js runtime.
    • Async workflows: Using await for recursion with await sub_query(...), await sub_query_batch(...), await sub_query_map(...), await sub_query_strict(...), or await sub_aleph(...).
    • JS-native manipulation: Working with JS-native arrays and objects.
    • Recipe building: Using the JS/TS Recipe DSL to build and compile recipe payloads (Note: execution of recipes still uses the Python runtime).
    • Extensive local helpers: Access to specialized extraction (e.g., extract_emails, extract_routes), text utilities (grep_v, diff), collection helpers (group_by, frequency), and semantic_search.