import { Project, RegisterProjectArgs, MCPError, ErrorCode } from '../models/types.js';
import { ProjectManager } from '../services/project_manager.js';
import { join } from 'path';
// Helper to generate a simple ID from name
function generateId(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
}
export async function registerProject(args: RegisterProjectArgs): Promise<{
success: boolean;
projectId: string;
message: string;
}> {
const projectManager = new ProjectManager();
// 1. Validate arguments
if (!args.name || !args.rootPath) {
throw new MCPError(
ErrorCode.INVALID_ARGUMENTS,
'Name and rootPath are required'
);
}
// 3. Validate path exists
const exists = await projectManager.validateProjectPath(args.rootPath);
if (!exists) {
throw new MCPError(
ErrorCode.INVALID_PATH,
`Project path does not exist or is not a directory: ${args.rootPath}`,
"Please create the directory first or check the path."
);
}
// 4. Generate ID if not provided
const id = args.id || generateId(args.name);
if (!id) {
throw new MCPError(ErrorCode.INVALID_ARGUMENTS, "Could not generate valid ID from name");
}
const now = new Date().toISOString();
const project: Project = {
id,
name: args.name,
rootPath: args.rootPath,
specPaths: args.specPaths || ['docs/', 'specs/'],
claudeStatePath: '.claude/',
active: true,
created: now,
lastAccessed: now
};
// 2. Check if project already exists (handled inside addProject)
// 5. Save to projects.json
await projectManager.addProject(project);
// 6. Return success
return {
success: true,
projectId: id,
message: `Project '${args.name}' registered successfully at ${args.rootPath}`
};
}