@dypai-ai/mcp 1.5.13 → 1.5.15

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dypai-ai/mcp",
3
- "version": "1.5.13",
3
+ "version": "1.5.15",
4
4
  "description": "DYPAI MCP Server — AI agent toolkit for building and deploying full-stack apps",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -153,7 +153,7 @@ This stores classification metadata only. It does not create users, roles, login
153
153
  },
154
154
  },
155
155
  { name: "list_ai_models", description: "List only the DYPAI Managed AI models that are active for a project. Returns the project-gated OpenRouter model catalog priced in AI Credits per 1M tokens, RPM limit, max output tokens, active/available counts, billing metadata, and the exact node parameters to use. Call this before creating or editing an AI Agent node with DYPAI Managed models. Agents must not invent or use inactive model ids. Use provider='openrouter' and do NOT set credential_id; DYPAI uses the platform OpenRouter key and deducts usage from the organization's AI Credits.", inputSchema: { type: "object", properties: { project_id: { type: "string", description: "Project UUID whose plan and Model Gateway settings determine the active Managed AI catalog." } }, required: ["project_id"] } },
156
- { name: "create_project", description: "Create a new DYPAI project (free plan). Creates a full project with database, engine, GitHub repo, and frontend hosting. BLOCKS by default until provisioning finishes (~60s typical, 120s max) — when it returns, the project_id is ready to use with execute_sql, endpoint tools, etc. Pass wait_until_ready:false for batch flows.\n\nName collision: if another project in the same org already uses the name (case-insensitive), returns {error:'name_taken', existing_project_id, suggestions:[...]}. Pick a different name or use the existing project.\n\nProject limits are enforced by the DYPAI API at organization/workspace scope according to the workspace plan. If it returns {error:'project_limit_reached'}, do not retry create_project; show list_projects for that organization and ask the user to reuse, archive/pause, upgrade the workspace to Pro, or add capacity.\n\nIMPORTANT: before calling, check for a matching template with `search_project_templates`. Passing a `template_slug` drops in a ready-made schema + endpoints + UI that cover 70% of common app types. Use built-in bases when appropriate: `private-admin` for private internal tools, `user-accounts` for apps with signup/login users, `landing-admin` for public landing plus admin, and `blank` only when no base fits.", inputSchema: { type: "object", properties: { name: { type: "string", description: "Project name (e.g. 'My Veterinary App')" }, organization_id: { type: "string", description: "Optional. Uses default org if omitted." }, description: { type: "string" }, template_slug: { type: "string", description: "RECOMMENDED. Project template slug to start from (e.g. 'clinic', 'gym', 'private-admin', 'user-accounts', 'landing-admin', 'blank'). Always call search_project_templates first to find the best match." }, wait_until_ready: { type: "boolean", description: "If true (default), blocks until provisioning completes and the project is ready for all operations. If false, returns immediately with status='provisioning' — caller must poll get_project before using.", default: true } }, required: ["name"] } },
156
+ { name: "create_project", description: "Create a new DYPAI project (free plan). Creates a full project with database, engine, GitHub repo, and frontend hosting. BLOCKS by default until provisioning finishes (~60s typical, 120s max) — when it returns, the project_id is ready to use with execute_sql, endpoint tools, etc. Pass wait_until_ready:false for batch flows.\n\nName collision: if another project in the same org already uses the name (case-insensitive), returns {error:'name_taken', existing_project_id, suggestions:[...]}. Pick a different name or use the existing project.\n\nProject limits are enforced by the DYPAI API at organization/workspace scope according to the workspace plan. If it returns {error:'project_limit_reached'}, do not retry create_project; show list_projects for that organization and ask the user to reuse, archive/pause, upgrade the workspace to Pro, or add capacity.\n\nIMPORTANT: before calling, check for a matching Studio template with `search_project_templates`. Passing a `template_slug` can drop in ready-made UI, schema, endpoints, and agent instructions. Prefer Studio catalog slugs when the returned brief is a strong fit; legacy fallback bases (`private-admin`, `user-accounts`, `landing-admin`, `blank`) remain available when no catalog template fits.", inputSchema: { type: "object", properties: { name: { type: "string", description: "Project name (e.g. 'My Veterinary App')" }, organization_id: { type: "string", description: "Optional. Uses default org if omitted." }, description: { type: "string" }, template_slug: { type: "string", description: "RECOMMENDED. Template slug returned by search_project_templates. Use Studio catalog slugs when a strong template fits; legacy fallback bases remain supported when no catalog template fits." }, wait_until_ready: { type: "boolean", description: "If true (default), blocks until provisioning completes and the project is ready for all operations. If false, returns immediately with status='provisioning' — caller must poll get_project before using.", default: true } }, required: ["name"] } },
157
157
  { name: "get_app_credentials", description: "Lists available credentials in the current application. Returns API keys, anon key, service role key, and engine URL needed for SDK configuration.", inputSchema: { type: "object", properties: { project_id: { type: "string" } }, required: [] } },
158
158
 
159
159
  // ── Database ──────────────────────────────────────────────────────────────
@@ -496,7 +496,7 @@ endpoint YAML and \`dypai_push\`. This tool does NOT modify the definition.`,
496
496
  { name: "search_docs", description: "Search DYPAI documentation. Use this when unsure about SDK usage, auth patterns, workflow nodes, or platform features. Returns relevant documentation chunks.", inputSchema: { type: "object", properties: { query: { type: "string", description: "What you want to learn about" } }, required: ["query"] } },
497
497
  { name: "search_design_patterns", description: "Search compact DYPAI UI/design recipes. Use before designing substantial screens.", inputSchema: { type: "object", properties: { query: { type: "string", description: "Design need, with starter/domain/screen/style context when known." }, starter_slug: { type: "string", description: "Optional: private-admin, user-accounts, landing-admin, or blank." }, app_type: { type: "string", description: "Optional domain/app type." }, screen_type: { type: "string", description: "Optional screen/workflow." }, visual_style: { type: "string", description: "Optional style." }, category: { type: "string", description: "Optional category." }, limit: { type: "integer", default: 3, minimum: 1, maximum: 4 } }, required: ["query"] } },
498
498
  { name: "search_workflow_templates", description: "Search workflow templates by description. Returns ready-to-use workflow code for common patterns: CRUD operations, payment gateways, email sending, AI chatbots, data pipelines, etc.", inputSchema: { type: "object", properties: { query: { type: "string", description: "What the workflow should do (e.g. 'send email', 'stripe payment')" }, category: { type: "string", description: "Optional: AI, Database, Payments, Communication, Logic, Storage" } }, required: ["query"] } },
499
- { name: "search_project_templates", description: "Search project starter templates by description. Returns template metadata and slugs for marketplace templates plus built-in bases: private-admin, user-accounts, landing-admin, and blank.", inputSchema: { type: "object", properties: { query: { type: "string", description: "What kind of project starter you need (e.g. 'gym app', 'private admin dashboard', 'user accounts portal', 'landing plus admin')" }, category: { type: "string", description: "Optional category filter" } }, required: ["query"] } },
499
+ { name: "search_project_templates", description: "Search DYPAI Studio project templates by description. Studio catalog results are the primary source and return compact template briefs plus selection_guidance: recommended_slug, confidence, fallback_slug, and agent_rule. Legacy fallback bases are returned only when no Studio template is a strong fit.", inputSchema: { type: "object", properties: { query: { type: "string", description: "What kind of project starter you need, in the user's language (e.g. 'app para barberia con reservas', 'private admin dashboard', 'landing plus admin')" }, category: { type: "string", description: "Optional category/type filter." }, limit: { type: "integer", default: 5, minimum: 1, maximum: 10 }, include_drafts: { type: "boolean", default: false, description: "Internal/local testing only. Include draft Studio catalog templates." } }, required: ["query"] } },
500
500
  ]
501
501
 
502
502
  // ── Server Instructions ──────────────────────────────────────────────────────
@@ -511,11 +511,11 @@ const SERVER_INSTRUCTIONS = `You are building full-stack applications on the DYP
511
511
 
512
512
  ## What NOT to do
513
513
 
514
- - *"Te propongo Next.js + TypeScript + Tailwind + Prisma + SQLite..."*
515
- - *"Podrías usar Supabase / Firebase / MongoDB / Vercel..."*
516
- - *"¿Qué base de datos prefieres, PostgreSQL o SQLite?"*
517
- - *"¿Prefieres Tailwind o CSS Modules?"*
518
- - Asking "which framework" unless the user explicitly says *"I want to compare platforms"* or *"what are my options"*.
514
+ - Do not say: *"Te propongo Next.js + TypeScript + Tailwind + Prisma + SQLite..."*
515
+ - Do not say: *"Podrías usar Supabase / Firebase / MongoDB / Vercel..."*
516
+ - Do not ask: *"¿Qué base de datos prefieres, PostgreSQL o SQLite?"*
517
+ - Do not ask: *"¿Prefieres Tailwind o CSS Modules?"*
518
+ - Do not ask "which framework" unless the user explicitly says *"I want to compare platforms"* or *"what are my options"*.
519
519
 
520
520
  These are ALL dead signals: Next.js is already what DYPAI scaffolds. The DB is already PostgreSQL (via DYPAI engine). Auth is built in. Tailwind is already in the templates. Prisma / ORMs are not used — you write SQL directly in workflow endpoints.
521
521
 
@@ -524,19 +524,23 @@ These are ALL dead signals: Next.js is already what DYPAI scaffolds. The DB is a
524
524
  First reflex, always:
525
525
 
526
526
  1. **Acknowledge briefly** what they want to build (one short line, their language).
527
- 2. **\`search_project_templates(query: "<keywords from their request>")\`** — keywords in their language. Templates cover common app types (gym, clinic, waitlist, saas dashboard, etc.).
528
- 3. **Decide: marketplace template, built-in base, or blank.** Marketplace templates are only right when the match is OBVIOUS and STRONG:
529
- - User says *"app para mi gimnasio"* + there's \`gym-manager\` (exact domain + feature overlap) template.
530
- - User says *"algo para gestionar reservas"* + there's \`gym-manager\` (soft match, many interpretations) → use a built-in base or **blank**. Don't assume they want the gym's specific schema (classes, memberships, check-ins) — they didn't ask for it.
531
- - Built-in bases are safe defaults:
527
+ 2. **\`search_project_templates(query: "<keywords from their request>")\`** — keywords in their language. The Studio catalog returns compact briefs plus \`selection_guidance\`: recommended slug, confidence, fallback slug, and the rule for deciding.
528
+ 3. **Decide: Studio catalog template, legacy base, or blank.** Use \`selection_guidance\` explicitly:
529
+ - confidence "strong": use \`recommended_slug\` if it does not contradict the user's plan.
530
+ - confidence "medium": use it only if the user's answers confirm the same domain/workflow.
531
+ - confidence "weak": do not auto-use it; use \`fallback_slug\`.
532
+ - confidence "fallback": use \`recommended_slug\` unless the app plan clearly requires a different returned slug.
533
+ - Strong fit: user says *"app para barberia con reservas"* and the result includes a booking/barber vertical with matching routes/endpoints. Use that template and preserve what it already has.
534
+ - Strong fit: user asks for a broad business app and a matching vertical template says what to adapt and what not to rebuild. Use the template, then customize it.
535
+ - Weak fit: user says *"algo para gestionar reservas"* and only a very specific unrelated vertical matches softly. Use a safer legacy base or blank. Don't inherit domain-specific schema they did not ask for.
536
+ - Legacy bases are fallback defaults when no Studio catalog template fits:
532
537
  - private/internal/admin/dashboard/backoffice/business management → \`private-admin\`
533
538
  - end-user signup/login/customer/member portal/marketplace/SaaS accounts → \`user-accounts\`
534
539
  - public landing/marketing site plus private admin → \`landing-admin\`
535
540
  - no clear access pattern or explicitly custom/from scratch → \`blank\`
536
- - User is a dev with a concrete spec (*"crea un proyecto con estas 3 tablas y estos endpoints"*) usually **blank**, unless they explicitly want one of the built-in bases.
537
- - No marketplace or built-in base fits **blank**.
538
- 4. **Call it** \`create_project(name: "<their name>", template_slug: "<matched_slug>" | "private-admin" | "user-accounts" | "landing-admin" | "blank")\`.
539
- If you went with a template, acknowledge in ONE line what's included so the user can push back: *"Lo arranco con la plantilla X, que trae socios, clases y pagos. ¿Te vale o prefieres algo más simple?"*
541
+ - Concrete spec: user is a dev with a concrete spec (*"crea un proyecto con estas 3 tablas y estos endpoints"*). Usually use **blank**, unless a Studio template exactly matches the requested product.
542
+ 4. **Call it** \`create_project(name: "<their name>", template_slug: "<slug from search_project_templates>")\`.
543
+ If you went with a catalog template, use its \`agent_brief\`: preserve \`already_included\`, follow \`adaptation_steps\`, and do not rebuild \`do_not_rebuild\`.
540
544
  If you went blank, just say: *"Arranco un proyecto en blanco y lo construimos a medida."*
541
545
  5. **After \`create_project\`** → ask for an absolute workspace path, then \`dypai_pull\` + \`manage_frontend(sync)\` (see next section).
542
546
 
@@ -556,7 +560,7 @@ target workspace package.json and install them locally (never globally or in
556
560
  the MCP server). Then wire the kit into the app, apply SQL if needed, validate
557
561
  endpoints, and verify the frontend.
558
562
 
559
- **The template system exists to save time when the fit is obvious, not to force-match every request.** When in doubt → blank is always correct. Iterating up from blank is cheaper than deleting 80% of a mismatched template.
563
+ **The template system exists to save time when the fit is obvious, not to force-match every request.** When in doubt → use the safest legacy base or blank. Iterating up from a smaller base is cheaper than deleting 80% of a mismatched template.
560
564
 
561
565
  ## The one legit follow-up question
562
566
 
@@ -1121,7 +1125,7 @@ async function handleRequest(msg) {
1121
1125
  return makeResponse(id, {
1122
1126
  protocolVersion: "2024-11-05",
1123
1127
  capabilities: { tools: {} },
1124
- serverInfo: { name: "dypai", version: "1.5.13" },
1128
+ serverInfo: { name: "dypai", version: "1.5.14" },
1125
1129
  instructions: SERVER_INSTRUCTIONS,
1126
1130
  })
1127
1131
  }
@@ -1,9 +1,14 @@
1
+ import crypto from "crypto"
1
2
  import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync, copyFileSync } from "fs"
3
+ import { tmpdir } from "os"
2
4
  import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep } from "path"
3
5
  import { fileURLToPath } from "url"
6
+ import { proxyToolCall } from "./proxy.js"
4
7
 
5
8
  const __dirname = dirname(fileURLToPath(import.meta.url))
6
9
  const DEFAULT_LIMIT = 5
10
+ const DEFAULT_R2_BUCKET = "organizations-storage"
11
+ const DEFAULT_R2_PREFIX = "capability-kits"
7
12
 
8
13
  function readJson(path) {
9
14
  return JSON.parse(readFileSync(path, "utf8"))
@@ -46,10 +51,220 @@ function resolveKitsRoot(inputRoot) {
46
51
  if (fromThisFile) return join(fromThisFile, "dypai-capability-kits")
47
52
 
48
53
  throw new Error(
49
- "Capability kit repo not found. Set DYPAI_CAPABILITY_KITS_ROOT to the local dypai-capability-kits directory.",
54
+ "Capability kit repo not found locally. Set DYPAI_CAPABILITY_KITS_ROOT for local authoring or configure R2 credentials for remote kit install.",
50
55
  )
51
56
  }
52
57
 
58
+ function tryResolveKitsRoot(inputRoot) {
59
+ try {
60
+ return resolveKitsRoot(inputRoot)
61
+ } catch {
62
+ return null
63
+ }
64
+ }
65
+
66
+ function hasLocalKit(root, slug, version) {
67
+ return Boolean(root) && existsSync(join(root, "kits", slug, version, "kit.json"))
68
+ }
69
+
70
+ function sha256Buffer(buffer) {
71
+ return crypto.createHash("sha256").update(buffer).digest("hex")
72
+ }
73
+
74
+ function safeLogicalPath(path) {
75
+ if (typeof path !== "string" || !path.trim()) throw new Error("Remote kit file path is invalid")
76
+ const normalized = path.replace(/\\/g, "/").replace(/^\/+/, "")
77
+ if (!normalized || normalized.includes("..") || isAbsolute(normalized)) {
78
+ throw new Error(`Unsafe remote kit file path: ${path}`)
79
+ }
80
+ return normalized
81
+ }
82
+
83
+ function parseR2SourceUri(sourceUri, slug) {
84
+ if (typeof sourceUri !== "string" || !sourceUri.startsWith("r2://")) return null
85
+ const withoutScheme = sourceUri.slice("r2://".length)
86
+ const slash = withoutScheme.indexOf("/")
87
+ if (slash <= 0) throw new Error(`Invalid capability kit source_uri: ${sourceUri}`)
88
+ const bucket = withoutScheme.slice(0, slash)
89
+ const rootKey = withoutScheme.slice(slash + 1).replace(/^\/+|\/+$/g, "")
90
+ if (!rootKey || !rootKey.split("/").includes(slug)) {
91
+ throw new Error(`Capability kit source_uri does not look like a kit root for ${slug}: ${sourceUri}`)
92
+ }
93
+ return { bucket, rootKey }
94
+ }
95
+
96
+ function resolveRemoteKitLocation(slug, sourceUri) {
97
+ const parsed = parseR2SourceUri(sourceUri, slug)
98
+ if (parsed) return parsed
99
+ const bucket = process.env.CAPABILITY_KITS_R2_BUCKET
100
+ || process.env.DYPAI_CAPABILITY_KITS_R2_BUCKET
101
+ || process.env.CLOUDFLARE_R2_CAPABILITY_KITS_BUCKET
102
+ || DEFAULT_R2_BUCKET
103
+ const prefix = (process.env.CAPABILITY_KITS_R2_PREFIX || DEFAULT_R2_PREFIX).replace(/^\/+|\/+$/g, "")
104
+ return {
105
+ bucket,
106
+ rootKey: `${prefix}/${slug}`.replace(/^\/+|\/+$/g, "").replace(/\/+/g, "/"),
107
+ }
108
+ }
109
+
110
+ function resolveR2Config(slug, sourceUri) {
111
+ const endpoint = (process.env.CLOUDFLARE_R2_ENDPOINT_URL || "").replace(/\/$/, "")
112
+ const accessKeyId = process.env.CLOUDFLARE_R2_ACCESS_KEY_ID || ""
113
+ const secretAccessKey = process.env.CLOUDFLARE_R2_SECRET_ACCESS_KEY || ""
114
+ if (!endpoint || !accessKeyId || !secretAccessKey) {
115
+ throw new Error("Capability kit R2 storage is not configured. Set CLOUDFLARE_R2_ENDPOINT_URL, CLOUDFLARE_R2_ACCESS_KEY_ID, and CLOUDFLARE_R2_SECRET_ACCESS_KEY.")
116
+ }
117
+ return { endpoint, accessKeyId, secretAccessKey, ...resolveRemoteKitLocation(slug, sourceUri) }
118
+ }
119
+
120
+ function hmac(key, value, encoding) {
121
+ return crypto.createHmac("sha256", key).update(value).digest(encoding)
122
+ }
123
+
124
+ function sha256Hex(value) {
125
+ return crypto.createHash("sha256").update(value).digest("hex")
126
+ }
127
+
128
+ function encodeRfc3986(value) {
129
+ return encodeURIComponent(value).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)
130
+ }
131
+
132
+ function r2Url(config, key) {
133
+ const encodedKey = key.split("/").map(encodeRfc3986).join("/")
134
+ return new URL(`${config.endpoint}/${encodeRfc3986(config.bucket)}/${encodedKey}`)
135
+ }
136
+
137
+ function signingKey(secretAccessKey, dateStamp) {
138
+ const dateKey = hmac(`AWS4${secretAccessKey}`, dateStamp)
139
+ const dateRegionKey = hmac(dateKey, "auto")
140
+ const dateRegionServiceKey = hmac(dateRegionKey, "s3")
141
+ return hmac(dateRegionServiceKey, "aws4_request")
142
+ }
143
+
144
+ function signedR2Headers(config, method, url, payloadHash) {
145
+ const now = new Date()
146
+ const amzDate = now.toISOString().replace(/[:-]|\.\d{3}/g, "")
147
+ const dateStamp = amzDate.slice(0, 8)
148
+ const canonicalHeaders = [
149
+ `host:${url.host}`,
150
+ `x-amz-content-sha256:${payloadHash}`,
151
+ `x-amz-date:${amzDate}`,
152
+ "",
153
+ ].join("\n")
154
+ const signedHeaders = "host;x-amz-content-sha256;x-amz-date"
155
+ const canonicalRequest = [
156
+ method,
157
+ url.pathname,
158
+ url.searchParams.toString(),
159
+ canonicalHeaders,
160
+ signedHeaders,
161
+ payloadHash,
162
+ ].join("\n")
163
+ const credentialScope = `${dateStamp}/auto/s3/aws4_request`
164
+ const stringToSign = [
165
+ "AWS4-HMAC-SHA256",
166
+ amzDate,
167
+ credentialScope,
168
+ sha256Hex(canonicalRequest),
169
+ ].join("\n")
170
+ const signature = hmac(signingKey(config.secretAccessKey, dateStamp), stringToSign, "hex")
171
+
172
+ return {
173
+ Authorization: `AWS4-HMAC-SHA256 Credential=${config.accessKeyId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`,
174
+ "x-amz-content-sha256": payloadHash,
175
+ "x-amz-date": amzDate,
176
+ }
177
+ }
178
+
179
+ async function r2Get(config, key) {
180
+ const url = r2Url(config, key)
181
+ const response = await fetch(url, {
182
+ method: "GET",
183
+ headers: signedR2Headers(config, "GET", url, sha256Hex(Buffer.alloc(0))),
184
+ })
185
+ if (!response.ok) {
186
+ throw new Error(`R2 GET failed for ${key}: ${response.status} ${await response.text()}`)
187
+ }
188
+ return Buffer.from(await response.arrayBuffer())
189
+ }
190
+
191
+ function parseRemoteFilesManifest(value, slug, requestedVersion) {
192
+ const parsed = JSON.parse(value)
193
+ if (!isObject(parsed)) throw new Error("Remote kit manifest must be a JSON object")
194
+ if (parsed.slug && parsed.slug !== slug) throw new Error(`Remote kit manifest slug mismatch: expected ${slug}, got ${parsed.slug}`)
195
+ if (parsed.version && parsed.version !== requestedVersion) {
196
+ throw new Error(`Remote kit manifest version mismatch: expected ${requestedVersion}, got ${parsed.version}`)
197
+ }
198
+ if (!Array.isArray(parsed.files) || !parsed.files.length) {
199
+ throw new Error(`Remote kit manifest for ${slug} has no files`)
200
+ }
201
+ return parsed
202
+ }
203
+
204
+ function cachedRemoteKitComplete(kitDirPath, manifest) {
205
+ return (manifest.files || []).every((file) => {
206
+ try {
207
+ const rel = safeLogicalPath(file.path)
208
+ const path = join(kitDirPath, rel)
209
+ if (!existsSync(path)) return false
210
+ return file.sha256 ? sha256Buffer(readFileSync(path)) === file.sha256 : true
211
+ } catch {
212
+ return false
213
+ }
214
+ })
215
+ }
216
+
217
+ function remoteKitKey(config, logicalPath) {
218
+ return `${config.rootKey}/${logicalPath}`.replace(/\/+/g, "/").replace(/^\/+/, "")
219
+ }
220
+
221
+ async function downloadRemoteCapabilityKit(slug, version, sourceUri) {
222
+ const config = resolveR2Config(slug, sourceUri)
223
+ const manifestKey = remoteKitKey(config, "manifest.files.json")
224
+ const manifestText = (await r2Get(config, manifestKey)).toString("utf8")
225
+ const manifest = parseRemoteFilesManifest(manifestText, slug, version)
226
+ const contentHash = typeof manifest.contentHash === "string" && manifest.contentHash.trim()
227
+ ? manifest.contentHash.trim()
228
+ : sha256Buffer(Buffer.from(manifestText))
229
+ const cacheRoot = join(tmpdir(), "dypai-capability-kits", slug, contentHash)
230
+ const kitDirPath = join(cacheRoot, "kits", slug, version)
231
+ const localManifestPath = join(kitDirPath, "manifest.files.json")
232
+
233
+ if (existsSync(localManifestPath) && cachedRemoteKitComplete(kitDirPath, manifest)) {
234
+ return cacheRoot
235
+ }
236
+
237
+ mkdirSync(kitDirPath, { recursive: true })
238
+ for (const file of manifest.files || []) {
239
+ const rel = safeLogicalPath(file.path)
240
+ const buffer = await r2Get(config, remoteKitKey(config, rel))
241
+ if (file.sha256 && sha256Buffer(buffer) !== file.sha256) {
242
+ throw new Error(`Capability kit file hash mismatch for ${slug}/${rel}`)
243
+ }
244
+ const target = join(kitDirPath, rel)
245
+ mkdirSync(dirname(target), { recursive: true })
246
+ writeFileSync(target, buffer)
247
+ }
248
+ writeFileSync(localManifestPath, manifestText)
249
+ return cacheRoot
250
+ }
251
+
252
+ async function resolveKitsRootForKit({ kits_root, slug, version, source_uri }) {
253
+ const sourceMode = (process.env.DYPAI_CAPABILITY_KITS_SOURCE || "").toLowerCase()
254
+ const localRoot = sourceMode === "remote" ? null : tryResolveKitsRoot(kits_root)
255
+ if (hasLocalKit(localRoot, slug, version)) return { root: localRoot, source: "local_repo" }
256
+ const remoteRoot = await downloadRemoteCapabilityKit(slug, version, source_uri)
257
+ return { root: remoteRoot, source: "r2" }
258
+ }
259
+
260
+ function shouldSearchRemote(kitsRoot) {
261
+ const source = (process.env.DYPAI_CAPABILITY_KITS_SOURCE || "").toLowerCase()
262
+ if (source === "local") return false
263
+ if (source === "remote") return true
264
+ if (kitsRoot || process.env.DYPAI_CAPABILITY_KITS_ROOT) return false
265
+ return Boolean(process.env.DYPAI_TOKEN)
266
+ }
267
+
53
268
  function resolveWorkspaceRoot(inputRoot) {
54
269
  if (inputRoot) {
55
270
  const root = resolve(inputRoot)
@@ -306,7 +521,7 @@ function writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, nami
306
521
  if (overwrite === "fail") throw new Error(`Target already exists: ${targetRel}`)
307
522
  if (overwrite !== "replace" || !ownedTargets(record, manifest.slug, manifest.version).has(targetRel)) {
308
523
  skipped.push(targetRel)
309
- return
524
+ return false
310
525
  }
311
526
  }
312
527
 
@@ -319,6 +534,7 @@ function writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, nami
319
534
  copyFileSync(sourceAbs, targetAbs)
320
535
  }
321
536
  copied.push(targetRel)
537
+ return true
322
538
  }
323
539
 
324
540
  export const searchCapabilityKitsTool = {
@@ -330,11 +546,24 @@ export const searchCapabilityKitsTool = {
330
546
  query: { type: "string", description: "Feature need and app domain, e.g. booking calendar for hotel reservations." },
331
547
  limit: { type: "integer", default: DEFAULT_LIMIT, minimum: 1, maximum: 10 },
332
548
  filters: { type: "object", description: "Optional filters: category, maturity, app_type, screen_type, feature_tag, requires." },
333
- kits_root: { type: "string", description: "Optional local path to dypai-capability-kits. Defaults to DYPAI_CAPABILITY_KITS_ROOT or sibling repo." },
549
+ kits_root: { type: "string", description: "Optional local path to dypai-capability-kits. When omitted, the tool may use the remote MCP registry so local behavior matches production." },
334
550
  },
335
551
  required: ["query"],
336
552
  },
337
553
  async execute({ query, limit = DEFAULT_LIMIT, filters = {}, kits_root }) {
554
+ if (shouldSearchRemote(kits_root)) {
555
+ try {
556
+ const remote = await proxyToolCall("search_capability_kits", { query, limit, filters })
557
+ if (isObject(remote)) {
558
+ return { ...remote, source: remote.source || "mcp_cloud" }
559
+ }
560
+ return remote
561
+ } catch (error) {
562
+ if (!tryResolveKitsRoot(kits_root)) throw error
563
+ process.stderr.write(`Capability kit remote search failed, falling back to local repo: ${error.message}\n`)
564
+ }
565
+ }
566
+
338
567
  const root = resolveKitsRoot(kits_root)
339
568
  const kits = listKits(root)
340
569
  .filter(({ manifest }) => matchesFilter(manifest, filters))
@@ -368,13 +597,13 @@ export const searchCapabilityKitsTool = {
368
597
  },
369
598
  }
370
599
 
371
- function inspectCapabilityKit({ slug, version = "1.0.0", kits_root }) {
372
- const root = resolveKitsRoot(kits_root)
600
+ async function inspectCapabilityKit({ slug, version = "1.0.0", kits_root, source_uri }) {
601
+ const { root, source } = await resolveKitsRootForKit({ kits_root, slug, version, source_uri })
373
602
  const { dir, manifest } = loadKit(root, slug, version)
374
603
  return {
375
604
  ok: true,
376
605
  operation: "inspect",
377
- source: "local_repo",
606
+ source,
378
607
  kitsRoot: root,
379
608
  kit: manifest,
380
609
  assets: assetIndex(dir),
@@ -395,13 +624,20 @@ async function applyCapabilityKit({
395
624
  overwrite = "skip",
396
625
  workspace_root,
397
626
  kits_root,
627
+ source_uri,
398
628
  }) {
399
- const root = resolveKitsRoot(kits_root)
629
+ const { root, source } = await resolveKitsRootForKit({ kits_root, slug, version, source_uri })
400
630
  const workspaceRoot = resolveWorkspaceRoot(workspace_root)
401
631
  const { dir, manifest } = loadKit(root, slug, version)
402
632
  const record = readInstallRecord(workspaceRoot)
403
633
  const previousEntries = (record.kits || []).filter((item) => item.slug === manifest.slug && item.version === manifest.version)
404
634
  const copied = []
635
+ const copiedByKind = {
636
+ frontend: [],
637
+ backend: [],
638
+ database: [],
639
+ }
640
+ const databaseTargetsBySource = new Map()
405
641
  const skipped = []
406
642
 
407
643
  const installFrontend = install.frontend !== false
@@ -416,7 +652,9 @@ async function applyCapabilityKit({
416
652
  if (target.frontendDir) {
417
653
  targetRel = join(target.frontendDir, stripFrontendPrefix(asset.source)).split(sep).join("/")
418
654
  }
419
- writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "frontend", overwrite, record, copied, skipped })
655
+ if (writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "frontend", overwrite, record, copied, skipped })) {
656
+ copiedByKind.frontend.push(targetRel)
657
+ }
420
658
  }
421
659
  }
422
660
 
@@ -429,7 +667,9 @@ async function applyCapabilityKit({
429
667
  if (target.endpointDir) {
430
668
  targetRel = join(target.endpointDir, adaptTarget(asset.source.split("/").pop(), manifest, naming)).split(sep).join("/")
431
669
  }
432
- writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "backend", overwrite, record, copied, skipped })
670
+ if (writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "backend", overwrite, record, copied, skipped })) {
671
+ copiedByKind.backend.push(targetRel)
672
+ }
433
673
  }
434
674
  }
435
675
 
@@ -443,7 +683,10 @@ async function applyCapabilityKit({
443
683
  if (target.databaseDir) {
444
684
  targetRel = join(target.databaseDir, source.split("/").pop()).split(sep).join("/")
445
685
  }
446
- writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "database", overwrite, record, copied, skipped })
686
+ if (writeTemplateFile({ sourceAbs, targetRel, workspaceRoot, manifest, naming, kind: "database", overwrite, record, copied, skipped })) {
687
+ copiedByKind.database.push(targetRel)
688
+ databaseTargetsBySource.set(source, targetRel)
689
+ }
447
690
  }
448
691
  }
449
692
 
@@ -460,6 +703,14 @@ async function applyCapabilityKit({
460
703
  const databaseTables = normalizeList(manifest.databaseManifest?.tables).map((table) =>
461
704
  adaptTarget(table, manifest, naming),
462
705
  )
706
+ const schemaSource = installDatabase && isObject(manifest.databaseManifest) && typeof manifest.databaseManifest.schema === "string"
707
+ ? manifest.databaseManifest.schema
708
+ : ""
709
+ const seedSource = installDatabase && isObject(manifest.databaseManifest) && typeof manifest.databaseManifest.seed === "string"
710
+ ? manifest.databaseManifest.seed
711
+ : ""
712
+ const schemaFile = schemaSource ? databaseTargetsBySource.get(schemaSource) : undefined
713
+ const seedFile = seedSource ? databaseTargetsBySource.get(seedSource) : undefined
463
714
 
464
715
  const previousFiles = previousEntries.flatMap((item) => Array.isArray(item.files) ? item.files : [])
465
716
  const fileMap = new Map()
@@ -488,11 +739,15 @@ async function applyCapabilityKit({
488
739
  return {
489
740
  ok: true,
490
741
  operation: "apply",
742
+ source,
491
743
  slug: manifest.slug,
492
744
  version: manifest.version,
493
745
  workspaceRoot,
494
746
  installed: {
495
747
  files: copied,
748
+ frontendFiles: copiedByKind.frontend,
749
+ backendFiles: copiedByKind.backend,
750
+ databaseFiles: copiedByKind.database,
496
751
  skipped,
497
752
  recordFile: ".dypai/kits/installed.json",
498
753
  },
@@ -503,7 +758,12 @@ async function applyCapabilityKit({
503
758
  },
504
759
  database: {
505
760
  tables: databaseTables,
761
+ sqlFiles: copiedByKind.database,
762
+ schemaFile,
763
+ seedFile,
506
764
  requiresExecuteSql: Boolean(manifest.install?.requiresExecuteSql || databaseTables.length),
765
+ applyWith: "execute_sql",
766
+ schemaRefresh: "automatic_after_successful_ddl",
507
767
  },
508
768
  dependencies,
509
769
  nextSteps: [
@@ -511,7 +771,9 @@ async function applyCapabilityKit({
511
771
  ? [`Add any missing package dependencies to the target workspace package.json, then install them locally before building: ${dependencies.join(", ")}.`]
512
772
  : []),
513
773
  "Wire installed frontend components into the selected app page or route.",
514
- ...(databaseTables.length ? ["Apply the installed schema SQL with execute_sql if the tables do not already exist."] : []),
774
+ ...(databaseTables.length ? [
775
+ `Before backend tests, ensure tables exist (${databaseTables.join(", ")}): read ${schemaFile || "the installed schema SQL"} and execute its safe CREATE/ALTER statements with execute_sql. Do not edit dypai/schema.sql; it refreshes automatically.`,
776
+ ] : []),
515
777
  ...(backendEndpoints.length ? ["Run backend validation and endpoint tests for installed/renamed endpoints."] : []),
516
778
  "Run frontend verification after wiring imports and data callbacks.",
517
779
  ],
@@ -527,6 +789,7 @@ export const manageCapabilityKitTool = {
527
789
  operation: { type: "string", enum: ["inspect", "apply"], description: "inspect returns manifest/assets. apply copies kit files into the workspace." },
528
790
  slug: { type: "string" },
529
791
  version: { type: "string", default: "1.0.0" },
792
+ source_uri: { type: "string", description: "Optional r2://... kit root from search results. Used when the kit is not available locally." },
530
793
  targetFeature: { type: "string", description: "Domain/feature being built, e.g. hotel reservations." },
531
794
  install: {
532
795
  type: "object",
@@ -555,7 +818,7 @@ export const manageCapabilityKitTool = {
555
818
  },
556
819
  overwrite: { type: "string", enum: ["skip", "fail", "replace"], default: "skip" },
557
820
  workspace_root: { type: "string", description: "Optional absolute path to the target app workspace." },
558
- kits_root: { type: "string", description: "Optional local path to dypai-capability-kits." },
821
+ kits_root: { type: "string", description: "Optional local path to dypai-capability-kits. Pass this to force local authoring source; omit it to allow R2 fallback." },
559
822
  },
560
823
  required: ["operation", "slug"],
561
824
  },
@@ -19,9 +19,9 @@ Scaffolds a project directory with:
19
19
  - MCP config for your IDE
20
20
  - .env with engine URL
21
21
 
22
- Use search_project_templates first to find available templates, then pass the template slug here.
23
- Use "private-admin" for private internal tools, "user-accounts" for apps with signup/login users,
24
- "landing-admin" for public landing plus admin, or "blank" only when no base fits.`,
22
+ Use search_project_templates first, then pass the returned template slug here.
23
+ Prefer Studio catalog template slugs when a strong brief fits. Legacy fallback
24
+ bases ("private-admin", "user-accounts", "landing-admin", "blank") remain available when no catalog template fits.`,
25
25
 
26
26
  inputSchema: {
27
27
  type: "object",
@@ -36,7 +36,7 @@ Use "private-admin" for private internal tools, "user-accounts" for apps with si
36
36
  },
37
37
  template: {
38
38
  type: "string",
39
- description: 'Template slug (e.g. "clinic", "gym", "private-admin", "user-accounts", "landing-admin", "blank"). Use search_project_templates to find available templates.',
39
+ description: 'Template slug returned by search_project_templates. Studio catalog slugs are preferred; legacy fallback bases remain supported.',
40
40
  default: "blank",
41
41
  },
42
42
  },
@@ -60,7 +60,7 @@ Use "private-admin" for private internal tools, "user-accounts" for apps with si
60
60
  // Try to download template from API
61
61
  let files = []
62
62
  try {
63
- const res = await api.get(`/api/engine/${project_id}/frontend/template?slug=${template}`)
63
+ const res = await api.get(`/api/engine/${project_id}/frontend/template?template=${encodeURIComponent(template)}`)
64
64
  if (res.files) files = res.files
65
65
  } catch {}
66
66