Spaces:
Configuration error
Download README.md from phanquocdat123/MCP: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/spaces/phanquocdat123/MCP/resolve/main/README.md
- Command line
-
hf download hf://spaces/phanquocdat123/MCP/README.md
-
curl -L -o README.md https://huggingface.co/spaces/phanquocdat123/MCP/resolve/main/README.md
Gemini + MCP Agent
AI agent dùng Gemini function calling làm bộ não, và MCP (Model Context Protocol) làm lớp kết nối tool. Thiết kế modular để thêm tool mới mà không cần sửa code, chỉ sửa file config.
Kiến trúc
User input → Agent.chat() → Gemini (quyết định có cần tool không)
│
có function_call?
│
MCPManager.call_tool() ──► đúng MCP server (stdio/SSE)
│
trả kết quả về Gemini → lặp lại tới khi có câu trả lời cuối
core/config_loader.py— đọc & validateconfig/mcp_servers.jsoncore/mcp_manager.py— mở kết nối tới nhiều MCP server cùng lúc, gom tool thành 1 registry, đặt tên tool dạngserver__toolđể tránh trùngcore/tool_adapter.py— convert JSON Schema của MCP sangFunctionDeclarationcủa Geminicore/agent.py— vòng lặp hội thoại, xử lý multi-turn tool callingmain.py— CLI demo, dễ thay bằng Telegram/web handler
Cài đặt
cd gemini_mcp_agent
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# rồi điền GEMINI_API_KEY vào .env
# Cần Node.js nếu dùng MCP server dạng npx (ví dụ server-filesystem)
Chạy thử
python main.py
Cách thêm tool mới (mở rộng)
Mọi tool đến từ 1 MCP server. Có 2 cách:
1. Dùng MCP server có sẵn (nhanh nhất)
Cộng đồng đã publish rất nhiều MCP server (filesystem, fetch, GitHub, Slack,
Google Drive, Postgres...). Bạn chỉ cần thêm 1 entry vào
config/mcp_servers.json:
{
"name": "github",
"enabled": true,
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "..." }
}
Không cần sửa 1 dòng code nào — MCPManager tự động list tool, convert
sang Gemini function, và route tool call tới đúng server.
2. Tự viết MCP server riêng (custom tool)
Ví dụ bạn muốn expose tool đọc dữ liệu từ Google Sheets của Hương Kid. Viết 1 MCP server Python nhỏ:
# my_tools_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("huong-kid-tools")
@mcp.tool()
def get_inventory(product_name: str) -> str:
"""Tra cứu tồn kho theo tên sản phẩm."""
# gọi Google Sheets API ở đây
return f"Còn 12 cái {product_name}"
if __name__ == "__main__":
mcp.run()
Rồi khai báo trong config:
{
"name": "huong_kid",
"enabled": true,
"transport": "stdio",
"command": "python",
"args": ["/path/to/my_tools_server.py"]
}
Xong — Gemini sẽ tự thấy tool huong_kid__get_inventory và gọi khi cần.
3. Kết nối MCP server từ xa (SSE)
Nếu tool của bạn chạy như 1 service riêng (ví dụ deploy trên HF Spaces như Second Brain bot):
{
"name": "second_brain",
"enabled": true,
"transport": "sse",
"url": "https://your-space.hf.space/mcp/sse",
"headers": { "Authorization": "Bearer ..." }
}
Nhúng vào Telegram bot (Second Brain)
Thay vì CLI, trong handler Telegram bạn chỉ cần giữ 1 Agent instance
sống suốt vòng đời app (mở MCPManager 1 lần khi start), rồi mỗi tin nhắn
gọi:
reply = await agent.chat(update.message.text)
await update.message.reply_text(reply)
Lưu ý triển khai
MAX_TOOL_ITERATIONStrongagent.pychặn vòng lặp gọi tool vô hạn — tăng nếu workflow của bạn cần gọi nhiều tool nối tiếp hơn 8 lần.- Một MCP server bị lỗi kết nối sẽ chỉ bị log & bỏ qua, không làm sập
toàn bộ agent (xem
MCPManager.connect_all). - Nếu chạy trên HF Spaces (hạn chế outbound như bạn đã gặp ở Second Brain),
MCP server dạng
stdiochạy local trong cùng container vẫn hoạt động bình thường vì không cần outbound network; chỉ transportssera ngoài mới cần quan tâm tới relay như bạn từng làm với Apps Script.