🚀 xcsift-mcp
xcsift-mcp 是一個 MCP(模型上下文協議)服務器,它封裝了 xcsift,使 AI 編碼助手能夠將 Xcode 構建輸出解析為結構化、高效利用令牌的格式。

🚀 快速開始
xcsift-mcp 為 AI 編碼助手(如 Claude、OpenCode、Cursor 等)提供了以下工具:
- 解析:將原始的
xcodebuild 或 swift build/test 輸出解析為結構化的 JSON 或 TOON 格式。
- 執行:執行構建命令並自動獲取解析結果。
- 提取:提取錯誤、警告、測試失敗信息和鏈接器錯誤,並附帶文件/行信息。
- 分析:從測試運行中分析代碼覆蓋率。
輸出經過優化,以提高令牌使用效率,TOON 格式比 JSON 格式的令牌數量減少 30 - 60%。
✨ 主要特性
- 支持多種 AI 編碼助手集成。
- 提供多種解析和構建執行工具。
- 支持 JSON 和 TOON 兩種輸出格式,可根據需求選擇。
- 提供豐富的資源和提示模板,方便開發和調試。
📦 安裝指南
前提條件
- Python 3.10 及以上版本。
- macOS 系統(xcsift 僅支持 macOS)。
- pipx(可通過
brew install pipx 進行安裝)。
從源代碼安裝
git clone https://github.com/johnnyclem/xcsift_mcp.git
cd xcsift_mcp
pipx install -e ".[dev]"
通過 Homebrew 安裝
brew install johnnyclem/xcsift-mcp/xcsift-mcp
xcsift 二進制文件
服務器在首次運行時,如果 xcsift 二進制文件尚未安裝,將自動從 GitHub 發佈版本中下載。該二進制文件將緩存到 ~/.local/share/xcsift-mcp/bin/ 目錄下。
你也可以通過 Homebrew 手動安裝:
brew install xcsift
💻 使用示例
運行服務器
xcsift-mcp
xcsift-mcp --transport http --port 8000
與 AI 助手集成
Claude Desktop
在 claude_desktop_config.json 中添加以下內容:
{
"mcpServers": {
"xcsift": {
"command": "xcsift-mcp"
}
}
}
OpenCode
在 opencode.json(或 opencode.jsonc)中添加以下內容:
{
"mcp": {
"xcsift": {
"type": "local",
"command": ["xcsift-mcp"]
}
}
}
或者運行 opencode mcp add 並按照交互式提示操作。
Cursor
在 Cursor 設置的 MCP 配置中添加,或在 .cursor/mcp.json 中添加以下內容:
{
"mcpServers": {
"xcsift": {
"command": "xcsift-mcp"
}
}
}
示例代碼
解析現有構建輸出
result = parse_xcodebuild_output(
output="<raw xcodebuild output>",
format="toon",
include_warnings=True
)
運行構建並獲取解析結果
result = xcodebuild(
action="build",
scheme="MyApp",
destination="platform=iOS Simulator,name=iPhone 15",
output_format="json"
)
運行帶覆蓋率的測試
result = swift_test(
enable_code_coverage=True,
output_format="toon"
)
僅提取錯誤信息
errors = extract_errors(output="<raw xcodebuild output>")
📚 詳細文檔
可用工具
解析工具
| 工具 |
描述 |
parse_xcodebuild_output |
將原始的 xcodebuild/swift 輸出解析為 JSON 或 TOON 格式 |
extract_errors |
僅提取帶有文件/行信息的錯誤 |
extract_warnings |
僅提取帶有文件/行/類型信息的警告 |
extract_test_failures |
提取帶有斷言消息的失敗測試 |
get_build_summary |
獲取包含錯誤/警告計數的快速摘要 |
構建執行工具
| 工具 |
描述 |
xcodebuild |
運行 xcodebuild 並自動解析輸出 |
swift_build |
為 SPM 項目運行 swift build |
swift_test |
運行 swift test 並可選擇收集覆蓋率 |
run_shell_build_command |
運行任意構建命令並解析輸出 |
工具參數
parse_xcodebuild_output
| 參數 |
類型 |
默認值 |
描述 |
output |
字符串 |
必需 |
原始的 xcodebuild/swift 輸出(使用 2>&1 捕獲標準錯誤) |
format |
"json" | "toon" |
"json" |
輸出格式 |
include_warnings |
布爾值 |
false |
是否包含詳細的警告列表 |
include_coverage |
布爾值 |
false |
如果可用,是否包含覆蓋率數據 |
xcodebuild
| 參數 |
類型 |
默認值 |
描述 |
action |
"build" | "test" | "clean" | "analyze" |
"build" |
構建操作 |
scheme |
字符串 |
無 |
要構建的方案 |
project |
字符串 |
無 |
.xcodeproj 文件的路徑 |
workspace |
字符串 |
無 |
.xcworkspace 文件的路徑 |
destination |
字符串 |
無 |
目標(例如,"platform=iOS Simulator,name=iPhone 15") |
configuration |
"Debug" | "Release" |
無 |
構建配置 |
enable_code_coverage |
布爾值 |
false |
是否為測試啟用覆蓋率 |
output_format |
"json" | "toon" |
"json" |
輸出格式 |
timeout |
整數 |
600 |
超時時間(秒) |
swift_build
| 參數 |
類型 |
默認值 |
描述 |
configuration |
"debug" | "release" |
"debug" |
構建配置 |
package_path |
字符串 |
無 |
Swift 包的路徑 |
target |
字符串 |
無 |
要構建的特定目標 |
output_format |
"json" | "toon" |
"json" |
輸出格式 |
timeout |
整數 |
300 |
超時時間(秒) |
swift_test
| 參數 |
類型 |
默認值 |
描述 |
package_path |
字符串 |
無 |
Swift 包的路徑 |
filter_test |
字符串 |
無 |
過濾測試(例如,"MyTests.testFoo") |
enable_code_coverage |
布爾值 |
false |
是否啟用覆蓋率收集 |
parallel |
布爾值 |
true |
是否並行運行測試 |
output_format |
"json" | "toon" |
"json" |
輸出格式 |
timeout |
整數 |
600 |
超時時間(秒) |
輸出格式
JSON 格式
標準的結構化 JSON 輸出:
{
"status": "failed",
"summary": {
"errors": 1,
"warnings": 3,
"failed_tests": 0,
"linker_errors": 0,
"build_time": "3.2s"
},
"errors": [
{
"file": "main.swift",
"line": 15,
"message": "use of undeclared identifier 'unknown'"
}
],
"warnings": [
{
"file": "view.swift",
"line": 20,
"message": "variable 'temp' was never used",
"type": "compile"
}
]
}
TOON 格式(令牌優化)
比 JSON 格式的令牌數量減少 30 - 60%:
status: failed
summary:
errors: 1
warnings: 3
failed_tests: 0
linker_errors: 0
build_time: 3.2s
errors[1]{file,line,message}:
main.swift,15,"use of undeclared identifier 'unknown'"
warnings[1]{file,line,message,type}:
view.swift,20,"variable 'temp' was never used","compile"
何時使用每種格式:
- JSON:當你需要以編程方式解析輸出或與其他工具集成時使用。
- TOON:當發送給大語言模型以減少令牌使用和 API 成本時使用。
可用資源
| 資源 URI |
描述 |
xcsift://version |
xcsift 版本和安裝信息 |
xcsift://config-template |
示例 .xcsift.toml 配置 |
xcsift://output-formats |
關於輸出格式的文檔 |
xcsift://help |
全面的幫助文檔 |
可用提示
| 提示 |
描述 |
參數 |
analyze_build_failure |
分析構建失敗的模板 |
errors, code_context |
fix_compiler_errors |
修復 Swift/ObjC 編譯器錯誤的模板 |
errors, file_content |
improve_test_coverage |
提高測試覆蓋率的模板 |
coverage_report, target_coverage |
debug_test_failures |
調試測試失敗的模板 |
test_output, test_code |
fix_linker_errors |
修復鏈接器錯誤的模板 |
linker_errors, project_structure |
analyze_build_performance |
分析構建性能的模板 |
build_info |
🔧 技術細節
項目結構
xcsift_mcp/
├── src/xcsift_mcp/
│ ├── __init__.py # 包初始化
│ ├── __main__.py # 入口點
│ ├── server.py # FastMCP 服務器
│ ├── xcsift_installer.py # 自動下載 xcsift
│ ├── resources.py # MCP 資源
│ ├── prompts.py # 提示模板
│ └── tools/
│ ├── parse.py # 解析工具
│ └── build.py # 構建執行工具
├── tests/
│ ├── fixtures/ # 示例構建輸出
│ └── test_*.py # 測試文件
├── pyproject.toml
└── README.md
架構
┌─────────────────────────────────────────────────────────────────┐
│ AI 助手 (Claude, OpenCode, 等) │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ MCP 協議 (標準輸入輸出/HTTP) │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ xcsift MCP 服務器 (Python) │
│ ┌─────────────────┐ ┌──────────────────┐ ┌────────────────┐ │
│ │ 工具 (9) │ │ 資源 (4) │ │ 提示 (6) │ │
│ │ - parse_output │ │ - version │ │ - analyze │ │
│ │ - xcodebuild │ │ - config │ │ - fix_errors │ │
│ │ - swift_build │ │ - formats │ │ - coverage │ │
│ │ - swift_test │ │ - help │ │ - debug │ │
│ └─────────────────┘ └──────────────────┘ └────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ xcsift 命令行工具 (子進程) │
└─────────────────────────────────────────────────────────────────┘
🔧 故障排除
找不到 xcsift
如果無法自動下載 xcsift,請手動安裝:
brew install xcsift
權限被拒絕
確保 xcsift 二進制文件具有執行權限:
chmod +x ~/.local/share/xcsift-mcp/bin/xcsift
構建超時
對於長時間的構建,可以增加超時參數:
xcodebuild(scheme="MyApp", timeout=1200)
📄 許可證
本項目採用 MIT 許可證,詳情請參閱 LICENSE 文件。
致謝