swift_package_build
Build Swift packages by configuring paths, targets, architectures, and configurations for optimized compilation and execution.
Instructions
Builds a Swift Package with swift build
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| architectures | No | Architectures to build for (e.g. arm64, x86_64) | |
| configuration | No | Build configuration: 'debug' (default) or 'release' | |
| packagePath | Yes | Path to the Swift package root (Required) | |
| parseAsLibrary | No | Add -parse-as-library flag for @main support (default: false) | |
| targetName | No | Optional target to build |
Implementation Reference
- src/tools/build-swift-package.ts:28-82 (handler)The async handler function that implements the core logic for building a Swift package using the 'swift build' command with customizable options.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'); } },
- Zod-based input schema defining parameters for the swift_package_build tool.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 exported registration function that sets up the tool on the MCP server, 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:160-164 (registration)Central tool registry entry that conditionally enables and registers the swift_package_build tool based on 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 build parameters used by the tool.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)');