@decantr/mcp-server 1.0.0-beta.8 → 1.0.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.
@@ -1,1166 +0,0 @@
1
- // src/index.ts
2
- import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
- import {
5
- ListToolsRequestSchema,
6
- CallToolRequestSchema
7
- } from "@modelcontextprotocol/sdk/types.js";
8
-
9
- // src/tools.ts
10
- import { existsSync, readFileSync } from "fs";
11
- import { readFile as readFile2 } from "fs/promises";
12
- import { join as join2, dirname as dirname2 } from "path";
13
- import { validateEssence, evaluateGuard, isV3 as isV32 } from "@decantr/essence-spec";
14
- import { resolvePatternPreset } from "@decantr/registry";
15
-
16
- // src/helpers.ts
17
- import { readFile, writeFile, mkdir } from "fs/promises";
18
- import { join, dirname } from "path";
19
- import { RegistryAPIClient } from "@decantr/registry";
20
- import { isV3, migrateV2ToV3 } from "@decantr/essence-spec";
21
- var MAX_INPUT_LENGTH = 1e3;
22
- function validateStringArg(args, field) {
23
- const val = args[field];
24
- if (!val || typeof val !== "string") {
25
- return `Required parameter "${field}" must be a non-empty string.`;
26
- }
27
- if (val.length > MAX_INPUT_LENGTH) {
28
- return `Parameter "${field}" exceeds maximum length of ${MAX_INPUT_LENGTH} characters.`;
29
- }
30
- return null;
31
- }
32
- function fuzzyScore(query, text) {
33
- const q = query.toLowerCase();
34
- const t = text.toLowerCase();
35
- if (t === q) return 100;
36
- if (t.startsWith(q)) return 90;
37
- if (t.includes(q)) return 80;
38
- let qi = 0;
39
- for (let ti = 0; ti < t.length && qi < q.length; ti++) {
40
- if (t[ti] === q[qi]) qi++;
41
- }
42
- return qi === q.length ? 60 : 0;
43
- }
44
- var _apiClient = null;
45
- function getAPIClient() {
46
- if (!_apiClient) {
47
- _apiClient = new RegistryAPIClient({
48
- baseUrl: process.env.DECANTR_API_URL || void 0,
49
- apiKey: process.env.DECANTR_API_KEY || void 0
50
- });
51
- }
52
- return _apiClient;
53
- }
54
- async function readEssenceFile(essencePath) {
55
- const resolvedPath = essencePath || join(process.cwd(), "decantr.essence.json");
56
- const raw = await readFile(resolvedPath, "utf-8");
57
- const essence = JSON.parse(raw);
58
- return { essence, raw, path: resolvedPath };
59
- }
60
- async function writeEssenceFile(essencePath, essence) {
61
- const dir = dirname(essencePath);
62
- await mkdir(dir, { recursive: true });
63
- await writeFile(essencePath, JSON.stringify(essence, null, 2) + "\n", "utf-8");
64
- }
65
- async function mutateEssenceFile(essencePath, mutate) {
66
- const { essence, path } = await readEssenceFile(essencePath);
67
- const v3 = isV3(essence) ? structuredClone(essence) : migrateV2ToV3(essence);
68
- const updated = mutate(v3);
69
- await writeEssenceFile(path, updated);
70
- return { essence: updated, path };
71
- }
72
- async function readDriftLog(projectRoot) {
73
- const root = projectRoot || process.cwd();
74
- const logPath = join(root, ".decantr", "drift-log.json");
75
- try {
76
- const raw = await readFile(logPath, "utf-8");
77
- return JSON.parse(raw);
78
- } catch {
79
- return [];
80
- }
81
- }
82
- async function writeDriftLog(entries, projectRoot) {
83
- const root = projectRoot || process.cwd();
84
- const logPath = join(root, ".decantr", "drift-log.json");
85
- await mkdir(dirname(logPath), { recursive: true });
86
- await writeFile(logPath, JSON.stringify(entries, null, 2) + "\n", "utf-8");
87
- return logPath;
88
- }
89
-
90
- // src/tools.ts
91
- var ZONE_ORDER = ["public", "gateway", "primary", "auxiliary"];
92
- function deriveZones(inputs) {
93
- const zoneMap = /* @__PURE__ */ new Map();
94
- for (const input of inputs) {
95
- const existing = zoneMap.get(input.role);
96
- if (existing) {
97
- existing.archetypes.push(input.archetypeId);
98
- existing.features.push(...input.features);
99
- existing.descriptions.push(input.description);
100
- } else {
101
- zoneMap.set(input.role, {
102
- role: input.role,
103
- archetypes: [input.archetypeId],
104
- shell: input.shell,
105
- features: [...input.features],
106
- descriptions: [input.description]
107
- });
108
- }
109
- }
110
- for (const zone of zoneMap.values()) {
111
- zone.features = [...new Set(zone.features)];
112
- }
113
- return ZONE_ORDER.filter((role) => zoneMap.has(role)).map((role) => zoneMap.get(role));
114
- }
115
- var GATEWAY_TRIGGER_MAP = {
116
- auth: "authentication",
117
- login: "authentication",
118
- mfa: "authentication",
119
- payment: "payment",
120
- subscription: "payment",
121
- checkout: "payment",
122
- onboarding: "onboarding",
123
- "setup-wizard": "onboarding",
124
- welcome: "onboarding",
125
- invite: "invitation",
126
- "access-code": "invitation"
127
- };
128
- function resolveGatewayTrigger(features) {
129
- for (const feature of features) {
130
- const trigger = GATEWAY_TRIGGER_MAP[feature];
131
- if (trigger) return trigger;
132
- }
133
- return "authentication";
134
- }
135
- function deriveTransitions(zones) {
136
- const transitions = [];
137
- const roles = new Set(zones.map((z) => z.role));
138
- const gateway = zones.find((z) => z.role === "gateway");
139
- const gatewayTrigger = gateway ? resolveGatewayTrigger(gateway.features) : "authentication";
140
- const hasApp = roles.has("primary") || roles.has("auxiliary");
141
- const hasGateway = roles.has("gateway");
142
- const hasPublic = roles.has("public");
143
- if (hasPublic && hasGateway) {
144
- transitions.push({ from: "public", to: "gateway", type: "conversion", trigger: gatewayTrigger });
145
- }
146
- if (hasPublic && hasApp && !hasGateway) {
147
- transitions.push({ from: "public", to: "app", type: "conversion", trigger: "navigation" });
148
- }
149
- if (hasGateway && hasApp) {
150
- transitions.push({ from: "gateway", to: "app", type: "gate-pass", trigger: gatewayTrigger });
151
- transitions.push({ from: "app", to: "gateway", type: "gate-return", trigger: gatewayTrigger });
152
- }
153
- if (hasApp && hasPublic) {
154
- transitions.push({ from: "app", to: "public", type: "navigation", trigger: "external" });
155
- }
156
- return transitions;
157
- }
158
- var READ_ONLY = {
159
- readOnlyHint: true,
160
- destructiveHint: false,
161
- idempotentHint: true,
162
- openWorldHint: false
163
- };
164
- var READ_ONLY_NETWORK = {
165
- readOnlyHint: true,
166
- destructiveHint: false,
167
- idempotentHint: true,
168
- openWorldHint: true
169
- };
170
- var WRITE_TOOL = {
171
- readOnlyHint: false,
172
- destructiveHint: false,
173
- idempotentHint: false,
174
- openWorldHint: false
175
- };
176
- var TOOLS = [
177
- // 1. decantr_read_essence — local read
178
- {
179
- name: "decantr_read_essence",
180
- title: "Read Essence",
181
- description: "Read and return the current decantr.essence.json file from the working directory. For v3 files, optionally filter by layer (dna, blueprint, or full).",
182
- inputSchema: {
183
- type: "object",
184
- properties: {
185
- path: { type: "string", description: "Optional path to essence file. Defaults to ./decantr.essence.json." },
186
- layer: { type: "string", enum: ["dna", "blueprint", "full"], description: "For v3 essences: return only the specified layer. Defaults to full." }
187
- }
188
- },
189
- annotations: READ_ONLY
190
- },
191
- // 2. decantr_validate — local read
192
- {
193
- name: "decantr_validate",
194
- title: "Validate Essence",
195
- description: "Validate a decantr.essence.json file against the schema and guard rules. For v3, reports DNA vs Blueprint violations separately.",
196
- inputSchema: {
197
- type: "object",
198
- properties: {
199
- path: { type: "string", description: "Path to essence file. Defaults to ./decantr.essence.json." }
200
- }
201
- },
202
- annotations: READ_ONLY
203
- },
204
- // 3. decantr_search_registry — network
205
- {
206
- name: "decantr_search_registry",
207
- title: "Search Registry",
208
- description: "Search the Decantr community content registry for patterns, archetypes, recipes, and styles.",
209
- inputSchema: {
210
- type: "object",
211
- properties: {
212
- query: { type: "string", description: 'Search query (e.g. "kanban", "neon", "dashboard")' },
213
- type: { type: "string", description: "Filter by type: pattern, archetype, recipe, style" }
214
- },
215
- required: ["query"]
216
- },
217
- annotations: READ_ONLY_NETWORK
218
- },
219
- // 4. decantr_resolve_pattern — network
220
- {
221
- name: "decantr_resolve_pattern",
222
- title: "Resolve Pattern",
223
- description: "Get full pattern details including layout spec, components, presets, and code examples.",
224
- inputSchema: {
225
- type: "object",
226
- properties: {
227
- id: { type: "string", description: 'Pattern ID (e.g. "hero", "data-table", "kpi-grid")' },
228
- preset: { type: "string", description: 'Optional preset name (e.g. "product", "content")' },
229
- namespace: { type: "string", description: 'Namespace (default: "@official")' }
230
- },
231
- required: ["id"]
232
- },
233
- annotations: READ_ONLY_NETWORK
234
- },
235
- // 5. decantr_resolve_archetype — network
236
- {
237
- name: "decantr_resolve_archetype",
238
- title: "Resolve Archetype",
239
- description: "Get archetype details including default pages, layouts, features, and suggested theme.",
240
- inputSchema: {
241
- type: "object",
242
- properties: {
243
- id: { type: "string", description: 'Archetype ID (e.g. "saas-dashboard", "ecommerce")' },
244
- namespace: { type: "string", description: 'Namespace (default: "@official")' }
245
- },
246
- required: ["id"]
247
- },
248
- annotations: READ_ONLY_NETWORK
249
- },
250
- // 6. decantr_resolve_recipe — network
251
- {
252
- name: "decantr_resolve_recipe",
253
- title: "Resolve Recipe",
254
- description: "Get recipe decoration rules including shell styles, spatial hints, visual effects, and pattern preferences.",
255
- inputSchema: {
256
- type: "object",
257
- properties: {
258
- id: { type: "string", description: 'Recipe ID (e.g. "auradecantism")' },
259
- namespace: { type: "string", description: 'Namespace (default: "@official")' }
260
- },
261
- required: ["id"]
262
- },
263
- annotations: READ_ONLY_NETWORK
264
- },
265
- // 7. decantr_resolve_blueprint — network
266
- {
267
- name: "decantr_resolve_blueprint",
268
- title: "Resolve Blueprint",
269
- description: "Get a blueprint (app composition) with its archetype list, suggested theme, personality traits, and full page structure.",
270
- inputSchema: {
271
- type: "object",
272
- properties: {
273
- id: { type: "string", description: 'Blueprint ID (e.g. "saas-dashboard", "ecommerce", "portfolio")' },
274
- namespace: { type: "string", description: 'Namespace (default: "@official")' }
275
- },
276
- required: ["id"]
277
- },
278
- annotations: READ_ONLY_NETWORK
279
- },
280
- // 8. decantr_suggest_patterns — network
281
- {
282
- name: "decantr_suggest_patterns",
283
- title: "Suggest Patterns",
284
- description: "Given a page description, suggest appropriate patterns from the registry. Returns ranked pattern matches with layout specs and component lists.",
285
- inputSchema: {
286
- type: "object",
287
- properties: {
288
- description: { type: "string", description: 'Description of the page or section (e.g. "dashboard with metrics and charts", "settings form with toggles")' }
289
- },
290
- required: ["description"]
291
- },
292
- annotations: READ_ONLY_NETWORK
293
- },
294
- // 9. decantr_check_drift — local read
295
- {
296
- name: "decantr_check_drift",
297
- title: "Check Drift",
298
- description: "Check if code changes violate the design intent captured in the Essence spec. For v3, returns separate dna_violations and blueprint_drift with autoFixable flags.",
299
- inputSchema: {
300
- type: "object",
301
- properties: {
302
- path: { type: "string", description: "Path to essence file. Defaults to ./decantr.essence.json." },
303
- page_id: { type: "string", description: 'Page ID being modified (e.g. "overview", "settings")' },
304
- components_used: {
305
- type: "array",
306
- items: { type: "string" },
307
- description: "List of component names used in the generated code. Checked against page layout patterns."
308
- },
309
- theme_used: { type: "string", description: "Theme/style name used in the generated code" }
310
- }
311
- },
312
- annotations: READ_ONLY
313
- },
314
- // 10. decantr_create_essence — network (fetches archetype)
315
- {
316
- name: "decantr_create_essence",
317
- title: "Create Essence",
318
- description: "Generate a valid v3 Essence spec skeleton from a project description. Returns a structured essence.json template based on the closest matching archetype and blueprint.",
319
- inputSchema: {
320
- type: "object",
321
- properties: {
322
- description: { type: "string", description: 'Natural language project description (e.g. "SaaS dashboard with analytics, user management, and billing")' },
323
- framework: { type: "string", description: 'Target framework (e.g. "react", "vue", "svelte"). Defaults to "react".' }
324
- },
325
- required: ["description"]
326
- },
327
- annotations: READ_ONLY_NETWORK
328
- },
329
- // 11. decantr_accept_drift — WRITE tool (NEW)
330
- {
331
- name: "decantr_accept_drift",
332
- title: "Accept Drift",
333
- description: "Resolve guard violations by accepting, scoping, rejecting, or deferring drift. For DNA violations, requires explicit confirmation. Updates the essence file or drift log.",
334
- inputSchema: {
335
- type: "object",
336
- properties: {
337
- violations: {
338
- type: "array",
339
- items: {
340
- type: "object",
341
- properties: {
342
- rule: { type: "string" },
343
- page_id: { type: "string" },
344
- details: { type: "string" }
345
- },
346
- required: ["rule"]
347
- },
348
- description: "The violations to resolve."
349
- },
350
- resolution: {
351
- type: "string",
352
- enum: ["accept", "accept_scoped", "reject", "defer"],
353
- description: "How to resolve: accept updates the essence, accept_scoped limits to a page, reject is a no-op, defer logs for later."
354
- },
355
- scope: { type: "string", description: "For accept_scoped: the page or section scope." },
356
- path: { type: "string", description: "Path to essence file. Defaults to ./decantr.essence.json." },
357
- confirm_dna: { type: "boolean", description: "Required to be true when accepting DNA-layer violations." }
358
- },
359
- required: ["violations", "resolution"]
360
- },
361
- annotations: WRITE_TOOL
362
- },
363
- // 12. decantr_update_essence — WRITE tool (NEW)
364
- {
365
- name: "decantr_update_essence",
366
- title: "Update Essence",
367
- description: "Mutate the essence file: add/remove/update pages, update DNA or blueprint fields, add/remove features. Operates on v3 format (auto-migrates v2).",
368
- inputSchema: {
369
- type: "object",
370
- properties: {
371
- operation: {
372
- type: "string",
373
- enum: ["add_page", "remove_page", "update_page_layout", "update_dna", "update_blueprint", "add_feature", "remove_feature"],
374
- description: "The mutation operation to perform."
375
- },
376
- payload: {
377
- type: "object",
378
- description: "Operation-specific payload. See tool docs for each operation."
379
- },
380
- path: { type: "string", description: "Path to essence file. Defaults to ./decantr.essence.json." }
381
- },
382
- required: ["operation", "payload"]
383
- },
384
- annotations: WRITE_TOOL
385
- },
386
- // 13. decantr_get_section_context — local read
387
- {
388
- name: "decantr_get_section_context",
389
- title: "Get Section Context",
390
- description: "Get the self-contained context for a specific section of the project. Returns guard rules, theme tokens, decorators, pattern specs, zone context, and pages \u2014 everything an AI needs to work on that section.",
391
- inputSchema: {
392
- type: "object",
393
- properties: {
394
- section_id: { type: "string", description: 'Section ID (archetype ID, e.g., "ai-chatbot", "auth-full", "settings-full")' }
395
- },
396
- required: ["section_id"]
397
- },
398
- annotations: READ_ONLY
399
- }
400
- ];
401
- async function handleTool(name, args) {
402
- const apiClient = getAPIClient();
403
- switch (name) {
404
- case "decantr_read_essence": {
405
- const essencePath = args.path || join2(process.cwd(), "decantr.essence.json");
406
- try {
407
- const raw = await readFile2(essencePath, "utf-8");
408
- const essence = JSON.parse(raw);
409
- const layer = args.layer;
410
- if (layer && isV32(essence)) {
411
- if (layer === "dna") return essence.dna;
412
- if (layer === "blueprint") return essence.blueprint;
413
- }
414
- return essence;
415
- } catch (e) {
416
- return { error: `Could not read essence file: ${e.message}` };
417
- }
418
- }
419
- case "decantr_validate": {
420
- const essencePath = args.path || join2(process.cwd(), "decantr.essence.json");
421
- let essence;
422
- try {
423
- essence = JSON.parse(await readFile2(essencePath, "utf-8"));
424
- } catch (e) {
425
- return { valid: false, errors: [`Could not read: ${e.message}`], guardViolations: [] };
426
- }
427
- const result = validateEssence(essence);
428
- let guardViolations = [];
429
- if (result.valid && typeof essence === "object" && essence !== null) {
430
- try {
431
- guardViolations = evaluateGuard(essence, {});
432
- } catch {
433
- }
434
- }
435
- if (result.valid && typeof essence === "object" && essence !== null && isV32(essence)) {
436
- const dnaViolations = guardViolations.filter((v) => v.layer === "dna");
437
- const blueprintViolations = guardViolations.filter((v) => v.layer === "blueprint");
438
- const otherViolations = guardViolations.filter((v) => !v.layer);
439
- return {
440
- ...result,
441
- format: "v3",
442
- dna_violations: dnaViolations,
443
- blueprint_violations: blueprintViolations,
444
- guardViolations: otherViolations
445
- };
446
- }
447
- return { ...result, guardViolations };
448
- }
449
- case "decantr_search_registry": {
450
- const err = validateStringArg(args, "query");
451
- if (err) return { error: err };
452
- try {
453
- const response = await apiClient.search({
454
- q: args.query,
455
- type: args.type
456
- });
457
- return {
458
- total: response.total,
459
- results: response.results.map((r) => ({
460
- type: r.type,
461
- id: r.slug,
462
- namespace: r.namespace,
463
- name: r.name,
464
- description: r.description,
465
- install: `decantr get ${r.type} ${r.slug}`
466
- }))
467
- };
468
- } catch (e) {
469
- return { error: `Search failed: ${e.message}` };
470
- }
471
- }
472
- case "decantr_resolve_pattern": {
473
- const err = validateStringArg(args, "id");
474
- if (err) return { error: err };
475
- const namespace = args.namespace || "@official";
476
- try {
477
- const pattern = await apiClient.getPattern(namespace, args.id);
478
- const result = { found: true, ...pattern };
479
- if (args.preset && typeof args.preset === "string") {
480
- const preset = resolvePatternPreset(pattern, args.preset);
481
- if (preset) result.resolvedPreset = preset;
482
- }
483
- return result;
484
- } catch {
485
- return { found: false, message: `Pattern "${args.id}" not found in ${namespace}.` };
486
- }
487
- }
488
- case "decantr_resolve_archetype": {
489
- const err = validateStringArg(args, "id");
490
- if (err) return { error: err };
491
- const namespace = args.namespace || "@official";
492
- try {
493
- const archetype = await apiClient.getArchetype(namespace, args.id);
494
- return { found: true, ...archetype };
495
- } catch {
496
- return { found: false, message: `Archetype "${args.id}" not found in ${namespace}.` };
497
- }
498
- }
499
- case "decantr_resolve_recipe": {
500
- const err = validateStringArg(args, "id");
501
- if (err) return { error: err };
502
- const namespace = args.namespace || "@official";
503
- try {
504
- const recipe = await apiClient.getRecipe(namespace, args.id);
505
- return { found: true, ...recipe };
506
- } catch {
507
- return { found: false, message: `Recipe "${args.id}" not found in ${namespace}.` };
508
- }
509
- }
510
- case "decantr_resolve_blueprint": {
511
- const err = validateStringArg(args, "id");
512
- if (err) return { error: err };
513
- const namespace = args.namespace || "@official";
514
- try {
515
- const blueprint = await apiClient.getBlueprint(namespace, args.id);
516
- let topology = null;
517
- const composeEntries = blueprint.compose || blueprint.data?.compose;
518
- if (composeEntries && Array.isArray(composeEntries) && composeEntries.length > 0) {
519
- const zoneInputs = [];
520
- const archetypePromises = composeEntries.map(async (entry) => {
521
- const arcId = typeof entry === "string" ? entry : entry.archetype;
522
- try {
523
- const arch = await apiClient.getContent("archetypes", namespace, arcId);
524
- const archData = arch.data || arch;
525
- const explicitRole = typeof entry === "object" ? entry.role : void 0;
526
- zoneInputs.push({
527
- archetypeId: arcId,
528
- role: explicitRole || archData.role || "auxiliary",
529
- shell: archData.pages?.[0]?.shell || "sidebar-main",
530
- features: archData.features || [],
531
- description: archData.description || ""
532
- });
533
- } catch {
534
- }
535
- });
536
- await Promise.all(archetypePromises);
537
- if (zoneInputs.length > 0) {
538
- const zones = deriveZones(zoneInputs);
539
- const transitions = deriveTransitions(zones);
540
- const primaryArchetype = zoneInputs.find((z) => z.role === "primary");
541
- topology = {
542
- zones: zones.map((z) => ({
543
- role: z.role,
544
- archetypes: z.archetypes,
545
- shell: z.shell,
546
- features: z.features,
547
- purpose: z.descriptions.join(" ")
548
- })),
549
- transitions,
550
- entryPoints: {
551
- anonymous: "/",
552
- authenticated: primaryArchetype ? `/${primaryArchetype.archetypeId}` : "/home"
553
- }
554
- };
555
- }
556
- }
557
- return { found: true, ...blueprint, ...topology ? { topology } : {} };
558
- } catch {
559
- return { found: false, message: `Blueprint "${args.id}" not found in ${namespace}.` };
560
- }
561
- }
562
- case "decantr_suggest_patterns": {
563
- const err = validateStringArg(args, "description");
564
- if (err) return { error: err };
565
- const desc = args.description.toLowerCase();
566
- try {
567
- const patternsResponse = await apiClient.listContent("patterns", {
568
- namespace: "@official",
569
- limit: 100
570
- });
571
- const suggestions = [];
572
- for (const p of patternsResponse.items) {
573
- const searchable = [
574
- p.name || "",
575
- p.description || "",
576
- ...p.components || [],
577
- ...p.tags || []
578
- ].join(" ").toLowerCase();
579
- let score = 0;
580
- const words = desc.split(/\s+/);
581
- for (const word of words) {
582
- if (word.length < 3) continue;
583
- if (searchable.includes(word)) score += 10;
584
- }
585
- if (desc.includes("dashboard") && ["kpi-grid", "chart-grid", "data-table", "filter-bar"].includes(p.id)) score += 20;
586
- if (desc.includes("metric") && p.id === "kpi-grid") score += 15;
587
- if (desc.includes("chart") && p.id === "chart-grid") score += 15;
588
- if (desc.includes("table") && p.id === "data-table") score += 15;
589
- if (desc.includes("form") && p.id === "form-sections") score += 15;
590
- if (desc.includes("setting") && p.id === "form-sections") score += 15;
591
- if (desc.includes("landing") && ["hero", "cta-section", "card-grid"].includes(p.id)) score += 20;
592
- if (desc.includes("hero") && p.id === "hero") score += 20;
593
- if (desc.includes("ecommerce") && ["card-grid", "filter-bar", "detail-header"].includes(p.id)) score += 15;
594
- if (desc.includes("product") && p.id === "card-grid") score += 15;
595
- if (desc.includes("feed") && p.id === "activity-feed") score += 15;
596
- if (desc.includes("filter") && p.id === "filter-bar") score += 15;
597
- if (desc.includes("search") && p.id === "filter-bar") score += 10;
598
- if (score > 0) {
599
- const preset = p.presets ? Object.values(p.presets)[0] : null;
600
- suggestions.push({
601
- id: p.id,
602
- score,
603
- name: p.name || p.id,
604
- description: p.description || "",
605
- components: p.components || [],
606
- layout: preset?.layout ? preset.layout.layout : "grid"
607
- });
608
- }
609
- }
610
- suggestions.sort((a, b) => b.score - a.score);
611
- return {
612
- query: args.description,
613
- suggestions: suggestions.slice(0, 5),
614
- total: suggestions.length
615
- };
616
- } catch (e) {
617
- return { error: `Could not fetch patterns: ${e.message}` };
618
- }
619
- }
620
- case "decantr_check_drift": {
621
- const essencePath = args.path || join2(process.cwd(), "decantr.essence.json");
622
- let essence;
623
- try {
624
- essence = JSON.parse(await readFile2(essencePath, "utf-8"));
625
- } catch (e) {
626
- return { error: `Could not read essence: ${e.message}` };
627
- }
628
- const validation = validateEssence(essence);
629
- if (!validation.valid) {
630
- return { drifted: true, reason: "invalid_essence", errors: validation.errors };
631
- }
632
- const violations = [];
633
- if (args.theme_used && typeof args.theme_used === "string") {
634
- let expectedStyle;
635
- if (isV32(essence)) {
636
- expectedStyle = essence.dna.theme.style;
637
- } else {
638
- const expectedTheme = essence.theme;
639
- expectedStyle = expectedTheme?.style;
640
- }
641
- if (expectedStyle && args.theme_used !== expectedStyle) {
642
- violations.push({
643
- rule: "theme-match",
644
- severity: "critical",
645
- message: `Theme drift: code uses "${args.theme_used}" but Essence specifies "${expectedStyle}". Do not switch themes.`,
646
- ...isV32(essence) ? { layer: "dna", autoFixable: false } : {}
647
- });
648
- }
649
- }
650
- if (args.page_id && typeof args.page_id === "string") {
651
- let pages;
652
- if (isV32(essence)) {
653
- pages = essence.blueprint.pages;
654
- } else {
655
- pages = essence.structure || [];
656
- }
657
- if (!pages.find((p) => p.id === args.page_id)) {
658
- violations.push({
659
- rule: "page-exists",
660
- severity: "critical",
661
- message: `Page "${args.page_id}" not found in Essence structure. Add it to the Essence before generating code for it.`,
662
- ...isV32(essence) ? {
663
- layer: "blueprint",
664
- autoFixable: true,
665
- autoFix: { type: "add_page", patch: { id: args.page_id } }
666
- } : {}
667
- });
668
- }
669
- }
670
- if (args.components_used && Array.isArray(args.components_used) && args.page_id && typeof args.page_id === "string") {
671
- let pages;
672
- if (isV32(essence)) {
673
- pages = essence.blueprint.pages;
674
- } else {
675
- pages = essence.structure || [];
676
- }
677
- const page = pages.find((p) => p.id === args.page_id);
678
- if (page && page.layout) {
679
- const expectedPatterns = /* @__PURE__ */ new Set();
680
- for (const item of page.layout) {
681
- if (typeof item === "string") {
682
- expectedPatterns.add(item);
683
- } else if (typeof item === "object" && item !== null && "pattern" in item) {
684
- expectedPatterns.add(item.pattern);
685
- }
686
- }
687
- const componentsUsed = args.components_used;
688
- const unmatchedComponents = [];
689
- for (const comp of componentsUsed) {
690
- const compLower = comp.toLowerCase().replace(/[_\s]/g, "-");
691
- let matched = false;
692
- for (const pattern of expectedPatterns) {
693
- const patternLower = pattern.toLowerCase();
694
- if (compLower.includes(patternLower) || patternLower.includes(compLower) || fuzzyScore(compLower, patternLower) >= 60) {
695
- matched = true;
696
- break;
697
- }
698
- }
699
- if (!matched) {
700
- unmatchedComponents.push(comp);
701
- }
702
- }
703
- if (unmatchedComponents.length > 0) {
704
- violations.push({
705
- rule: "component-pattern-match",
706
- severity: "warning",
707
- message: `Components [${unmatchedComponents.join(", ")}] do not match any pattern in page "${args.page_id}" layout. Expected patterns: [${[...expectedPatterns].join(", ")}].`,
708
- ...isV32(essence) ? { layer: "blueprint", autoFixable: false } : {}
709
- });
710
- }
711
- }
712
- }
713
- try {
714
- const guardViolations = evaluateGuard(essence, {
715
- pageId: args.page_id
716
- });
717
- for (const gv of guardViolations) {
718
- violations.push({
719
- rule: gv.rule || "guard",
720
- severity: gv.severity || "warning",
721
- message: gv.message || "Guard violation",
722
- ...gv.layer ? { layer: gv.layer } : {},
723
- ...gv.autoFixable !== void 0 ? { autoFixable: gv.autoFixable } : {},
724
- ...gv.autoFix ? { autoFix: gv.autoFix } : {}
725
- });
726
- }
727
- } catch {
728
- }
729
- if (isV32(essence)) {
730
- const dnaViolations = violations.filter((v) => v.layer === "dna");
731
- const blueprintDrift = violations.filter((v) => v.layer === "blueprint");
732
- const other = violations.filter((v) => !v.layer);
733
- return {
734
- drifted: violations.length > 0,
735
- dna_violations: dnaViolations,
736
- blueprint_drift: blueprintDrift,
737
- other_violations: other,
738
- checkedAgainst: essencePath
739
- };
740
- }
741
- return {
742
- drifted: violations.length > 0,
743
- violations,
744
- checkedAgainst: essencePath
745
- };
746
- }
747
- case "decantr_create_essence": {
748
- const err = validateStringArg(args, "description");
749
- if (err) return { error: err };
750
- const desc = args.description.toLowerCase();
751
- const framework = args.framework || "react";
752
- const archetypeScores = [];
753
- const archetypeIds = [
754
- "saas-dashboard",
755
- "ecommerce",
756
- "portfolio",
757
- "content-site",
758
- "financial-dashboard",
759
- "cloud-platform",
760
- "gaming-platform",
761
- "ecommerce-admin",
762
- "workbench"
763
- ];
764
- for (const id of archetypeIds) {
765
- let score = 0;
766
- if (desc.includes("dashboard") && id.includes("dashboard")) score += 20;
767
- if (desc.includes("saas") && id.includes("saas")) score += 20;
768
- if (desc.includes("ecommerce") && id.includes("ecommerce")) score += 20;
769
- if (desc.includes("shop") && id.includes("ecommerce")) score += 15;
770
- if (desc.includes("portfolio") && id.includes("portfolio")) score += 20;
771
- if (desc.includes("blog") && id.includes("content")) score += 15;
772
- if (desc.includes("content") && id.includes("content")) score += 15;
773
- if (desc.includes("finance") && id.includes("financial")) score += 20;
774
- if (desc.includes("cloud") && id.includes("cloud")) score += 15;
775
- if (desc.includes("game") && id.includes("gaming")) score += 15;
776
- if (desc.includes("admin") && id.includes("admin")) score += 15;
777
- if (desc.includes("analytics") && id.includes("dashboard")) score += 10;
778
- if (desc.includes("tool") && id === "workbench") score += 10;
779
- if (score > 0) archetypeScores.push({ id, score });
780
- }
781
- archetypeScores.sort((a, b) => b.score - a.score);
782
- const bestMatch = archetypeScores[0]?.id || "saas-dashboard";
783
- let pages;
784
- let features = [];
785
- try {
786
- const archetype = await apiClient.getArchetype("@official", bestMatch);
787
- pages = archetype.pages;
788
- features = archetype.features || [];
789
- } catch {
790
- }
791
- const rawPages = pages || [{ id: "home", shell: "full-bleed", default_layout: ["hero"] }];
792
- const defaultShell = rawPages[0]?.shell || "sidebar-main";
793
- const essence = {
794
- version: "3.0.0",
795
- dna: {
796
- theme: {
797
- style: "auradecantism",
798
- mode: "dark",
799
- recipe: "auradecantism",
800
- shape: "rounded"
801
- },
802
- spacing: {
803
- base_unit: 4,
804
- scale: "linear",
805
- density: "comfortable",
806
- content_gap: "4"
807
- },
808
- typography: {
809
- scale: "modular",
810
- heading_weight: 600,
811
- body_weight: 400
812
- },
813
- color: {
814
- palette: "semantic",
815
- accent_count: 1,
816
- cvd_preference: "auto"
817
- },
818
- radius: {
819
- philosophy: "rounded",
820
- base: 8
821
- },
822
- elevation: {
823
- system: "layered",
824
- max_levels: 3
825
- },
826
- motion: {
827
- preference: "subtle",
828
- duration_scale: 1,
829
- reduce_motion: true
830
- },
831
- accessibility: {
832
- wcag_level: "AA",
833
- focus_visible: true,
834
- skip_nav: true
835
- },
836
- personality: ["professional"]
837
- },
838
- blueprint: {
839
- shell: defaultShell,
840
- pages: rawPages.map((p) => ({
841
- id: p.id,
842
- ...p.shell !== defaultShell ? { shell_override: p.shell } : {},
843
- layout: p.default_layout || []
844
- })),
845
- features
846
- },
847
- meta: {
848
- archetype: bestMatch,
849
- target: framework,
850
- platform: { type: "spa", routing: "hash" },
851
- guard: { mode: "strict", dna_enforcement: "error", blueprint_enforcement: "warn" }
852
- }
853
- };
854
- return {
855
- essence,
856
- archetype: bestMatch,
857
- format: "v3",
858
- instructions: `Save this as decantr.essence.json in your project root. Review the dna (design tokens), blueprint (pages/features), and meta (project config) sections and adjust to match your needs. The guard rules will validate your code against this spec.`,
859
- _generated: {
860
- matched_archetype: bestMatch,
861
- confidence: archetypeScores[0]?.score || 0,
862
- alternatives: archetypeScores.slice(1, 4).map((a) => a.id),
863
- description: args.description
864
- }
865
- };
866
- }
867
- case "decantr_accept_drift": {
868
- const violations = args.violations;
869
- const resolution = args.resolution;
870
- if (!violations || !Array.isArray(violations) || violations.length === 0) {
871
- return { error: 'Required parameter "violations" must be a non-empty array.' };
872
- }
873
- if (!resolution || !["accept", "accept_scoped", "reject", "defer"].includes(resolution)) {
874
- return { error: 'Required parameter "resolution" must be one of: accept, accept_scoped, reject, defer.' };
875
- }
876
- const hasDnaViolation = violations.some((v) => {
877
- const rule = v.rule;
878
- return ["style", "recipe", "density", "theme-mode", "accessibility", "theme-match"].includes(rule);
879
- });
880
- if (hasDnaViolation && resolution !== "reject" && resolution !== "defer" && !args.confirm_dna) {
881
- return {
882
- error: "DNA-layer violations detected. Set confirm_dna: true to accept changes to design axioms (theme, style, density, etc.).",
883
- requires_confirmation: true,
884
- dna_rules_affected: violations.filter(
885
- (v) => ["style", "recipe", "density", "theme-mode", "accessibility", "theme-match"].includes(v.rule)
886
- ).map((v) => v.rule)
887
- };
888
- }
889
- if (resolution === "reject") {
890
- return {
891
- status: "rejected",
892
- message: "Violations rejected. No changes made. Revert the code to match the essence spec.",
893
- violations_count: violations.length
894
- };
895
- }
896
- if (resolution === "defer") {
897
- const projectRoot = args.path ? dirname2(args.path) : void 0;
898
- const existingLog = await readDriftLog(projectRoot);
899
- const newEntries = violations.map((v) => ({
900
- rule: v.rule,
901
- page_id: v.page_id,
902
- details: v.details,
903
- resolution: "deferred",
904
- scope: args.scope || void 0,
905
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
906
- }));
907
- const updatedLog = [...existingLog, ...newEntries];
908
- const logPath = await writeDriftLog(updatedLog, projectRoot);
909
- return {
910
- status: "deferred",
911
- message: `${violations.length} violation(s) deferred to drift log.`,
912
- log_path: logPath,
913
- total_deferred: updatedLog.length
914
- };
915
- }
916
- try {
917
- const { essence, path } = await mutateEssenceFile(args.path, (v3) => {
918
- for (const v of violations) {
919
- applyDriftAcceptance(v3, v, resolution, args.scope);
920
- }
921
- return v3;
922
- });
923
- return {
924
- status: resolution === "accept_scoped" ? "accepted_scoped" : "accepted",
925
- message: `${violations.length} violation(s) resolved. Essence updated.`,
926
- path,
927
- scope: resolution === "accept_scoped" ? args.scope || "unscoped" : void 0
928
- };
929
- } catch (e) {
930
- return { error: `Failed to update essence: ${e.message}` };
931
- }
932
- }
933
- case "decantr_update_essence": {
934
- const operation = args.operation;
935
- const payload = args.payload;
936
- if (!operation) {
937
- return { error: 'Required parameter "operation" is missing.' };
938
- }
939
- if (!payload || typeof payload !== "object") {
940
- return { error: 'Required parameter "payload" must be an object.' };
941
- }
942
- const validOps = ["add_page", "remove_page", "update_page_layout", "update_dna", "update_blueprint", "add_feature", "remove_feature"];
943
- if (!validOps.includes(operation)) {
944
- return { error: `Invalid operation "${operation}". Must be one of: ${validOps.join(", ")}` };
945
- }
946
- try {
947
- const { essence, path } = await mutateEssenceFile(args.path, (v3) => {
948
- return applyEssenceUpdate(v3, operation, payload);
949
- });
950
- return {
951
- status: "updated",
952
- operation,
953
- path,
954
- summary: describeUpdate(operation, payload)
955
- };
956
- } catch (e) {
957
- return { error: `Failed to update essence: ${e.message}` };
958
- }
959
- }
960
- case "decantr_get_section_context": {
961
- const err = validateStringArg(args, "section_id");
962
- if (err) return { error: err };
963
- const sectionId = args.section_id;
964
- let essence;
965
- try {
966
- const result = await readEssenceFile();
967
- essence = result.essence;
968
- } catch {
969
- return { error: "No valid essence file found. Run decantr init first." };
970
- }
971
- if (!isV32(essence)) {
972
- return { error: "Section context requires a v3 essence file. Run decantr migrate first." };
973
- }
974
- const sections = essence.blueprint.sections || [];
975
- const section = sections.find((s) => s.id === sectionId);
976
- if (!section) {
977
- return {
978
- error: `Section "${sectionId}" not found.`,
979
- available_sections: sections.map((s) => ({ id: s.id, role: s.role, pages: s.pages.length }))
980
- };
981
- }
982
- const contextPath = join2(process.cwd(), ".decantr", "context", `section-${sectionId}.md`);
983
- if (existsSync(contextPath)) {
984
- return {
985
- section_id: sectionId,
986
- role: section.role,
987
- shell: section.shell,
988
- features: section.features,
989
- pages: section.pages.map((p) => ({ id: p.id, route: p.route, layout: p.layout })),
990
- context: readFileSync(contextPath, "utf-8")
991
- };
992
- }
993
- return {
994
- section_id: sectionId,
995
- role: section.role,
996
- shell: section.shell,
997
- features: section.features,
998
- description: section.description,
999
- pages: section.pages.map((p) => ({ id: p.id, route: p.route, layout: p.layout })),
1000
- note: "Section context file not found. Run decantr refresh to generate it."
1001
- };
1002
- }
1003
- default:
1004
- return { error: `Unknown tool: ${name}` };
1005
- }
1006
- }
1007
- function applyDriftAcceptance(essence, violation, resolution, scope) {
1008
- switch (violation.rule) {
1009
- case "theme-match":
1010
- case "style": {
1011
- if (violation.details) {
1012
- essence.dna.theme.style = violation.details;
1013
- }
1014
- break;
1015
- }
1016
- case "page-exists":
1017
- case "structure": {
1018
- if (violation.page_id) {
1019
- const existing = essence.blueprint.pages.find((p) => p.id === violation.page_id);
1020
- if (!existing) {
1021
- essence.blueprint.pages.push({
1022
- id: violation.page_id,
1023
- layout: []
1024
- });
1025
- }
1026
- }
1027
- break;
1028
- }
1029
- case "layout": {
1030
- break;
1031
- }
1032
- case "recipe": {
1033
- if (violation.details) {
1034
- essence.dna.theme.recipe = violation.details;
1035
- }
1036
- break;
1037
- }
1038
- case "density": {
1039
- break;
1040
- }
1041
- default:
1042
- break;
1043
- }
1044
- }
1045
- function applyEssenceUpdate(essence, operation, payload) {
1046
- switch (operation) {
1047
- case "add_page": {
1048
- const id = payload.id;
1049
- if (!id) throw new Error('Payload must include "id" for add_page.');
1050
- const existing = essence.blueprint.pages.find((p) => p.id === id);
1051
- if (existing) throw new Error(`Page "${id}" already exists.`);
1052
- essence.blueprint.pages.push({
1053
- id,
1054
- layout: payload.layout || [],
1055
- ...payload.shell_override ? { shell_override: payload.shell_override } : {},
1056
- ...payload.surface ? { surface: payload.surface } : {}
1057
- });
1058
- break;
1059
- }
1060
- case "remove_page": {
1061
- const id = payload.id;
1062
- if (!id) throw new Error('Payload must include "id" for remove_page.');
1063
- const idx = essence.blueprint.pages.findIndex((p) => p.id === id);
1064
- if (idx === -1) throw new Error(`Page "${id}" not found.`);
1065
- essence.blueprint.pages.splice(idx, 1);
1066
- break;
1067
- }
1068
- case "update_page_layout": {
1069
- const id = payload.id;
1070
- const layout = payload.layout;
1071
- if (!id) throw new Error('Payload must include "id" for update_page_layout.');
1072
- if (!layout || !Array.isArray(layout)) throw new Error('Payload must include "layout" array for update_page_layout.');
1073
- const page = essence.blueprint.pages.find((p) => p.id === id);
1074
- if (!page) throw new Error(`Page "${id}" not found.`);
1075
- page.layout = layout;
1076
- break;
1077
- }
1078
- case "update_dna": {
1079
- for (const [key, value] of Object.entries(payload)) {
1080
- if (key in essence.dna && typeof value === "object" && value !== null && !Array.isArray(value)) {
1081
- essence.dna[key] = {
1082
- ...essence.dna[key],
1083
- ...value
1084
- };
1085
- } else {
1086
- essence.dna[key] = value;
1087
- }
1088
- }
1089
- break;
1090
- }
1091
- case "update_blueprint": {
1092
- for (const [key, value] of Object.entries(payload)) {
1093
- if (key === "pages") continue;
1094
- essence.blueprint[key] = value;
1095
- }
1096
- break;
1097
- }
1098
- case "add_feature": {
1099
- const feature = payload.feature;
1100
- if (!feature) throw new Error('Payload must include "feature" for add_feature.');
1101
- if (!essence.blueprint.features.includes(feature)) {
1102
- essence.blueprint.features.push(feature);
1103
- }
1104
- break;
1105
- }
1106
- case "remove_feature": {
1107
- const feature = payload.feature;
1108
- if (!feature) throw new Error('Payload must include "feature" for remove_feature.');
1109
- const idx = essence.blueprint.features.indexOf(feature);
1110
- if (idx === -1) throw new Error(`Feature "${feature}" not found.`);
1111
- essence.blueprint.features.splice(idx, 1);
1112
- break;
1113
- }
1114
- }
1115
- return essence;
1116
- }
1117
- function describeUpdate(operation, payload) {
1118
- switch (operation) {
1119
- case "add_page":
1120
- return `Added page "${payload.id}".`;
1121
- case "remove_page":
1122
- return `Removed page "${payload.id}".`;
1123
- case "update_page_layout":
1124
- return `Updated layout for page "${payload.id}".`;
1125
- case "update_dna":
1126
- return `Updated DNA: ${Object.keys(payload).join(", ")}.`;
1127
- case "update_blueprint":
1128
- return `Updated blueprint: ${Object.keys(payload).join(", ")}.`;
1129
- case "add_feature":
1130
- return `Added feature "${payload.feature}".`;
1131
- case "remove_feature":
1132
- return `Removed feature "${payload.feature}".`;
1133
- default:
1134
- return `Performed ${operation}.`;
1135
- }
1136
- }
1137
-
1138
- // src/index.ts
1139
- var VERSION = "0.2.0";
1140
- var server = new Server(
1141
- { name: "decantr", version: VERSION },
1142
- { capabilities: { tools: {} } }
1143
- );
1144
- server.setRequestHandler(ListToolsRequestSchema, async () => {
1145
- return { tools: TOOLS };
1146
- });
1147
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
1148
- const { name, arguments: args } = request.params;
1149
- try {
1150
- const result = await handleTool(name, args ?? {});
1151
- const response = {
1152
- content: [{ type: "text", text: JSON.stringify(result, null, 2) }]
1153
- };
1154
- if (result && typeof result === "object" && "error" in result) {
1155
- response.isError = true;
1156
- }
1157
- return response;
1158
- } catch (err) {
1159
- return {
1160
- content: [{ type: "text", text: JSON.stringify({ error: err.message }) }],
1161
- isError: true
1162
- };
1163
- }
1164
- });
1165
- var transport = new StdioServerTransport();
1166
- await server.connect(transport);