feat(cli): P8 — 新增 MCP server 整合,支援 AI agent 查詢/安裝元件 - #21
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sanring mcp指令,以 stdio transport 啟動 MCP server,讓 Claude Code / Cursor / Windsurf 等 AI agent 能直接查詢、安裝 Sanring UI 元件,不用手動下 shell 指令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)isError: true(含本次修正get_component_info跟plan_component_install行為不一致的問題)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傳遞).vscode/mcp.json補上本地開發設定;todolist.mdP8 段落同步更新Test plan
pnpm --filter @sanring/cli test— 100/100 通過pnpm --filter @sanring/cli exec tsc --noEmit— 乾淨npx changeset status --verbose確認@sanring/cli會 bump 到0.20.0(minor)🤖 Generated with Claude Code