swift_package_build
Build Swift packages using swift build with options for target, configuration, architecture, and parse-as-library flag support.
Instructions
Builds a Swift Package with swift build
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| packagePath | Yes | Path to the Swift package root (Required) | |
| targetName | No | Optional target to build | |
| configuration | No | Build configuration: 'debug' (default) or 'release' | |
| architectures | No | Architectures to build for (e.g. arm64, x86_64) | |
| parseAsLibrary | No | Add -parse-as-library flag for @main support (default: false) |
Implementation Reference
- src/tools/build-swift-package.ts:34-82 (handler)The core handler function that executes the 'swift build' command. It validates the packagePath parameter, resolves the path, builds the command arguments based on optional parameters (targetName, configuration, architectures, parseAsLibrary), runs the command via executeCommand, handles success/error responses, and provides user-friendly output.}): Promise<ToolResponse> => { const pkgValidation = validateRequiredParam('packagePath', params.packagePath); if (!pkgValidation.isValid) return pkgValidation.errorResponse!; const resolvedPath = path.resolve(params.packagePath); const args: string[] = ['build', '--package-path', resolvedPath]; if (params.configuration && params.configuration.toLowerCase() === 'release') { args.push('-c', 'release'); } if (params.targetName) { args.push('--target', params.targetName); } if (params.architectures) { for (const arch of params.architectures) { args.push('--arch', arch); } } if (params.parseAsLibrary) { args.push('-Xswiftc', '-parse-as-library'); } log('info', `Running swift ${args.join(' ')}`); try { const result = await executeCommand(['swift', ...args], 'Swift Package Build'); if (!result.success) { const errorMessage = result.error || result.output || 'Unknown error'; return createErrorResponse('Swift package build failed', errorMessage, 'BuildError'); } return { content: [ { type: 'text', text: '✅ Swift package build succeeded.' }, { type: 'text', text: '💡 Next: Run tests with swift_package_test or execute with swift_package_run', }, { type: 'text', text: result.output }, ], }; } catch (error) { const message = error instanceof Error ? error.message : String(error); log('error', `Swift package build failed: ${message}`); return createErrorResponse('Failed to execute swift build', message, 'SystemError'); } },
- Zod input schema defining parameters for the swift_package_build tool, referencing shared Swift Package schemas from common.ts.packagePath: z.string().describe('Path to the Swift package root (Required)'), targetName: z.string().optional().describe('Optional target to build'), configuration: swiftConfigurationSchema, architectures: swiftArchitecturesSchema, parseAsLibrary: parseAsLibrarySchema, },
- src/tools/build-swift-package.ts:16-84 (registration)The registration function that registers the 'swift_package_build' tool on the MCP server using registerTool from common.ts, including name, description, schema, and handler.export function registerBuildSwiftPackageTool(server: McpServer): void { registerTool( server, 'swift_package_build', 'Builds a Swift Package with swift build', { packagePath: z.string().describe('Path to the Swift package root (Required)'), targetName: z.string().optional().describe('Optional target to build'), configuration: swiftConfigurationSchema, architectures: swiftArchitecturesSchema, parseAsLibrary: parseAsLibrarySchema, }, async (params: { packagePath: string; targetName?: string; configuration?: 'debug' | 'release'; architectures?: ('arm64' | 'x86_64')[]; parseAsLibrary?: boolean; }): Promise<ToolResponse> => { const pkgValidation = validateRequiredParam('packagePath', params.packagePath); if (!pkgValidation.isValid) return pkgValidation.errorResponse!; const resolvedPath = path.resolve(params.packagePath); const args: string[] = ['build', '--package-path', resolvedPath]; if (params.configuration && params.configuration.toLowerCase() === 'release') { args.push('-c', 'release'); } if (params.targetName) { args.push('--target', params.targetName); } if (params.architectures) { for (const arch of params.architectures) { args.push('--arch', arch); } } if (params.parseAsLibrary) { args.push('-Xswiftc', '-parse-as-library'); } log('info', `Running swift ${args.join(' ')}`); try { const result = await executeCommand(['swift', ...args], 'Swift Package Build'); if (!result.success) { const errorMessage = result.error || result.output || 'Unknown error'; return createErrorResponse('Swift package build failed', errorMessage, 'BuildError'); } return { content: [ { type: 'text', text: '✅ Swift package build succeeded.' }, { type: 'text', text: '💡 Next: Run tests with swift_package_test or execute with swift_package_run', }, { type: 'text', text: result.output }, ], }; } catch (error) { const message = error instanceof Error ? error.message : String(error); log('error', `Swift package build failed: ${message}`); return createErrorResponse('Failed to execute swift build', message, 'SystemError'); } }, ); }
- src/utils/register-tools.ts:161-164 (registration)Top-level tool registration entry in the toolRegistrations array. Conditionally registers registerBuildSwiftPackageTool based on the XCODEBUILDMCP_TOOL_SWIFT_PACKAGE_BUILD environment variable.register: registerBuildSwiftPackageTool, groups: [ToolGroup.SWIFT_PACKAGE_WORKFLOW], envVar: 'XCODEBUILDMCP_TOOL_SWIFT_PACKAGE_BUILD', },
- src/tools/common.ts:86-100 (schema)Shared Zod schemas for Swift Package tools: configuration, architectures, and parseAsLibrary options, used by swift_package_build and similar tools.export const swiftConfigurationSchema = z .enum(['debug', 'release']) .optional() .describe("Build configuration: 'debug' (default) or 'release'"); export const swiftArchitecturesSchema = z .enum(['arm64', 'x86_64']) .array() .optional() .describe('Architectures to build for (e.g. arm64, x86_64)'); export const parseAsLibrarySchema = z .boolean() .optional() .describe('Add -parse-as-library flag for @main support (default: false)');