Xcode工具文档
Xcode MCP服务器(又称Xcode工具)的全面参考。这些工具使AI助手能够与Xcode工作区进行交互——管理文件、构建项目、运行测试、渲染预览等。
苹果文档
先决条件
- Xcode 26.3+已安装并在开放式工作区中运行
- 配置了Xcode集成的MCP服务器
注: 大多数工具都需要 tabIdentifier 标识要在哪个Xcode工作区选项卡上操作的参数。安装
通过以下方式将Xcode MCP服务器添加到您的编码工具中 xcrun mcpbridge:
克劳德代码:
claude mcp add --transport stdio xcode -- xcrun mcpbridge食品法典:
codex mcp add xcode -- xcrun mcpbridge证实 claude mcp list 或 codex mcp list.
模式
tools.json 包含直接从生成的完整MCP工具定义(名称、标题、描述、输入/输出模式) xcrun mcpbridge.
目录
- X代码列表窗口
- Xcodels - Xcodeglow - Xcodegrep - XcodeRead - XcodeWrite - XcodeUpdate - Xcodemakedir - XcodeMV - XcodeRM
- 建筑项目 - GetBuildLog - 执行代码段
- GetTestList - 运行所有测试 - RunSome测试
- XcodeRefreshCodeIssuesInFile - Xcodelistnavigator问题
- 渲染审查
- 文档搜索
______________________________________________________________________
工作区
X代码列表窗口
列出当前Xcode窗口及其工作区信息。使用此项获取 tabIdentifier 所有其他工具所需的值。
参数: 无
例子:
XcodeListWindows()______________________________________________________________________
文件操作
Xcodels
列出给定路径下Xcode项目结构中的文件和目录。在项目导航器层次结构上操作,而不是在文件系统上操作。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
path | string | 是 | 要浏览的项目路径(例如。, ProjectName/Sources/) |
recursive | boolean | 否 | 递归列出所有文件(截断为100行)。违约: true |
ignore | string\[\] | 否 | 要跳过的模式 |
例子:
XcodeLS(tabIdentifier: "...", path: "MyApp/Sources/")Xcodeglow
在Xcode项目中查找与通配符模式匹配的文件。支持 *, **, ?, [abc],以及 {swift,m} 语法。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
pattern | string | No | Glob模式(例如。, **/*.swift).默认为 **/* |
path | string | 否 | 要搜索的目录(默认为项目根目录) |
例子:
XcodeGlob(tabIdentifier: "...", pattern: "**/*.swift")Xcodegrep
在Xcode项目结构中使用正则表达式模式搜索文件内容。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
pattern | string | 是 | 要搜索的正则表达式模式 |
path | string | 否 | 要搜索的文件或目录(默认为root) |
glob | string | 否 | 仅搜索与此glob匹配的文件 |
type | string | 否 | 文件类型快捷方式(swift, js, py等等) |
outputMode | string | 否 | content, filesWithMatches (默认),或 count |
ignoreCase | boolean | 否 | 不区分大小写的匹配 |
multiline | boolean | 否 | 允许图案跨越多行 |
showLineNumbers | boolean | 否 | 显示行号(仅限内容模式) |
linesBefore | integer | 否 | 每次匹配前的上下文行 |
linesAfter | integer | 否 | 每次匹配后的上下文行 |
linesContext | integer | 否 | 每次匹配前后的上下文行 |
headLimit | integer | 否 | N个结果后停止 |
例子:
XcodeGrep(
tabIdentifier: "...",
pattern: "func viewDidLoad",
type: "swift",
outputMode: "content",
linesAfter: 5
)XcodeRead
读取带有行号的文件内容(cat -n 格式)。支持大文件的偏移/限制。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
filePath | string | 是 | 项目相对文件路径(例如。, ProjectName/Sources/MyFile.swift) |
offset | integer | 否 | 开始读取的行号 |
limit | integer | 否 | 要读取的行数(默认值:最多600行) |
例子:
XcodeRead(tabIdentifier: "...", filePath: "MyApp/Sources/ContentView.swift")XcodeWrite
在Xcode项目中创建或覆盖文件。自动将新文件添加到项目结构中。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
filePath | string | 是 | 项目相对文件路径 |
content | string | 是 | 要写入的文件内容 |
例子:
XcodeWrite(
tabIdentifier: "...",
filePath: "MyApp/Sources/NewFeature.swift",
content: "import Foundation\n\nstruct NewFeature {\n}\n"
)XcodeUpdate
通过查找和替换文本来编辑文件。在项目结构路径上运行。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
filePath | string | 是 | 项目相对文件路径 |
oldString | string | 是 | 要查找的文本 |
newString | string | 是 | 替换文本(必须与 oldString) |
replaceAll | boolean | 否 | 替换所有匹配项。违约: false |
例子:
XcodeUpdate(
tabIdentifier: "...",
filePath: "MyApp/Sources/ContentView.swift",
oldString: "Hello, World!",
newString: "Hello, SwiftUI!"
)Xcodemakedir
在Xcode项目导航器中创建目录和组。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
directoryPath | string | 是 | 新目录的项目相对路径 |
例子:
XcodeMakeDir(tabIdentifier: "...", directoryPath: "MyApp/Sources/ViewModels")XcodeMV
在项目导航器中移动、复制或重命名文件和目录。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
sourcePath | string | 是 | 项目导航器中的源路径 |
destinationPath | string | 是 | 目标路径或新名称 |
operation | string | 否 | move 或 copy |
overwriteExisting | boolean | 否 | 覆盖目标位置的文件 |
例子:
XcodeMV(
tabIdentifier: "...",
sourcePath: "MyApp/Sources/OldName.swift",
destinationPath: "MyApp/Sources/NewName.swift"
)XcodeRM
从Xcode项目中删除文件和目录。可选择删除底层文件系统文件。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
path | string | 是 | 要删除的项目路径 |
recursive | boolean | 否 | 递归删除目录和内容 |
deleteFiles | boolean | 否 | 同时将文件移至回收站。违约: true |
例子:
XcodeRM(tabIdentifier: "...", path: "MyApp/Sources/Deprecated.swift")______________________________________________________________________
构建与运行
建筑项目
使用活动方案构建Xcode项目并等待完成。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
例子:
BuildProject(tabIdentifier: "...")GetBuildLog
从当前或最近的生成中检索生成日志条目。按严重性、文件模式或消息正则表达式过滤。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
severity | string | 否 | 最低严重性: error (默认), warning,或 remark |
pattern | string | 否 | 用于按消息、任务描述、命令行或控制台输出进行筛选的正则表达式 |
glob | string | 否 | 按文件路径或任务位置筛选的Glob |
例子:
GetBuildLog(tabIdentifier: "...", severity: "warning")执行代码段
在特定源文件的上下文中构建并运行代码段。该代码段可以访问该文件中的所有声明,包括 fileprivate 一个。输出来自 print 声明。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
codeSnippet | string | 是 | 要执行的Swift代码 |
sourceFilePath | string | 是 | 项目到上下文文件的相对路径 |
timeout | integer | 否 | 最大等待时间(秒)。默认值:120 |
注: 仅适用于构建应用程序、框架、库或CLI可执行文件的目标中的源文件。
例子:
ExecuteSnippet(
tabIdentifier: "...",
sourceFilePath: "MyApp/Sources/Models/User.swift",
codeSnippet: "let user = User(name: \"Test\")\nprint(user)"
)______________________________________________________________________
测试
GetTestList
从活动方案的活动测试计划中获取所有可用测试。结果仅限于100次测试。完整列表已写入 fullTestListPath 以grep友好的格式使用grep,键如下 TEST_TARGET, TEST_IDENTIFIER,或 TEST_FILE_PATH 找到具体的测试。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
例子:
GetTestList(tabIdentifier: "...")运行所有测试
运行活动方案的活动测试计划中的每个测试。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
例子:
RunAllTests(tabIdentifier: "...")RunSome测试
按目标和标识符运行特定测试。使用 GetTestList 首先发现可用的测试标识符。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
tests | array | Yes | 测试说明符数组(见下文) |
每个测试说明符对象:
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
targetName | string | 是 | 测试目标名称 |
testIdentifier | string | Yes | XCTestIdentifier格式的测试标识符 |
例子:
RunSomeTests(
tabIdentifier: "...",
tests: [
{ "targetName": "MyAppTests", "testIdentifier": "MyAppTests/LoginTests/testValidLogin" }
]
)______________________________________________________________________
诊断
XcodeRefreshCodeIssuesInFile
检索特定文件的当前编译器诊断(错误、警告、注释)。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
filePath | string | 是 | 项目相对文件路径 |
例子:
XcodeRefreshCodeIssuesInFile(
tabIdentifier: "...",
filePath: "MyApp/Sources/ContentView.swift"
)Xcodelistnavigator问题
从Xcode的Issue Navigator中列出问题,包括构建错误、包解析问题和工作区配置问题。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
severity | string | 否 | 最低严重性: error (默认), warning,或 remark |
pattern | string | 否 | 按消息筛选的正则表达式 |
glob | string | 否 | 按文件路径筛选的Glob |
例子:
XcodeListNavigatorIssues(tabIdentifier: "...", severity: "warning")______________________________________________________________________
预览
渲染审查
构建并渲染SwiftUI预览,返回结果UI的快照。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tabIdentifier | string | 是 | 工作区选项卡标识符 |
sourceFilePath | string | 是 | 投影包含预览的文件的相对路径 |
previewDefinitionIndexInFile | integer | 否 | 从零开始的索引 #Preview 宏观或 PreviewProvider 在文件中。违约: 0 |
timeout | integer | 否 | 最大等待时间(秒)。默认值:120 |
例子:
RenderPreview(
tabIdentifier: "...",
sourceFilePath: "MyApp/Sources/Views/ProfileView.swift"
)______________________________________________________________________
文档
文档搜索
使用语义匹配搜索Apple开发者文档。可用于查找API、框架和使用模式。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
query | string | 是 | 搜索查询 |
frameworks | string\[\] | 否 | 将搜索限制到特定框架。如果省略,则搜索全部 |
作者
阿特姆·诺维奇科夫,https://artemnovichkov.com
