iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
自我挑戰組

30 天工程師雜學之旅系列 第 4

Kiro-4 用 Kiro Spec 打造 FastAPI MCP Server — 從需求到實作的真實案例

  • 分享至 

  • xImage
  •  

1. 前言:從腦海藍圖到落地專案

在上一篇,我聊了 Kiro 的 Spec 系統如何讓「計劃」和「執行」貼得更近。
今天,我要用一個實際案例,帶你看看我如何用 Kiro 的 Spec 功能,把一個大目標——在 FastAPI 專案中加入 MCP(Model Context Protocol)Server 支援——拆解、規劃,最後逐步完成(關於這個MCP 之後會再寫一篇專文介紹)。

這篇不只是功能介紹,而是把我實際走過的流程、Kiro 生成的內容,以及中途互動的細節全都還原給你。


2. 起點:Prompt 與初始需求

我給 Kiro 的第一個 Prompt 很直接:

Add MCP server support to this FastAPI backend using fastapi_mcp. Include basic setup, example tools, authentication, and minimal docs/tests.

這句話就像是丟給 Kiro 一個大方向,接下來它進入 Requirements 階段,自動生成了一份完整的 requirements.md


3. Requirements:Kiro 生成的第一版需求

Kiro 會將需求轉換為 EARS 格式 的 User Story + Acceptance Criteria。以下擷取部分(完整內容更長):

**User Story:** As an AI assistant or MCP client, I want to connect to QueryPal's MCP server, so that I can access database exploration capabilities programmatically.

#### Acceptance Criteria

1. WHEN an MCP client connects to the server THEN the system SHALL establish a secure connection using the MCP protocol
2. WHEN the connection is established THEN the system SHALL provide a list of available tools and their schemas
3. IF authentication is required THEN the system SHALL validate credentials before allowing access
4. WHEN the client disconnects THEN the system SHALL clean up resources and close the connection gracefully

它還自動涵蓋了:

  • 資料庫結構探索
  • 自然語言轉 MongoDB 查詢
  • 查詢執行與分頁
  • 文件 CRUD 操作
  • FastAPI 無縫整合
  • MCP 伺服器的監控與設定

💡 互動亮點
在這個階段,我臨時想到要加入 enable/disable write operations 的功能(用環境變數控制 MCP 是否允許資料寫入)。我只用一句話補充給 Kiro,它就把這個需求加進了 Requirements 的相關章節,並且後續會反映到 Design 與 Tasks 中。


4. Design:從需求到系統藍圖

批准需求後,Kiro 進入 Design 階段,掃描專案並生成 design.md。它的重點包括:

  • 整合模式:使用 fastapi_mcp 自動將 FastAPI endpoint 轉換為 MCP 工具
  • 系統架構圖(Mermaid 繪製)
  • 關鍵設計原則
    1. Endpoint-First — 每個 MCP 工具對應一個 FastAPI endpoint
    2. 重用現有服務層
    3. 用環境變數控制 MCP 功能(含 write control)
    4. 支援 HTTP 與 SSE 兩種傳輸方式
  • 介面與元件
    • MCP 配置(config.py
    • MCP 專用路由(routes/mcp.py
    • 與 Azure AD 整合的驗證機制

diagram
系統架構圖


5. Tasks:具體的可執行清單

接下來,Kiro 自動生成 tasks.md,將設計拆解成一個個可執行的任務。例如前三項:

- [x] 1. Set up MCP configuration and directory structure
  - Create MCP configuration module with env var support
  - Create basic MCP directory structure (config.py, schemas.py, auth.py, server.py)
  - Update requirements.txt to ensure fastapi_mcp dependency is configured

- [x] 2. Create MCP request/response schemas
  - Define Pydantic models for MCP request/response
  - Implement validation for database/collection names and query params
  - Write unit tests for schema validation

- [x] 3. Implement MCP authentication integration
  - Integrate with Azure AD authentication
  - Handle authentication failures
  - Write unit tests for authentication scenarios

🛠 使用心得

  • 我選擇手動觸發每個 task,這樣可以在完成後做階段性的檢查。
  • 中途我有手動改過程式碼,只要點 refresh task,Kiro 就會根據最新檔案重新分析,更新 tasks.md 狀態。
  • scope 變更(加 write control)後,只有相關的 task 被更新,不會動到無關的任務。

6. 實作過程的互動與觀察

整個過程下來,我發現 Kiro 的 Spec 系統有幾個很強的點:

  1. 任務拆解到 AI Agent 適合的大小
    • 每個 task 都是獨立且上下文清晰的單位,適合直接交給 AI agent 或自己手動完成。
  2. 進度追蹤與同步更新
    • 修改程式後 refresh 就能讓任務狀態與實際代碼保持一致。
  3. 精準的 scope 變更處理
    • 像 write control 這種新增需求,Kiro 會自動將它加到 Requirements、更新 Design,並在 Tasks 裡補充相關任務,不會推倒重來。

7. 心得與結語

如果用一句話形容這次經驗:

Kiro 就像是能規劃、能追蹤、還能自己動手做的專案助理

Copilot 在即時輔助、補全上很強,但 Kiro 在 規劃 + 落地執行 這件事上的確更像一個合作夥伴。
尤其當需求變更時,它的反應和更新流程讓我少了很多自己對任務清單手動修補的麻煩。

下一篇,我會把這個 MCP Server 的其中一個任務完整走一遍,看看從點擊「開始」到功能完成,中間發生了什麼事。


上一篇
Kiro-3 用 Kiro Spec 系統打造有脈絡的開發流程 — 規劃到執行全攻略
下一篇
Kiro-5 用 Kiro Spec 打造 FastAPI MCP Server — 任務 3 的完整實戰
系列文
30 天工程師雜學之旅22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言