Using Serena
This guide covers using Serena, a powerful coding agent toolkit that provides semantic code retrieval and editing capabilities to agentic workflows.
What is Serena?
Section titled “What is Serena?”Serena is an MCP server that enhances AI agents with IDE-like tools for understanding and manipulating code. Instead of reading entire files or performing text-based searches, agents can use Serena to:
- Find symbols - Locate functions, classes, types, and variables by name
- Navigate relationships - Discover references, implementations, and dependencies
- Edit at symbol level - Insert, replace, or modify code entities precisely
- Analyze semantically - Understand code structure using language servers
Serena supports 30+ programming languages through Language Server Protocol (LSP) integration, including Go, Python, TypeScript, JavaScript, Rust, Java, C/C++, and many more.
Quick Start
Section titled “Quick Start”Basic Configuration
Section titled “Basic Configuration”Add Serena to your workflow using the short syntax with a list of languages:
---engine: copilotpermissions: contents: readtools: serena: ["go", "typescript", "python"]---This enables Serena for Go, TypeScript, and Python code analysis.
Common Use Cases
Section titled “Common Use Cases”Code analysis workflow:
---engine: copilotpermissions: contents: readtools: serena: ["go"] github: toolsets: [default]---
# Code Quality Analyzer
Analyze Go code in this repository and provide recommendations:
1. Use Serena to find all exported functions2. Check for missing documentation3. Identify code patterns and suggest improvementsCode refactoring workflow:
---engine: claudepermissions: contents: writetools: serena: ["typescript", "javascript"] edit:---
# Refactor TypeScript Code
Refactor the codebase to use modern TypeScript features:
1. Find all class declarations2. Convert to functional components where appropriate3. Update type definitionsConfiguration Options
Section titled “Configuration Options”Short Syntax (Array)
Section titled “Short Syntax (Array)”The simplest way to configure Serena - just list the languages:
tools: serena: ["go", "typescript"]This uses default settings for each language server.
Long Syntax (Detailed Configuration)
Section titled “Long Syntax (Detailed Configuration)”For fine-grained control over language servers:
tools: serena: version: latest args: ["--verbose"] languages: go: version: "1.21" go-mod-file: "go.mod" gopls-version: "v0.14.2" typescript: python: version: "3.12"Configuration fields:
version- Serena version to use (default:latest)args- Additional command-line arguments (e.g.,["--verbose"])languages- Language-specific configuration
Language-specific options:
For Go:
version- Go runtime versiongo-mod-file- Path togo.mod(default:"go.mod")gopls-version- gopls language server version (default:latest)
For Python:
version- Python runtime version
For TypeScript/JavaScript:
- No additional configuration required (uses default language server)
Custom Go Module Path
Section titled “Custom Go Module Path”For projects with go.mod in a subdirectory:
tools: serena: languages: go: go-mod-file: "backend/go.mod" gopls-version: "latest"Language Support
Section titled “Language Support”Serena supports 30+ programming languages through Language Server Protocol (LSP):
| Category | Languages |
|---|---|
| Systems | C, C++, Rust, Go, Zig |
| JVM | Java, Kotlin, Scala, Groovy (partial) |
| Web | JavaScript, TypeScript, Dart, Elm |
| Dynamic | Python, Ruby, PHP, Perl, Lua |
| Functional | Haskell, Elixir, Erlang, Clojure, OCaml |
| Scientific | R, Julia, MATLAB, Fortran |
| Shell | Bash, PowerShell |
| Other | C#, Swift, Nix, Markdown, YAML, TOML |
Available Tools
Section titled “Available Tools”When Serena is enabled, the agent has access to these semantic code tools:
Symbol Navigation
Section titled “Symbol Navigation”find_symbol- Search for functions, classes, types, and variables by namefind_referencing_symbols- Find all references to a symbolget_symbol_definition- Get the full definition of a symbollist_symbols_in_file- List all symbols defined in a file
Code Editing
Section titled “Code Editing”replace_symbol_body- Replace the implementation of a function or methodinsert_after_symbol- Add code after a specific symbolinsert_before_symbol- Add code before a specific symboldelete_symbol- Remove a symbol definition
Project Analysis
Section titled “Project Analysis”find_files- Locate files matching patternsget_project_structure- Analyze directory structureanalyze_imports- Examine import dependencies
Memory Configuration
Section titled “Memory Configuration”Serena maintains analysis state in memory for faster operations. Configure memory location:
---tools: serena: ["go"] cache-memory: key: serena-analysis---
# In your workflow instructions:# Memory location: /tmp/gh-aw/cache-memory/serena/The agent should create this directory before using Serena:
mkdir -p /tmp/gh-aw/cache-memory/serenaThis caches language server indexes and analysis results for improved performance.
Practical Examples
Section titled “Practical Examples”Example 1: Finding Unused Functions
Section titled “Example 1: Finding Unused Functions”---engine: copilottools: serena: ["go"] github: toolsets: [default]---
# Find Unused Code
Analyze the Go codebase for unused exported functions:
1. Configure Serena memory: `mkdir -p /tmp/gh-aw/cache-memory/serena`2. Use `find_symbol` to list all exported functions3. Use `find_referencing_symbols` to check usage4. Report functions with no referencesExample 2: Automated Refactoring
Section titled “Example 2: Automated Refactoring”---engine: claudepermissions: contents: writetools: serena: ["python"] edit:---
# Modernize Python Code
Update Python code to use type hints:
1. Find all function definitions without type hints2. Analyze function signatures and return types3. Add appropriate type annotations using `replace_symbol_body`4. Verify changes maintain correctnessExample 3: Code Quality Analysis
Section titled “Example 3: Code Quality Analysis”---engine: copilottools: serena: ["go"]---
# Analyze Test Coverage
Review Go test files for completeness:
1. List all exported functions in source files2. Check corresponding test files3. Identify functions without test coverage4. Generate report with missing testsBest Practices
Section titled “Best Practices”1. Configure Memory Early
Section titled “1. Configure Memory Early”Always set up Serena’s cache directory at the start of your workflow:
mkdir -p /tmp/gh-aw/cache-memory/serenaThis enables faster analysis on subsequent operations.
2. Use Symbol-Level Operations
Section titled “2. Use Symbol-Level Operations”Prefer Serena’s symbol tools over file-level edits when possible:
✅ Good: Use replace_symbol_body to update a function❌ Avoid: Read entire file, modify text, write back3. Specify Language Versions
Section titled “3. Specify Language Versions”For Go projects, explicitly configure go-mod-file location:
tools: serena: languages: go: go-mod-file: "go.mod" gopls-version: "latest"4. Combine with Other Tools
Section titled “4. Combine with Other Tools”Serena works well alongside other tools:
tools: serena: ["go"] # Semantic analysis github: # Repository access toolsets: [default] edit: # File operations bash: # Build and test5. Start Small
Section titled “5. Start Small”For large codebases, begin with targeted analysis:
1. Focus on specific packages or modules2. Use symbol search with filters3. Gradually expand scope based on findingsCommon Workflows
Section titled “Common Workflows”Code Analysis Workflow
Section titled “Code Analysis Workflow”---name: Daily Code Quality Checkon: schedule: - cron: dailypermissions: contents: readtools: serena: ["go"] github: toolsets: [default]---
# Mission: Analyze code quality daily
1. Configure Serena cache: `/tmp/gh-aw/cache-memory/serena/`2. Find all exported functions3. Check for missing documentation4. Identify complex functions (high cyclomatic complexity)5. Create issue with findingsRefactoring Workflow
Section titled “Refactoring Workflow”---name: Automated Refactoringon: workflow_dispatchpermissions: contents: writetools: serena: ["typescript"] edit:safe-outputs: create-pull-request: title-prefix: "[refactor] "---
# Mission: Refactor TypeScript code
1. Find deprecated API usage2. Use Serena to locate all references3. Replace with modern equivalents4. Verify changes compile5. Create pull request with changesDocumentation Workflow
Section titled “Documentation Workflow”---name: Documentation Checkon: issuespermissions: contents: read issues: writetools: serena: ["go"] github: toolsets: [default]---
# Mission: Verify function documentation
1. Parse issue for target file2. Use Serena to list all exported symbols3. Check for missing GoDoc comments4. Report findings in issue commentTroubleshooting
Section titled “Troubleshooting”Language Server Not Found
Section titled “Language Server Not Found”Problem: Serena cannot find the language server for your language.
Solution: Check that dependencies are installed. For Go:
go install golang.org/x/tools/gopls@latestMemory Permission Issues
Section titled “Memory Permission Issues”Problem: Serena cannot write to cache directory.
Solution: Ensure cache directory exists with proper permissions:
mkdir -p /tmp/gh-aw/cache-memory/serenachmod 755 /tmp/gh-aw/cache-memory/serenaGo Module Path Not Found
Section titled “Go Module Path Not Found”Problem: gopls cannot find go.mod file.
Solution: Explicitly configure the path:
tools: serena: languages: go: go-mod-file: "path/to/go.mod"Slow Initial Analysis
Section titled “Slow Initial Analysis”Problem: First run takes a long time to analyze code.
Solution: This is expected as language servers build indexes. Subsequent runs use cached data and are much faster. Consider:
- Enabling cache-memory for persistence
- Running analysis workflows on schedule (daily) to maintain warm cache
- Limiting scope to specific packages for large codebases
Advanced Configuration
Section titled “Advanced Configuration”Multiple Language Support
Section titled “Multiple Language Support”Enable Serena for multiple languages in one workflow:
tools: serena: languages: go: version: "1.21" go-mod-file: "backend/go.mod" typescript: python: version: "3.12"Custom Language Server Settings
Section titled “Custom Language Server Settings”For advanced users, pass custom arguments to Serena:
tools: serena: version: latest args: ["--verbose", "--log-level=debug"] languages: go: gopls-version: "v0.14.2"Integrated with Repository Memory
Section titled “Integrated with Repository Memory”Combine Serena with repository memory for persistent state:
tools: serena: ["go"] repo-memory: branch-name: memory/serena-analysis description: "Serena analysis cache" file-glob: ["memory/serena/*.json"]Related Documentation
Section titled “Related Documentation”- Using MCPs - General MCP server configuration
- Tools Reference - Complete tools configuration
- Getting Started with MCPs - MCP introduction
- Safe Outputs - Automated pull requests and issues
- Frontmatter Reference - All configuration options
External Resources
Section titled “External Resources”- Serena GitHub Repository - Official repository
- Serena Documentation - Comprehensive user guide
- Language Support - Supported languages and dependencies
- Serena Tools Reference - Complete tool documentation