本仓库展示了如何将模型上下文协议(MCP)服务器与ArcGIS Pro插件集成。目标是将ArcGIS Pro的功能作为MCP工具公开,以便GitHub Copilot(代理模式)或任何MCP客户端能够与您的GIS环境进行交互。
本项目可让Copilot(代理模式)直接在ArcGIS Pro中查询地图、列出图层、统计要素数量、缩放至图层等。要使用该项目,需要完成以下步骤:
.mcp.json在Visual Studio中配置为MCP服务器。ArcGisProMcpSample/
+- ArcGisProBridgeAddIn/ # ArcGIS Pro插件项目(同进程)
¦ +- Config.daml
¦ +- Module.cs
¦ +- ProBridgeService.cs # 命名管道服务器 + 命令处理程序
¦ +- IpcModels.cs # IPC请求/响应数据传输对象
+- ArcGisMcpServer/ # MCP服务器项目(.NET 8)
¦ +- Program.cs
¦ +- Tools/ProTools.cs # MCP工具定义(桥接客户端)
¦ +- Ipc/BridgeClient.cs # 命名管道客户端
¦ +- Ipc/IpcModels.cs # 共享IPC数据传输对象
+- .mcp.json # VS Copilot的MCP服务器清单
插件在ArcGIS Pro启动时启动一个命名管道服务器。它处理以下操作:
pro.getActiveMapNamepro.listLayerspro.countFeaturespro.zoomToLayer以下是Module.cs(示例中在一个按钮中)的代码:
protected override bool Initialize()
{
_service = new ProBridgeService("ArcGisProBridgePipe");
_service.Start();
return true; // 初始化成功
}
protected override bool CanUnload()
{
_service?.Dispose();
return true;
}
以下是ProBridgeService处理程序的代码:
case "pro.countFeatures":
{
if (req.Args == null ||
!req.Args.TryGetValue("layer", out string? layerName) ||
string.IsNullOrWhiteSpace(layerName))
return new(false, "参数 'layer' 是必需的", null);
int count = await QueuedTask.Run(() =>
{
var fl = MapView.Active?.Map?.Layers
.OfType()
.FirstOrDefault(l => l.Name.Equals(layerName, StringComparison.OrdinalIgnoreCase));
if (fl == null) return 0;
using var fc = fl.GetFeatureClass();
return (int)fc.GetCount();
});
return new(true, null, new { count });
}
MCP服务器使用官方的ModelContextProtocol NuGet包。
以下是Program.cs的代码:
await Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddSingleton(new BridgeClient("ArcGisProBridgePipe"));
services.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly(typeof(ProTools).Assembly);
})
.RunConsoleAsync();
以下是示例工具的代码:
[McpServerToolType]
public static class ProTools
{
private static BridgeClient _client;
public static void Configure(BridgeClient client) => _client = client;
[McpServerTool(Title = "统计图层中的要素数量", Name = "pro.countFeatures")]
public static async Task<object> CountFeatures(string layer)
{
var r = await _client.OpAsync("pro.countFeatures", new() { ["layer"] = layer });
if (!r.Ok) throw new Exception(r.Error);
var count = ((System.Text.Json.JsonElement)r.Data).GetProperty("count").GetInt32();
return new { layer, count };
}
}
.mcp.json清单将以下内容放在解决方案根目录(.mcp.json)中:
{
"servers": {
"arcgis": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"McpServer/ArcGisMcpServer/ArcGisMcpServer.csproj"
]
}
}
}
.mcp.json并启动MCP服务器。pro.listLayers ?返回活动地图中的图层pro.countFeatures layer=Buildings ?返回要素数量pro.selectByAttribute、pro.getCurrentExtent、pro.exportLayer等操作扩展工具。