Skip to content

feat(cli): P8 — 新增 MCP server 整合,支援 AI agent 查詢/安裝元件 - #21

Merged
jack755051 merged 8 commits into
mainfrom
feat/mcp-server
Aug 7, 2026
Merged

jack755051 merged 8 commits into
mainfrom
feat/mcp-server

Conversation

@jack755051

Copy link
Copy Markdown
Contributor

Summary

  • 新增 sanring mcp 指令,以 stdio transport 啟動 MCP server,讓 Claude Code / Cursor / Windsurf 等 AI agent 能直接查詢、安裝 Sanring UI 元件,不用手動下 shell 指令
  • 曝露 5 個 tool:list_components、search_components(名稱優先)、get_component_info(files/componentDeps/sharedDeps/peerDeps)、plan_component_install(dry-run 預覽,不動專案)、add_component(cwd 指定 Angular project root,子程序執行 sanring add --yes)
  • 所有 tool handler 都有 runtime input validation;元件找不到時統一回傳 isError: true(含本次修正 get_component_info 跟 plan_component_install 行為不一致的問題)
  • registry 有做 cache 避免重複 fetch;add_component 走 async spawn,並驗證 cwd 底下有 angular.json 才允許執行
  • 補上 packages/cli/src/commands/mcp.test.ts(Client + InMemoryTransport,涵蓋 tools/list、search、detail、plan、not-found isError、add tool boundary)與 mcp.e2e.test.ts(StdioClientTransport 真實 spawn 編譯後的 CLI,驗證 cliBin 路徑解析與 --registry 傳遞)
  • README 補上 Claude Code / local development 兩種設定方式與 5-tool 說明表格;.vscode/mcp.json 補上本地開發設定;todolist.md P8 段落同步更新

Test plan

  • pnpm --filter @sanring/cli test — 100/100 通過
  • pnpm --filter @sanring/cli exec tsc --noEmit — 乾淨
  • eslint(cli 相關檔案)— 乾淨
  • npx changeset status --verbose 確認 @sanring/cli 會 bump 到 0.20.0(minor)
  • 手動用 Claude Code / Cursor 連線本地 build 過的 CLI,實際跑一次 5 個 tool(尚未做,建議 review 前補)
  • Merge 後記得依版本異動檢查清單補齊:首頁版本徽章、docs changelog 頁新增一筆 CLI entry

🤖 Generated with Claude Code

charlie-tai and others added 8 commits August 6, 2026 16:09
Implement stdio MCP server via @modelcontextprotocol/sdk (lower-level Server
API, NodeNext ESM compatible, no zod dependency). Exposes four tools:
list_components, search_components, get_component_info, add_component.
add_component spawns `sanring add --yes` as a subprocess in the target project.

Usage: add `sanring mcp` to .claude/mcp.json as a stdio MCP server.

Branch rationale: keeping separate from main until core UI functionality is stable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
解決 index.ts 衝突:同時保留 mcpCommand(此 branch)與 migrateCommand(main)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- fetchRegistry 結果 cache 在 server instance 內,同 session 只 fetch 一次
- spawnSync → spawn(async),不再阻塞 event loop
- add_component 先驗證 angular.json 存在,cwd 錯誤提早回報清楚訊息

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- 新增 requireStrings() helper:驗證 args 必填欄位型別與非空,失敗回傳 isError MCP content
- search_components / get_component_info / add_component 三個 handler 換掉 unsafe cast
- 補 validation 測試:missing field、空白字串均回傳 isError + 清楚訊息

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- 新 tool:preview 會寫入哪些 files、auto-install 哪些 component deps、需安裝哪些 peer packages
- add_component description 補 hint 引導 agent 先呼叫 plan_component_install
- 補測試:驗證 plan 回傳正確內容且不修改專案

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- 新增 mcp.e2e.test.ts:build CLI → 以 stdio 啟動真實 MCP server → 呼叫 add_component → 驗證檔案寫入
- 修正 cliBin 路徑:dist/commands/index.js → dist/index.js(多了一層 ../)
- 修正 add spawn:registryUrl 納入 closure,帶 --registry 給 add 指令(原本 e2e 會打 production registry)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- README:mcp 章節工具數從 4 → 5,補 plan_component_install 說明
- .vscode/mcp.json:加入 sanring MCP server 本地開發設定
- registry-fixture:補 utilsPeerDependencies 支援(mcp.test.ts 已使用)
- todolist:移除「暫存於 feat/mcp-server」標記,更新完成狀態

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
get_component_info 找不到元件時原本沒回傳 isError: true,跟
plan_component_install 同樣情境不一致,依賴 isError 判斷失敗的
AI agent 會誤判為成功呼叫。補上並加測試保護;todolist.md P8
內文同步更新成實際的 5 個 tool;新增 changeset。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@jack755051
jack755051 merged commit 239c52a into main Aug 7, 2026
3 of 5 checks passed
@jack755051
jack755051 deleted the feat/mcp-server branch August 7, 2026 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants