Introducing a new way to review your PRs.

Read the announcement

oraios / serena

A powerful MCP toolkit for coding, providing semantic retrieval and editing capabilities - the IDE for your agent

View on GitHub

13 documents · Updated Nov 16, 2026

docs / serena-configuration-and-cli

Serena Configuration and CLI

Coverage: src/…/cli.py, src/serena/config/**, src/serena/resources/config/**, src/…/project.local.template.yml, src/…/project.template.yml, src/…/serena_config.template.yml, .serena/**, .env.example.

Overview

Serena's configuration and CLI layer is centered on src/…/cli.py, which exposes the serena entrypoint and wires subcommand groups for startup, config editing, project lifecycle, contexts, modes, prompts, tools, and memories. The config objects behind those commands live in src/…/serena_config.py; filesystem locations are centralized in src/…/serena_paths.py; context and mode YAML loading is implemented in src/…/context_mode.py; and MCP client auto-registration helpers are in src/…/client_setup.py.

The global user home is resolved by SerenaPaths in src/…/serena_paths.py. It uses $SERENA_HOME when set and non-empty, otherwise defaults to ~/.serena. This home holds the global serena_config.yml, user-defined contexts/, modes/, prompt_templates/, memories/global/, news cache files, and dated log files. SerenaPaths.get_next_log_file_path() creates logs under <SERENA_HOME>/logs/YYYY-MM-DD/ and is used by serena start-mcp-server and serena start-project-server in src/…/cli.py.

SerenaConfig.from_config_file() in src/…/serena_config.py is the global config loader. It resolves <SERENA_HOME>/serena_config.yml, migrates an old repository-root serena_config.yml if present, generates a missing file from src/…/serena_config.template.yml, restricts POSIX permissions to 0600, loads YAML while preserving comments, performs legacy migrations, and instantiates registered projects from the projects path list. The template defines global defaults for language_backend, agent_interface, dashboard settings, JetBrains plugin settings, log level, LSP tracing, language-server-specific settings, language-server auto-detection priorities, global ignored paths, memory filtering patterns, tool/API inclusions and exclusions, base/default modes, token counting, per-project Serena folder location, trusted project path patterns, auth_secret, and the project registry. A missing or empty auth_secret is replaced with a persisted UUID during load.

Project configuration is represented by ProjectConfig in src/…/serena_config.py. serena project create and serena project index in src/…/cli.py call ProjectConfig.autogenerate(), which copies src/…/project.template.yml, fills project_name and detected or requested language_servers, writes the project file to the configured project Serena folder, and seeds project.local.yml from src/…/project.local.template.yml. The project template controls language servers, encoding, activation commands, line endings, per-project backend/interface overrides, gitignore use, LSP workspace folders, ignored paths, read-only mode, tool/API inclusion rules, default and added modes, initial prompt, symbol info budget, and memory filters. project.local.yml is an intentionally sparse local override file; ProjectConfig.load() overlays any keys present there onto project.yml, tracks those override keys, and ProjectConfig.save() writes overridden keys back only to the local file while preserving template comments in the shared project file.

Per-project data folder resolution is global-configurable. SerenaConfig.project_serena_folder_location defaults to $projectDir/.serena, but get_configured_project_serena_folder() substitutes $projectDir and $projectFolderName, rejects unknown placeholders, and returns an absolute path. get_project_serena_folder() first uses an existing configured folder, then falls back to an existing in-project .serena, and otherwise returns the configured path for creation. get_project_yml_location() appends project.yml to that resolved folder. The repository's own .serena/project.yml is a concrete project config for this repo, naming the project serena, enabling Python and TypeScript language servers, embedding mem:critical_info in initial_prompt, and carrying current schema keys such as agent_interface, ls_workspace_folders, ls_additional_workspace_folders, and REPL API include/exclude lists.

Mode and context definitions are YAML-backed. SerenaAgentMode and SerenaAgentContext in src/…/context_mode.py can load either a registered name or a path that contains a separator or ends in .yml/.yaml. User YAMLs under <SERENA_HOME>/modes and <SERENA_HOME>/contexts override same-named internal files under src/serena/resources/config/modes and src/serena/resources/config/contexts. Mode YAMLs contain description, optional Jinja prompt text, tool inclusion fields, and REPL API inclusion fields; context YAMLs add tool_description_overrides, single_project, and structured_tool_output. Built-in modes include interactive, editing, planning, one-shot, benchmark, onboarding, no-onboarding, no-memories, and query-projects; the JetBrains backend has an internal mode at src/…/jetbrains.yml that disables LSP symbolic tools and enables JetBrains-specific alternatives. Built-in contexts tailor Serena for CLI/IDE/desktop clients such as agent, claude-code, codex, codebuddy, grok, vscode, chatgpt, and JetBrains assistants.

The MCP startup flow begins in TopLevelCommands.start_mcp_server() in src/…/cli.py. It initializes stderr, memory, and file logging before config-dependent objects are constructed. Startup accepts project selection by path/name (--project, deprecated --project-file, or deprecated positional project), context selection, --mode replacement modes, --add-mode additive modes, backend/interface overrides, transport settings, dashboard/log/window overrides, LSP tracing, tool timeout, and --project-from-cwd. find_project_root() walks from the current directory upward and returns the nearest ancestor with .serena/project.yml or .git; this prevents a nested git worktree from being captured by an outer Serena project. The command passes mode selection and overrides to SerenaMCPFactory.create_mcp_server(), and records a warning to be surfaced later if --project-from-cwd found no root.

Other CLI commands share the same config layer. serena init calls SerenaConfig.init() to create/load the global config and set the chosen built-in backend. serena config edit opens the generated global config. serena mode and serena context subcommands list internal and user YAMLs, create custom YAMLs from templates or internal definitions, edit only user files, and delete only user files. serena prompts commands manage user prompt YAML overrides under <SERENA_HOME>/prompt_templates, merging them with internal prompt templates in src/serena/resources/config/prompt_templates. serena print-system-prompt loads the requested context and optional modes, applies with_headless_mode_overrides() to disable dashboard and GUI behavior, and instantiates a SerenaAgent to render the startup instructions.

Client setup is intentionally thin. src/…/client_setup.py defines handlers for Claude Code, CodeBuddy, Codex, and Grok. Each handler detects client availability by running the client's version/help command, produces a serena start-mcp-server command with a client-specific context and --project-from-cwd, and applies the client's native MCP registration command. serena init reports applicable setup commands, and serena setup <client> validates applicability before applying the matching handler.

Coupling

  • If a field is added to SerenaConfig or ProjectConfig in src/…/serena_config.py, also update src/…/serena_config.template.yml or src/…/project.template.yml. The loaders apply dataclass defaults for missing fields and save migrated files with transferred template comments, so an omitted template entry can silently create undocumented config that later appears in user files.
  • If project-local override semantics change in ProjectConfig.load() or ProjectConfig.save() in src/…/serena_config.py, verify src/…/project.local.template.yml still explains the safe workflow. The loader tracks only keys present in project.local.yml, and save intentionally avoids overwriting those keys in shared project.yml.
  • If context or mode YAML schemas change in src/…/context_mode.py, update both src/…/context.template.yml and src/…/mode.template.yml, plus any built-in YAMLs using the affected fields. These files are loaded with yaml.safe_load() and passed directly into dataclass constructors, so stale keys or missing required fields fail at startup rather than through a separate schema validator.
  • If project_serena_folder_location behavior changes in src/…/serena_config.py, check find_project_root() and project CLI commands in src/…/cli.py. --project-from-cwd only detects an in-project .serena/project.yml or .git, while actual project config loading may resolve project.yml to a centralized configured folder.
  • If client-specific context names or startup flags change in src/…/*.yml, update src/…/client_setup.py. The setup handlers hard-code --context=<client> and --project-from-cwd into registered MCP commands, so a context rename or single-project behavior change can break newly registered clients.
  • If tool or REPL API inclusion fields change in src/…/serena_config.py, update global config, project template, mode template, and context template together. The same inclusion dataclasses are shared by global config, project config, modes, and contexts, but their YAML examples live in separate resource files.

Risks

  • src/…/serena_config.py persists global project registry changes by reloading the on-disk config under <config>.lock in _persist_projects(). If future code bypasses add_registered_project() or remove_registered_project() and calls _save() on a runtime config with transient CLI overrides, it can persist temporary settings or lose concurrent registry edits.
  • src/…/context_mode.py treats any string containing a path separator or ending in .yml/.yaml as a file path. If a built-in context or mode name is introduced with those characters, SerenaAgentContext.load() or SerenaAgentMode.load() will look for a file instead of resolving the registered name.
  • src/…/cli.py --project-from-cwd detection does not consult the configurable centralized project_serena_folder_location from src/…/serena_config.py; it only sees .serena/project.yml in ancestors or .git. In deployments that store project metadata outside the project tree, cwd auto-detection falls back to the git root and may start without an existing generated project config until later activation logic handles it.