site logo

Marico's space

Azure APIM MCP 自动化:桥接问题

编程技术 2026-07-07 17:41:11 10

Part 3里聊了可观测性,这篇说说自动化这座大山怎么爬。

Azure APIM MCP 现在还是预览版,这意味着什么?你的基础设施即代码(IaC)流水线直接抓瞎:

  • ARM 模板?不支持
  • Bicep?不支持
  • Terraform Provider?不支持
  • Azure Service Operator(ASO)?也不支持

现实就是这么残酷:标准 IaC 工具一个都用不了。要么手动在 Azure Portal 里点点点,要么自己造轮子做自动化。

但在你冲动地去写 REST API 脚本之前,先聊聊为什么这个问题值得关注,以及它对你的架构意味着什么。

为什么这个问题不一样

不只是自动化的问题

大多数 IaC 缺口都是小麻烦,写几行 REST API 调用,打包成脚本,丢到流水线里跑,齐活。

MCP 不一样,原因如下:

AI 智能体(Agent)会读取你的 API 描述。这些描述直接变成工具定义,决定了大语言模型什么时候、怎么调用你的接口。描述写得烂?智能体理解偏差。摘要误导?调用错接口。安全注意事项没写?数据直接泄漏。

这是个披着自动化外衣的治理问题。

真正要命的地方

Diagram

没有proper的治理机制:

  • AI 智能体看到工具描述之前,没人审查过
  • API 质量没人在暴露给 MCP 之前把关
  • 哪些操作能变成智能体可调用的工具,没人管
  • 暴露了什么、什么时候暴露的,没有审计记录

这不是 Terraform 部署虚拟机失败的问题。这是让 AI 智能体直接访问你的业务逻辑,却没有proper的监管

治理优先的方法论

我们需要什么

在担心自动化工具之前,先把这些问题想清楚:

  1. MCP 暴露的单一数据源(哪些 API 要变成工具?)
  2. 工具质量审查关卡(描述对智能体友好吗?)
  3. 变更审批流程(谁决定什么东西能暴露?)
  4. MCP 操作的审计跟踪(改了什么、什么时候改的?)
  5. 回滚能力(暴露错了能撤回来吗?)

这是 GitOps 的地盘。不是因为 Kubernetes 或者 Helm Chart,而是因为我们需要:

  • 配置版本控制
  • 变更前做 Pull Request 审查
  • 生产环境审批关卡
  • 谁改了什么的历史记录

OpenAPI 即配置

核心思路:OpenAPI 规范就是你的配置文件。

paths: /users/{userId}/profile: get: operationId: getUserProfile summary: "Retrieve user profile information" description: "Get comprehensive profile data for the authenticated user including preferences, settings, and basic account information. This tool respects user privacy and only returns data the user has permission to access." x-mcp-enabled: true # 治理标记 parameters: - name: userId in: path required: true description: "User identifier - must match authenticated user" schema: type: string

那个 x-mcp-enabled: true 标记是治理元数据,不只是个技术开关。它说明:

  • 有人审查过这个操作的描述
  • 有人决定 AI 智能体应该有权限访问
  • 有人验证过安全模型
  • 有人批准了这个 API 暴露给 MCP

没这个标记 = 不暴露给 MCP。就这么简单。

三种桥接方案

聊聊怎么填补 IaC 的空白,在 ASO 追上之前。

提前声明:这些是我们正在探索的方案,不是生产验证过的解决方案。都是基于几个月折腾 APIM MCP 的经验加上几年的 GitOps 积累,当作架构思路看就行。

方案一:集中式自动化仓库

思路:搞个独立仓库,轮询 APIM 实例,检测带 x-mcp-enabled 标记的 API,然后应用 MCP overlay。

Diagram

特点:

  • 关注点分离:标准 API 部署流程不动
  • 非侵入式:overlay 在外部跑,不碰现有工作流
  • 易于移除:ASO 支持 MCP 之后,删掉 overlay 仓库就行
  • 治理得以保留:只有带 x-mcp-enabled 的 API 才会被转换

代价:

  • 最终一致性(基于轮询,不是实时)
  • 多租户场景需要跨订阅凭证
  • 集中监控但分布式执行

方案二:事件驱动的 Kubernetes Job

思路:用 ArgoCD PostSync 钩子,在 API 部署之后检测 MCP 启用的 API,然后做转换。

Diagram

特点:

  • 事件驱动:API 部署完立即转换
  • Kubernetes 原生:契合现有 ArgoCD 工作流
  • 集群级别隔离:每个集群管自己的 APIM 实例

代价:

  • 在 Helm Chart 里埋 workaround(清理起来麻烦)
  • 需要配置 Kubernetes 服务账号的 RBAC
  • 排查问题看 Pod 日志,不是集中日志

方案三:构建时生成

思路:Maven 插件或者构建工具生成两份输出:标准 API Spec + MCP Overlay 配置。

Diagram

特点:

  • 零运维开销:所有构建自动处理
  • 契合现有工作流:Maven → Helm → ArgoCD
  • 逻辑集中:一个插件,所有 API 都受益

代价:

  • 需要改构建工具(父 POM、插件)
  • 前期开发时间较长
  • 还是要在 Kubernetes 里配置 Azure 凭证

三种方案的共同点

不管选哪个方案,核心原则都一样:

  1. OpenAPI Spec 是单一数据源(不重复配置)
  2. 标准 API 部署流程不变(MCP 是 overlay,不是替代品)
  3. 通过标记管控(通过 x-mcp-enabled 显式选择加入)
  4. 易于移除(ASO 支持 MCP 之后可以清理)
  5. 支持完整 CRUD(创建、更新、删除 MCP 暴露)

选哪个?看你的组织情况:

  • 集中管控 + 多租户?选方案一
  • Kubernetes 原生 + 集群级别隔离?选方案二
  • 最小运维开销?选方案三

治理框架

拆解一下 MCP 暴露场景下治理到底意味着什么。

审查流程

典型的审批链路:

Developer → OpenAPI with x-mcp-enabled → Pull Request → Domain Review → Security Review → Principal Architect → Merge → Automated Deployment → MCP Server Live

审查什么:

方面 审查重点 跳过风险
描述质量 对 AI 智能体来说清晰吗? 意图被误解,调用错误 API
安全模型 认证要求写清楚了吗? 未授权访问、数据泄露
数据暴露 敏感数据标记了吗? 隐私违规、合规漏洞
操作安全性 只读还是写操作? 意外修改数据
限流 节流配置合理吗? 资源耗尽、拒绝服务攻击

工具质量标准

MCP 启用操作的最低要求:

# BAD:对 AI 智能体来说太模糊
get: summary: "Get user" x-mcp-enabled: true # GOOD:清晰、可操作、有约束
get: summary: "Retrieve user profile information" description: | Get comprehensive profile data for the authenticated user including preferences, settings, and basic account information. **Authentication required**: User can only access their own profile. **Data privacy**: Returns only non-sensitive user data. **Rate limit**: 100 requests per minute. x-mcp-enabled: true security: - BearerAuth: []

为什么重要:AI 智能体依赖描述来理解:

  • 工具做什么
  • 什么时候用它
  • 返回什么数据
  • 需要什么权限

描述烂 = 智能体行为烂。

CODEOWNERS 管控 MCP

强制审查要求:

# 在 Git 仓库里
/apis/customer-management/ @customer-team @security-team
/apis/payment-processing/ @payment-team @compliance-team @security-team
/apis/public-data/ @platform-team # 任何带 x-mcp-enabled 的文件需要首席架构师审批
**/openapi*.yaml @principal-architect

这样强制:

  • 领域专家审查自己的 API
  • 安全团队看到所有 MCP 启用的操作
  • 首席架构师有最终审批权

审计跟踪

Git 就是审计跟踪:

{ "commit": "abc123", "author": "developer@org.com", "timestamp": "2026-01-12T10:30:00Z", "changes": [ { "file": "apis/customer-management/openapi.yaml", "operation": "/users/{userId}/profile", "change": "Added x-mcp-enabled: true", "reviewers": ["security-team", "principal-architect"], "approved_at": "2026-01-12T11:00:00Z" } ]
}

每个 MCP 暴露变更都存在 Git 历史里。谁改的、什么时候改的、谁批的,一清二楚。

API 质量前提

说个扎心的真相:在一个烂 API 集合上套 MCP,别指望有好结果。

什么样的 API 才能"MCP 就绪"?

不是所有 API 都应该变成 MCP 工具。考虑这些:

特征 MCP 就绪 不适合 MCP
文档 清晰、完整、对智能体友好 稀疏、过时、只给人看
稳定性 版本化管理、契约稳定 频繁破坏性变更
错误处理 结构化错误响应 通用 500,没有详情
安全模型 认证授权定义清晰 权限配置随意
性能 响应时间稳定可靠 时不时超时
幂等性 重试安全 重试有副作用

AI 智能体是不宽容的消费者。它们不会调试你的 API,不会提工单,只会挂得很难看。

功能性 API vs 技术性 API 的问题

我的立场:AI 智能体需要功能性 API,不是技术性 API

技术性 API(大多数公司有的):

# CRUD 操作,以数据库为中心
POST /api/v1/customers
GET /api/v1/customers/{id}
PUT /api/v1/customers/{id}
DELETE /api/v1/customers/{id} POST /api/v1/accounts
GET /api/v1/accounts/{id}
PUT /api/v1/accounts/{id}

AI 智能体看到这个:"有创建、读取、更新、删除...但我到底为用户做什么?"

功能性 API(智能体需要的):

# 业务能力,以意图为导向
POST /customers/onboard - "Onboard a new customer with KYC verification" POST /customers/{id}/verify-identity - "Verify customer identity using government documents" POST /accounts/open - "Open a new bank account for verified customer" POST /payments/transfer - "Transfer money between accounts with fraud checks"

AI 智能体看到这个:"我能开户客户、验证身份、开账户、转账——我理解业务意图。"

这对 MCP 为什么重要

Diagram

用技术性 API:

  • 智能体要编排多个底层操作
  • 没有事务边界(第 3 步失败了怎么办?)
  • 业务规则分散在智能体逻辑里
  • 调用顺序错误风险高

用功能性 API:

  • 一次 API 调用表达业务意图
  • 事务由后端处理
  • 业务规则集中在 API 里
  • 风险更低,审计更容易

重构的问题

Diagram

三条路:

  1. API 已经是功能性的?加 MCP 标记,部署
  2. API 可以重构?先做技术性 → 功能性转换
  3. API 没救了?从头建新的功能性 API

不要把 CRUD 端点暴露给 AI 智能体,然后指望它们编排业务逻辑。这是等着发生产故障。

真实案例

烂 API(技术性,别 MCP 化这个):

/api/customers: post: summary: "Create customer" description: "Insert customer record into database" requestBody: content: application/json: schema: type: object properties: firstName: string lastName: string email: string # 还有50多个字段...

AI 智能体看到这个:"我能创建客户记录,但 KYC 呢?合规检查呢?账户设置呢?我是不是要调其他 API?调哪些?"

好 API(功能性,MCP 就绪):

/customers/onboard: post: summary: "Onboard new customer with full verification" description: | Complete customer onboarding process including: - Identity verification (government ID + liveness check) - KYC/AML compliance screening - Risk assessment - Welcome email and account activation **Business rules enforced:** - Customer must be 18+ years old - Must pass identity verification - Must clear compliance screening - Duplicate detection performed automatically **What happens on success:** - Customer record created with verified status - Welcome email sent - Customer can proceed to account opening **Authentication required:** Staff or authorized system only **Rate limit:** 10 requests per minute x-mcp-enabled: true requestBody: required: true content: application/json: schema: type: object required: [firstName, lastName, dateOfBirth, governmentId] properties: firstName: type: string description: "Customer's legal first name" lastName: type: string description: "Customer's legal last name" dateOfBirth: type: string format: date description: "Date of birth (must be 18+ years ago)" governmentId: type: object description: "Government-issued identification" required: [type, number, issuingCountry] properties: type: type: string enum: [passport, drivers_license, national_id] number: type: string issuingCountry: type: string responses: '201': description: "Customer onboarded successfully" content: application/json: schema: type: object properties: customerId: type: string description: "Unique customer identifier" verificationStatus: type: string enum: [verified, pending_review] nextSteps: type: array items: type: string example: ["Open savings account", "Apply for credit card"] '400': description: "Invalid input or failed verification" content: application/json: schema: type: object properties: error: type: string reason: type: string enum: [underage, failed_identity_check, failed_compliance, duplicate_customer]

AI 智能体看到这个:"非常清晰。开客户、验证身份、执行规则、告诉我下一步做什么。我知道什么时候用这个、需要什么数据。"

为什么技术性 API 会让 AI 智能体挂掉

问题一:编排负担

技术方案(智能体要自己搞懂):
1. POST /customers (创建记录)
2. POST /kyc-checks (验证身份)
3. GET /kyc-checks/{id} (等待结果)
4. POST /compliance-screenings (AML 检查)
5. GET /compliance-screenings/{id} (等待结果)
6. PUT /customers/{id} (更新状态为已验证)
7. POST /emails (发送欢迎邮件) 如果第 4 步失败了怎么办?如果第 6 步失败但第 7 步成功了怎么办?

功能方案(一次调用):

POST /customers/onboard (原子性执行所有操作)

问题二:业务规则发现

技术性 API 说:"给你个数据库表,自己看着办"
功能性 API 说:"这是业务流程,规则我来管"

问题三:错误语义

技术性 API: 400 Bad Request (哪里出问题了?)
功能性 API: 400 Bad Request - reason: "failed_identity_check" (哦,身份验证失败了)

这不是 API 设计的洁癖。这是智能体能跑起来还是引发生产故障的区别。

GitOps 原则应用于 MCP

聊聊"GitOps"在这个场景下到底意味着什么。

单一数据源

所有东西都在 Git 里:

  • x-mcp-enabled 标记的 OpenAPI 规范
  • APIM 策略模板
  • 安全配置
  • 访问控制列表

不在 Azure Portal 里改东西。不在 Git 里的东西就不存在。

声明式配置

我们想要的:

# 这个操作应该是 MCP 工具
x-mcp-enabled: true

不是:

# 命令式修改状态
az apim mcp create --name ...

桥接自动化读取声明式配置,然后同步 Azure 状态。

合并前审查

Feature Branch → Pull Request → Automated Validation → Human Review → Approval → Merge to Main → Automated Deployment

禁止野路子部署。每个变更都要走审查。

回滚能力

用 Git 标签做部署标记:

git tag deploy-20260112-103000

回滚 = 切回之前的标签:

git checkout deploy-20260112-100000
./deploy.sh # 重新应用之前的状态

可观测性

跟踪自动化做了什么:

  • 扫描了哪些 API?
  • 哪些操作带 x-mcp-enabled
  • 创建/更新/删除了哪些 MCP 服务器?
  • 有没有失败或错误?

流水线日志就是你的审计跟踪。

我们在探索什么

坦诚交代:我们也在摸索阶段。说说 Grand Central 考虑什么方案。

我们倾向的方案

Maven 插件扩展,在构建时生成 MCP overlay 配置。

为什么吸引我们:

  • 契合现有的 OpenAPI → Maven → Helm → ArgoCD 工作流
  • 部署后零运维开销
  • 逻辑集中(一个插件,所有 API 都受益)
  • ASO 支持 MCP 之后易于移除

工作原理: