Roslyn MCP服务器
一个模型上下文协议(MCP)服务器,为提供基于Roslyn的代码分析工具。NET解决方案和项目。
特性
- 加载。NET解决方案和项目 -支持
.sln和.csproj文件 - 类型信息检索 -获取有关类型的详细信息,包括成员、可访问性和文档
- 查找引用 -在整个解决方案中搜索对符号的所有引用
- 符号格式 -用源位置信息格式化符号
- 描述符号 -按名称或位置获取任何符号的详细信息
- 转到定义 -查找符号的源定义
先决条件
- .NET 9.0 SDK或更高版本
- MSBuild(包含在.NET SDK中)
建筑
dotnet build src/RoslynMcpServer/RoslynMcpServer.csproj跑步
服务器通过JSON-RPC 2.0通过stdin/stdout进行通信:
dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csproj以DLL运行
对于生产使用,构建Release配置并直接运行编译的DLL:
# Build Release configuration
dotnet build -c Release src/RoslynMcpServer/RoslynMcpServer.csproj
# Run the DLL directly (replace with your actual installation path)
dotnet /RoslynMcpServer/src/RoslynMcpServer/bin/Release/net9.0/RoslynMcpServer.dll服务器对JSON-RPC消息使用换行符分隔的JSON(NDJSON),如 MCP STDIO传输规范。消息由换行符(LF)分隔,不得包含嵌入的换行符。所有日志记录和诊断输出都发送到STDERR,保持STDOUT干净,以便JSON-RPC通信。
自检STDIO
要验证服务器是否使用干净的STDIO正常工作:
# Test the JSON-RPC communication (NDJSON format)
# Replace with your actual installation path
printf '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":1}\n' | \
dotnet /RoslynMcpServer/src/RoslynMcpServer/bin/Release/net9.0/RoslynMcpServer.dll
# Expected: JSON-RPC response as a single line terminated with newline, no extra text on STDOUT可用工具
Roslyn MCP服务器为提供了9个强大的工具。NET代码分析。每个工具都有AI模型看到的特定描述,以及定义可用参数的详细输入模式。
工具概述
| 工具名称 | 用途 | 主要功能 |
|---|---|---|
| load_工程 | 加载。NET解决方案/项目 | 初始化分析工作区 |
| get_type_info | 类型信息检索 | 带分页的成员、方法、属性 |
| 查找引用 | 查找符号用法 | 解决方案中的所有引用 |
| describe_符号 | 符号详细信息 | 按名称或文件位置 |
| goto定义 | 导航到源 | 查找定义符号的位置 |
| get_method_dependency | 分析方法调用 | 调用图、读/写、调用者 |
| get_heritage_tree | 类型层次结构 | 基类、接口、派生类型 |
| 获取所有实现 | 接口实现 | 查找所有具体实现 |
| test_symbol_formatting | 诊断工具 | 验证服务器功能 |
1.load_project(以前称为load_solution)
型号说明: “加载.NET项目文件(.csproj)或解决方案(.sln)进行分析。首选.csproj文件。仅使用绝对路径!”
目的: 加载a。NET解决方案或项目文件存储到内存中,以便其他工具进行分析。
参数:
path(字符串,必填):.csproj(首选)或.sln文件的绝对路径
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "load_project",
"arguments": {
"path": "/absolute/path/to/MyProject.csproj" // Replace with your actual project path
}
}],
"id": 1
}示例响应:
{
"success": true,
"solutionPath": "/absolute/path/to/MyProject.csproj", // Your actual path will be here
"projectCount": 3,
"projects": [
{"name": "MyProject", "tfm": "net9.0"},
{"name": "MyProject.Tests", "tfm": "net9.0"}
]
}使用说明:
- 始终使用绝对路径
- 为了更快地加载,更喜欢.csproj文件而不是.sln文件
- 在使用任何其他分析工具之前必须调用
- 该工具保持与“load_solution”名称的向后兼容性
2.get_type_info
型号说明: 获取有关类型的详细信息,包括成员、继承和文档
目的: 检索有关类型的全面信息,包括其所有成员(字段、属性、方法、事件),并支持大型类型的分页。
参数:
fullyQualifiedName(string,必填):类型的完全限定名(例如,System.string、MyNamespace.MyClass)page(整数,可选):页码(从1开始),默认值:1,最小值:1pageSize(整数,可选):每页结果数,默认值:200,范围:1-500
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "get_type_info",
"arguments": {
"fullyQualifiedName": "System.Collections.Generic.List`1",
"pageSize": 20
}
}],
"id": 2
}示例响应:
{
"success": true,
"symbol": {
"display": "class List",
"file": null,
"line": -1,
"column": -1
},
"totalMembers": 85,
"page": 1,
"pageSize": 20,
"members": [
{
"Name": "Count",
"Kind": "Property",
"Accessibility": "Public",
"IsStatic": false,
"Type": "int",
"Parameters": null
},
{
"Name": "Add",
"Kind": "Method",
"Accessibility": "Public",
"IsStatic": false,
"Type": "void",
"Parameters": "T item"
}
],
"nextCursor": "page=2"
}使用说明:
- 对具有多个成员的类型使用分页(例如,System.String有200多个成员)
- 成员包括字段、属性、方法、构造函数、事件和嵌套类型
- 泛型类型应包括回溯符号(例如。,
List1for列表\`)
3.查找引用
型号说明: “在加载的解决方案中查找对符号的所有引用”
目的: 搜索整个加载的解决方案,以查找对指定符号(类型、方法、属性、字段等)的所有用法和引用。
参数:
fullyQualifiedName(string,必填):符号的完全限定名page(整数,可选):页码(从1开始),默认值:1,最小值:1pageSize(整数,可选):每页结果数,默认值:200,范围:1-500timeoutMs(整数,可选):超时(毫秒),默认值:60000,范围:1000-300000
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "find_references",
"arguments": {
"fullyQualifiedName": "MyApp.Services.IUserService.GetUserAsync",
"pageSize": 25,
"timeoutMs": 120000
}
}],
"id": 3
}示例响应:
{
"success": true,
"total": 47,
"page": 1,
"pageSize": 25,
"references": [
{
"file": "/src/MyApp/Controllers/UserController.cs",
"line": 42,
"column": 15,
"text": "var user = await _userService.GetUserAsync(userId);"
},
{
"file": "/src/MyApp/Services/OrderService.cs",
"line": 108,
"column": 20,
"text": "var customer = await userService.GetUserAsync(order.CustomerId);"
}
],
"nextCursor": "eyJvZmZzZXQiOjI1fQ=="
}使用说明:
- 搜索包括所有直接引用、方法调用、类型实例化和继承
- 使用
nextCursor用于分页(下一页的不透明标记) - 增加
timeoutMs用于大型解决方案或复杂符号 - 结果包括包含引用的确切代码行
4.描述符号
型号说明: “按完全限定名或文件位置获取有关符号的详细信息”
目的: 通过符号的完全限定名或指定文件中的位置,检索有关任何符号的详细信息。有助于理解特定代码位置的内容。
参数(选项1-按名称):
fullyQualifiedName(string):符号的完全限定名(提供此OR文件/行/列)
参数(选项2-按位置):
file(string):文件路径(与行和列一起使用)line(整数):行号(从1开始,与文件和列一起使用)column(整数):列号(从1开始,与文件和行一起使用)
示例请求(按名称):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "describe_symbol",
"arguments": {
"fullyQualifiedName": "System.Linq.Enumerable.Where"
}
}],
"id": 4
}示例请求(按职位):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "describe_symbol",
"arguments": {
"file": "/src/MyApp/Services/UserService.cs",
"line": 42,
"column": 20
}
}],
"id": 5
}示例响应:
{
"success": true,
"display": "Task UserService.GetUserAsync(int userId)",
"file": "/src/MyApp/Services/UserService.cs",
"line": 42,
"column": 17,
"kind": "Method",
"containingType": "MyApp.Services.UserService",
"containingNamespace": "MyApp.Services",
"isAsync": true,
"returnType": "Task",
"accessibility": "Public"
}使用说明:
- 基于位置的查找对于IDE中的“转到定义”场景很有用
- 返回关于符号的全面元数据,包括其种类、包含类型和命名空间
- 适用于任何符号:类型、方法、属性、字段、参数、局部变量
5.goto_定义
型号说明: “查找符号的源定义”
目的: 定位定义符号的确切源代码位置。如果源代码不可用(例如,对于框架类型),则返回元数据定义位置。
参数:
fullyQualifiedName(string,必填):符号的完全限定名
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "goto_definition",
"arguments": {
"fullyQualifiedName": "MyApp.Core.Domain.User.Validate"
}
}],
"id": 6
}示例响应:
{
"success": true,
"display": "bool User.Validate()",
"file": "/src/MyApp.Core/Domain/User.cs",
"line": 156,
"column": 17,
"isSourceDefinition": true,
"isFromMetadata": false,
"kind": "Method",
"containingType": "MyApp.Core.Domain.User",
"containingNamespace": "MyApp.Core.Domain"
}使用说明:
- 对导航和代码探索至关重要
isSourceDefinition指示是否找到实际源代码isFromMetadata指示定义是否来自已编译的程序集- 返回精确的文件路径、行和列,以便精确导航
6.方法依赖关系
型号说明: 分析方法依赖关系:调用、读取/写入字段/属性;可选调用者
目的: 提供对方法依赖关系的深入分析,包括它调用的内容、它读取或写入的字段/属性以及可选的调用者。支持具有可配置深度的传递性分析。
参数:
fullyQualifiedName(string):完全限定名(或使用文件/行/列)file(string):文件路径(fullyQualifiedName的替代)line(整数):行号(从1开始,与文件一起使用)column(整数):列号(从1开始,与文件一起使用)depth(整数,可选):传递调用的遍历深度,默认值:1,最小值:1includeCallers(boolean,可选):包括根方法的调用者列表,默认值:falsetreatPropertiesAsMethods(boolean,可选):将属性访问器报告为方法调用,默认值:truepage(整数,可选):呼叫列表的页码,默认值:1,最小值:1pageSize(整数,可选):每页结果数,默认值:200,范围:1-500timeoutMs(整数,可选):超时(毫秒),默认值:60000,范围:1000-300000
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "get_method_dependencies",
"arguments": {
"fullyQualifiedName": "MyApp.Services.OrderService.ProcessOrderAsync",
"depth": 3,
"includeCallers": true,
"treatPropertiesAsMethods": true,
"pageSize": 100
}
}],
"id": 7
}示例响应:
{
"success": true,
"symbol": {
"display": "async Task OrderService.ProcessOrderAsync(Order order)",
"file": "/src/MyApp/Services/OrderService.cs",
"line": 45,
"column": 17
},
"totalCalls": 12,
"calls": [
{
"display": "Task IPaymentService.ProcessPaymentAsync(decimal amount)",
"file": "/src/MyApp/Services/PaymentService.cs",
"line": 23,
"column": 17
},
{
"display": "void ILogger.LogInformation(string message)",
"file": null,
"line": -1,
"column": -1
}
],
"reads": [
{
"display": "OrderSettings OrderService._settings",
"file": "/src/MyApp/Services/OrderService.cs",
"line": 12,
"column": 19
}
],
"writes": [
{
"display": "DateTime Order.ProcessedAt",
"file": "/src/MyApp/Models/Order.cs",
"line": 34,
"column": 19
}
],
"callers": [
{
"display": "Task OrderController.CreateOrder(OrderDto dto)",
"file": "/src/MyApp/Controllers/OrderController.cs",
"line": 67,
"column": 15
}
],
"page": 1,
"pageSize": 100,
"nextCursor": null
}使用说明:
depth控制遍历调用图的深度(1=仅直接调用,2+=可传递)- 使用
includeCallers了解哪些代码依赖于此方法 reads和writes帮助识别数据依赖关系和潜在的副作用- 非常适合理解方法复杂性和重构影响
9.测试_符号_格式化
型号说明: “使用位置信息演示符号格式的测试工具”
目的: 一种诊断工具,用于测试和演示服务器的符号格式化功能。有助于验证服务器是否正常工作。
参数:
- 无需
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "test_symbol_formatting",
"arguments": {}
}],
"id": 11
}示例响应:
{
"success": true,
"message": "Symbol formatting test completed",
"examples": [
{
"symbol": "System.String",
"formatted": "class String",
"location": "metadata"
},
{
"symbol": "MyApp.Core.User",
"formatted": "class User",
"location": "/src/MyApp.Core/User.cs:10:5"
}
]
}工具使用模式
常见工作流
1.代码导航流程:
load_project → describe_symbol → goto_definition → find_references2.类型分析流程:
load_project → get_type_info → get_inheritance_tree → get_all_implementations3.依赖性分析流程:
load_project → get_method_dependencies (depth:3, includeCallers:true)4.重构准备:
load_project → find_references → get_method_dependencies → get_inheritance_tree最佳实践
- 始终先加载:呼叫
load_project在任何其他工具之前 - 使用绝对路径:所有文件路径必须是绝对的,而不是相对的
- 分页:对大型结果集使用分页以避免超时
- 超时:增加
timeoutMs用于大型代码库上的复杂操作 - 职位与姓名:使用文件/行/列进行精确定位,FQN用于已知符号
性能注意事项
- 项目加载:更喜欢
.csproj超过.sln为了更快的装载 - 深度限制:使用合理的深度值(1-3)进行相关性分析
- 解决方案范围:设置
solutionOnly: true将结果限制在代码中 - 缓存:服务器缓存加载的解决方案以供后续操作
STDIO示例(CLI)
所有工具都通过MCP通过STDIO使用NDJSON进行通信。以下是使用以下命令行界面(CLI)的快速代码片段 printf 通过管道连接到正在运行的服务器。
注意:请先加载解决方案或项目。
# Load solution or project (replace with actual path)
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"load_solution","arguments":{"path":"/your.sln"}}],"id":1}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojget_method_dependences:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"get_method_dependencies","arguments":{"fullyQualifiedName":"MyApp.Core.Utils.DoWork","depth":2,"includeCallers":true}}],"id":2}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojget_heritage_tree:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"get_inheritance_tree","arguments":{"fullyQualifiedName":"MyApp.Core.BaseType","direction":"descendants","includeInterfaces":true}}],"id":3}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csproj获取所有实现:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"get_all_implementations","arguments":{"fullyQualifiedName":"MyApp.Core.IMyInterface","includeDerivedInterfaces":true}}],"id":4}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojget_type_info:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"get_type_info","arguments":{"fullyQualifiedName":"System.String","pageSize":10}}],"id":5}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojfind_references:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"find_references","arguments":{"fullyQualifiedName":"MyApp.Core.SomeType","pageSize":50}}],"id":6}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojdescribe_symbol(按名称):
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"describe_symbol","arguments":{"fullyQualifiedName":"System.Console.WriteLine"}}],"id":7}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojdescribe_symbol(按位置):
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"describe_symbol","arguments":{"file":"/absolute/path/to/File.cs","line":42,"column":15}}],"id":8}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojgoto_定义:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"goto_definition","arguments":{"fullyQualifiedName":"MyApp.Core.SomeType.SomeMethod"}}],"id":9}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csprojtest_symbol_formatting:
printf '{"jsonrpc":"2.0","method":"tools/call","params":[{"name":"test_symbol_formatting","arguments":{}}],"id":10}\n' \
| dotnet run --project src/RoslynMcpServer/RoslynMcpServer.csproj7.获取_遗产_树
型号说明: 返回完整的继承树(祖先、接口、后代;可选覆盖)
目的: 分析类型的完整继承层次结构,显示基类、实现的接口和派生类型。可以选择包括虚拟成员的覆盖信息。
参数:
fullyQualifiedName(string):目标类型名称(或使用文件/行/列)file(string):文件路径(fullyQualifiedName的替代)line(整数):行号(从1开始,与文件一起使用)column(整数):列号(从1开始,与文件一起使用)direction(字符串,可选):树方向-“两者”、“祖先”或“后代”,默认值:“两者”includeInterfaces(boolean,可选):包括已实现的接口,默认值:trueincludeOverrides(布尔值,可选):包括每个成员的覆盖信息,默认值:falsemaxDepth(整数,可选):子代树的最大深度,默认值:10,范围:1-100solutionOnly(boolean,可选):仅在加载的解决方案中包含具有源代码的类型,默认值:truepage(整数,可选):平面子体列表的页码,默认值:1pageSize(整数,可选):每页结果数,默认值:200,范围:1-500timeoutMs(整数,可选):超时(毫秒),默认值:60000,范围:1000-300000
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "get_inheritance_tree",
"arguments": {
"fullyQualifiedName": "MyApp.Core.Domain.BaseEntity",
"direction": "descendants",
"includeInterfaces": true,
"includeOverrides": true,
"maxDepth": 5
}
}],
"id": 8
}示例响应:
{
"success": true,
"root": {
"display": "abstract class BaseEntity",
"file": "/src/MyApp.Core/Domain/BaseEntity.cs",
"line": 5,
"column": 14
},
"ancestors": [
{"display": "class System.Object"}
],
"interfaces": [
{"display": "interface IEntity"},
{"display": "interface IValidatable"}
],
"descendantsTree": {
"display": "class User : BaseEntity",
"file": "/src/MyApp.Core/Domain/User.cs",
"children": [
{
"display": "class AdminUser : User",
"file": "/src/MyApp.Core/Domain/AdminUser.cs",
"children": []
}
]
},
"descendantsFlat": [
{"display": "class User : BaseEntity"},
{"display": "class AdminUser : User"},
{"display": "class Product : BaseEntity"}
],
"overrides": {
"Validate": [
{"type": "User", "hasOverride": true},
{"type": "Product", "hasOverride": true}
]
},
"page": 1,
"pageSize": 200,
"total": 3
}使用说明:
descendantsTree提供分层视图,descendantsFlat提供线性列表- 使用
solutionOnly: false在层次结构中包含框架类型 includeOverrides显示哪些派生类型覆盖虚拟/抽象成员- 对于理解类型层次结构和多态行为至关重要
8.全部实现
型号说明: 列出接口的所有实现,或特定接口成员的实现
目的: 查找接口或特定接口成员的所有具体实现。有助于理解合约是如何在代码库中实现的。
参数:
fullyQualifiedName(string):目标接口或接口成员(或使用文件/行/列)file(string):文件路径(fullyQualifiedName的替代)line(整数):行号(从1开始,与文件一起使用)column(整数):列号(从1开始,与文件一起使用)member(字符串,可选):FQN指向接口类型时的特定成员名称solutionOnly(boolean,可选):仅在加载的解决方案中包含实现,默认值:trueincludeDerivedInterfaces(布尔值,可选):包括派生接口,默认值:truepage(整数,可选):页码,默认值:1pageSize(整数,可选):每页结果数,默认值:200,范围:1-500timeoutMs(整数,可选):超时(毫秒),默认值:60000,范围:1000-300000
示例请求(接口):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "get_all_implementations",
"arguments": {
"fullyQualifiedName": "MyApp.Services.IUserService",
"includeDerivedInterfaces": true,
"solutionOnly": true
}
}],
"id": 9
}示例请求(特定成员):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": [{
"name": "get_all_implementations",
"arguments": {
"fullyQualifiedName": "MyApp.Services.IUserService",
"member": "GetUserAsync",
"includeDerivedInterfaces": false
}
}],
"id": 10
}示例响应:
{
"success": true,
"interface": {
"display": "interface IUserService",
"file": "/src/MyApp/Services/IUserService.cs",
"line": 5
},
"derivedInterfaces": [
{
"display": "interface IExtendedUserService : IUserService",
"file": "/src/MyApp/Services/IExtendedUserService.cs"
}
],
"implementations": [
{
"display": "class UserService : IUserService",
"file": "/src/MyApp/Services/UserService.cs",
"line": 12
},
{
"display": "class CachedUserService : IUserService",
"file": "/src/MyApp/Services/CachedUserService.cs",
"line": 8
},
{
"display": "class MockUserService : IUserService",
"file": "/tests/MyApp.Tests/Mocks/MockUserService.cs",
"line": 6
}
],
"memberImplementations": {
"GetUserAsync": [
{"type": "UserService", "hasImplementation": true},
{"type": "CachedUserService", "hasImplementation": true},
{"type": "MockUserService", "hasImplementation": true}
]
},
"total": 3,
"page": 1,
"pageSize": 200
}使用说明:
- 对于依赖注入分析和理解接口使用至关重要
- 使用
member用于查找特定方法/属性实现的参数 includeDerivedInterfaces帮助跟踪接口继承链- 与…结合
solutionOnly: false包括框架实现
AI友好使用(0.2.0)
Roslyn MCP服务器现在包括对人工智能友好的包装器,提供对服务器功能的确定性、非交互式访问。这些工具输出干净的NDJSON,并将所有日志路由到STDERR,使其成为自动化系统和AI代理的理想选择。
所有工具都需要事先 load_project 在使用分析命令之前调用。
单发命令
使用bash包装器执行一次性命令:
# Load a project (always do this first)
# Replace with your actual project path
./tools/ai/roslyn-ai.sh --load "/YourProject.csproj"
# Get type information
./tools/ai/roslyn-ai.sh --type "System.String" 20
# Find references
./tools/ai/roslyn-ai.sh --refs "MyNamespace.MyClass"
# Get method dependencies
./tools/ai/roslyn-ai.sh --deps "MyClass.MyMethod" 2 true
# Get inheritance tree
./tools/ai/roslyn-ai.sh --tree "MyClass" descendants会话模式
在一次调用中加载项目并执行命令:
# Example using the session helper
# Replace with your actual project path
./examples/ai/session.sh "/YourProject.csproj"
# Manual session mode
./tools/ai/roslyn-ai.sh --session "/YourProject.csproj" type System.String 10批处理模式
从文件或stdin执行多个命令:
# Using the batch example
# Replace with your actual project path
./examples/ai/run_batch.sh "/YourProject.csproj"
# Manual batch mode with file
./tools/ai/roslyn-ai.sh --batch commands.txt
# Batch mode from stdin (replace
with actual path)
echo -e "load
/project.csproj\ntype System.String\nrefs MyClass" | ./tools/ai/roslyn-ai.sh --batchPython接口
使用Python包装器进行编程访问:
from tools.ai.roslyn_mcp_ai import RoslynMCP
# Context manager approach (recommended)
with RoslynMCP() as mcp:
# Load project first (replace with your actual project path)
result = mcp.load_project("/your/actual/path/to/project.csproj")
if not result.success:
print(f"Error: {result.error}")
return
# Get type information
result = mcp.get_type_info("System.String")
print(result.data)
# CLI usage (replace with actual path)
python3 ./tools/ai/roslyn_mcp_ai.py \
--project "/project.csproj" \
--command type \
--target "System.String" \
--page-size 20 \
--json特性
- 确定性输出:所有响应都是一致的NDJSON
- 清洁STDOUT:只有数据输出,所有日志都转到STDERR
- 错误处理:错误时的非零退出代码
- 超时:可配置的操作超时
- 多种格式:Bash脚本、Python类和CLI工具
可用命令
| 命令 | 描述 | 参数 |
|---|---|---|
load | 加载项目/解决方案 | ` |
| ` | ||
type | 获取类型信息 | [page-size] |
refs | 查找参考文献 | `` |
symbol | 描述符号 | `` |
def | 转到定义 | `` |
deps | 获取依赖关系 | [depth] [include-callers] |
tree | 获取继承树 | [direction] |
impl | 获取实现 | `` |
test | 测试格式 | (无参数) |
备注:所有文件路径都必须是绝对路径。包装器要求首先构建主服务器。
协议
服务器使用JSON-RPC 2.0实现模型上下文协议(MCP)。消息使用NDJSON(换行符分隔的JSON)格式传输。
消息格式
请求:
{"jsonrpc":"2.0","method":"","params":
,"id":}\n答复:
{"jsonrpc":"2.0","id":,"result":}\n交通(STDIO)
服务器使用NDJSON格式的MCP STDIO传输:
- 格式:每行一个JSON对象,终止于
\n(LF) - 编码:UTF-8
- 没有嵌入换行符:JSON消息不能包含文本换行符
- 输出流:
- STDOUT:仅限有效的MCP消息 - STDERR:所有日志和诊断输出
有关更多详细信息,请参阅 MCP传输规范.
错误处理
- 缺少或无效的参数返回错误
isError: true - 超时通过适当的错误消息得到妥善处理
- WorkspaceFailed事件记录到stderr进行调试
已知问题
OpenSolutionAsync在某些环境中可能会挂起。服务器包括90秒的超时作为解决方法。- 目标框架检测可能不适用于所有项目类型。
发展
该项目的结构如下:
src/RoslynMcpServer/
Infrastructure/ # MCP protocol implementation
McpServer.cs # Main MCP server with tool registration
JsonRpcLoop.cs # JSON-RPC message handling
Roslyn/ # Roslyn integration
WorkspaceHost.cs # MSBuild workspace management
SymbolFormatting.cs # Symbol display formatting
Tools/ # MCP tool implementations
LoadSolutionTool.cs # Solution/project loading
GetTypeInfoTool.cs # Type information retrieval
FindReferencesTool.cs # Reference finding
DescribeSymbolTool.cs # Symbol description by name or position
GotoDefinitionTool.cs # Find symbol source definition许可证
麻省理工学院
