mcp-xcode
Version:
MCP server that wraps Xcode command-line tools for iOS/macOS development workflows
108 lines • 3.49 kB
JavaScript
import { exec, execSync } from 'child_process';
import { promisify } from 'util';
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
const execAsync = promisify(exec);
export async function executeCommand(command, options = {}) {
const defaultOptions = {
timeout: 300000, // 5 minutes default timeout
maxBuffer: 10 * 1024 * 1024, // 10MB max buffer
...options,
};
try {
const { stdout, stderr } = await execAsync(command, defaultOptions);
return {
stdout: stdout.trim(),
stderr: stderr.trim(),
code: 0,
};
}
catch (error) {
// Handle timeout and other execution errors
if (error.code === 'ETIMEDOUT') {
throw new McpError(ErrorCode.InternalError, `Command timed out after ${defaultOptions.timeout}ms: ${command}`);
}
return {
stdout: error.stdout?.trim() || '',
stderr: error.stderr?.trim() || error.message || '',
code: error.code || 1,
};
}
}
export function executeCommandSync(command) {
try {
const stdout = execSync(command, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
return {
stdout: stdout.trim(),
stderr: '',
code: 0,
};
}
catch (error) {
return {
stdout: error.stdout?.trim() || '',
stderr: error.stderr?.trim() || error.message || '',
code: error.status || 1,
};
}
}
export function buildXcodebuildCommand(action, projectPath, options = {}) {
const parts = ['xcodebuild'];
// Add project or workspace
if (options.workspace || projectPath.endsWith('.xcworkspace')) {
parts.push('-workspace', `"${projectPath}"`);
}
else {
parts.push('-project', `"${projectPath}"`);
}
// Add scheme if provided
if (options.scheme) {
parts.push('-scheme', `"${options.scheme}"`);
}
// Add configuration if provided
if (options.configuration) {
parts.push('-configuration', options.configuration);
}
// Add destination if provided
if (options.destination) {
parts.push('-destination', `"${options.destination}"`);
}
// Add SDK if provided
if (options.sdk) {
parts.push('-sdk', options.sdk);
}
// Add derived data path if provided
if (options.derivedDataPath) {
parts.push('-derivedDataPath', `"${options.derivedDataPath}"`);
}
// Add JSON flag if requested
if (options.json) {
parts.push('-json');
}
// Add action (build, clean, archive, etc.)
if (action) {
parts.push(action);
}
return parts.join(' ');
}
export function buildSimctlCommand(action, options = {}) {
const parts = ['xcrun', 'simctl'];
// Add action
parts.push(action);
// Add JSON flag if requested and supported
if (options.json && ['list'].includes(action)) {
parts.push('-j');
}
// Add device ID for device-specific actions
if (options.deviceId && ['boot', 'shutdown', 'delete'].includes(action)) {
parts.push(options.deviceId);
}
// Add device creation parameters
if (action === 'create' && options.name && options.deviceType && options.runtime) {
parts.push(`"${options.name}"`, options.deviceType, options.runtime);
}
return parts.join(' ');
}
//# sourceMappingURL=command.js.map