import { z } from 'zod'; import { FileManager } from '../utils/file-manager.js'; import { DesignData } from '../types.js'; import { VisualFormatter } from '../utils/visual-formatter.js'; const createDesignSchema = z.object({ action: z.literal('create'), projectPath: z.string().min(1, 'projectPath is required and must be absolute'), title: z.string().min(1, 'title is required'), description: z.string().min(1, 'description is required'), architecture: z.string().min(1, 'architecture is required'), components: z.array(z.object({ name: z.string(), purpose: z.string(), technology: z.string(), responsibilities: z.array(z.string()) })), dataModels: z.array(z.object({ name: z.string(), fields: z.array(z.object({ name: z.string(), type: z.string() })) })), apiDesign: z.object({ endpoints: z.array(z.object({ method: z.string(), path: z.string(), description: z.string() })) }), securityConsiderations: z.array(z.string()), testingStrategy: z.object({ unit: z.array(z.string()).optional(), integration: z.array(z.string()).optional(), e2e: z.array(z.string()).optional() }) }); const createBatchDesignSchema = z.object({ action: z.literal('createBatch'), projectPath: z.string().min(1, 'projectPath is required and must be absolute'), designs: z.array(z.object({ title: z.string(), description: z.string(), architecture: z.string(), components: z.array(z.object({ name: z.string(), purpose: z.string(), technology: z.string(), responsibilities: z.array(z.string()) })), dataModels: z.array(z.object({ name: z.string(), fields: z.array(z.object({ name: z.string(), type: z.string() })) })), apiDesign: z.object({ endpoints: z.array(z.object({ method: z.string(), path: z.string(), description: z.string() })) }), securityConsiderations: z.array(z.string()), testingStrategy: z.object({ unit: z.array(z.string()).optional(), integration: z.array(z.string()).optional(), e2e: z.array(z.string()).optional() }) })) }); const updateDesignSchema = z.object({ action: z.literal('update'), projectPath: z.string().min(1, 'projectPath is required and must be absolute'), content: z.string().min(1, 'content is required') }); const validateDesignSchema = z.object({ action: z.literal('validate'), projectPath: z.string().min(1, 'projectPath is required and must be absolute') }); const designSchema = z.union([ createDesignSchema, createBatchDesignSchema, updateDesignSchema, validateDesignSchema ]); export async function handleSpecDesign(args: any) { try { const validatedArgs = designSchema.parse(args); const { action, projectPath } = validatedArgs; const fileManager = new FileManager(projectPath); switch (action) { case 'create': { const { title, description, architecture, components, dataModels, apiDesign, securityConsiderations, testingStrategy } = validatedArgs; const designContent = generateDesignFromParams({ title, description, architecture, components, dataModels, apiDesign, securityConsiderations, testingStrategy: { unit: testingStrategy.unit || [], integration: testingStrategy.integration || [], e2e: testingStrategy.e2e || [] } }); await fileManager.writeFile('design.md', designContent); return { content: [{ type: "text", text: `✅ Design created successfully!\n\nTitle: ${title}\nDescription: ${description}\nComponents: ${components.length}\nData Models: ${dataModels.length}\nAPI Endpoints: ${apiDesign.endpoints.length}` }] }; } case 'createBatch': { const { designs } = validatedArgs; // Combine all designs into a single file const combinedContent = designs.map((design, index) => generateDesignFromParams(design) ).join('\n\n---\n\n'); await fileManager.writeFile('design.md', combinedContent); return { content: [{ type: "text", text: `✅ Batch design created successfully!\n\nCombined ${designs.length} design specifications into design.md` }] }; } case 'update': { const { content } = validatedArgs; await fileManager.writeFile('design.md', content); return { content: [{ type: "text", text: `✅ Design updated successfully!` }] }; } case 'validate': { try { const content = await fileManager.readFile('design.md'); const validation = validateDesignConsistency(content); return { content: [{ type: "text", text: validation.isValid ? `✅ Design validation passed!\n\nAll sections are properly structured.` : `❌ Design validation failed!\n\nErrors:\n${validation.errors.join('\n')}` }] }; } catch (error) { return { content: [{ type: "text", text: "❌ No design.md file found to validate" }] }; } } default: throw new Error(`Unknown action: ${action}`); } } catch (error: any) { return { content: [{ type: "text", text: `❌ Error in spec-design: ${error.message}` }] }; } } function generateDesignFromParams(params: DesignData): string { const { title, description, architecture, components, dataModels, apiDesign, securityConsiderations, testingStrategy } = params; let result = `# ${title}\n\n`; // Overview section result += VisualFormatter.createBoxHeader('OVERVIEW', '📋') + '\n\n'; result += `${description}\n\n`; // Architecture section result += VisualFormatter.createBoxHeader('ARCHITECTURE', '🏗️') + '\n\n'; result += `${architecture}\n\n`; // Components section result += VisualFormatter.createBoxHeader('COMPONENTS', '🧩') + '\n\n'; const componentItems = components.map(comp => ({ title: comp.name, items: [ `Purpose: ${comp.purpose}`, `Technology: ${comp.technology}`, 'Responsibilities:', ...comp.responsibilities.map(resp => ` • ${resp}`) ] })); result += VisualFormatter.createHierarchicalList(componentItems) + '\n'; // Data Models section result += VisualFormatter.createBoxHeader('DATA MODELS', '🗄️') + '\n\n'; dataModels.forEach(model => { result += VisualFormatter.createDataModel(model.name, model.fields) + '\n\n'; }); // API Design section result += VisualFormatter.createBoxHeader('API DESIGN', '🔌') + '\n\n'; result += VisualFormatter.createAPIEndpoints(apiDesign.endpoints) + '\n'; // Security Considerations section result += VisualFormatter.createBoxHeader('SECURITY CONSIDERATIONS', '🔒') + '\n\n'; const securityItems = securityConsiderations.map(consideration => ({ title: consideration, items: [] })); result += VisualFormatter.createHierarchicalList(securityItems) + '\n'; // Testing Strategy section result += VisualFormatter.createBoxHeader('TESTING STRATEGY', '🧪') + '\n\n'; const testingItems = [ { title: 'Unit Testing', items: testingStrategy.unit || ['To be defined'] }, { title: 'Integration Testing', items: testingStrategy.integration || ['To be defined'] }, { title: 'End-to-End Testing', items: testingStrategy.e2e || ['To be defined'] } ]; result += VisualFormatter.createHierarchicalList(testingItems) + '\n'; result += '\n' + VisualFormatter.createSectionSeparator() + '\n'; result += '*Generated using spec-mcp-tools*'; return result; } function validateDesignConsistency(content: string): { isValid: boolean; errors: string[] } { const errors: string[] = []; // Check for required sections const requiredSections = ['# ', '## 📋 Overview', '## 🏗️ Architecture', '## 🧩 Components']; for (const section of requiredSections) { if (!content.includes(section)) { errors.push(`Missing required section: ${section}`); } } return { isValid: errors.length === 0, errors }; } // Export the tool function export const specDesignTool = { name: "spec-design", description: "Create and manage technical design.md", inputSchema: { type: "object", properties: { action: { type: "string", enum: ["create", "createBatch", "update", "validate"], description: "Action: create from parameters, create multiple designs (batch), update design, or validate consistency" }, projectPath: { type: "string", description: "Absolute path to the project directory (must be full path, not relative)" }, title: { type: "string", description: "Design title (for single create)" }, description: { type: "string", description: "Design overview/description (for single create)" }, architecture: { type: "string", description: "High-level architecture description (for single create)" }, components: { type: "array", items: { type: "object", properties: { name: { type: "string" }, purpose: { type: "string" }, technology: { type: "string" }, responsibilities: { type: "array", items: { type: "string" } } } }, description: "Array of system components (for single create)" }, dataModels: { type: "array", items: { type: "object", properties: { name: { type: "string" }, fields: { type: "array", items: { type: "object", properties: { name: { type: "string" }, type: { type: "string" } } } } } }, description: "Array of data models with fields (for single create)" }, apiDesign: { type: "object", description: "API design specifications (for single create)", properties: { endpoints: { type: "array", items: { type: "object", properties: { method: { type: "string" }, path: { type: "string" }, description: { type: "string" } } } } } }, securityConsiderations: { type: "array", items: { type: "string" }, description: "Array of security considerations (for single create)" }, testingStrategy: { type: "object", description: "Testing strategy by type (for single create)", properties: { unit: { type: "array", items: { type: "string" } }, integration: { type: "array", items: { type: "string" } }, e2e: { type: "array", items: { type: "string" } } } }, designs: { type: "array", items: { type: "object", properties: { title: { type: "string", description: "Design title" }, description: { type: "string", description: "Design overview" }, architecture: { type: "string", description: "Architecture description" }, components: { type: "array", items: { type: "object", properties: { name: { type: "string" }, purpose: { type: "string" }, technology: { type: "string" }, responsibilities: { type: "array", items: { type: "string" } } } }, description: "System components" }, dataModels: { type: "array", items: { type: "object", properties: { name: { type: "string" }, fields: { type: "array", items: { type: "object", properties: { name: { type: "string" }, type: { type: "string" } } } } } }, description: "Data models" }, apiDesign: { type: "object", description: "API design", properties: { endpoints: { type: "array", items: { type: "object", properties: { method: { type: "string" }, path: { type: "string" }, description: { type: "string" } } } } } }, securityConsiderations: { type: "array", items: { type: "string" }, description: "Security considerations" }, testingStrategy: { type: "object", description: "Testing strategy", properties: { unit: { type: "array", items: { type: "string" } }, integration: { type: "array", items: { type: "string" } }, e2e: { type: "array", items: { type: "string" } } } } }, required: ["title"] }, description: "Array of designs to create (for createBatch)" }, content: { type: "string", description: "Updated content (for update)" } }, required: ["action", "projectPath"] }, handler: handleSpecDesign };