ArcGIS Pro MCP服务器插件

该服务通过MCP协议将ArcGIS Pro功能暴露给GitHub Copilot或其他MCP客户端,实现GIS环境的交互操作。

  • by nicogis
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 nicogis/mcp-server-arcgis-pro-addin

使用说明

带ArcGIS Pro插件的MCP服务器(C#.NET 8)

此存储库演示了如何集成 模型上下文协议(MCP)服务器 带着一个 ArcGIS Pro附加模块目标是将ArcGIS Pro功能作为MCP工具公开,以便GitHub Copilot(在代理模式下)或任何MCP客户端可以与您的GIS环境交互。


概述

  • ArcGIS Pro附加模块 (C#与ArcGIS Pro SDK):运行 过程中的 使用ArcGIS Pro,并通过本地IPC通道(命名管道)公开GIS操作。
  • MCP服务器 (.NET 8控制台应用程序):定义MCP工具,通过命名管道与外接程序通信,并通过以下方式在Visual Studio中配置为MCP服务器 .mcp.json.

这可以允许Copilot(代理模式)直接在ArcGIS Pro中查询地图、列出图层、计数特征、缩放到图层等。


先决条件

  • Visual Studio 2022 17.14或更晚 (用于MCP代理模式支持)
  • ArcGIS Pro SDK for。网
  • 安装ArcGIS Pro(同一台机器)
  • .NET 8 SDK

溶液结构

ArcGisProMcpSample/
+- ArcGisProBridgeAddIn/           # ArcGIS Pro Add-In project (in-process)
¦  +- Config.daml
¦  +- Module.cs
¦  +- ProBridgeService.cs          # Named Pipe server + command handler
¦  +- IpcModels.cs                 # IPC request/response DTOs
+- ArcGisMcpServer/                # MCP server project (.NET 8)
¦  +- Program.cs
¦  +- Tools/ProTools.cs            # MCP tool definitions (bridge client)
¦  +- Ipc/BridgeClient.cs          # Named Pipe client
¦  +- Ipc/IpcModels.cs             # Shared IPC DTOs
+- .mcp.json                       # MCP server manifest for VS Copilot

ArcGIS Pro附加模块

插件启动了一个 命名管道服务器 ArcGIS Pro发布。它处理以下操作:

  • pro.getActiveMapName
  • pro.listLayers
  • pro.countFeatures
  • pro.zoomToLayer

例子: Module.cs (示例中的按钮)

protected override bool Initialize()
{
    _service = new ProBridgeService("ArcGisProBridgePipe");
    _service.Start();
    return true; // initialization successful
}

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, "arg 'layer' required", null);

    int count = await QueuedTask.Run(() =>
    {
        var fl = MapView.Active?.Map?.Layers
            .OfType<FeatureLayer>()
            .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服务器(.NET 8)

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 = "Count features in a layer", 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"
      ]
    }
  }
}

在Visual Studio中运行

  1. 在中打开解决方案 Visual Studio 2022(=17.14).
  2. 确保ArcGIS Pro在加载外接程序的情况下运行(因此命名管道存在)。
  3. 在VS中,打开 副驾驶聊天代理模式.
  4. 副驾驶阅读 .mcp.json 并启动MCP服务器。
  5. 键入聊天:
    • pro.listLayers ?返回活动地图中的图层
    • pro.countFeatures layer=Buildings ?返回功能计数

后续步骤

  • 通过以下操作扩展工具 pro.selectByAttribute, pro.getCurrentExtent, pro.exportLayer.
  • 为IPC通信添加重试/超时逻辑。
  • 将MCP服务器容器化以进行部署。 ...