监视 Azure API 管理 中的 MCP 服务器流量

在本文中,你将了解 Azure API 管理 会为发往 MCP 服务器的流量发出哪些遥测数据,如何为工具参数和结果启用有效负载日志记录,以及如何在 Azure Monitor 中查询这些数据。  

Prerequisites

MCP 服务器的默认遥测

对于每个 MCP 请求,API 管理都会使用 MCP 特定的维度写入 Application Insights 请求行,并设置标准持续时间字段。 可以在不更改任何配置的情况下绘制每个工具的延迟图表。 有关详细信息,请参阅本文后面的 MCP 遥测参考 部分。  

Note

MCP 遥测遵循生成式 AI 的 OpenTelemetry 语义约定,该约定定义了标准遥测属性名称(例如), gen_ai.*因此数据在工具之间保持一致。

为参数和结果启用有效负载日志记录

默认情况下,API 管理不会捕获工具调用的参数和结果。 若要为 MCP 服务器启用捕获,请执行以下操作:

  1. Azure 门户中,转到 API 管理实例。 

  2. 选择 API>MCP 服务器,然后选择要记录的 MCP 服务器。 

  3. 选择 “设置>诊断日志”。 

  4. 启用前端和后端负载日志记录。 选择“保存”。 

注意

工具参数和结果可以包括提示、客户数据或机密。 仅在需要启用有效负载日志的 MCP 服务器和环境中启用该功能。 在广泛推出之前应用清理或声明允许列表。 

使用 KQL 查询 MCP 流量

下面是可以在Azure Monitor中运行以分析 MCP 流量的示例 Kusto 查询。 在这些示例中,如适用,请将 sales-mcp 替换为你的 MCP 服务器名称。

列出给定 MCP 服务器上的最后 50 个工具调用

requests
| where customDimensions["api.type"] == "Mcp"
  and customDimensions["service.name"] == "sales-mcp"
  and customDimensions["gen_ai.operation.name"] == "tools/call"
| project timestamp,
          tool       = customDimensions["gen_ai.tool.name"],
          session    = customDimensions["gen_ai.conversation.id"],
          client     = strcat(customDimensions["user_agent.name"], "/",
                              customDimensions["user_agent.version"]),
          durationMs = duration,
          success
| order by timestamp desc
| take 50

按工具调用量排名靠前的 MCP 客户端

requests
| where customDimensions["api.type"] == "Mcp"
  and customDimensions["gen_ai.operation.name"] == "tools/call"
| summarize calls = count()
    by client = strcat(customDimensions["user_agent.name"], "/",
                       customDimensions["user_agent.version"])
| top 10 by calls desc

过去 24 小时内每个工具的 p50 和 p95 延迟

requests
| where customDimensions["api.type"] == "Mcp"
  and customDimensions["gen_ai.operation.name"] == "tools/call"
  and timestamp > ago(24h)
| summarize p50   = percentile(duration, 50),
            p95   = percentile(duration, 95),
            calls = count()
    by tool = tostring(customDimensions["gen_ai.tool.name"])
| order by p95 desc

各工具错误率随时间变化

requests
| where customDimensions["api.type"] == "Mcp"
  and customDimensions["gen_ai.operation.name"] == "tools/call"
| summarize total    = count(),
            failures = countif(success == false)
    by bin(timestamp, 5m),
       tool = tostring(customDimensions["gen_ai.tool.name"])
| extend errorRate = todouble(failures) / total
| render timechart

检查发送给特定工具的参数

对于这种情况,请确保已为 MCP 服务器启用有效负载日志记录。

requests
| where customDimensions["api.type"] == "Mcp"
  and customDimensions["service.name"] == "sales-mcp"
  and customDimensions["gen_ai.tool.name"] == "create_quote"
  and timestamp > ago(1h)
| project timestamp,
          session = customDimensions["gen_ai.conversation.id"],
          args    = customDimensions["gen_ai.tool.call.arguments"],
          result  = customDimensions["gen_ai.tool.call.result"]

使用跟踪策略添加自定义维度

若要捕获不在内置架构中的数据(例如自定义 x-agent-id 标头、JWT 声明或关联 ID)请使用 MCP 服务器范围内的 跟踪 策略。 

Warning

不要通过附加到 MCP 范围的策略访问 context.Response.Body。 MCP 响应会流式传输,读取正文会中断流。 

MCP 遥测参考文档

以下维度显示在每个 MCP 请求上:

财产 说明
gen_ai.operation.name JSON-RPC 方法(tools/list 或 tools/call)。
gen_ai.conversation.id MCP 会话 ID。
network.protocol.name 协议名称(MCP)。
network.protocol.version 协议版本。
auth.type 入站身份验证方法。
user_agent.name MCP 客户端名称(例如 vscode 或 claude-desktop)。
user_agent.version MCP 客户端版本。
service.name MCP服务器名称。
service.version MCP 服务器版本。
api.type API 类型鉴别器(Mcp)。
error.message 失败时的错误字符串。
error.type 失败时的错误类别。

工具/列表上的其他字段

指标 说明
ToolCount 响应中返回的工具数量。

工具/调用的其他字段

财产 说明
gen_ai.tool.name 代理调用的工具。
gen_ai.tool.type 工具类型。
gen_ai.tool.call.arguments 参数 JSON。 仅在启用有效负载日志记录时显示。
gen_ai.tool.call.result 结果 JSON。 仅在启用有效负载日志记录时显示。