问题描述
在昨天的博文中“【Azure APIM】通过 API Management 公开现有 MCP Server 的试验 (一) ”,介绍了如何在APIM中配置MCP Server。接下来,将介绍,如何把APIM中已经存在的API转换为MCP Server呢?
问题解答
在APIM MCP的文档介绍中,有将APIM公开为MCP服务器的步骤,本文将根据这个步骤操作并进行验证。
登录 Azure 门户,进入目标 APIM 实例后,按以下步骤创建 MCP Server:
- 在左侧菜单中选择 APIs > MCP servers。
- 选择 + Create MCP server。
- 选择 Expose an API as an MCP server。
- 在 Backend MCP server 中,选择APIM的默认API(Echo API),并勾选全部的Operations (这里将产生一个隐藏错误)
- 在 New MCP server 区域填写 APIM 中的 MCP Server 名称与描述,如 mcp echo server
- 可选地将 MCP Server 加入一个 Product。这样客户端便可以通过该 Product 的订阅密钥访问服务。
- 选择 Create,等待 APIM 创建完成。
动态展示图:
创建完成后,复制页面中的“MCP Server URL”,作为下一步在VS Code中配置MCP Server的URL。
在VS Code中添加MCP配置
可以通过命令面板执行 MCP: Add Server 添加服务器,也可以直接在工作区的 .vscode/mcp.json 中配置。
示例配置如下:
"my-mcp-server-echo": {
"url": "https://xxxxxxxx.azure-api.cn/mcp-echo-server/mcp",
"type": "http",
"headers": {
"Ocp-Apim-Subscription-Key": "xxxxxxxxxxxxxxxxxxx"
}
}
只是在启动MCP服务的时候,遇见了错误:
2026-08-04 21:00:31.984 [info] Starting server my-mcp-server-echo
2026-08-04 21:00:31.985 [info] Connection state: Starting
2026-08-04 21:00:31.987 [info] Starting server from LocalProcess extension host
2026-08-04 21:00:31.989 [info] Connection state: Running
2026-08-04 21:05:35.085 [info] Error connecting to https://xxxx.azure-api.cn/mcp-echo-server/mcp for async notifications, will retry
2026-08-04 21:05:35.086 [warning] Error reading SSE stream: TypeError: terminated
2026-08-04 21:05:35.091 [info] Connection state: Error Error sending message to https://xxxxx.azure-api.cn/mcp-echo-server/mcp: TypeError: fetch failed
这个错误的原因与已知的Bug有关:https://learn.microsoft.com/zh-cn/azure/api-management/export-rest-mcp-server#prerequisites
如果在 API Management 服务实例的全局范围(所有 API),通过 Application Insights 或 Azure Monitor 启用诊断日志记录,请将“前端响应”的“要记录的有效负载字节数”设置设定为 0。
此设置可防止在所有 API 中意外记录响应主体,并有助于确保 MCP 服务器的正常运行。
若要选择性地记录特定 API 的有效负载,请在 API 范围内单独配置设置,从而允许对响应日志记录进行有针对性的控制。
目前的解决办法是,不要选择所有的API,为测试把API转换为MCP Tools的功能,只需要选择一个 Get 接口用于实验。
- 导航到APIM的MCP服务页面 --> 选择MCP服务的Tools页面。如下,只勾选 Retrieve Resource 操作作为测试。
测试MCP调用
在 GitHub Copilot Chat 中切换至 Agent 模式,输入“调用apim echo mcp服务的get接口,获取test资源”,并查看mcp的调用情况
参考资料
将 API 管理中的 REST API 公开为 MCP 服务器 : https://learn.microsoft.com/zh-cn/azure/api-management/export-rest-mcp-server
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!