Serena: The IDE for Coding Agents

repository·main·Indexed 31 days ago

https://github.com/oraios/serena

An MCP toolkit providing semantic, symbol-aware tools for code retrieval, editing, and refactoring. Serena bridges the gap between raw text manipulation and IDE-level understanding for AI agents using either open-source Language Servers (LSP) or the Serena JetBrains Plugin via the Model Context Protocol (MCP).

Tokens
35.7K
Snippets
60
Records
174
Agent score
97%

What's inside serena-agent

  1. Overview of Serena: The IDE for Coding Agents

    main

    Serena is a toolset designed for AI coding agents, providing semantic code retrieval, editing, refactoring, and debugging capabilities. Unlike tools that rely on low-level line numbers or primitive text search, Serena operates at the symbol level, exploiting the relational structure of codebases to make agent workflows faster and more reliable.

    Key characteristics:

    • Agent-First Design: Uses high-level abstractions for complex tasks like cross-file renames and reference lookups.
    • MCP Integration: Integrates with any client/LLM via the Model Context Protocol (MCP), including terminal clients (Claude Code, Codex), IDEs (VSCode, Cursor, JetBrains), and desktop/web clients (Claude Desktop, OpenWebUI).
    • Dual Backends: Supports either open-source Language Servers (LSP) for over 40 languages or the premium Serena JetBrains Plugin for advanced IDE-level analysis.
  2. Understand Serena's language support mechanisms

    main

    Serena provides symbolic code querying and editing capabilities using two primary technologies:

    1. Language Servers (LSP): An open-source approach that integrates with various Language Server Protocol implementations. Serena supports multiple language servers in parallel, enabling polyglot project support.
    2. Serena JetBrains Plugin: Leverages the deep code analysis and refactoring capabilities of JetBrains IDEs. This plugin supports any language or framework already supported by your JetBrains IDE and integrates seamlessly with your existing IDE instance.

    For a detailed comparison of capabilities between the JetBrains Plugin and LSP-based servers, refer to the Features section.

  3. Understand Serena's Memory System

    main

    Serena uses a Markdown-based memory system that both humans and AI agents can read, create, and edit. Memories are stored in two scopes:

    • Project-specific memories: Stored in .serena/memories/ within your project folder. These can be versioned and committed to your repository.
    • Global memories: Shared across all projects, stored in ~/.serena/memories/global/. Use the global/ prefix in memory names to target this scope.

    Agents are informed of available memory names at startup and decide which ones to read based on the names and internal references.

  4. Identify Serena's core dependencies

    main

    Serena relies on two primary specialized libraries for its core functionality:

    • solidlsp: A fork of microsoft/multilspy optimized for fully synchronous language server communication.
    • interprompt: A prompt templating library used for managing agent prompts.
  5. Use Serena with non-JetBrains editors via JetBrains Plugin

    main

    If you do not use a JetBrains IDE as your primary editor, you can still leverage the deep integration capabilities of the Serena JetBrains Plugin by running a JetBrains IDE instance in the background.

    To use this setup:

    1. Open your project in a JetBrains IDE (the Community Edition is sufficient for this purpose).
    2. Ensure the project is fully indexed by the JetBrains IDE.
    3. Serena will then be able to connect to the running JetBrains instance to access its symbolic and semantic intelligence, even while you perform your primary coding work in a different editor.
  6. Use Git worktrees with Serena

    main

    To parallelize tasks, you can use git-worktree alongside Serena. To ensure that project-specific settings, configurations, and memories are shared across different worktrees, you must add the .serena folder to your version control system.

    When launching a CLI agent from within a worktree using the --project-from-cwd flag, Serena will activate the worktree itself. Even if the worktree is located within another Serena project directory (e.g., inside a .claude/worktrees/ folder), the nearest project boundary takes precedence. The worktree's own .git pointer file will override any ancestor's .serena/project.yml, ensuring that all file operations resolve correctly against the specific worktree.

  7. Configure Serena for VSCode via MCP hooks

    main

    To prevent 'agent drift' in VSCode (where the agent forgets Serena's instructions or fails to load tools), it is strongly recommended to set up reminder hooks. These hooks use remind to nudge the agent toward symbolic tools, activate to prompt project activation at session start, and cleanup to manage session data.

    Create the file ~/.copilot/hooks/serena-hooks.json with the following content:

    {
        "hooks": {
            "PreToolUse": [
                {
                    "type": "command",
                    "command": "serena-hooks remind --client=vscode"
                }
            ],
            "SessionStart": [
                {
                    "type": "command",
                    "command": "serena-hooks activate --client=vscode"
                }
            ],
            "Stop": [
                {
                    "type": "command",
                    "command": "serena-hooks cleanup --client=vscode"
                }
            ]
        }
    }
  8. Use Serena with Terminal-based and IDE-based MCP clients

    main

    Serena can be integrated with various MCP-enabled clients. For many of these clients, it is recommended to use the ide context to reduce tool duplication.

    Terminal-Based Clients

    Clients like Gemini-CLI, Qwen3-Coder, rovodev, OpenHands CLI, opencode, and CodeBuddy-Code benefit from Serena's symbolic tools. When setting them up, add the argument --context ide to the Serena command.

    IDEs and Coding Extensions

    For IDE extensions and AI-enabled IDEs such as Cline, Roo-Code, Cursor, and Windsurf, Serena provides efficient tools for symbolic operations. It is recommended to use the ide context by adding the --context ide argument.

    Local GUIs and Agent Frameworks

    Serena works out of the box with several local GUI applications and agent frameworks, including:

    • Jan
    • OpenHands
    • OpenWebUI
    • Agno
  9. Set up C/C++ support with clangd or ccls

    main

    To provide reliable code intelligence for C/C++ projects, Serena requires a compile_commands.json file at the repository root. This file must include proper C++ standard flags (e.g., -std=c++17) and all necessary include paths (-I flags) for cross-file reference finding.

    Using clangd (Default)

    Serena automatically downloads and manages clangd. Because clangd requires absolute paths, Serena will automatically detect relative paths in your compile_commands.json and transform them into absolute paths, writing a new file to .serena/compile_commands.json by default.

    Using ccls

    ccls requires manual installation and can be used if it performs better for your specific environment. Unlike clangd, ccls handles relative paths in compile_commands.json natively, so no transformed file is created.

    Installation Commands:

    Linux:

    # Ubuntu/Debian (22.04+)
    sudo apt install ccls
    
    # Fedora/RHEL
    sudo dnf install ccls
    
    # Arch Linux
    sudo pacman -S ccls

    macOS:

    brew install ccls

    Windows:

    choco install ccls
  10. Set up Serena for Grok

    main

    Serena provides native support for xAI's Grok Build CLI. Run: serena setup grok.

    Manual Global Setup: grok mcp add --scope user serena -- serena start-mcp-server --context=grok --project-from-cwd

    Manual Project-Level Setup: grok mcp add --scope project serena -- serena start-mcp-server --context=grok --project "$(pwd)"

    Verification: Run grok inspect to verify Serena is listed.

    Hooks: To enable hooks globally, create ~/.grok/hooks/serena-hooks.json. For a single project, use .grok/hooks/serena-hooks.json (requires /hooks-trust in Grok). Note that Grok ignores SessionStart stdout, so only PreToolUse and Stop are used:

    {
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "grep|read_file|run_terminal_command",
            "hooks": [
              {
                "type": "command",
                "command": "serena-hooks remind --client=grok",
                "timeout": 5
              }
            ]
          }
        ],
        "Stop": [
          {
            "hooks": [
              {
                "type": "command",
                "command": "serena-hooks cleanup --client=grok",
                "timeout": 5
              }
            ]
          }
        ]
      }
    }
  11. Compare Serena vs. Built-in tools for code editing

    main

    When deciding between using Serena's semantic tools and standard built-in text tools (like search_replace or open), follow these usage rules based on the task type:

    Use Serena for:

    • Move refactoring: Moving symbols or files while automatically updating imports (unique capability).
    • Safe/Propagated deletion: Using safe_delete to prevent accidental breakage by checking for usages, or performing propagated deletes.
    • Semantic navigation: Exploring type hierarchies, looking up external dependency symbols, or finding code references via find_referencing_symbols.
    • Structural edits: Using insert_after_symbol or replace_symbol_body when you have a stable name path.
    • Chained edits: When performing multiple edits in a single file, Serena's name-path addressing remains stable even as line numbers shift.

    Use Built-in tools for:

    • Small text edits: For 1-3 line changes, built-in search_replace is often more token-efficient.
    • Non-code files: Reading or editing configs, docs, or notebooks.
    • Free-text search: Searching for log strings, URLs, or magic constants using search_project.
    • File/System operations: Creating new files, directory listing, or running terminal commands (e.g., git, pytest).
  12. Use Route 1 (Recommended) to generate a Clang database for Unreal Engine

    main

    This is the preferred method as it emits clang-native commands, allowing clangd to resolve system and engine headers accurately using system-include paths.

    Requirements:

    • Install the "C++ Clang tools for Windows" component via the Visual Studio Installer (Modify > Individual Components).
    • Ensure the project has been built at least once so *.generated.h headers exist.

    Command: Run the following from your engine directory, replacing placeholders with your project details. The -OutputDir flag is mandatory to prevent the file from landing in the engine root.

    <Engine>\Binaries\DotNET\UnrealBuildTool\UnrealBuildTool.exe -mode=GenerateClangDatabase -project="<YourProject>.uproject" <YourProject>Editor Win64 Development -OutputDir="<YourProject's directory>"

    Note: You can add -NoExecCodeGenActions to the command to skip redundant code generation if you have already built the target.