网络上下文服务器
🔍通过深度理解 .NET 代码库,增强你的 AI 编码助手

NetContextServer 使 VS Code 等 AI 编码助手能够通过模型上下文协议 (MCP)深入了解您的 .NET 代码库。这意味着更准确的代码建议、更好的问题答案以及更高效的编码体验。

✨ 主要特点
🧠语义代码搜索:通过用自然语言描述您要查找的内容来查找代码
🔍智能导航:帮助 AI 工具了解您的项目结构和依赖关系
🛡️内置安全性:安全文件访问,自动保护敏感数据
🚀 VS Code 集成:与 VS Code 无缝设置,增强编码辅助
📦软件包分析:了解您的依赖关系并获取更新建议
📊测试覆盖率分析:深入了解您的测试覆盖率
⚡快速高效:大型代码库的快速索引和响应时间
Related MCP server: SQL Server MCP
🚀 快速入门
克隆和构建:
git clone https://github.com/willibrandon/NetContextServer.git
cd NetContextServer
dotnet build
设置(可选,用于语义搜索):
# Set Azure OpenAI credentials in environment:
AZURE_OPENAI_ENDPOINT=your_endpoint
AZURE_OPENAI_API_KEY=your_key
开始使用:
# Point to your project
dotnet run --project src/NetContextClient/NetContextClient.csproj -- set-base-dir --directory "path/to/your/project"
# Try semantic search
dotnet run --project src/NetContextClient/NetContextClient.csproj -- semantic-search --query "find authentication logic"
👉**阅读我们的入门指南,**了解详细的设置说明和最佳实践。
🔌 与 VS Code 集成
打开 Visual Studio Code
按Ctrl + Shift + P (或在 macOS 上Cmd + Shift + P )
输入“配置 MCP 服务器”
进入:
{
"command": "dotnet",
"args": ["run", "--project", "path/to/NetContextServer/src/NetContextServer/NetContextServer.csproj"]
}
现在 VS Code 可以理解你的代码库了!试着问它这样的问题:
📚 文档
特征
📁项目和文件列表:列出解决方案中的所有项目和源文件
🔍代码搜索:在代码库中搜索特定模式或文本
🧠语义搜索:根据含义查找代码,而不仅仅是精确的文本匹配
📖文件内容访问:读取具有安全检查和大小限制的源文件
🛡️安全性:内置敏感文件和目录访问保护措施
🎯模式管理:用于控制文件访问的灵活忽略模式
📊覆盖率分析:解析和分析测试覆盖率数据
💭结构化思维:记录并验证复杂操作的推理
从源代码构建
克隆存储库:
git clone https://github.com/willibrandon/NetContextServer.git
cd NetContextServer
构建解决方案:
运行测试(可选):
运行命令
使用客户端与您的代码库进行交互:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- <command> [options]
环境设置
对于语义搜索功能,您需要设置以下环境变量:
用法
基本命令
设置基本目录:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- set-base-dir --directory "D:\YourProject"
获取基目录:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- get-base-dir
获取版本信息:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- version
列出项目:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- list-projects-in-dir --directory "D:\YourProject\src"
列出源文件:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- list-source-files --project-dir "D:\YourProject\src\YourProject"
分析包:
# Set your base directory first
dotnet run --project src/NetContextClient/NetContextClient.csproj -- set-base-dir --directory "path/to/your/project"
# Run the package analysis
dotnet run --project src/NetContextClient/NetContextClient.csproj -- analyze-packages
示例输出:
Project: MyProject.csproj
Found 2 package(s):
- ✅ Newtonsoft.Json (13.0.1)
Used in 5 location(s)
Dependencies:
└─ Newtonsoft.Json
└─ System.*
└─ System.ComponentModel
- 🔄 Microsoft.Extensions.DependencyInjection (5.0.2 → 6.0.1)
Update available: 6.0.1
Dependencies:
└─ Microsoft.Extensions.DependencyInjection
└─ Microsoft.*
└─ Microsoft.Extensions.DependencyInjection.Abstractions
分析测试覆盖率:
# Set your base directory first
dotnet run --project src/NetContextClient/NetContextClient.csproj -- set-base-dir --directory "path/to/your/project"
# Analyze coverage from a Coverlet JSON report
dotnet run --project src/NetContextClient/NetContextClient.csproj -- coverage-analysis --report-path "TestResults/coverage.json"
# Get a coverage summary
dotnet run --project src/NetContextClient/NetContextClient.csproj -- coverage-summary --report-path "TestResults/coverage.json"
覆盖率分析输出示例:
[{
"filePath": "src/MyProject/Services/UserService.cs",
"coveragePercentage": 85.3,
"uncoveredLines": [42, 43, 88],
"branchCoverage": {
"ValidateUser()": 75.0,
"GetUserById()": 100.0
},
"recommendation": "Consider adding tests for the user validation error paths"
}
]
搜索命令
文本搜索:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- search-code --text "authentication"
语义搜索:
# Search with default number of results (5)
dotnet run --project src/NetContextClient/NetContextClient.csproj -- semantic-search --query "handle user authentication"
# Search with custom number of results
dotnet run --project src/NetContextClient/NetContextClient.csproj -- semantic-search --query "database connection string" --top 10
语义搜索功能:
使用嵌入来根据含义查找代码
返回按相关性排序的代码片段
显示行号和相似度分数
首次搜索时自动索引您的代码
模式管理
添加忽略模式:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- add-ignore-patterns --patterns "*.txt" "*.log"
查看当前模式:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- get-ignore-patterns
删除特定模式:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- remove-ignore-patterns --patterns "*.txt"
清除用户模式:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- clear-ignore-patterns
查看状态文件位置:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- get-state-file-location
默认忽略模式
为保护敏感信息,默认情况下会忽略以下模式:
*.env环境文件
appsettings.*.json - 应用程序设置
*.pfx - 证书文件
*.key密钥文件
*.pem ——PEM 文件
*password* - 名称中包含“密码”的文件
*secret* - 名称中包含“secret”的文件
安全功能
路径安全:只能在指定的基目录内访问文件
模式验证:忽略模式验证语法是否正确
大小限制:大文件内容将被截断,以防止出现内存问题
敏感文件保护:内置模式保护常见的敏感文件
示例工作流程
设置项目的基本目录:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- set-base-dir --directory "D:\Projects\MyApp"
检查服务器版本和配置:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- version
设置自定义忽略模式:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- add-ignore-patterns --patterns "*.generated.cs" "*.designer.cs"
列出所有项目:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- list-projects-in-dir --directory "D:\Projects\MyApp\src"
分析项目的包依赖关系:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- analyze-packages
搜索认证相关代码:
dotnet run --project src/NetContextClient/NetContextClient.csproj -- semantic-search --query "user authentication and authorization logic"
与AI编码工具集成
NetContextServer 实现了模型上下文协议 (MCP) ,允许与支持该协议的 AI 编码助手无缝集成,例如:
与 VS Code 一起使用:
配置 VS Code 以使用 NetContextServer 作为其 MCP 提供程序
通过完整的代码库上下文享受增强的代码帮助
要测试和调试 MCP 交互,您可以使用模型上下文协议检查器。这是一款可视化测试工具,可帮助您检查和验证 MCP 客户端与服务器之间的通信。访问检查器文档,了解更多其特性和功能。
NetContextServer 使 AI 协作者能够请求有关您的代码库的特定信息,从而使 AI 的建议与您的特定项目结构和编码模式更加相关和准确。
错误处理
服务器针对常见场景提供了清晰的错误消息:
未找到目录
访问被拒绝(在基目录之外)
无效模式
超出文件大小限制
受限文件类型
缺少语义搜索的环境变量
贡献
分叉存储库
创建功能分支
提交你的更改
推送到分支
创建拉取请求
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。