#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
ListResourcesRequestSchema,
ReadResourceRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { fileURLToPath } from "url";
import { XcodeProjectManager } from "./xcode-project-manager.js";
class XcodeMCPServer {
constructor() {
this.server = new Server(
{
name: "xcode-mcp-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
resources: {},
},
}
);
this.setupHandlers();
this.projectManagers = new Map();
}
setupHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "xcode_read_project",
description: "Read and parse an Xcode project file (.xcodeproj)",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_list_schemes",
description: "List all build schemes in an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_list_targets",
description: "List all targets in an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_add_test_target",
description: "Add a test target to an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
targetName: {
type: "string",
description: "Name for the test target (optional, defaults to ProjectNameTests)",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_add_file_to_target",
description: "Add a file to an Xcode project target",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
filePath: {
type: "string",
description: "Relative path to the file from project root",
},
targetName: {
type: "string",
description: "Name of the target to add the file to",
},
},
required: ["projectPath", "filePath", "targetName"],
},
},
{
name: "xcode_build",
description: "Build an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
scheme: {
type: "string",
description: "Build scheme name (optional)",
},
configuration: {
type: "string",
description: "Build configuration: Debug or Release (default: Debug)",
enum: ["Debug", "Release"],
default: "Debug",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_test",
description: "Run tests in an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
scheme: {
type: "string",
description: "Test scheme name (optional)",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_get_build_settings",
description: "Get build settings for a project or scheme",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
scheme: {
type: "string",
description: "Scheme name (optional)",
},
configuration: {
type: "string",
description: "Build configuration: Debug or Release (default: Debug)",
enum: ["Debug", "Release"],
default: "Debug",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_clean",
description: "Clean build artifacts from an Xcode project",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
scheme: {
type: "string",
description: "Build scheme name (optional)",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_configure_test_target",
description: "Configure a test target for an Xcode project. Ensures test directory exists and creates necessary pbxproj entries.",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
targetName: {
type: "string",
description: "Name for the test target (optional, defaults to ProjectNameTests)",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_remove_file_from_target",
description: "Remove a file from an Xcode project target",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
filePath: {
type: "string",
description: "Relative path to the file from project root",
},
targetName: {
type: "string",
description: "Name of the target to remove the file from",
},
},
required: ["projectPath", "filePath", "targetName"],
},
},
{
name: "xcode_get_build_errors",
description: "Get build errors from the last build attempt",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
scheme: {
type: "string",
description: "Build scheme name (optional)",
},
},
required: ["projectPath"],
},
},
{
name: "xcode_list_files_in_target",
description: "List all files in a specific target",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
targetName: {
type: "string",
description: "Name of the target",
},
},
required: ["projectPath", "targetName"],
},
},
{
name: "xcode_validate_project",
description: "Validate Xcode project structure and check for common issues",
inputSchema: {
type: "object",
properties: {
projectPath: {
type: "string",
description: "Path to the .xcodeproj file",
},
},
required: ["projectPath"],
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case "xcode_read_project": {
const manager = new XcodeProjectManager(args.projectPath);
const project = await manager.readProject();
return {
content: [
{
type: "text",
text: JSON.stringify(project, null, 2),
},
],
};
}
case "xcode_list_schemes": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.listSchemes();
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_list_targets": {
const manager = new XcodeProjectManager(args.projectPath);
const project = await manager.readProject();
return {
content: [
{
type: "text",
text: JSON.stringify({ targets: project.targets }, null, 2),
},
],
};
}
case "xcode_add_test_target": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.addTestTarget(args.targetName);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_add_file_to_target": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.addFileToTarget(
args.filePath,
args.targetName
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_build": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.build(args.scheme, args.configuration);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_test": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.test(args.scheme);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_get_build_settings": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.getBuildSettings(
args.scheme,
args.configuration
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_clean": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.clean(args.scheme);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_configure_test_target": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.configureTestTarget(args.targetName);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_remove_file_from_target": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.removeFileFromTarget(
args.filePath,
args.targetName
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_get_build_errors": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.getBuildErrors(args.scheme);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_list_files_in_target": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.listFilesInTarget(args.targetName);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "xcode_validate_project": {
const manager = new XcodeProjectManager(args.projectPath);
const result = await manager.validateProject();
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
return {
content: [
{
type: "text",
text: JSON.stringify(
{
error: error.message,
stack: error.stack,
},
null,
2
),
},
],
isError: true,
};
}
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error("Xcode MCP server running on stdio");
}
}
const server = new XcodeMCPServer();
server.run().catch(console.error);