中文 | English
CSharp MCP Server 是一个基于 Roslyn 的 Model Context Protocol (MCP) Server,提供强大的 C# 代码分析和导航功能。
- 摆脱 LSP 限制: 直接使用 Roslyn API,获取完整的符号范围信息
- 丰富的语义分析: 继承层次、调用图、类型成员分析
- Token 优化: 分层响应、智能截断
- 批量操作: 减少交互次数
获取文档中的所有符号。
参数:
{
"file_path": "string (必需) - 文件路径,支持绝对路径、相对路径、仅文件名模糊匹配",
"line_number": "int? (可选) - 行号,用于模糊匹配",
"symbol_name": "string? (可选) - 符号名称,用于验证和模糊匹配",
"detail_level": "DetailLevel (可选) - 输出详细级别: Compact, Summary, Standard, Full",
"include_body": "bool (可选) - 是否包含方法体,默认 true",
"body_max_lines": "int (可选) - 方法体最大行数,默认 100",
"filter_kinds": "SymbolKind[] (可选) - 符号类型过滤"
}响应:
## Symbols: FileName.cs
**Total: N symbol(s)**
- **SymbolName** (Method):12-45 - public static void MethodName(string param)
- Documentation...跳转到符号定义。
参数:
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"detail_level": "DetailLevel (可选)",
"include_body": "bool (可选)",
"body_max_lines": "int (可选)"
}响应:
### Definition: `MethodName`
(lines 12-45)
**Signature**:
```csharp
void MethodName(string param);Implementation:
public void MethodName(string param)
{
// ...
}
---
#### `find_references`
查找符号的所有引用。
**参数**:
```json
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"symbol_kind": "string (可选) - 按符号类型过滤: NamedType, Method, Property, Field",
"include_context": "bool (可选) - 是否包含上下文代码",
"context_lines": "int (可选) - 上下文代码行数"
}
响应:
## References: `MethodName`
**Found 10 reference(s)**
### FileName.cs
- Line 25: ContainingMethod
```csharp
MethodName("value");
---
#### `resolve_symbol`
获取符号的完整信息(包含文档、定义和部分引用)。
**参数**:
```json
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"detail_level": "DetailLevel (可选)",
"include_body": "bool (可选)",
"body_max_lines": "int (可选)"
}
响应:
## Symbol: `MethodName`
**Location**:
- File: path/to/file.cs
- Lines: 12-45
**Type**:
- Kind: Method
- Containing Type: ClassName
- Namespace: MyNamespace
**Modifiers**:
- Accessibility: Public
- Static: yes
**Signature**:
```csharp
void MethodName(string param);References (5 found):
- file.cs:25 in OtherMethod
---
#### `search_symbols`
搜索整个工作区中的符号。
**参数**:
```json
{
"query": "string (必需) - 搜索查询,支持通配符如 My.*, *.Controller",
"symbol_kind": "string (可选) - 按符号类型过滤: NamedType, Method, Property, Field",
"detail_level": "DetailLevel (可选)",
"max_results": "int (可选) - 最大结果数量,默认 100"
}
响应:
## Search Results: "MyClass.*"
**Found 5 symbol(s)**
### Namespace: MyNamespace
- **MyClass** (Class) - MyClass.cs:10
- **MyMethod** (Method) - MyClass.cs:25查找接口、抽象类或虚方法/抽象方法的所有实现。
参数:
{
"symbolName": "string (必需) - 接口、基类或方法的名称",
"filePath": "string (可选) - 包含符号的文件路径",
"lineNumber": "int (可选) - 符号声明附近的 1-based 行号",
"symbolKind": "string (可选) - 按符号类型过滤: NamedType, Method, Property",
"maxResults": "int (可选) - 返回的最大实现数量,默认 50"
}智能消歧: 当多个符号匹配名称时,工具按优先级自动选择:
- 接口 > 抽象类 > 抽象方法 > 虚方法 > 普通方法
- 如果自动选择失败,返回候选列表供用户选择
响应:
# Implementations: `IInterface`
## Summary
Found **5** implementation(s)
**Kind**: Interface
## Implementations
- **Class1** | `Class` | File1.cs:10
- **Class2** | `Class` | File2.cs:25获取类型的继承层次结构。
参数:
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"include_derived": "bool (可选) - 是否包含派生类",
"max_derived_depth": "int (可选) - 派生类最大深度"
}响应:
## Inheritance Hierarchy: `MyClass`
**Base Types**:
- BaseType1
- BaseType2
**Implemented Interfaces**:
- IEnumerable<T>
- IDisposable
**Derived Types** (3, depth: 2):
- **DerivedClass** (Class) - DerivedClass.cs:10获取方法的调用图。
参数:
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"direction": "CallGraphDirection (可选) - 调用方向: Both, In, Out",
"max_depth": "int (可选) - 最大深度",
"include_external_calls": "bool (可选) - 是否包含外部调用"
}响应:
## Call Graph: `MethodName`
**Statistics**:
- Total callers: 5
- Total callees: 10
- Cyclomatic complexity: 3
**Called By** (5):
- **CallerMethod** - CallerClass.cs:20
- at CallerMethod (CallerClass.cs:20)
**Calls** (10):
- **CalledMethod** - UtilClass.cs:50获取类型的成员。
参数:
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"include_inherited": "bool (可选) - 是否包含继承的成员",
"filter_kinds": "SymbolKind[] (可选) - 成员类型过滤"
}响应:
## Type Members: `MyClass`
**Total: 15 member(s)**
### Method
- **Method1** (public static Method) - MyClass.cs:25
- **Method2** (public virtual Method) - MyClass.cs:50整合多个信息源获取完整符号信息,减少API调用次数。
参数:
{
"file_path": "string (必需)",
"line_number": "int? (可选)",
"symbol_name": "string? (可选)",
"sections": "SymbolCompleteSections (可选) - 要获取的信息部分",
"detail_level": "DetailLevel (可选)",
"body_max_lines": "int (可选)",
"include_references": "bool (可选)",
"max_references": "int (可选)",
"include_inheritance": "bool (可选)",
"include_call_graph": "bool (可选)",
"call_graph_max_depth": "int (可选)"
}SymbolCompleteSections 是一个标志枚举:
Basic = 1- 基本信息Signature = 2- 签名信息Documentation = 4- 文档注释SourceCode = 8- 源代码References = 16- 引用位置Inheritance = 32- 继承层次CallGraph = 64- 调用图All = 127- 所有信息
响应:
## Symbol: `MethodName`
**Type**: Method
**Signature**:
```csharp
void MethodName(string param);Documentation: Method description...
Inheritance:
- Base Types: BaseType1
- Interfaces: IEnumerable
Call Graph:
- Callers: 5
- Callees: 10
- Complexity: 3
---
#### `batch_get_symbols`
批量获取符号信息,使用并行处理提高性能。
**参数**:
```json
{
"symbols": "FileLocationParams[] (必需) - 符号位置列表",
"detail_level": "DetailLevel (可选)",
"include_body": "bool (可选)",
"body_max_lines": "int (可选)",
"max_concurrency": "int (可选) - 最大并发数"
}
响应:
## Batch Symbol Query Results
**Total**: 3 | **Success**: 2 | **Errors**: 1
### ✅ Method1
- Type: Method
- Location: File1.cs:25
- Signature: void Method1()
### ✅ Method2
- Type: Method
- Location: File1.cs:50
- Signature: int Method2(string param)
### ❌ Unknown
Error: Symbol not found获取编译诊断信息(错误、警告和信息)。
参数:
{
"file_path": "string? (可选) - 文件路径,不指定则获取整个工作区的诊断",
"include_warnings": "bool (可选) - 是否包含警告",
"include_info": "bool (可选) - 是否包含信息",
"include_hidden": "bool (可选) - 是否包含隐藏诊断",
"severity_filter": "DiagnosticSeverity[] (可选) - 严重性过滤"
}DiagnosticSeverity 枚举:
ErrorWarningInfoHidden
响应:
## Diagnostics Report
**Summary**:
- Errors: 2
- Warnings: 5
- Info: 1
- Files affected: 3
### File1.cs
- ❌ **CS0219** (Line 15): Variable is assigned but its value is never used
- ⚠️ **CS0168** (Line 20): The variable 'x' is declared but never used控制输出详细级别,优化 token 使用。
Compact- 最小信息,仅名称和位置Summary- 简要信息,包含基本签名Standard- 标准信息,包含文档和部分源代码Full- 完整信息,包含所有源代码
符号类型分类。
类型:
Class,Struct,Interface,Enum,Record,Delegate,Attribute
成员:
Method,Property,Field,Event,Constructor,Destructor
其他:
Namespace,Parameter,Local,TypeParameter,Unknown
可访问性。
Public,Internal,Protected,ProtectedInternal,PrivateProtected,Private,NotApplicable
工具支持多种文件路径格式:
- 绝对路径:
C:\Project\MyFile.cs - 相对路径:
src\MyFile.cs - 仅文件名:
MyFile.cs(模糊匹配工作区中的文件)
使用 detail_level 参数控制输出详细程度:
- 快速浏览: 使用
Compact获取符号列表 - 理解代码: 使用
Summary或Standard查看签名和文档 - 深入研究: 使用
Full获取完整源代码
body_max_lines限制返回的源代码行数max_references限制返回的引用数量max_results限制搜索结果数量
- 使用
batch_get_symbols一次性查询多个符号 - 使用
get_symbol_complete获取所有需要的信息
- 使用
sections参数指定需要的信息部分 - 使用
filter_kinds过滤不需要的符号类型
所有工具在遇到错误时返回:
{
"error": "Error message"
}常见错误:
- 文件未找到:
"File not found: path/to/file.cs" - 符号未找到:
"Symbol not found: SymbolName" - 工作区未加载:
"Workspace not loaded" - 无效参数:
"Invalid parameters type"
{
"file_path": "MyClass.cs",
"filter_kinds": ["Method"],
"detail_level": "Summary"
}{
"file_path": "MyClass.cs",
"line_number": 25,
"symbol_name": "MyMethod",
"direction": "Out",
"max_depth": 2
}{
"symbols": [
{"file_path": "MyClass.cs", "line_number": 25},
{"file_path": "MyClass.cs", "line_number": 50}
],
"detail_level": "Standard",
"max_concurrency": 5
}{
"file_path": "MyClass.cs",
"line_number": 25,
"symbol_name": "MyMethod",
"sections": "All",
"include_references": true,
"max_references": 20,
"include_inheritance": true,
"include_call_graph": true
}- 使用批量查询: 使用
batch_get_symbols替代多次单独调用 - 控制详细级别: 使用
Summary进行快速浏览 - 限制结果数量: 设置
max_results、max_references等参数 - 按需加载: 使用
sections仅获取需要的信息
| 操作 | 目标性能 |
|---|---|
| 加载解决方案 | < 5s |
| 获取文档符号 | < 500ms |
| 搜索符号 (1000+) | < 1s |
| 查找引用 | < 2s |
| 调用图分析 | < 3s |
| 继承层次分析 | < 1s |
| 批量查询 (10个) | < 1s |