Observability Agent — Tích hợp MCP và giám sát hệ thống với Claude SDK
Trong các bài trước, agents của chúng ta bị giới hạn ở web search và filesystem. Agents thực tế cần tương tác với databases, APIs, Git repos, CI/CD pipelines. Bài này hướng dẫn tích hợp hệ thống bên ngoài qua Model Context Protocol (MCP) — biến agent thành observability system tự động.
Bài viết dựa trên Claude Cookbooks chính thức của Anthropic.
MCP là gì?
Model Context Protocol (MCP) là open-source standard cho AI-tool integrations. Nó cho phép kết nối dễ dàng giữa agents và hệ thống bên ngoài — databases, Git, GitHub, Prometheus, Slack, và hàng trăm services khác.
Thay vì viết custom integrations cho mỗi service, bạn chỉ cần khai báo MCP server và agent tự biết cách sử dụng.
Step 1: Git MCP Server — Local Git Operations
Bắt đầu đơn giản: cho agent khả năng làm việc với Git repositories.
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
options = ClaudeAgentOptions(
model="claude-sonnet-4-6",
mcp_servers={
"git": {
"command": "uvx",
"args": ["mcp-server-git",
"--repository", "/path/to/repo"]
}
},
allowed_tools=["mcp__git__*"] # Cho phép tất cả Git tools
)
Git MCP server cung cấp 13 tools: examine commit history, check file changes, create branches, make commits. Agent từ passive observer trở thành active participant trong development workflow.
Ví dụ sử dụng
async with ClaudeSDKClient(options=options) as client:
await client.query(
"Phân tích 20 commits gần nhất. "
"Tìm pattern: ai commit nhiều nhất? "
"Files nào thay đổi thường xuyên nhất?"
)
async for msg in client.receive_response():
process(msg)
Step 2: GitHub MCP Server — Full Platform Integration
Nâng cấp từ local Git lên toàn bộ GitHub ecosystem — issues, pull requests, CI/CD workflows, code security alerts.
options = ClaudeAgentOptions(
mcp_servers={
"github": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": os.getenv("GITHUB_TOKEN")
}
}
},
allowed_tools=["mcp__github__*"]
)
GitHub MCP server cung cấp hơn 100 tools — quản lý issues, PRs, monitor CI/CD, analyze security alerts. Hoạt động với cả public và private repos.
Real Use Case: Observability Agent
Kết hợp Git + GitHub MCP để tạo self-healing observability system:
- Monitor GitHub Actions workflows tự động
- Detect failures — phân biệt real failures vs security restrictions
- Analyze test failures chi tiết
- Report actionable insights cho team
Modular Architecture
# observability_agent/agent.py
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
async def send_query(prompt, continue_conversation=False):
options = ClaudeAgentOptions(
model="claude-sonnet-4-6",
system_prompt="Bạn là observability engineer...",
mcp_servers={"github": github_config},
allowed_tools=["mcp__github__*"]
)
async with ClaudeSDKClient(options=options) as client:
await client.query(prompt)
async for msg in client.receive_response():
yield msg
Module hóa cho phép reuse — gọi send_query() từ bất kỳ đâu.
Multi-turn investigations
# Turn 1: Kiểm tra CI/CD status
await send_query("Check GitHub Actions cho repo X — có failures không?")
# Turn 2: Drill down (giữ context)
await send_query(
"Phân tích chi tiết test failures",
continue_conversation=True
)
Kết hợp tất cả
Qua 3 notebooks, chúng ta đã xây dựng nền tảng vững cho production agents:
| Notebook | Đã học |
|---|---|
| 00 - Research Agent | query(), ClaudeSDKClient, WebSearch, basic loops |
| 01 - Chief of Staff | CLAUDE.md, output styles, plan mode, subagents, hooks |
| 02 - Observability | MCP servers, external integrations, workflow automation |
Patterns này là foundation cho production-ready agentic systems có khả năng handle real-world complexity trong khi maintain governance và observability.
Bước tiếp theo: Đọc SRE Agent để học cách cho agent write access an toàn vào infrastructure, hoặc Migration từ OpenAI nếu đang chuyển đổi SDK.
Bai viet co huu ich khong?
Bản quyền thuộc về tác giả. Vui lòng dẫn nguồn khi chia sẻ.






