WpfVisualTreeMcp
    
用于检查WPF应用程序Visual Trees的MCP服务器-使AI代理能够实时调试和分析WPF UI层次结构
📢 最新更新: 看 发布_拒绝.md 最近的改进包括屏幕截图捕获、DLL自动注入、Adorner Layer/Popup遍历和IPC死锁修复。
概述
为何这很重要
调试WPF UI问题传统上需要使用专门的工具进行手动检查。该项目通过MCP公开WPF检查功能,使AI代理能够:
- 了解UI结构 在代码审查期间
- 诊断绑定错误 自动地
- 建议修复 布局问题
- 协助UI重构 任务
- 分析可视化树层次结构 实时的
特性
岩芯检查
- 流程挖掘 -列出所有可供检查的正在运行的WPF应用程序
- 可视化树导航 -遍历完整的视觉树层次结构
- 逻辑树访问 -浏览逻辑树结构
- 财产清查 -读取任何UI元素的所有依赖属性
绑定和资源
- 绑定分析 -使用转换器、StringFormat、FallbackValue、MultiBinding支持检查绑定
- DataContext检查 -查看DataContext类型、属性、INPC状态和继承链
- 绑定错误检测 -自动捕获并报告带有分类的绑定错误
- 资源枚举 -在任何范围内浏览资源词典
- 款式检查 -查看应用的样式和模板
搜索和监控
- 元素搜索 -按类型、名称或属性值查找元素
- 深度搜索 -搜索整个树,包括Adorner Layer和Popup元素
- 物业监控 -实时监控属性变化
互动与导出
- 截图捕捉 -捕获AI代理可见的窗口/元素屏幕截图
- 元素突出显示 -在运行中的应用程序中直观地突出显示元素
- 布局信息 -获取详细的布局指标
- 树木出口 -将可视化树导出为XAML或JSON格式
- 自动注入 -将Inspector注入正在运行的WPF进程中(无需更改源代码)
快速开始
先决条件
- Windows 10/11
- .NET 8.0 SDK 或以后
- 要检查的WPF应用程序
安装
选项1:下载版本(推荐)
从下载最新版本 :
- 下载
WpfVisualTreeMcp-vX.X.X-win-x64.zip - 提取到文件夹(例如。,
C:\Tools\WpfVisualTreeMcp) - MCP服务器可执行文件位于
server\WpfVisualTreeMcp.Server.exe
选项2:从源代码构建
git clone https://github.com/faze79/WpfVisualTreeMcp.git
cd WpfVisualTreeMcp
dotnet build -c Release选项3:。NET工具(即将推出)
dotnet tool install -g WpfVisualTreeMcp配置
服务器使用 微软官方/Anthopic MCP SDK。网,保证与Claude Code和其他MCP客户端的兼容性。
克劳德代码
选项1:命令行(推荐)
使用 claude mcp add 命令直接添加服务器:
# Add to current project only
claude mcp add wpf-visual-tree -- C:/path/to/WpfVisualTreeMcp/publish/WpfVisualTreeMcp.Server.exe
# Add globally (available in all projects)
claude mcp add --scope user wpf-visual-tree -- C:/path/to/WpfVisualTreeMcp/publish/WpfVisualTreeMcp.Server.exe您可以验证服务器是否已添加:
claude mcp list选项2:项目级JSON配置
创建或编辑 .mcp.json 在项目根目录中:
{
"mcpServers": {
"wpf-visual-tree": {
"command": "C:/path/to/WpfVisualTreeMcp/publish/WpfVisualTreeMcp.Server.exe",
"args": []
}
}
}选项3:全局JSON配置
添加 ~/.claude/settings.json:
{
"mcpServers": {
"wpf-visual-tree": {
"command": "C:/path/to/WpfVisualTreeMcp/publish/WpfVisualTreeMcp.Server.exe",
"args": []
}
}
}重要提示:
- 使用构建的绝对路径
.exe文件 - 使用正斜杠(
/)在Windows上的路径中 - 用于生产的内置发布模式:
dotnet build -c Release - 配置更改后重新启动Claude代码
光标
添加到光标设置(.cursor/mcp.json):
{
"mcpServers": {
"wpf-visual-tree": {
"command": "C:/path/to/WpfVisualTreeMcp/publish/WpfVisualTreeMcp.Server.exe",
"args": []
}
}
}自托管模式(推荐)
为了使WPF应用程序可检查,请添加对检查器DLL的引用,并在启动时对其进行初始化:
- 将项目引用添加到
WpfVisualTreeMcp.Inspector
- 在你的
App.xaml.cs:
using System.Diagnostics;
using System.Windows;
using WpfVisualTreeMcp.Inspector;
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Initialize the WPF Visual Tree Inspector
InspectorService.Initialize(Process.GetCurrentProcess().Id);
}
protected override void OnExit(ExitEventArgs e)
{
InspectorService.Instance?.Dispose();
base.OnExit(e);
}
}这使得MCP服务器能够通过命名管道连接到您的应用程序进行实时检查。
用法示例
列出正在运行的WPF应用程序
Use the wpf_list_processes tool to show all running WPF applications.连接并检查视觉树
Attach to the MyApp.exe WPF application and show me the visual tree of the main window.查找绑定错误
Check the attached WPF application for any binding errors and explain what's causing them.搜索元素
Find all Button elements in the visual tree that have IsEnabled set to false.导出可视化树
Export the visual tree of the current window to JSON format so I can analyze the structure.建筑
┌─────────────────────────────────────────────────────────────┐
│ AI Agent (Claude Code) │
└─────────────────────────┬───────────────────────────────────┘
│ MCP Protocol (stdio)
▼
┌─────────────────────────────────────────────────────────────┐
│ WpfVisualTreeMcp Server │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │
│ │ MCP Handler │ │ Tree Navigator │ │ Injector │ │
│ │ (Tools/Res.) │ │ & Inspector │ │ Manager │ │
│ └────────┬────────┘ └────────┬────────┘ └──────┬──────┘ │
└───────────┼────────────────────┼───────────────────┼────────┘
│ │ │
└────────────────────┼───────────────────┘
│ Named Pipes / IPC
▼
┌─────────────────────────────────────────────────────────────┐
│ Target WPF Application │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ Injected Inspector DLL ││
│ │ • VisualTreeHelper access ││
│ │ • LogicalTreeHelper access ││
│ │ • Property/Binding inspection ││
│ │ • Resource dictionary enumeration ││
│ └─────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────┘有关详细的体系结构文档,请参阅 docs/ARCHITECTURE.md.
可用工具
| 工具 | 说明 |
|---|---|
wpf_list_processes | 列出所有正在运行的WPF应用程序 |
wpf_attach | 附加到WPF应用程序(支持 auto_inject 用于DLL注入) |
wpf_get_visual_tree | 获取可视化树层次结构(默认深度:25,最大深度:100) |
wpf_get_element_properties | 获取元素的所有依赖属性 |
wpf_find_elements | 按类型或名称搜索元素(部分匹配) |
wpf_find_elements_deep | 在所有窗口(包括装饰器/弹出窗口)中进行深度搜索 |
wpf_capture_screenshot | 捕获窗口或元素的屏幕截图(返回图像) |
wpf_get_bindings | 获取元素的数据绑定(包括MultiBinding、转换器、StringFormat) |
wpf_get_binding_errors | 列出所有捕获的绑定错误 |
wpf_clear_binding_errors | 清除捕获的绑定错误列表 |
wpf_get_data_context | 获取DataContext类型、属性、INPC状态和继承链 |
wpf_get_resources | 枚举资源字典 |
wpf_get_styles | 获取应用的样式和模板 |
wpf_watch_property | 监视属性的更改 |
wpf_highlight_element | 在视觉上突出显示一个元素 |
wpf_get_layout_info | 获取布局信息 |
wpf_export_tree | 将可视化树导出为XAML或JSON |
有关完整的工具文档,请参阅 docs/TOOLS_REFERENCE.md.
路线图
第一阶段:岩芯检查✅
- \[x\] 项目结构和架构
- \[x\] 进程发现和枚举
- \[x\] 基本流程附件
- \[x\] 可视化树导航
- \[x\] 财产检查
- \[x\] 元素搜索
第二阶段:高级功能✅
- \[x\] 通过命名管道进行IPC通信
- \[x\] 绑定分析和错误检测
- \[x\] 资源字典枚举
- \[x\] 样式和模板检查
- \[x\] 物业变更监控(带通知)
第3阶段:交互与诊断✅
- \[x\] 元素高亮叠加
- \[x\] XAML/JSON导出
- \[x\] 屏幕截图(返回MCP
ImageContentBlock) - \[x\] DLL自动注入正在运行的WPF进程(x64/x86)
- \[x\] AdornerLayer和Popup可视化树遍历
未来的考虑因素
- \[\]视觉树差异/比较
- \[\]性能诊断
- \[\]可视化树修改功能
贡献
欢迎投稿!请随时提交拉取请求。
开发设置
- 克隆存储库:
git clone https://github.com/faze79/WpfVisualTreeMcp.git
cd WpfVisualTreeMcp- 在Visual Studio 2022或VS代码中打开:
code .
# or
start WpfVisualTreeMcp.sln- 构建和运行测试:
dotnet build
dotnet test- 运行示例WPF应用程序进行测试:
dotnet run --project samples/SampleWpfApp项目结构
WpfVisualTreeMcp/
├── src/
│ ├── WpfVisualTreeMcp.Server/ # MCP Server (.NET 8) - Uses official MCP SDK
│ │ ├── Program.cs # Server initialization with MCP SDK
│ │ ├── WpfTools.cs # 17 WPF inspection tools
│ │ └── Services/ # Process & IPC management
│ ├── WpfVisualTreeMcp.Inspector/ # Injected DLL (.NET Framework 4.8)
│ ├── WpfVisualTreeMcp.Injector/ # Managed injection logic (CreateRemoteThread)
│ ├── WpfVisualTreeMcp.Bootstrapper/ # Native C++ DLL for CLR hosting
│ └── WpfVisualTreeMcp.Shared/ # Shared models & IPC contracts
├── samples/
│ └── SampleWpfApp/ # Test application
├── tests/
│ └── WpfVisualTreeMcp.Tests/ # Unit tests (48 tests)
├── publish/ # Published server + native DLLs
│ └── native/{x64,x86}/ # Architecture-specific bootstrapper
└── docs/ # Documentation技术细节
- MCP-SDK:使用 官方C#MCP SDK 来自微软/Anthropic
- 协议:通过stdio传输的JSON-RPC 2.0
- 目标框架: .NET 8.0(服务器)/。NET框架4.8+。NET 8.0-windows(检查器,双目标)
- IPC:用于服务器到应用程序通信的命名管道
- 工具:通过自动发现17个检查工具
[McpServerTool]属性
致谢
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
