🚀 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 文件。
致谢