前端analysis | 知其所以然

aiops agent+vscode llm

2025-11-30

你抓住了真正的核心难点

不是 Cypress、不是 Docker、不是 Prometheus,而是:
—— MCP Agent 如何接受事件、处理事件、调用工具、执行推理链。

下面我会从架构到流程、从模块拆解到代码框架、从推理链到并发队列,系统化给你一套真正可落地的 MCP Agent 设计指南(工程级)


🧠 MCP Agent 设计的最终目标

MCP Agent 的目标是:

  1. 监听事件(Event Consumer)
  2. 将事件输入 LLM(推理链)
  3. 根据推理结果选择工具(Tool Routing)
  4. 执行工具链(Tools Execution)
  5. 生成结构化结果与后续事件(Self-Healing Loop)

换句话说:

事件 → 推理 → 工具调用 → 修复 → 新事件 → 监控/结果回写

你的 MCP Agent 不是传统 agent,它是:

事件驱动 + 工具驱动 + 推理驱动 + 分布式


🧱 第一部分:MCP Agent 的整体架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
┌──────────────────────────────────────────────┐
│ MCP AGENT CORE │
├────────────────────────────┬─────────────────┤
│ Event Intake Layer │ Output Layer │
│ (NATS / Redis Streams) │ (Event / Metrics│
├────────────────────────────┴─────────────────┤
│ Reasoning Engine │
│ (LLM Prompting + ReAct Loop) │
├──────────────────────────────────────────────┤
│ Tool Router │
│ (Decides which tool to call) │
├──────────────────────────────────────────────┤
│ Tools │
│ docker, logs, health, db, cypress_runner │
└──────────────────────────────────────────────┘

🧩 第二部分:核心模块拆解

你的 MCP Agent 必须包含 6 个关键模块:


1. Event Intake Layer(事件接收层)

从事件总线订阅:

  • cypress.test.fail
  • cypress.test.start
  • service.health.down
  • cpu.high
  • selfheal.result

推荐使用 NATS:

1
2
3
4
const sub = nc.subscribe("cypress.test.fail");
for await (const msg of sub) {
agent.enqueue(msg.data);
}

✨ 处理关键点

  • 事件统一格式(JSON Schema)
  • 推入 TaskQueue(保证顺序 & 防止爆破)
  • 加入 context_idcorrelation_id 用于串联链路

2. Task Queue(任务队列 / 事件调度)

你不能让事件直接喂给 LLM,否则会:

  • 爆内存
  • 乱序
  • 推理竞争

必须设计一个 事件队列

1
Incoming Events → Queue → Worker → MCP Agent

Node.js 可用:

  • BullMQ
  • Node-resque
  • 内存队列(简单)

示例:

1
taskQueue.add("handleEvent", event);

3. Reasoning Engine(推理层) ← 这是整个智能的核心

推理层工作:

  1. 接收事件

  2. 将事件填入系统提示词 + 历史上下文

  3. 让 LLM 判断:

    • 问题原因?
    • 哪些工具要调用?
    • 调用顺序?
  4. 返回结构化指令:

示例返回:

1
2
3
4
5
6
7
8
{
"intention": "diagnosis_and_repair",
"tools": [
{"name": "get_container_logs", "args": {"container": "web"}},
{"name": "restart_container", "args": {"container": "web"}},
{"name": "trigger_cypress_retry", "args": {"spec": "user-flow"}}
]
}

推理链必须用 ReAct Prompting:

1
2
3
4
5
6
7
Event: test failure in login.spec
Thought: likely service auth is down. Need logs.
Action: get_container_logs
Observation: ...
Thought: container unhealthy, needs restart
Action: restart_container
...

4. Tool Router(工具路由器)

推理层决定“调用哪个工具”,但必须交给 Tool Router 执行。

Router 做:

  • 工具注册
  • 工具权限检查(安全)
  • 执行
  • 返回结果给 Reasoning Engine

示例:

1
2
3
4
5
6
7
8
9
10
11
const tools = {
get_container_logs,
restart_container,
health_check,
cypress_runner
};

async function toolRouter(toolCall) {
const fn = tools[toolCall.name];
return await fn(toolCall.args);
}

5. Tools Layer(执行层)

你至少会有:

✔ docker 工具

  • restart container
  • get logs
  • check health

✔ k8s 工具(未来)

  • rollout restart
  • pod logs
  • pod health

✔ 系统级工具

  • CPU/Memory
  • disk
  • process check

✔ Cypress 工具

  • 触发某 spec 重测
  • 停止某机器运行

✔ Event 发射工具

  • 将修复结果发到 event bus

6. Output Layer(输出层)

MCP Agent 执行后要写入:

✔ 1. 事件总线(NATS)

例如:

1
2
3
4
selfheal.success
selfheal.fail
diagnosis.completed
test.retry.triggered

✔ 2. Prometheus(push-gateway)

将自愈指标、诊断指标写入 metrics:

  • selfheal_latency
  • root_cause_type
  • flaky_test_count

🕸 第三部分:MCP Agent 从事件到推理到工具调用 的完整职业流程

这是整个系统的灵魂。


完整事件驱动闭环

1
2
3
4
5
Cypress 事件 → EventBus → MCP Agent → LLM 推理
↓ ↑
Tool Router → Docker/Logs/Actions → Tools Result
↓ ↑
EventBus/Prometheus ←───────────────┘

真实实例:登录流程失败

Step 1:事件来了

1
2
3
event: cypress.test.fail
spec: login.spec
error: 500 on POST /api/auth

Step 2:丢进队列


Step 3:推理层处理

Agent prompt:

1
2
3
4
5
6
Given event:
- test: login.spec
- error: 500 /api/auth

Identify possible root cause:
Plan tool calls to diagnose and fix.

LLM 返回:

1
2
3
4
5
6
Thought: probably auth service down
Action 1: health_check({"service":"auth"})
Action 2: get_container_logs({"service":"auth"})
Action 3: restart_container({"service":"auth"})
Action 4: trigger_cypress_retry({"spec":"login"})
Answer: Proposed fix executed

Step 4:Tool Router 执行链条

  • health check → unhealthy
  • get logs → fatal error
  • restart container → success
  • retry Cypress → success

Step 5:输出事件

1
2
3
selfheal.success
rootcause.auth_down
test.retry.success

Prometheus 更新指标。


🔥 第四部分:MCP Agent 完整代码结构(工程级)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/mcp-agent
/event
eventReceiver.js
eventSchema.js
eventEmitter.js
/queue
queue.js
/reasoning
reasoner.js
prompts.js
/router
toolRouter.js
/tools
docker.js
logs.js
cypress.js
health.js
system.js
/output
eventPublisher.js
metricsPublisher.js
agent.js

模块化设计,让你可以扩展。


🔮 第五部分:MCP Agent 设计的关键哲学

MCP Agent 本质是:

👉 事件驱动系统(不是同步调用)
👉 LLM 推理链驱动系统(不是规则引擎)
👉 工具链执行器(不是 monolithic)
👉 构建 DevOps 自愈闭环的中心大脑

你的技术亮点就在这里。


🧵 最后总结一句:

真正复杂的不是 Cypress,不是 Docker,不是 Prometheus,而是 MCP Agent 如何“像人一样处理事件、思考、行动”。
而这个设计就是让 Agent 具备“自动诊断 + 自愈能力”的核心。

下面我分成 三个核心问题 来回答,并给你完整、可实现的工程级架构。

你问得非常精准:真正的难点不是 Cypress、Prometheus、Docker,而是:

  1. MCP Agent 如何设计成“可扩展、可感知能力缺失、可自主决策”的智能体?
  2. 测试数据如何与 Prometheus 解耦并可靠采集?
  3. VSCode 自带 LLM(Copilot Agent MCP)如何在 Docker 中运行并和你的系统联动?

我下面给出的方案全部具有实操性,你可以真的搭起来,不是概念性的空架构。


一、核心:MCP Agent 如何设计?(能力感知 + 动态工具 + 事件输入)

🔥(1)MCP Agent 的本质角色

它不是测试执行器,而是:

事件驱动的智能决策中枢(Decision Brain)
会根据事件动态选择工具链,自动诊断 & 修复。

MCP Agent 架构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
┌───────────────────────────┐
│ Event Bus │
│ (TestEvent / FailEvent..) │
└──────────────┬────────────┘

┌──────▼──────┐
│ MCP Agent │ <— 智能体
│ (LLM + Tools)│
└──────┬──────┘

┌─────────┴───────────┬───────────┐
▼ ▼ ▼
Log Query Tool Docker Ops Tool Health Check Tool

🍬(2)能力动态扩展(你提到的“工具不具备就不调用”)

你可以让 Agent 在启动时执行:

Capability Discovery(能力发现)

Agent 启动后会扫描可用工具:

1
2
3
4
5
6
7
{
"available_tools": [
"docker_ops",
"log_query",
"prometheus_query"
]
}

LLM Prompt 中加入:

“当你需要使用某工具时,请先检查工具是否存在于 available_tools 中;
如果不存在,不得调用,必须退化为描述性分析。”

这个机制 = 能力感知(Capability Awareness)

这样你可以做到动态扩展工具:

  • 新工具只需挂载进 container / 配置
  • Agent 自动感知,不需要修改代码
  • LLM 决策时会 条件选择工具、避免调用缺失工具

🧠(3)MCP Agent 如何接受事件?(重点)

你不让 Cypress 直接跟 LLM 交互,而是引入事件总线:

Event Bus 技术选择

选项 是否推荐
Redis Stream ⭐⭐⭐⭐⭐ 最推荐(轻量、分布式、多机器)
NATS ⭐⭐⭐ 高性能,但你当前集群小
Kafka ⭐ 太重,不适合 6 台集群
HTTP webhook ⭐ 临时方案但不持久化

建议 Redis Stream。

Cypress 发送测试事件:

1
2
3
4
5
6
7
{
"type": "test_complete",
"status": "failed",
"spec": "login.cy.js",
"timestamp": 1732939200,
"machine": "node-3"
}

MCP Agent 监听 Stream:

1
XREAD BLOCK 0 STREAMS test_events $


二、Prometheus 如何接入 Cypress 业务数据?(你的第2个问题)

Cypress 业务数据并不是指标格式,需要转换成 Prometheus Format。

关键:
不要让 Cypress 直接推指标
会绑死测试流程 → 你说得对,要完全解耦。


🎯 最优架构:Cypress → Event Bus → Metric Collector → Prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
Cypress

Emit JSON
│(decoupled)

Redis Stream (test_event)


Metric Collector (Node/Python)

Expose /metrics

Scraped by Prometheus

流程解释:

  1. Cypress 完成测试后只负责:
    ✔ 发送事件(状态、耗时、场景、数据)
    ❌ 不处理 metrics,不同步阻塞

  2. Metric Collector(微服务)负责:

    • 读取 Cypress 事件
    • 转换为 Prometheus Counter/Gauge/Histogram
    • 暴露成 /metrics endpoint
    • 被 Prometheus 定时 scrape

Prometheus 支持的数据类型

你可以将 Cypress 数据转成:

1️⃣ PASS/FAIL counter

1
2
cypress_test_pass_total{spec="login"} 12
cypress_test_fail_total{spec="login"} 3

2️⃣ 测试耗时 histogram

1
cypress_test_duration_seconds_bucket{spec="checkout",le="2"} 5

3️⃣ 业务自定义指标

如:

1
order_create_latency_seconds{env="uat"} 1.3

这个是 Cypress 专属创新点。



三、VSCode 自带的 LLM 如何放进 Docker?(你的第3个问题)

你想把 VSCode + MCP Agent(LLM) 集成在 Docker 容器里,让它作为服务运行。

是可以做到的。


方法1(推荐):使用 VSCode Web + Copilot Agent Server

你可以在 Docker 中运行:

1
2
3
code-server (VSCode Web)
+ Copilot Agent MCP Server
+ Your custom MCP Tools

Dockerfile 示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FROM ubuntu:22.04

# 安装 node
RUN apt-get update && apt-get install -y curl git
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs

# 安装 code-server
RUN curl -fsSL https://code-server.dev/install.sh | sh

# 安装 MCP agent(包括 LLM 工具)
RUN npm install -g @vscode/copilot-agent

# 复制工具
COPY mcp-tools /usr/local/mcp-tools

CMD ["code-server", "--bind-addr", "0.0.0.0:8080"]

启动后你就可以:

  • 通过浏览器访问 VSCode Web
  • 容器内推理 Copilot LLM
  • 作为 MCP Agent 服务,响应 Cypress 测试事件

方法2:独立 MCP Agent,不依赖 VSCode UI

直接运行:

1
npx @vscode/copilot-agent start --tools ./tools

这样容器内只运行 LLM/Tools,不需要 VSCode UI。


方法3:(高级)在 Container 中使用 OpenAI 本地模型 or Ollama

VSCode LLM 层换成:

  • OpenAI GPT
  • OpenAI O-model
  • Ollama (Llama3.1)
  • Azure AI

通过 MCP agent 的 config.json 指定:

1
2
3
4
5
{
"llm": "openai",
"api_key": "...",
"model": "gpt-5.1"
}

这样 LLM 就可插拔。


🧠 你最后得到的是一个真正的分布式 AIOps 级系统

完整大图如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                 ┌────────────────────────────┐
│ Prometheus/Grafana │
└───────────────▲────────────┘
│scrape
┌───────┴────────┐
│ Metric Collector│
└───────▲────────┘

┌─────────────┴──────────────┐
│ Redis Stream (events) │
└─────────────▲──────────────┘

┌─────────┴────────────┐
│ MCP Agent │
│ (LLM + Tools) │
└─────────▲────────────┘

┌──────────────┬──────────┴────────────┬─────────────┐
▼ ▼ ▼ ▼
Docker Ops Tool Log Query Tool Prometheus Query MLOps Tool

你现在具备:

  • ✔ 6 台机器(可分布式)
  • ✔ Cypress
  • ✔ Docker Compose
  • ✔ Prometheus
  • ✔ Grafana
  • ✔ Redis
  • ✔ MCP Agent(你的核心)

这是一个完整的 “自愈式智能测试平台”。

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏