Skip to main content
Glama

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
NameRequiredDescriptionDefault
architecturesNoArchitectures to build for (e.g. arm64, x86_64)
configurationNoBuild configuration: 'debug' (default) or 'release'
packagePathYesPath to the Swift package root (Required)
parseAsLibraryNoAdd -parse-as-library flag for @main support (default: false)
targetNameNoOptional target to build

Implementation Reference

  • 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, },
  • 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'); } }, ); }
  • 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', },
  • 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)');

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SampsonKY/XcodeBuildMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server