MCP

In version v2.11

Link


README — FastMCP + OpenAPI + Orchestrator/Proxy (Tổng hợp nhanh)

Tài liệu này tổng hợp toàn bộ “kiến thức nãy giờ” để bạn triển khai MCP theo hướng ít tool – dễ điều phối – độ tin cậy cao.


1) Ảnh tổng quan


2) FastMCP từ OpenAPI (điểm chính)


3) Orchestrator pattern (khuyên dùng)

3.1 Ý tưởng

3.2 Code mẫu siêu gọn

# orchestrator.py
from fastmcp import FastMCP, Client
from pydantic import BaseModel

orchestrator = FastMCP("Orchestrator")

client_a = Client("http://localhost:7001/mcp")
client_b = Client("http://localhost:7002/mcp")
client_c = Client("http://localhost:7003/mcp")

class In(BaseModel):
    input_data: str

class Out(BaseModel):
    result: str

@orchestrator.tool
async def execute_workflow(payload: In) -> Out:
    async with client_a, client_b, client_c:
        s1 = await client_a.call_tool("step1", {"data": payload.input_data}, timeout=5)
        s2 = await client_b.call_tool("step2", {"result": s1}, timeout=5)
        fin = await client_c.call_tool("final", {"input": s2}, timeout=5)
        return Out(result=str(fin))

if __name__ == "__main__":
    import asyncio; asyncio.run(orchestrator.run())

3.3 Ví dụ domain (tuyển sinh)

  1. A.step1: phân tích câu hỏi & lấy yêu cầu ngành (điểm sàn, bonus IELTS…).

  2. B.step2: tính eligible? + giải thích (tổng điểm, cộng điểm…).

  3. C.final: tạo câu trả lời tự nhiên cho thí sinh.

Ưu: LLM chỉ thấy 1 tool → ít rối; bạn kiểm soát chặt timeout/retry/luồng phụ thuộc.


4) Proxy/Chain pattern (khi nào nên dùng)

Snippet minh hoạ:

server_c = FastMCP("ServerC")
server_b = FastMCP.as_proxy(server_c, name="ServerB")
server_a = FastMCP.as_proxy(server_b, name="ServerA")

5) Hybrid “router” (linh hoạt mà vẫn gọn)

@orchestrator.tool
async def run(task: str, intent: str):
    async with client_a, client_b, client_c:
        if intent == "eligibility":
            return await client_b.call_tool("check_eligibility", {"q": task})
        if intent == "tuition":
            return await client_c.call_tool("get_tuition", {"q": task})
        return await client_a.call_tool("search_programs", {"q": task})

6) Best-practices (rất quan trọng)

  1. Giới hạn tool hiển thị: aim tổng ≤ 20–30 tool; tốt nhất 1–5 tool “nhiệm vụ”.

  2. Curate tên & mô tả: tên ngắn, có động từ, mô tả “khi nào dùng” + ví dụ arg.

  3. Allow-list mạnh tay khi convert OpenAPI: EXCLUDE phần admin/internal/thao tác nguy hiểm.

  4. Timeout + retry ở orchestrator; có thể thêm circuit breaker cho tool hay lỗi/treo.

  5. Kiểm tra dữ liệu giữa các bước (schema Pydantic) để tránh “rác” đẩy sang bước sau.

  6. Auth/Headers: cấu hình qua Client(..., headers=...) (Bearer/API key/…); có thể thêm per-call timeout.

  7. Quan sát/trace: trả về traces (tool, args rút gọn, attempt) để debug nhanh.

  8. Từng bước một: bootstrap bằng OpenAPI → đo lường → chắt lọc thành workflow ít tool.


7) Cách chạy nhanh

pip install fastmcp pydantic
python orchestrator.py
# gắn vào client MCP (Claude Desktop, OpenAI Responses API, v.v.)

Biến môi trường hữu ích


8) FAQ ngắn

Q: Có cần xác định rõ tool sẽ gọi không?

Q: Vì sao không expose hết tools?

Q: Khi nào dùng proxy chain?


9) Checklist triển khai


10) Tệp mẫu đề xuất

Notes last Touches Today

Today I learned 🧭

Summary 💬

Gratitude and Recognition 😍😍