代码搜索MCP服务器
一个闪电般的代码搜索与导航工具,专为Claude Code设计,助您快速查找文件、搜索代码、导航符号,并瞬间理解您的项目。只需让Claude执行“查找所有我的React组件”、“显示最近更改”或“查找UserService的定义”等指令,即可在毫秒级时间内获得结果。
使用.NET 9.0和COA MCP Framework 2.1.8构建,具备由Lucene支持的搜索功能,且搜索响应经过AI优化。
🚀 功能特性
- ⚡ 超快速搜索Lucene 索引功能支持在数百万行数据中实现即时搜索
- 🔍 智能代码分析自定义分析器保留代码模式,如
: ITool,[Fact]并且增强了CamelCase分词功能,支持完整的泛型类型(能够找到McpToolBase在搜索“McpToolBase”时 - 📁 文件发现基于模式的文件和目录搜索,支持模糊匹配
- 🧬 高级类型提取从(代码中)提取类型、接口、类和方法 25种编程语言 使用 julie-codesearch Rust 命令行工具(支持 C#、TypeScript、JavaScript、Python、Java、Rust、Go、C/C++、PHP、Ruby、Swift、Kotlin 以及另外 13 种专业语言)
- 🧭 代码导航符号搜索、查找引用和跳转到定义,无需编译
- 📝 行级搜索获取所有精确匹配的行号 - 比使用结构化JSON输出的grep更快
- 🔄 查找和替换在整个代码库中批量查找/替换,提供预览模式以确保安全,同时支持模糊匹配以处理拼写错误和变体
- 🔧 智能重构使用字节偏移替换的AST感知符号重命名——比文本搜索/替换更安全
- ✏️ 手术式编辑(或:精细手术式修改)插入、替换或删除特定行范围,而无需读取整个文件
- ⏱️ 最近使用的文件追踪并查找最近修改的文件
- 🔗 调用路径追踪跨语言追踪中的层次化调用链分析与语义桥接
- 🧠 语义搜索使用嵌入进行向量相似度搜索以查找概念上相似的代码
- 🎯 实时更新文件监视器会在更改时自动更新索引
- 📊 AI优化基于置信度的结果限制,实现高效令牌响应
- 🏠 混合本地索引存储在工作区中的索引
.coa/codesearch/indexes/支持多工作区
演出
- 启动时间:\` 模块(TS/JS)
- Razor/Blazor提取类型来自
@code并且@functions(名词)街区;方块;障碍物;(程序设计中的)模块 - 混合语言处理模板系统中的嵌入式代码
- 跨平台二进制文件构建中包含了为macOS(ARM64)、Linux(x64)和Windows(x64)预编译的julie-codesearch二进制文件
- 零依赖无需手动安装 Tree-sitter 库 - julie-codesearch 二进制文件自包含
📋 前提条件
- .NET 9.0 SDK 或更高版本
- 无需树坐(Tree-sitter)库 - julie-codesearch 二进制文件是自包含的,并包含在构建中
🚀 快速入门
从源代码构建
# Clone the repository
git clone https://github.com/anortham/coa-codesearch-mcp.git
cd coa-codesearch-mcp
# Build the project
dotnet build -c Release添加到Claude代码中
# macOS/Linux
claude mcp add codesearch /path/to/coa-codesearch-mcp/COA.CodeSearch.McpServer/bin/Release/net9.0/COA.CodeSearch.McpServer
# Windows
claude mcp add codesearch C:\path\to\coa-codesearch-mcp\COA.CodeSearch.McpServer\bin\Release\net9.0\COA.CodeSearch.McpServer.exe添加后:
- 完全重启Claude代码
- 克劳德现在将具备强大的搜索功能——只需自然提问即可!
可选:添加到 .gitignore 文件
# CodeSearch local indexes (can be regenerated)
.coa/注: NuGet包安装功能将在未来的版本中提供
🌟 使这特别之处
与基本的文件搜索不同,CodeSearch 能够理解你的代码:
- 智能模式识别发现
async Task,[Fact],interface IService模式,以及增强的通用类型CamelCase拆分功能 - 情境感知了解使用julie-codesearch原生tree-sitter提取功能时,C#类与JavaScript函数之间的区别
- 即时结果在数百万行代码中进行毫秒级搜索
- 模糊匹配即使文件名有拼写错误也能找到文件
- 内容相似度使用高级分析查找类似此文件的文件
- 最近活动追踪你最近一直在做的事情
- 多工作区支持同时索引和搜索多个项目,实现完美隔离
- 本地存储直接在您的工作区存储索引,实现快速访问
- 代码导航符号搜索、查找引用和跳转到定义,无需编译
- 结构化线性搜索比 grep 更好 - 返回包含精确行号和上下文的 JSON
- 安全批量编辑搜索/替换的预览模式可防止意外更改
- 类型感知通过julie-codesearch提取并索引25种语言的类型,以便在所有主要编程语言中进行精确导航
- 精确编辑无需完整读取文件,即可完成基于行的手术代码修改的完整编辑套件
🛠️ 可用工具 - 现在支持智能默认设置! ✨
注: 所有工具都支持智能默认设置——大多数参数都是可选的,并且默认值设置得合理 workspacePath 参数默认为所有工具当前的工作区目录。
核心搜索工具
| 工具 | 用途 | 关键参数(其余均为可选) | |||
|---|---|---|---|---|---|
| 项目 | 类型 | 描述 | index_workspace | workspacePath | 搜索索引文件 |
| (可选,默认为当前目录) | text_search | query | 使用语义/模糊/正则表达式模式搜索文件内容 | searchMode (必填), | |
| (可选:"auto"(自动), "exact"(精确), "fuzzy"(模糊), "semantic"(语义), "regex"(正则表达式)) | search_files | pattern | 🆕 按模式查找文件或目录 | resourceType (必填), | |
| (可选:“文件”,“目录”,“两者都有”) | recent_files | timeFrame | 获取最近修改的文件 |
(可选,例如,“2d”表示2天,“1w”表示1周)|
导航工具 | 工具 | 用途 | 关键参数(其余均为可选) | |------|---------|--------------------------------------| symbol_search | symbol | 按名称查找类、接口、方法 | (必填)| find_references | symbol | 查找符号的所有使用位置 | (必填)| goto_definition | symbol | 跳转到符号定义 |
(必填)|
高级搜索工具 | 工具 | 目的 | 关键参数(其他均为可选) | |------|---------|--------------------------------------| line_search | pattern | 获取所有带有行号的出现位置 | (必填)| search_and_replace | searchPattern | 在文件中替换模式,支持预览和模糊匹配 | replacePattern (必填),
(可选)|
重构工具 | 工具 | 用途 | 关键参数(其余均为可选) | |------|---------|--------------------------------------| smart_refactor | operation 具有字节偏移精度的AST感知符号重命名 params (必填),
(必填)|
编辑工具 | 工具 | 用途 | 关键参数(其他均为可选) | |------|---------|--------------------------------------| edit_lines | filePath | 🆕 统一线编辑(插入/替换/删除) | operation (必填), startLine (必需:“插入”,“替换”,“删除”),
(必填)|
分析工具 | 工具 | 用途 | 关键参数(其他参数均可选) | |------|---------|--------------------------------------| get_symbols_overview | 标题1 | 标题2 | 说明 | filePath | | 从文件中提取所有符号 | find_patterns (必填)| filePath | | 检测代码模式和质量问题 | trace_call_path (必填)| symbol |
| 分层调用链分析 |
(必填)|
💬 如何与Claude代码一起使用
安装后,只需自然地与Claude Code聊天即可!以下是一些您可以说的话的示例:
Claude will search for *.ts files in your project查找文件和代码
Claude will search for patterns like "async function" and "async Task"“查找所有我的TypeScript文件”
Claude will search file contents for the term "UserService"“在代码库中显示所有异步函数”
Claude will show recently modified files with timestamps“查找包含‘UserService’的文件”
“过去两天内哪些文件被更改了?”
Claude will look for .jsx, .tsx files and React patterns项目理解
Claude will find files with "test", "spec" in names or paths“查找我的所有React组件”
Claude will use content analysis to find structurally similar files“给我看所有的测试文件”
Claude will look for SQL patterns, ORM calls, etc.“查找与UserController.cs相似的文件”
“在我的项目中搜索所有数据库查询”
Claude will jump directly to where UserService class is defined
Shows exact line and column, with optional context snippet代码导航
Claude will find all places where UpdateUser is called
Groups results by file for easy scanning“查找UserService的定义”
Claude will find all classes matching the pattern like UserController, OrderController
Uses julie-codesearch tree-sitter-based extraction for accurate results across 25 languages“显示所有对UpdateUser方法的引用”
Claude will locate all implementations of the IRepository interface
Shows inheritance relationships and usage counts“搜索所有以Controller结尾的类”
“查找 IRepository 接口的实现位置”
Claude will extract types from 25 languages including C#, TypeScript, Python, Java, Rust, Go, C/C++, PHP, Ruby, Swift, Kotlin, and more
Uses julie-codesearch native tree-sitter extraction for LSP-quality results类型与代码分析
Claude will parse 25 languages and extract function/method definitions with signatures
Supports everything from C# to Bash to specialized languages like GDScript“查找我项目中的所有类和接口”
Claude will parse Vue SFCs and extract methods from JavaScript/TypeScript script blocks
julie-codesearch handles embedded languages in templating systems“显示我代码库中的所有函数和方法”
Claude will analyze Python files and extract class definitions and methods
Full support for Python's class hierarchies and method signatures“查找所有 Vue 组件方法”
Claude will parse Rust code and extract struct definitions and implementations
julie-codesearch provides native Rust tree-sitter integration“显示所有Python类及其方法”
“查找所有 Rust 结构体和实现块”
Claude will scan and index your files for fast searching开发工作流程
Claude will search for TODO, FIXME, HACK comments“为我的项目建立索引以便搜索”
Claude will find .json, .yaml, .config files“查找所有 TODO 注释”
“给我看配置文件”
Claude will use recent_files with a time filter to find stale code高级示例
Claude will use text_search to find try-catch blocks and exception handling“查找Services目录中30天内未被修改过的所有文件”
Claude will search for route decorators and endpoint definitions“在我的C#代码中搜索错误处理模式”
Claude will combine multiple searches to find React imports without useState/useEffect在我的项目中查找所有API端点
“给我看看那些导入了 React 但没有使用 hooks 的文件”
Claude will use line_search to find ALL occurrences with exact line numbers
Returns structured JSON instead of plain text grep output行级别搜索示例
Claude will return every console.log with file path and line number
Perfect for cleanup tasks before production deployment“显示包含‘Thread.Sleep’的每一行”
“查找所有带有行号的 console.log 语句”
Claude will use search_and_replace with preview mode first
Shows what will change before applying modifications搜索和替换示例
Claude will find and replace copyright patterns across all files
Supports regex patterns for complex replacements“将我的JavaScript文件中的所有‘var’声明替换为‘let’”
“将所有版权头信息更新为2025年”
Claude will use fuzzy mode with threshold 0.7-0.8
Finds: getUserData(), getUserDat() (typo), getUserData () (spacing), getUserDatta() (double-t)
Perfect for cleaning up inconsistent code patterns模糊匹配示例
Claude will use fuzzy search to find all similar variations
Handles typos, spacing issues, and minor differences automatically“使用模糊匹配替换 getUserData(),即使存在拼写错误”
“修复代码库中方法名的不一致性”
Claude will use smart_refactor with AST-aware symbol renaming
Finds all usages via SQLite identifiers table (LSP-quality)
Uses byte-offset replacement for precise, safe refactoring智能重构示例
Claude will find ALL references using symbol analysis
Replaces at exact byte positions (not regex)
Preview mode shows exactly what will change before applying“将所有地方的 UserService 重命名为 AccountService”
“将 UpdateUser 方法重命名为 UpdateUserAccount”
🔒 安全与线程安全
- 路径验证PathResolutionService 实现了全面的路径验证:
- 目录遍历保护阻止包含“..”序列的路径
- 路径长度验证防止路径过长(超过240个字符)
- 输入净化(或输入验证与清理)验证并规范化所有工作区路径
跨平台兼容性
- 处理路径分隔符和特殊文件夹线程安全
- 并发元数据访问信号量锁保护工作区元数据文件
- 原子文件操作元数据更新使用临时文件进行原子替换
- 锁管理文件级锁定可防止并发访问时发生损坏
安全的文件系统操作
- 所有输入/输出操作都包含错误处理和备用方案API安全
- 路径解析内部哈希目录从不通过HTTP API暴露
- 真实路径验证仅返回现有且可访问的工作区路径
- URL编码支持处理工作区路径中的特殊字符
备用处理或回退处理
优雅地处理无法解析或损坏的元数据 appsettings.json⚙️ 配置
{
"CodeSearch": {
"BasePath": "~/.coa/codesearch",
"LogsPath": "~/.coa/codesearch/logs",
"Lucene": {
"IndexRootPath": ".coa/codesearch/indexes",
"MaxIndexingConcurrency": 8,
"RAMBufferSizeMB": 256,
"SupportedExtensions": [".cs", ".js", ".ts", ".py", ".java", ...]
},
"FileWatcher": {
"Enabled": true,
"DebounceMilliseconds": 500
},
"QueryCache": {
"Enabled": true,
"MaxCacheSize": 1000,
"CacheDuration": "00:15:00"
}
}
}通过配置
:
- 🏗️ 建筑混合本地索引存储
.coa/codesearch/indexes/[workspace-name_hash]/
- 主工作区索引 - : - 索引存储在主工作区目录内的本地位置
- 每个工作区都有自己的独立索引,以实现快速、上下文感知的搜索支持在单个CodeSearch会话中管理多个工作区项目
- 跨平台锁管理SimpleFSLockFactory 确保在 macOS、Windows 和 Linux 之间的兼容性
~/.coa/codesearch/logs/日志 - :(全局日志记录位置)
配置
每个工作区的设置,具有工作区特定的隔离性 框架集成建立在
- COA MCP 框架 2.1.8
- :
- 自动工具发现
- 代币优化
- 渐进式响应披露
断路器模式
- 内存压力管理 搜索引擎
- Lucene.NET 4.8.0 后端
- 自定义代码分析器 对于编程语言模式
- 多因素评分 具有路径相关性、近期性以及类型匹配的特点
可配置分析仪
按文件类型
# Debug build
dotnet build -c Debug
# Release build
dotnet build -c Release
# Run tests
dotnet test🧪 开发
# Test indexing
mcp__codesearch__index_workspace --workspacePath "."
# Test search
mcp__codesearch__text_search --query "LuceneIndexService"
# Test file discovery
mcp__codesearch__recent_files --timeFrame "7d"建筑
测试
🔧 故障排除
Tell Claude: "Please index my project for searching first"常见问题
Ask Claude: "Rebuild the search index for this project"“克劳德说它找不到我知道存在的文件”
- “搜索结果似乎过时了”
- “克劳德对搜索请求的响应较慢”
尝试减少搜索结果数量:“查找最近的5个TypeScript文件”
Close Claude Code completely and restart it请Claude检查系统内存:“检查CodeSearch内存使用情况”
- “获取索引锁时出错”
dotnet --version - 安装问题:
cd coa-codesearch-mcp && dotnet build -c Release - 确保已安装 .NET 9.0:
尝试重新构建:
验证您的Claude代码配置中的路径是否正确 模板嵌入修复(v2.1.4+) Templates/ 背景
在2.1.4版本之前,CodeSearch在除开发工作区以外的其他工作区中无法连接,原因是它尝试从一个(此处原文不完整,假设意为“特定的或错误的路径/位置”)加载指令模板 仅存在于源代码仓库中的目录。 codesearch-instructions.scriban 解决方案:
模板现在已作为资源嵌入到程序集中,确保无论工作区位置如何,它们始终可用
- 模板被编译为二进制文件中的嵌入式资源。
Templates/codesearch-instructions.scriban - 技术细节:
COA.CodeSearch.McpServer.Templates.codesearch-instructions.scriban - 模板文件:
WithInstructionsFromTemplate嵌入式资源名称:WithTemplateInstructions加载机制:已从基于文件的( - ) 从资源基础型 (
(含嵌入内容)
框架修复:COA.Mcp.Framework v2.1.8+ 包含对缺失模板目录的防御性检查
这一变更确保了CodeSearch在所有工作区中都能一致运行,且不依赖于文件系统。 appsettings.json记录日志
{
"Logging": {
"LogLevel": {
"COA.CodeSearch": "Information" // Debug, Trace for more detail
}
}
}设置日志级别
:
📚 整合
- 与其他MCP服务器一起CodeSearch 与在 Claude 代码中配置的其他 MCP 服务器无缝协作:
- 金鱼MCP(注:MCP可能指某种特定概念或产品,但在此上下文中未给出具体含义,因此直接保留原样)处理会话管理、检查点以及任务跟踪
其他MCP服务器
# 1. Search for code patterns using CodeSearch
mcp__codesearch__text_search --query "async Task"
# 2. Navigate to specific definitions
mcp__codesearch__goto_definition --symbol "UserService"
# 3. Find all references to a method
mcp__codesearch__find_references --symbol "UpdateUser"可以组合用于全面的开发工作流程
示例工作流程: 📄 许可证 MIT 许可证 - 请参阅
许可证
- 文件。
- 🤝 贡献
- 为仓库创建分支(或:为仓库进行叉(fork)操作)
- 创建一个特性分支
- 进行你的更改
如适用,请添加测试
______________________________________________________________________
