Skip to main content
Glama
Agentic MCP Microservices

Agentic MCP Microservices

by sganapa

使用 MCP 与 Gemini 的企业 BAU 服务

一个可执行的验证性概念(POC),展示 AI 代理如何通过模型上下文协议(MCP)与现有的日常运营(BAU)REST 微服务协同工作。

该演示使用 Google Gemini 来发现 MCP 工具、检索客户与订单信息,并执行一次受控的订单取消操作。BAU 服务保持为独立的 REST API;MCP 提供这些 API 与代理之间的集成边界。

演示

所包含的场景要求代理执行以下操作:

  1. 检索 CUST-1001 的客户档案。

  2. 列出该客户的订单。

  3. 识别状态为 Processing 的订单。

  4. 使用提供的理由取消 ORD-9002。

  5. 总结已完成的工作。

预期的工具调用顺序为:

Gemini agent
    -> MCP tool discovery over stdio
    -> MCP server
    -> Customer and Order REST APIs
    -> Tool results returned to Gemini
    -> Final user-facing summary

Related MCP server: MCP API Tool Demo

架构

+------------------+       stdio        +------------------+       HTTP       +----------------------+
| Gemini agent     | <----------------> | MCP server       | <--------------> | Customer service     |
| agent_runner.py  |                    | server.py        |                  | localhost:8001       |
+------------------+                    +--------+---------+                  +----------------------+
                                                   |
                                                   | HTTP
                                                   v
                                         +----------------------+
                                         | Order service        |
                                         | localhost:8002       |
                                         +----------------------+

MCP 工具

工具

用途

get_customer_profile

检索客户档案、等级、状态和信用额度。

get_customer_orders

列出与某客户关联的订单。

cancel_order_by_id

当订单状态允许取消时取消该订单。

项目结构

.
├── agent/
│   └── agent_runner.py          # Gemini agent and MCP client
├── mcp_server/
│   └── server.py                # MCP tools and REST integration
├── services/
│   ├── customer_service.py      # Mock customer REST API
│   └── order_service.py         # Mock order REST API
├── tests/
│   └── test_services.py         # Service behavior tests
├── .env.example                 # Environment variable template
├── requirements.txt             # Python dependencies
└── mcp_microservices_poc_blueprint.md
                                # Detailed design and implementation reference

前提条件

  • Python 3.11 或更高版本

  • 来自 Google AI Studio 的 Gemini API 密钥

  • Windows PowerShell、macOS/Linux shell 或等效终端

  • 运行代理时能够访问 Gemini API 的网络连接

本项目不需要 Anthropic 或 OpenAI API 密钥。

Windows 快速开始

在项目目录中打开 PowerShell:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
Copy-Item .env.example .env

打开 .env,将 your_gemini_api_key_here 替换为你的密钥:

GOOGLE_API_KEY=your_gemini_api_key_here
GEMINI_MODEL=gemini-3.6-flash

切勿提交 .env 或将 API 密钥放入源代码中。所选模型必须对你的 Gemini 账户可用。

运行演示

使用三个终端。在每个终端中激活 .venv。

终端 1:客户服务

python services/customer_service.py

运行于 http://localhost:8001。

终端 2:订单服务

python services/order_service.py

运行于 http://localhost:8002。

终端 3:Gemini 代理

python agent/agent_runner.py

代理将 MCP 服务器作为子进程启动,发现其工具,并执行示例工作流。

验证本地服务

在两个服务运行期间执行以下命令:

Invoke-RestMethod http://localhost:8001/health
Invoke-RestMethod http://localhost:8002/health
Invoke-RestMethod http://localhost:8001/api/v1/customers/CUST-1001
Invoke-RestMethod http://localhost:8002/api/v1/orders/customer/CUST-1001

运行测试

在虚拟环境激活状态下:

python -m pytest -q

测试覆盖了不区分大小写的客户查找、客户不存在、取消处理中的订单,以及防止取消已发货订单的保护逻辑。

配置

变量

描述

默认值

GOOGLE_API_KEY

Gemini 认证密钥。

必填

GEMINI_MODEL

代理使用的 Gemini 模型。

gemini-3.6-flash

CUSTOMER_SERVICE_URL

客户服务基础 URL。

http://localhost:8001

ORDER_SERVICE_URL

订单服务基础 URL。

http://localhost:8002

范围与限制

这是一个本地、刻意保持小规模的 POC,旨在使集成模式易于检查和运行。

  • 客户和订单记录存储在内存中,服务重启时会被重置。

  • 服务未实现身份验证或授权。

  • 在真实系统中,取消操作应受到审批和业务策略控制的保护。

  • MCP 使用 stdio 进行本地进程集成。部署架构应使用经过身份验证的网络传输。

  • 下游错误以工具文本形式返回以便查看;生产系统应使用结构化错误、重试、追踪和指标。

  • 示例代理会发起真实的 Gemini API 请求,需要有效的密钥。

故障排查

GOOGLE_API_KEY is not set

确认项目目录中存在 .env 且包含有效密钥。从项目目录运行代理。

Gemini 模型返回 404 NOT_FOUND

在 .env 中将 GEMINI_MODEL 设置为你的账户已启用的模型。本 POC 中的默认值为 gemini-3.6-flash。

MCP 启动或工具发现失败

确认依赖已安装到当前激活的虚拟环境中:

python -m pip install -r requirements.txt

本项目将 MCP 固定在 1.x API 范围内,因为服务器实现使用了 FastMCP。

MCP 工具出现连接错误

确认两个 REST 服务正在端口 8001 和 8002 上运行。如果端口已被占用,请更新服务端口以及 .env 中对应的 URL。

延伸阅读

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    B
    maintenance
    MCP server that enables AI platforms to search products, customers, and warehouses, and prepare and submit sales orders to a fixed ERP endpoint with per-session bearer authentication.
    -