分享 Claude Code 结合 Serena MCP 免费实现高效 AI 辅助编程的具体步骤和技巧。
我发现SuperClaude和Serena看起来都很适合这个目的。我想免费使用,而这两者都是开源软件且符合我的需求。🤑 最后我选择了Serena,因为SuperClaude在内部使用Serena,所以我认为Serena是更值得首先学习的基础系统。好的,让我们看看Serena能做什么。🔍
Serena是一个编码智能体工具包,提供语义代码检索和编辑工具。https://github.com/oraios/serena
Serena不仅能节省token,还能加快AI响应速度并提高响应质量。你可以在Claude Code、Cursor、Cline、VSCode等工具中将Serena作为MCP(模型上下文协议)使用。Serena支持TypeScript、JavaScript、Python、Java、C#等多种编程语言。
通常,AI在响应前会读取所有代码。而Serena首先建立索引,以高效地读取代码。此外,Serena会在".serena/memories"目录中创建一些markdown文件来理解项目,并使用它们来更高效地处理流程。之后,Serena使用自己的工具不是读取全部代码,而是只读取必要的代码。Cursor已经有索引系统,但Claude Code没有,所以使用Serena能极大地提升你的Claude Code体验。
请注意,Serena更适合大型项目,在小型项目中可能表现不是最佳。
我看到有人说Serena可以节省大约70%的token!根据我的经历,Serena确实能节省token并延长达到Claude Code限额前的时间。太棒了!😀
使用Serena只需3个步骤。1️⃣ 安装 2️⃣ 入门 3️⃣ 索引
claude mcp add serena -- uvx --from git+https://github.com/oraios/serena serena start-mcp-server --context ide-assistant --project $(pwd)
↓ 除Claude Code外的安装方式,请查看官方文档。https://github.com/oraios/serena?tab=readme-ov-file#quick-start
安装Serena并运行Claude Code后,它会自动在浏览器中启动一个日志仪表板,并在根目录中创建.serena文件夹。在.serena内,还会创建一个项目配置文件project.yml。
当第一次使用Serena时,需要运行入门流程。入门是Serena读取和理解项目并为你的编码做准备的过程。入门会在.serena\memories文件夹中创建记忆(markdown文件)。这个入门过程会读取许多文件并消耗很多token,所以要小心不要用尽token。要运行入门,需要在聊天中要求Claude Code(或其他工具)启动Serena入门流程。此外,在我的情况下,一些记忆部分不准确,我需要修复它们。
入门流程也创建了一个缓存文件.serena\cache\typescript\document_symbols_cache_v23-06-25.pkl。当我用Cursor编辑器打开缓存时,它是乱码但我可以看到索引已被创建。但是,索引似乎没有覆盖整个项目。所以,我运行了下面的命令并为整个项目创建了索引。
uvx --from git+https://github.com/oraios/serena serena project index
我在之前制作的项目中尝试了Serena入门流程。
↓ 使用Playwright和Mock Service Worker(MSW)进行SSR API测试的Next.js项目 https://dev.to/webdeveloperhyper/how-to-test-nextjs-ssr-api-playwright-msw-part-2-parallel-test-3akj
Serena读取了package.json、playwright.config.ts、tsconfig.json、tests\example.spec.ts、mock-server.ts等文件,并生成了project.yml(配置文件)和记忆(markdown文件)如下。
↓ .serena/project.yml
# language of the project (csharp, python, rust, java, typescript, go, cpp, or ruby)
# * For C, use cpp
# * For JavaScript, use typescript
# Special requirements:
# * csharp: Requires the presence of a .sln file in the project folder.
language: typescript
# whether to use the project's gitignore file to ignore files
# Added on 2025-04-07
ignore_all_files_in_gitignore: true
# list of additional paths to ignore
# same syntax as gitignore, so you can use * and **
# Was previously called `ignored_dirs`, please update your config if you are using that.
# Added (renamed) on 2025-04-07
ignored_paths: []
# whether the project is in read-only mode
# If set to true, all editing tools will be disabled and attempts to use them will result in an error
# Added on 2025-04-18
read_only: false
# list of tool names to exclude. We recommend not excluding any tools, see the readme for more details.
# Below is the complete list of tools for convenience.
# To make sure you have the latest list of tools, and to view their descriptions,
# execute `uv run scripts/print_tool_overview.py`.
#
# * `activate_project`: Activates a project by name.
# * `check_onboarding_performed`: Checks whether project onboarding was already performed.
# * `create_text_file`: Creates/overwrites a file in the project directory.
# * `delete_lines`: Deletes a range of lines within a file.
# * `delete_memory`: Deletes a memory from Serena's project-specific memory store.
# * `execute_shell_command`: Executes a shell command.
# * `find_referencing_code_snippets`: Finds code snippets in which the symbol at the given location is referenced.
# * `find_referencing_symbols`: Finds symbols that reference the symbol at the given location (optionally filtered by type).
# * `find_symbol`: Performs a global (or local) search for symbols with/containing a given name/substring (optionally filtered by type).
# * `get_current_config`: Prints the current configuration of the agent, including the active and available projects, tools, contexts, and modes.
# * `get_symbols_overview`: Gets an overview of the top-level symbols defined in a given file.
# * `initial_instructions`: Gets the initial instructions for the current project.
# Should only be used in settings where the system prompt cannot be set,
# e.g. in clients you have no control over, like Claude Desktop.
# * `insert_after_symbol`: Inserts content after the end of the definition of a given symbol.
# * `insert_at_line`: Inserts content at a given line in a file.
# * `insert_before_symbol`: Inserts content before the beginning of the definition of a given symbol.
# * `list_dir`: Lists files and directories in the given directory (optionally with recursion).
# * `list_memories`: Lists memories in Serena's project-specific memory store.
# * `onboarding`: Performs onboarding (identifying the project structure and essential tasks, e.g. for testing or building).
# * `prepare_for_new_conversation`: Provides instructions for preparing for a new conversation (in order to continue with the necessary context).
# * `read_file`: Reads a file within the project directory.
# * `read_memory`: Reads the memory with the given name from Serena's project-specific memory store.
# * `remove_project`: Removes a project from the Serena configuration.
# * `replace_lines`: Replaces a range of lines within a file with new content.
# * `replace_symbol_body`: Replaces the full definition of a symbol.
# * `restart_language_server`: Restarts the language server, may be necessary when edits not through Serena happen.
# * `search_for_pattern`: Performs a search for a pattern in the project.
# * `summarize_changes`: Provides instructions for summarizing the changes made to the codebase.
# * `switch_modes`: Activates modes by providing a list of their names
# * `think_about_collected_information`: Thinking tool for pondering the completeness of collected information.
# * `think_about_task_adherence`: Thinking tool for determi
↓ .serena\memories\code_style_and_conventions.md
# Code Style and Conventions
## TypeScript Configuration
- **Strict Mode**: Enabled with full type checking
- **Target**: ES2017
- **Module System**: ESNext with bundler resolution
- **Path Mapping**: `@/*` maps to `./src/*`
- **JSX**: Preserve mode for Next.js processing
## Code Style Patterns
- **Async/Await**: Used consistently for asynchronous operations
- **Function Components**: React functional components with TypeScript
- **Export Default**: Components exported as default functions
- **Destructuring**: Props and search parameters destructured in function signatures
- **Template Literals**: Used for dynamic strings and URLs
- **Error Boundaries**: Try-catch blocks for API error handling
## Naming Conventions
- **Components**: PascalCase (e.g., `Home`, `DemoTitle`)
- **Variables**: camelCase (e.g., `mockPort`, `pokemon`)
- **Files**: kebab-case for general files, PascalCase for React components
- **Constants**: UPPER_SNAKE_CASE for environment variables
@/ 路径映射使用className 属性next/font 加载 Geist Sans 和 Geist Mono
↓ .serena\memories\project_overview.md
这是一个 Next.js 15 演示应用,用于展示如何通过 Playwright 和 MSW(Mock Service Worker)进行并行测试。该应用会展示从模拟 API 获取的 Pokemon 数据,并且能够利用 MSW 的动态模拟能力在不同 Pokemon 之间切换。
src/app/ - Next.js App Router 结构src/mocks/ - MSW 处理程序和服务器设置tests/ - Playwright 测试文件mock-server.ts - 用于测试与 MSW 通信的桥接服务器
↓ .serena\memories\project_structure.md
├── src/ │ ├── app/ # Next.js App Router │ │ ├── favicon.ico # 网站图标 │ │ ├── globals.css # 全局 CSS 样式 │ │ ├── layout.tsx # 根布局组件 │ │ └── page.tsx # 首页组件 │ └── mocks/ # MSW 模拟设置 │ ├── handlers.ts # API 请求处理程序 │ └── server.ts # MSW 服务器配置 ├── tests/ # Playwright 测试文件 │ └── example.spec.ts # Pokemon API 测试 ├── tests-examples/ # 其他测试示例 ├── public/ # 静态资源 ├── mock-server.ts # 模拟服务器桥接程序 ├── playwright.config.ts # Playwright 配置 ├── next.config.ts # Next.js 配置 ├── tsconfig.json # TypeScript 配置 ├── postcss.config.mjs # PostCSS 配置 ├── package.json # 依赖项和脚本 └── README.md # 项目文档
↓ .serena\memories\suggested_commands.md
bash
npm run dev
npm run build
npm start
bash
npx playwright test
npx playwright test --headed
npx playwright test tests/example.spec.ts
MOCK_SERVER_COUNT=4 npx playwright test
npx playwright show-report
bash
npx tsx mock-server.ts
npx tsx mock-server.ts 3001
MOCK_SERVER_COUNT=4 npx tsx mock-server.ts
dir 或 ls(使用 Git Bash/WSL 时)cdwhere 或 find(Git Bash)findstr 或 grep(Git Bash)git status、git add、git commit 等bash
npx tsc --noEmit
npx tsx filename.ts
↓ .serena\memories\task_completion_guidelines.md
由于该项目的 package.json 中没有明确的代码检查或格式化命令,因此应验证以下内容:
npx tsc --noEmit 检查类型错误npx playwright test,确保没有出现回归问题npm run dev 能够正常运行npm run build 检查构建错误MOCK_SERVER_COUNT 的处理逻辑是否得到保留
↓ .serena\memories\task_completion_guidelines.md
由于该项目的 package.json 中没有明确的代码检查或格式化命令,因此应验证以下内容:
npx tsc --noEmit 检查类型错误npx playwright test,确保没有出现回归问题npm run dev 能够正常运行npm run build 检查构建错误MOCK_SERVER_COUNT 的处理逻辑是否得到保留
↓ .serena\memories\tech_stack_and_dependencies.md
Serena 可以降低 token 使用量、缩短响应时间,同时也能提高代码质量。👍 使用 Serena 既简单又免费,何不试一试呢?
希望你从本文中学到了一些东西。😊 感谢阅读。Happy AI coding!🤖
某些评论仅对登录用户可见。登录查看全部评论。
如需进一步操作,你可以考虑屏蔽此人和/或举报滥用行为