cocoindex-code

repository·main·Indexed 25 days ago

https://github.com/cocoindex-io/cocoindex-code

An MCP server and CLI tool for indexing and querying codebases using embedding models. It supports local providers like SentenceTransformers and Ollama, as well as remote providers via LiteLLM (OpenAI, Gemini, Cohere). The tool provides semantic search via the `ccc search` command and an MCP `search` tool, as well as structural code search using `ccc grep`. It integrates with coding agents such as Claude Code and Grok.

Tokens
16.7K
Snippets
35
Records
94
Agent score
78%

What's inside cocoindex-code

  1. Perform structural code search with `ccc grep`

    main

    Unlike semantic search, ccc grep finds code by its structure using a by-example pattern that matches the syntax tree. It does not require an index, daemon, or embeddings and runs entirely locally. It uses metavariables with the \ sigil:

    • \NAME: Captures one node.
    • \(NAME*\): Captures a run of siblings.
    • \_ or \*: Matches anonymously.

    Patterns are matched per language; a single invocation scans all supported source files. Inside an initialized project, it honors .gitignore and include/exclude patterns.

  2. Choose an embedding provider path

    main

    Depending on your hardware, privacy needs, and codebase size, choose one of three paths:

    1. Local Sentence-Transformers: Best for most users and laptops. It is the fastest (in-process), private, and works offline. Requires installing the [full] extra.
    2. Cloud LiteLLM Remote: Best for large codebases or weak local hardware. Offers top performance with zero local resource usage, but involves per-token costs and data leaves your machine.
    3. Local LiteLLM: Best for power users with shared GPU resources. Provides flexibility and unified model management but requires managing a separate server (like Ollama or llama.cpp).
  3. Understand embedding model performance for code search

    main

    When choosing an embedding model for code search in cocoindex-code, consider the following performance characteristics:

    Architecture and CPU Speed

    • Encoder models (e.g., BERT/ModernBERT-based) process tokens in parallel and are significantly faster on CPU.
    • Decoder models (LLM-based) process tokens sequentially and can be 3–10× slower on CPU than encoder models of the same parameter count.

    Model Tiers

    Models are categorized by parameter count:

    • Micro: < 50M parameters (e.g., lightonai/LateOn-Code-edge)
    • Small: < 150M parameters (e.g., lightonai/LateOn-Code)
    • Medium: < 500M parameters (e.g., jinaai/jina-embeddings-v5-text-nano)
    • Large: > 500M parameters (e.g., microsoft/harrier-oss-v1-27b)

    Default Model

    The snowflake-arctic-embed-xs model is the default model used in cocoindex-code. It is an encoder-based model that is highly efficient on CPU.

  4. Configure Asymmetric Embedding Parameters

    main

    Some embedding models require different settings for indexing documents versus querying them (asymmetric retrieval). You can configure these using indexing_params and query_params under the embedding key in global_settings.yml.

    • indexing_params: Parameters used when embedding corpus content.
    • query_params: Parameters used when embedding a user query.

    Supported keys depend on the provider:

    • LiteLLM: Uses input_type (e.g., search_document or search_query).
    • SentenceTransformers: Uses prompt_name (e.g., passage or query).

    ccc doctor validates both sides of the configuration independently to ensure both indexing and query modes are correctly configured.

    embedding:
      provider: litellm
      model: cohere/embed-english-v3.0
      indexing_params:
        input_type: search_document
      query_params:
        input_type: search_query
  5. Integrate cocoindex-code with coding agents via Skills

    main

    The recommended way to use cocoindex-code is by installing the ccc skill. This allows coding agents (like Claude Code) to automatically handle initialization, indexing, and searching without manual configuration. The agent will automatically keep the index up to date as you work.

    To install the skill, use:

    npx skills add cocoindex-io/cocoindex-code

    You can nudge the agent to use semantic search by asking questions like "find how user sessions are managed" or by typing /ccc to invoke the skill directly.

  6. Filter ccc search results by language or path

    main

    You can narrow down ccc search results using language filters or path glob patterns.

    • By language (--lang): Use this flag multiple times to restrict results to specific languages.
    • By path (--path): Restrict results to a glob pattern relative to the project root. If omitted, it defaults to the current working directory.

    Examples:

  7. Connect a coding agent via MCP

    main

    You can connect coding agents like Claude Code or Codex to the cocoindex-code Docker container using the Model Context Protocol (MCP).

    Important: When using docker exec for MCP, use the -i flag (interactive) but do not use -it. The -t flag allocates a terminal which interferes with MCP's JSON messaging over stdin/stdout.

    Claude Code Example:

    claude mcp add cocoindex-code -- docker exec -i \
      -e COCOINDEX_CODE_HOST_CWD="$PWD" cocoindex-code ccc mcp

    Codex Example:

    codex mcp add cocoindex-code -- docker exec -i \
      -e COCOINDEX_CODE_HOST_CWD="$PWD" cocoindex-code ccc mcp
    {
      "mcpServers": {
        "cocoindex-code": {
          "type": "stdio",
          "command": "docker",
          "args": [
            "exec",
            "-i",
            "-e",
            "COCOINDEX_CODE_HOST_CWD=${PWD}",
            "cocoindex-code",
            "ccc",
            "mcp"
          ]
        }
      }
    }
  8. Install cocoindex-code via pipx or uv

    main

    You can install cocoindex-code using pipx or uv. There are two installation styles depending on whether you want local embeddings or a slim version that requires a cloud provider.

    Installation Styles

    • cocoindex-code[full] (Batteries-included): Includes sentence-transformers for local embeddings. No API key is required. The ccc init prompt defaults to Snowflake/snowflake-arctic-embed-xs.
    • cocoindex-code (Slim): LiteLLM-only. Requires a cloud embedding provider and an API key. Use this to avoid the ~1 GB dependency of torch and transformers.

    Commands

    Using pipx:

    pipx install 'cocoindex-code[full']          # batteries included (local embeddings)
    pipx upgrade cocoindex-code                  # upgrade

    Using uv:

    uv tool install --upgrade 'cocoindex-code[full]'
    pipx install 'cocoindex-code[full]'
  9. Use ccc for Semantic Code Search & Indexing

    main

    The ccc CLI provides semantic search over your codebase and manages the code index. It is designed to find concepts, functionality, or behaviors rather than exact syntax matches.

    Key Lifecycle Responsibilities:

    • Initialization: If you encounter an initialization error (e.g., "Not in an initialized project directory"), run ccc init from the project root, then ccc index to build the index, and retry your command.
    • Index Freshness: Run ccc index or ccc search --refresh when the index might be stale, such as at the start of a session or after significant code changes (new files, refactors, or renamed modules).
  10. Quick start with Docker Compose

    main

    You can bring up the environment using a single command. This mounts your home directory by default, allowing the container to access your code. Index data and embedding models persist in a Docker volume.

    macOS / Windows:

    docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex-code/refs/heads/main/docker/docker-compose.yml) up -d

    Linux: (Aligns file ownership with your host user)

    PUID=$(id -u) PGID=$(id -g) docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex-code/refs/heads/main/docker/docker-compose.yml) up -d

    Customizing the image: Set COCOINDEX_CODE_IMAGE to override the default (e.g., to use the :full variant).

    COCOINDEX_CODE_IMAGE=cocoindex/cocoindex-code:full docker compose up -d
    # macOS / Windows
    docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex-code/refs/heads/main/docker/docker-compose.yml) up -d
    
    # Linux
    PUID=$(id -u) PGID=$(id -g) docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex-code/refs/heads/main/docker/docker-compose.yml) up -d
  11. Configure Embedding Models

    main

    CocoIndex supports various embedding providers via global_settings.yml.

    Local Models (SentenceTransformers)

    Set provider: sentence-transformers. No API key is required.

    • General Purpose: nomic-ai/nomic-embed-text-v1.5
    • Code Optimized: nomic-ai/CodeRankEmbed (requires ~1 GB VRAM).

    API-based Models (LiteLLM)

    For most API providers, set provider: litellm (or omit it).

    Common Provider Configurations:

    Ollama (Local):

    embedding:
      model: ollama/nomic-embed-text
    # Set OLLAMA_API_BASE in envs if not localhost:11434

    OpenAI-compatible (vLLM, LM Studio, etc.):

    embedding:
      model: openai/your-model-name
    envs:
      OPENAI_BASE_URL: https://your-endpoint/v1
      OPENAI_API_KEY: your-api-key

    Azure OpenAI:

    embedding:
      model: azure/your-deployment-name
    envs:
      AZURE_API_KEY: your-api-key
      AZURE_API_BASE: https://your-resource.openai.azure.com
      AZURE_API_VERSION: "2024-06-01"

    Other Providers:

    • Gemini: model: gemini/gemini-embedding-001
    • Voyage: model: voyage/voyage-code-3
    • Cohere: model: cohere/embed-v4.0
    • AWS Bedrock: model: bedrock/amazon.titan-embed-text-v2:0

    Important: Switching embedding models changes the vector dimensions. You must run ccc reset && ccc index after changing models.

    embedding:
      provider: sentence-transformers
      model: nomic-ai/CodeRankEmbed