一个提供来自 .NET 项目的详细类型信息的 Model Context Protocol (MCP) 服务器,用于 AI 编码代理。
.NET 类型探索器 MCP 服务器是一个强大的工具,旨在帮助 AI 编码代理理解和处理 .NET 代码库。它提供了一种结构化的方式来探索 .NET 项目中的程序集、命名空间和类型,使得 AI 代理能够生成准确且上下文感知的代码建议。
该服务器通过反射从编译的 .NET 程序集中提取详细的类型信息,包括类、接口、方法、属性、字段和事件。这些信息随后可以通过一组工具供 AI 代理系统地探索代码库。
欢迎贡献!请随时提交拉取请求。
服务器支持同时查询多个 NuGet 包源。在 appsettings.json 中配置自定义源:
{
"Tools": {
"DefaultPageSize": 20,
"IntendResponse": false,
"NuGetSources": [
{
"Name": "nuget.org",
"Url": "https://api.nuget.org/v3/index.json",
"Enabled": true
},
{
"Name": "MyPrivateFeed",
"Url": "https://my-company.com/nuget/v3/index.json",
"Enabled": true
},
{
"Name": "dotnet-core (MyGet)",
"Url": "https://dotnet.myget.org/F/dotnet-core/api/v3/index.json",
"Enabled": true,
"Comment": "示例:.NET Core 预览包的公共 MyGet 源"
}
]
}
}
配置选项:
/v3/index.json 结尾)false 以临时禁用某个源而不删除它行为:
appsettings.json 中未配置任何源时添加 nuget.org优先级和去重:
当同一个包在多个源中找到时,服务器采用基于优先级的策略:
示例:
{
"NuGetSources": [
{ "Name": "Internal", "Url": "https://internal.company.com/nuget/v3/index.json", "Enabled": true },
{ "Name": "nuget.org", "Url": "https://api.nuget.org/v3/index.json", "Enabled": true }
]
}
在此配置中,如果一个包存在于两个源中,则使用“Internal”的信息。
dotnet build -c Release
dotnet publish -c Release -r <runtime-identifier> --self-contained false
将 <runtime-identifier> 替换为目标平台(例如,win-x64,linux-x64,osx-x64)。服务器作为 Docker 容器提供,提供了更好的隔离性和更简单的部署:
从 Docker Hub 拉取镜像:
docker pull vrogozhin/dotnet-types-explorer-mcp:latest
或本地构建:
git clone https://github.com/V0v1kkk/DotNetMetadataMcpServer.git
cd DotNetMetadataMcpServer
docker build -t dotnet-types-explorer-mcp .
运行容器:
docker run --rm -i \
-v /path/to/your/dotnet/projects:/workspace \
vrogozhin/dotnet-types-explorer-mcp:latest
Docker 部署的好处:
要使用 .NET 类型探索器 MCP 服务器与 AI 代理,您需要在 MCP 设置文件中进行配置。这里是一个示例配置:
{
"mcpServers": {
"dotnet-types-explorer": {
"command": "/path/to/DotNetMetadataMcpServer",
"args": [ "--homeEnvVariable", "/home/user" ],
"disabled": false,
"alwaysAllow": [],
"timeout": 300
}
}
}
将 /path/to/DotNetMetadataMcpServer 替换为已发布的可执行文件的实际路径,并将 /home/user 替换为您自己的主目录。
对于 Docker 部署,配置您的 MCP 客户端如下:
{
"mcpServers": {
"dotnet-types-explorer": {
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"-v",
"/path/to/your/dotnet/projects:/workspace",
"vrogozhin/dotnet-types-explorer-mcp:latest"
],
"disabled": false,
"alwaysAllow": [],
"timeout": 100
}
}
}
将 /path/to/your/dotnet/projects 替换为包含您的 .NET 项目的目录。容器将有权访问此目录中的所有项目。
当扫描项目时,服务器需要编译的程序集路径。MsBuildHelper 评估项目并尝试按以下顺序查找输出程序集:
第一个存在的输出获胜。如果没有找到输出,假设一个合理的默认路径并记录警告。这使本地运行(通常是 Debug)和 CI 运行(通常是 Release)具有一致的行为。确保在扫描之前以这些配置之一构建项目。
服务器提供五个主要工具,可以由 AI 代理使用:
此工具已在 Roo Code Visual Studio 扩展中进行了测试,这是一个支持 Model Context Protocol 的 AI 编码助手。您可以在 GitHub 上找到有关 Roo Code 的更多信息。
可能使用
.clinerules文件来指示您的编码助手使用 MCP 服务器。
如果您正在处理带有 NuGet 包引用的 .NET 项目,并需要使用这些包编写代码,您应该使用 Dotnet 类型探索器 MCP 服务器系统地探索这些包的 API。这在您不熟悉包的 API 或文档有限时尤为重要。
当处理 .NET 项目时:
请记住,此工具仅检查指定的项目及其 NuGet 依赖项。它不会跟踪解决方案中的其他项目引用。如果您需要分析多个项目,您需要分别扫描每个项目。
这种自顶向下的方法(程序集 → 命名空间 → 类型)是探索和理解 .NET 库最有效的方式,特别是当您需要编写使用它们的代码时。对于 API 可能不明显或记录不足的第三方库,这种方法尤其有价值。NuGet 工具通过提供直接访问包信息而无需将包已引用在项目中,补充了这一方法。
服务器使用以下过程提取类型信息:
根据过滤器和分页检索引用的程序集。
输入模式:
{
"type": "object",
"properties": {
"ProjectFileAbsolutePath": {
"type": "string"
},
"PageNumber": {
"type": "integer"
},
"FullTextFiltersWithWildCardSupport": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"ProjectFileAbsolutePath"
]
}
响应:
{
"AssemblyNames": ["Assembly1", "Assembly2", ...],
"CurrentPage": 1,
"AvailablePages": [1, 2, ...]
}
支持过滤和分页从指定的程序集检索命名空间。
输入模式:
{
"type": "object",
"properties": {
"ProjectFileAbsolutePath": {
"type": "string"
},
"AssemblyNames": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
},
"PageNumber": {
"type": "integer"
},
"FullTextFiltersWithWildCardSupport": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"ProjectFileAbsolutePath"
]
}
响应:
{
"Namespaces": ["Namespace1", "Namespace2", ...],
"CurrentPage": 1,
"AvailablePages": [1, 2, ...]
}
支持过滤和分页从指定的命名空间检索类型。
输入模式:
{
"type": "object",
"properties": {
"ProjectFileAbsolutePath": {
"type": "string"
},
"Namespaces": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
},
"PageNumber": {
"type": "integer"
},
"FullTextFiltersWithWildCardSupport": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"ProjectFileAbsolutePath"
]
}
响应:
{
"TypeData": [
{
"FullName": "Namespace.TypeName",
"Implements": ["Interface1", "Interface2", ...],
"Constructors": ["(param1, param2)", ...],
"Methods": ["ReturnType MethodName(param1, param2)", ...],
"Properties": ["PropertyType PropertyName { get; set; }", ...],
"Fields": ["FieldType FieldName", ...],
"Events": ["event EventHandlerType EventName", ...]
},
...
],
"CurrentPage": 1,
"AvailablePages": [1, 2, ...]
}
支持过滤和分页在 nuget.org 上搜索 NuGet 包。
输入模式:
{
"type": "object",
"properties": {
"SearchQuery": {
"type": "string"
},
"IncludePrerelease": {
"type": "boolean"
},
"PageNumber": {
"type": "integer"
},
"FullTextFiltersWithWildCardSupport": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"SearchQuery"
]
}
响应:
{
"Packages": [
{
"Id": "Newtonsoft.Json",
"Version": "13.0.3",
"Description": "Json.NET 是一个流行的高性能 JSON 框架",
"Authors": "James Newton-King",
"DownloadCount": 1000000,
"Published": "2023-03-08T00:00:00Z"
},
...
],
"CurrentPage": 1,
"AvailablePages": [1, 2, ...]
}
检索特定 NuGet 包的版本历史和依赖信息。
输入模式:
{
"type": "object",
"properties": {
"PackageId": {
"type": "string"
},
"IncludePrerelease": {
"type": "boolean"
},
"PageNumber": {
"type": "integer"
},
"FullTextFiltersWithWildCardSupport": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"PackageId"
]
}
响应:
{
"PackageId": "Newtonsoft.Json",
"Versions": [
{
"Id": "Newtonsoft.Json",
"Version": "13.0.3",
"Description": "Json.NET 是一个流行的高性能 JSON 框架",
"Authors": "James Newton-King",
"DownloadCount": 1000000,
"Published": "2023-03-08T00:00:00Z",
"DependencyGroups": [
{
"TargetFramework": ".NETStandard2.0",
"Dependencies": [
{
"Id": "System.Text.Json",
"VersionRange": "6.0.0"
}
]
}
]
},
...
],
"CurrentPage": 1,
"AvailablePages": [1,