@claudetools/tools 0.3.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ // =============================================================================
2
+ // CodeDNA MCP Tool Handlers
3
+ // =============================================================================
4
+ //
5
+ // Handlers for code generation tools that save 95-99% tokens by generating
6
+ // production code from compact Entity DSL specifications.
7
+ //
8
+ import { validateSpec } from '../codedna/parser.js';
9
+ import { TemplateRegistry } from '../codedna/registry.js';
10
+ import { ExpressApiGenerator } from '../codedna/generators/express-api.js';
11
+ // Singleton registry instance
12
+ const registry = new TemplateRegistry();
13
+ /**
14
+ * Handle codedna_generate_api tool call
15
+ */
16
+ export async function handleGenerateApi(args) {
17
+ const { spec, framework, options = {} } = args;
18
+ // Validate Entity DSL specification
19
+ const validation = validateSpec(spec);
20
+ if (!validation.valid) {
21
+ return {
22
+ error: 'Invalid entity specification',
23
+ details: validation.errors,
24
+ };
25
+ }
26
+ // Select generator based on framework
27
+ let generator;
28
+ if (framework === 'express') {
29
+ generator = new ExpressApiGenerator(registry);
30
+ }
31
+ else if (framework === 'fastapi') {
32
+ return {
33
+ error: 'FastAPI generator not yet implemented',
34
+ message: 'Currently only Express is supported. Coming soon: FastAPI, NestJS',
35
+ };
36
+ }
37
+ else if (framework === 'nestjs') {
38
+ return {
39
+ error: 'NestJS generator not yet implemented',
40
+ message: 'Currently only Express is supported. Coming soon: FastAPI, NestJS',
41
+ };
42
+ }
43
+ else {
44
+ return {
45
+ error: `Unsupported framework: ${framework}`,
46
+ supported: ['express', 'fastapi', 'nestjs'],
47
+ };
48
+ }
49
+ try {
50
+ // Generate code
51
+ const result = await generator.generate(validation.parsed, options);
52
+ return {
53
+ success: true,
54
+ files: result.files,
55
+ metadata: result.metadata,
56
+ tokenSavings: {
57
+ traditional: result.metadata.linesOfCode * 25, // ~25 tokens per line
58
+ codedna: 150, // ~150 tokens for MCP call
59
+ saved: result.metadata.estimatedTokensSaved,
60
+ percentSaved: ((result.metadata.estimatedTokensSaved / (result.metadata.linesOfCode * 25)) * 100).toFixed(1) + '%',
61
+ },
62
+ };
63
+ }
64
+ catch (error) {
65
+ return {
66
+ error: 'Code generation failed',
67
+ message: error instanceof Error ? error.message : String(error),
68
+ };
69
+ }
70
+ }
71
+ /**
72
+ * Handle codedna_generate_frontend tool call
73
+ */
74
+ export async function handleGenerateFrontend(args) {
75
+ const { spec, framework, options = {} } = args;
76
+ // Validate Entity DSL specification
77
+ const validation = validateSpec(spec);
78
+ if (!validation.valid) {
79
+ return {
80
+ error: 'Invalid entity specification',
81
+ details: validation.errors,
82
+ };
83
+ }
84
+ // Frontend generators not yet implemented
85
+ return {
86
+ error: 'Frontend generators not yet implemented',
87
+ message: 'Coming soon: Next.js, React, Vue frontend generation',
88
+ frameworks: ['nextjs', 'react', 'vue'],
89
+ status: 'planned',
90
+ };
91
+ }
92
+ /**
93
+ * Handle codedna_generate_component tool call
94
+ */
95
+ export async function handleGenerateComponent(args) {
96
+ const { spec, type, framework, options = {} } = args;
97
+ // Validate Entity DSL specification
98
+ const validation = validateSpec(spec);
99
+ if (!validation.valid) {
100
+ return {
101
+ error: 'Invalid entity specification',
102
+ details: validation.errors,
103
+ };
104
+ }
105
+ // Component generators not yet implemented
106
+ return {
107
+ error: 'Component generators not yet implemented',
108
+ message: 'Coming soon: Form, Table, Card, Modal components',
109
+ types: ['form', 'table', 'card', 'modal'],
110
+ frameworks: ['react', 'vue', 'svelte'],
111
+ status: 'planned',
112
+ };
113
+ }
114
+ /**
115
+ * Handle codedna_list_generators tool call
116
+ */
117
+ export async function handleListGenerators() {
118
+ try {
119
+ const generators = await registry.listGenerators();
120
+ return {
121
+ generators,
122
+ summary: {
123
+ total: generators.length,
124
+ byFramework: generators.reduce((acc, gen) => {
125
+ acc[gen.framework] = (acc[gen.framework] || 0) + 1;
126
+ return acc;
127
+ }, {}),
128
+ },
129
+ };
130
+ }
131
+ catch (error) {
132
+ return {
133
+ error: 'Failed to list generators',
134
+ message: error instanceof Error ? error.message : String(error),
135
+ };
136
+ }
137
+ }
138
+ /**
139
+ * Handle codedna_validate_spec tool call
140
+ */
141
+ export async function handleValidateSpec(args) {
142
+ const { spec } = args;
143
+ const validation = validateSpec(spec);
144
+ if (validation.valid && validation.parsed) {
145
+ return {
146
+ valid: true,
147
+ entity: {
148
+ name: validation.parsed.name,
149
+ fields: validation.parsed.fields.map(f => ({
150
+ name: f.name,
151
+ type: f.type,
152
+ constraints: f.constraints.map(c => c.kind),
153
+ })),
154
+ },
155
+ summary: {
156
+ totalFields: validation.parsed.fields.length,
157
+ hasReferences: validation.parsed.fields.some(f => f.type.kind === 'reference'),
158
+ hasEnums: validation.parsed.fields.some(f => f.type.kind === 'enum'),
159
+ hasConstraints: validation.parsed.fields.some(f => f.constraints.length > 0),
160
+ },
161
+ };
162
+ }
163
+ return {
164
+ valid: false,
165
+ errors: validation.errors,
166
+ };
167
+ }