@sundaeswap/sprinkles 0.7.0 → 0.8.1

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.
Files changed (95) hide show
  1. package/README.md +178 -181
  2. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js +4 -4
  3. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js.map +1 -1
  4. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js +25 -3
  5. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js.map +1 -1
  6. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js +15 -1
  7. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  8. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js +7 -9
  9. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -1
  10. package/dist/cjs/Sprinkle/__tests__/native-script.test.js +390 -0
  11. package/dist/cjs/Sprinkle/__tests__/native-script.test.js.map +1 -0
  12. package/dist/cjs/Sprinkle/__tests__/utility-actions.test.js +367 -0
  13. package/dist/cjs/Sprinkle/__tests__/utility-actions.test.js.map +1 -0
  14. package/dist/cjs/Sprinkle/actions/builtin/addressbook-actions.js +164 -0
  15. package/dist/cjs/Sprinkle/actions/builtin/addressbook-actions.js.map +1 -0
  16. package/dist/cjs/Sprinkle/actions/builtin/index.js +60 -3
  17. package/dist/cjs/Sprinkle/actions/builtin/index.js.map +1 -1
  18. package/dist/cjs/Sprinkle/actions/builtin/native-script.js +139 -0
  19. package/dist/cjs/Sprinkle/actions/builtin/native-script.js.map +1 -0
  20. package/dist/cjs/Sprinkle/actions/builtin/utility-actions.js +218 -0
  21. package/dist/cjs/Sprinkle/actions/builtin/utility-actions.js.map +1 -0
  22. package/dist/cjs/Sprinkle/actions/cli-adapter.js +20 -2
  23. package/dist/cjs/Sprinkle/actions/cli-adapter.js.map +1 -1
  24. package/dist/cjs/Sprinkle/actions/index.js +12 -0
  25. package/dist/cjs/Sprinkle/actions/index.js.map +1 -1
  26. package/dist/cjs/Sprinkle/actions/mcp-adapter.js +146 -4
  27. package/dist/cjs/Sprinkle/actions/mcp-adapter.js.map +1 -1
  28. package/dist/cjs/Sprinkle/index.js +282 -6
  29. package/dist/cjs/Sprinkle/index.js.map +1 -1
  30. package/dist/cjs/Sprinkle/schemas.js +17 -1
  31. package/dist/cjs/Sprinkle/schemas.js.map +1 -1
  32. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js +4 -4
  33. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js.map +1 -1
  34. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js +25 -3
  35. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js.map +1 -1
  36. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js +15 -1
  37. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  38. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js +7 -9
  39. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -1
  40. package/dist/esm/Sprinkle/__tests__/native-script.test.js +388 -0
  41. package/dist/esm/Sprinkle/__tests__/native-script.test.js.map +1 -0
  42. package/dist/esm/Sprinkle/__tests__/utility-actions.test.js +365 -0
  43. package/dist/esm/Sprinkle/__tests__/utility-actions.test.js.map +1 -0
  44. package/dist/esm/Sprinkle/actions/builtin/addressbook-actions.js +159 -0
  45. package/dist/esm/Sprinkle/actions/builtin/addressbook-actions.js.map +1 -0
  46. package/dist/esm/Sprinkle/actions/builtin/index.js +8 -3
  47. package/dist/esm/Sprinkle/actions/builtin/index.js.map +1 -1
  48. package/dist/esm/Sprinkle/actions/builtin/native-script.js +133 -0
  49. package/dist/esm/Sprinkle/actions/builtin/native-script.js.map +1 -0
  50. package/dist/esm/Sprinkle/actions/builtin/utility-actions.js +213 -0
  51. package/dist/esm/Sprinkle/actions/builtin/utility-actions.js.map +1 -0
  52. package/dist/esm/Sprinkle/actions/cli-adapter.js +20 -2
  53. package/dist/esm/Sprinkle/actions/cli-adapter.js.map +1 -1
  54. package/dist/esm/Sprinkle/actions/index.js +1 -1
  55. package/dist/esm/Sprinkle/actions/index.js.map +1 -1
  56. package/dist/esm/Sprinkle/actions/mcp-adapter.js +145 -5
  57. package/dist/esm/Sprinkle/actions/mcp-adapter.js.map +1 -1
  58. package/dist/esm/Sprinkle/index.js +260 -9
  59. package/dist/esm/Sprinkle/index.js.map +1 -1
  60. package/dist/esm/Sprinkle/schemas.js +16 -0
  61. package/dist/esm/Sprinkle/schemas.js.map +1 -1
  62. package/dist/types/Sprinkle/actions/builtin/addressbook-actions.d.ts +50 -0
  63. package/dist/types/Sprinkle/actions/builtin/addressbook-actions.d.ts.map +1 -0
  64. package/dist/types/Sprinkle/actions/builtin/index.d.ts +6 -2
  65. package/dist/types/Sprinkle/actions/builtin/index.d.ts.map +1 -1
  66. package/dist/types/Sprinkle/actions/builtin/native-script.d.ts +27 -0
  67. package/dist/types/Sprinkle/actions/builtin/native-script.d.ts.map +1 -0
  68. package/dist/types/Sprinkle/actions/builtin/utility-actions.d.ts +48 -0
  69. package/dist/types/Sprinkle/actions/builtin/utility-actions.d.ts.map +1 -0
  70. package/dist/types/Sprinkle/actions/cli-adapter.d.ts.map +1 -1
  71. package/dist/types/Sprinkle/actions/index.d.ts +2 -1
  72. package/dist/types/Sprinkle/actions/index.d.ts.map +1 -1
  73. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts +24 -0
  74. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts.map +1 -1
  75. package/dist/types/Sprinkle/index.d.ts +5 -2
  76. package/dist/types/Sprinkle/index.d.ts.map +1 -1
  77. package/dist/types/Sprinkle/schemas.d.ts +72 -0
  78. package/dist/types/Sprinkle/schemas.d.ts.map +1 -1
  79. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  80. package/package.json +1 -1
  81. package/src/Sprinkle/__tests__/builtin-actions.test.ts +4 -4
  82. package/src/Sprinkle/__tests__/cli-adapter.test.ts +24 -3
  83. package/src/Sprinkle/__tests__/fill-in-struct.test.ts +23 -1
  84. package/src/Sprinkle/__tests__/mcp-adapter.test.ts +7 -5
  85. package/src/Sprinkle/__tests__/native-script.test.ts +341 -0
  86. package/src/Sprinkle/__tests__/utility-actions.test.ts +348 -0
  87. package/src/Sprinkle/actions/builtin/addressbook-actions.ts +168 -0
  88. package/src/Sprinkle/actions/builtin/index.ts +41 -2
  89. package/src/Sprinkle/actions/builtin/native-script.ts +165 -0
  90. package/src/Sprinkle/actions/builtin/utility-actions.ts +285 -0
  91. package/src/Sprinkle/actions/cli-adapter.ts +18 -2
  92. package/src/Sprinkle/actions/index.ts +2 -1
  93. package/src/Sprinkle/actions/mcp-adapter.ts +179 -4
  94. package/src/Sprinkle/index.ts +264 -3
  95. package/src/Sprinkle/schemas.ts +20 -0
@@ -7,6 +7,6 @@ export { ActionRegistry } from "./registry.js";
7
7
  export { executeAction, detectMode, parseCliArgs } from "./runner.js";
8
8
  export { camelToKebab, kebabToCamel, coerceValue, parseArgvWithSchema, generateActionHelp, generateAppHelp, runCli } from "./cli-adapter.js";
9
9
  export { typeboxToJsonSchema, coerceMcpInput, getMcpSdk, createMcpServer, runMcp } from "./mcp-adapter.js";
10
- export { getBuiltinActions } from "./builtin/index.js";
10
+ export { getBuiltinActions, toNativeScript, completeWithScripts } from "./builtin/index.js";
11
11
  export { promptAndExecute } from "./tui-helpers.js";
12
12
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["ActionError","ActionRegistry","executeAction","detectMode","parseCliArgs","camelToKebab","kebabToCamel","coerceValue","parseArgvWithSchema","generateActionHelp","generateAppHelp","runCli","typeboxToJsonSchema","coerceMcpInput","getMcpSdk","createMcpServer","runMcp","getBuiltinActions","promptAndExecute"],"sources":["../../../../src/Sprinkle/actions/index.ts"],"sourcesContent":["/**\n * Barrel export for the actions subsystem.\n */\n\nexport type { IAction, IActionContext, IActionResult, IActionSuccess, IActionFailure, AnyAction } from \"./types.js\";\nexport { ActionError } from \"./types.js\";\n\nexport { ActionRegistry } from \"./registry.js\";\n\nexport { executeAction, detectMode, parseCliArgs } from \"./runner.js\";\n\nexport {\n camelToKebab,\n kebabToCamel,\n coerceValue,\n parseArgvWithSchema,\n generateActionHelp,\n generateAppHelp,\n runCli,\n} from \"./cli-adapter.js\";\n\nexport {\n typeboxToJsonSchema,\n coerceMcpInput,\n getMcpSdk,\n createMcpServer,\n runMcp,\n} from \"./mcp-adapter.js\";\n\nexport { getBuiltinActions } from \"./builtin/index.js\";\n\nexport { promptAndExecute } from \"./tui-helpers.js\";\n"],"mappings":"AAAA;AACA;AACA;;AAGA,SAASA,WAAW,QAAQ,YAAY;AAExC,SAASC,cAAc,QAAQ,eAAe;AAE9C,SAASC,aAAa,EAAEC,UAAU,EAAEC,YAAY,QAAQ,aAAa;AAErE,SACEC,YAAY,EACZC,YAAY,EACZC,WAAW,EACXC,mBAAmB,EACnBC,kBAAkB,EAClBC,eAAe,EACfC,MAAM,QACD,kBAAkB;AAEzB,SACEC,mBAAmB,EACnBC,cAAc,EACdC,SAAS,EACTC,eAAe,EACfC,MAAM,QACD,kBAAkB;AAEzB,SAASC,iBAAiB,QAAQ,oBAAoB;AAEtD,SAASC,gBAAgB,QAAQ,kBAAkB","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["ActionError","ActionRegistry","executeAction","detectMode","parseCliArgs","camelToKebab","kebabToCamel","coerceValue","parseArgvWithSchema","generateActionHelp","generateAppHelp","runCli","typeboxToJsonSchema","coerceMcpInput","getMcpSdk","createMcpServer","runMcp","getBuiltinActions","toNativeScript","completeWithScripts","promptAndExecute"],"sources":["../../../../src/Sprinkle/actions/index.ts"],"sourcesContent":["/**\n * Barrel export for the actions subsystem.\n */\n\nexport type { IAction, IActionContext, IActionResult, IActionSuccess, IActionFailure, AnyAction } from \"./types.js\";\nexport { ActionError } from \"./types.js\";\n\nexport { ActionRegistry } from \"./registry.js\";\n\nexport { executeAction, detectMode, parseCliArgs } from \"./runner.js\";\n\nexport {\n camelToKebab,\n kebabToCamel,\n coerceValue,\n parseArgvWithSchema,\n generateActionHelp,\n generateAppHelp,\n runCli,\n} from \"./cli-adapter.js\";\n\nexport {\n typeboxToJsonSchema,\n coerceMcpInput,\n getMcpSdk,\n createMcpServer,\n runMcp,\n} from \"./mcp-adapter.js\";\n\nexport { getBuiltinActions, toNativeScript, completeWithScripts } from \"./builtin/index.js\";\nexport type { NativeScriptInput } from \"./builtin/index.js\";\n\nexport { promptAndExecute } from \"./tui-helpers.js\";\n"],"mappings":"AAAA;AACA;AACA;;AAGA,SAASA,WAAW,QAAQ,YAAY;AAExC,SAASC,cAAc,QAAQ,eAAe;AAE9C,SAASC,aAAa,EAAEC,UAAU,EAAEC,YAAY,QAAQ,aAAa;AAErE,SACEC,YAAY,EACZC,YAAY,EACZC,WAAW,EACXC,mBAAmB,EACnBC,kBAAkB,EAClBC,eAAe,EACfC,MAAM,QACD,kBAAkB;AAEzB,SACEC,mBAAmB,EACnBC,cAAc,EACdC,SAAS,EACTC,eAAe,EACfC,MAAM,QACD,kBAAkB;AAEzB,SAASC,iBAAiB,EAAEC,cAAc,EAAEC,mBAAmB,QAAQ,oBAAoB;AAG3F,SAASC,gBAAgB,QAAQ,kBAAkB","ignoreList":[]}
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * Provides:
5
5
  * - TypeBox to JSON Schema conversion (typeboxToJsonSchema)
6
+ * - TypeBox to Zod conversion for MCP SDK compatibility (typeboxToZod)
6
7
  * - BigInt string coercion for MCP input (coerceMcpInput)
7
8
  * - Lazy MCP SDK import with graceful error (getMcpSdk)
8
9
  * - MCP server creation from registered actions (createMcpServer)
@@ -13,7 +14,7 @@ import { createRequire } from "module";
13
14
  import { fileURLToPath } from "url";
14
15
  import { dirname, resolve } from "path";
15
16
  import { bigIntReplacer } from "../encryption.js";
16
- import { isArray, isBigInt, isBoolean, isInteger, isLiteral, isNull, isNumber, isObject, isString, isUnion, isSensitive } from "../type-guards.js";
17
+ import { isArray, isBigInt, isBoolean, isInteger, isLiteral, isNull, isNumber, isObject, isOptional, isString, isUnion, isSensitive } from "../type-guards.js";
17
18
  import { executeAction } from "./runner.js";
18
19
 
19
20
  // Re-import Sprinkle as a type only to avoid circular deps
@@ -257,6 +258,145 @@ export function coerceMcpInput(input, schema) {
257
258
  return input;
258
259
  }
259
260
 
261
+ // ---------------------------------------------------------------------------
262
+ // TypeBox to Zod conversion
263
+ // ---------------------------------------------------------------------------
264
+
265
+ /**
266
+ * Zod module shape (the parts we use from zod).
267
+ * Typed loosely since zod is a transitive dependency of @modelcontextprotocol/sdk,
268
+ * not a direct dependency of Sprinkles.
269
+ */
270
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
271
+
272
+ let _zod;
273
+
274
+ /**
275
+ * Lazily import the Zod module.
276
+ * Zod is a transitive dependency of @modelcontextprotocol/sdk, so it's
277
+ * guaranteed to be available when MCP mode is used.
278
+ */
279
+ async function getZod() {
280
+ if (_zod) return _zod;
281
+ try {
282
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
283
+ const mod = await import("zod");
284
+ _zod = {
285
+ string: () => mod.string(),
286
+ number: () => mod.number(),
287
+ boolean: () => mod.boolean(),
288
+ literal: v => mod.literal(v),
289
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
290
+ object: shape => mod.object(shape),
291
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
292
+ array: item => mod.array(item),
293
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
294
+ union: members => mod.union(members),
295
+ nullType: () => mod.nullType(),
296
+ any: () => mod.any()
297
+ };
298
+ return _zod;
299
+ } catch {
300
+ throw new Error("MCP mode requires zod (via @modelcontextprotocol/sdk). Install @modelcontextprotocol/sdk.");
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Convert a TypeBox schema to a Zod schema for MCP SDK compatibility.
306
+ *
307
+ * The MCP SDK (v1.27+) expects Zod schemas for tool input validation,
308
+ * not plain JSON Schema objects. This function converts TypeBox schemas
309
+ * to their Zod equivalents so the SDK correctly registers tool parameters.
310
+ *
311
+ * BigInt fields are represented as z.string() with a regex pattern since
312
+ * JSON (and thus MCP) has no BigInt type. The coerceMcpInput function
313
+ * handles converting these string values back to BigInt at call time.
314
+ */
315
+ export async function typeboxToZod(schema) {
316
+ const z = await getZod();
317
+ return _typeboxToZod(schema, z);
318
+ }
319
+ function _typeboxToZod(schema, z) {
320
+ if (isBigInt(schema)) {
321
+ // BigInt -> string with regex pattern (JSON has no BigInt)
322
+ return z.string().regex(/^-?[0-9]+$/);
323
+ }
324
+ if (isString(schema)) {
325
+ let s = z.string();
326
+ if (schema.description) s = s.describe(schema.description);
327
+ return s;
328
+ }
329
+ if (isNumber(schema) || isInteger(schema)) {
330
+ let n = z.number();
331
+ if (schema.description) n = n.describe(schema.description);
332
+ return n;
333
+ }
334
+ if (isBoolean(schema)) {
335
+ let b = z.boolean();
336
+ if (schema.description) b = b.describe(schema.description);
337
+ return b;
338
+ }
339
+ if (isNull(schema)) {
340
+ return z.nullType();
341
+ }
342
+ if (isLiteral(schema)) {
343
+ return z.literal(schema.const);
344
+ }
345
+ if (isArray(schema)) {
346
+ const itemSchema = schema.items;
347
+ if (itemSchema) {
348
+ return z.array(_typeboxToZod(itemSchema, z));
349
+ }
350
+ return z.array(z.any());
351
+ }
352
+ if (isObject(schema)) {
353
+ const properties = schema.properties;
354
+ const shape = {};
355
+ for (const [propName, propSchema] of Object.entries(properties)) {
356
+ let zodProp = _typeboxToZod(propSchema, z);
357
+ if (isOptional(propSchema)) {
358
+ zodProp = zodProp.optional();
359
+ }
360
+ shape[propName] = zodProp;
361
+ }
362
+ return z.object(shape);
363
+ }
364
+ if (isUnion(schema)) {
365
+ const members = schema.anyOf.map(member => _typeboxToZod(member, z));
366
+ if (members.length >= 2) {
367
+ return z.union(members);
368
+ }
369
+ return members[0] ?? z.any();
370
+ }
371
+
372
+ // Fallback: accept anything
373
+ return z.any();
374
+ }
375
+
376
+ /**
377
+ * Convert a TypeBox object schema to a Zod "raw shape" (Record<string, ZodType>).
378
+ * This is the format expected by the MCP SDK's tool() API.
379
+ */
380
+ export async function typeboxToZodShape(schema) {
381
+ const z = await getZod();
382
+ if (!isObject(schema)) {
383
+ // Non-object input schemas get wrapped as { input: zodSchema }
384
+ return {
385
+ input: _typeboxToZod(schema, z)
386
+ };
387
+ }
388
+ const properties = schema.properties;
389
+ const shape = {};
390
+ for (const [propName, propSchema] of Object.entries(properties)) {
391
+ let zodProp = _typeboxToZod(propSchema, z);
392
+ if (isOptional(propSchema)) {
393
+ zodProp = zodProp.optional();
394
+ }
395
+ shape[propName] = zodProp;
396
+ }
397
+ return shape;
398
+ }
399
+
260
400
  // ---------------------------------------------------------------------------
261
401
  // Lazy MCP SDK import
262
402
  // ---------------------------------------------------------------------------
@@ -326,12 +466,12 @@ export async function createMcpServer(sprinkle, serverName
326
466
  });
327
467
  const actions = sprinkle.listActions();
328
468
  for (const action of actions) {
329
- const inputSchema = typeboxToJsonSchema(action.inputSchema);
469
+ const zodShape = await typeboxToZodShape(action.inputSchema);
330
470
 
331
471
  // Register the action as an MCP tool.
332
- // The high-level McpServer.tool() API accepts:
333
- // tool(name, description, schema, handler)
334
- server.tool(action.name, action.description, inputSchema,
472
+ // The high-level McpServer.tool() API accepts a Zod raw shape
473
+ // (Record<string, ZodType>) for input validation.
474
+ server.tool(action.name, action.description, zodShape,
335
475
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
336
476
  async input => {
337
477
  const coercedInput = coerceMcpInput(input, action.inputSchema);
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-adapter.js","names":["createRequire","fileURLToPath","dirname","resolve","bigIntReplacer","isArray","isBigInt","isBoolean","isInteger","isLiteral","isNull","isNumber","isObject","isString","isUnion","isSensitive","executeAction","loadPackageVersion","require","import","meta","url","__filename","__dirname","candidates","candidate","pkg","version","PACKAGE_VERSION","typeboxToJsonSchema","schema","type","pattern","description","result","undefined","title","examples","minLength","maxLength","default","writeOnly","minimum","maximum","itemSchema","items","minItems","maxItems","properties","required","convertedProperties","propName","propSchema","Object","entries","length","anyOf","map","member","value","const","console","warn","coerceMcpInput","input","test","BigInt","Math","trunc","Array","item","getMcpSdk","Error","getMcpStdioTransport","createMcpServer","sprinkle","serverName","McpServer","server","name","actions","listActions","action","inputSchema","tool","coercedInput","context","settings","success","content","text","JSON","stringify","data","error","code","message","details","isError","runMcp","StdioServerTransport","transport","connect","err","process","stderr","write","String"],"sources":["../../../../src/Sprinkle/actions/mcp-adapter.ts"],"sourcesContent":["/**\n * MCP adapter for Sprinkles actions.\n *\n * Provides:\n * - TypeBox to JSON Schema conversion (typeboxToJsonSchema)\n * - BigInt string coercion for MCP input (coerceMcpInput)\n * - Lazy MCP SDK import with graceful error (getMcpSdk)\n * - MCP server creation from registered actions (createMcpServer)\n * - MCP orchestrator that starts a stdio server (runMcp)\n */\n\nimport { createRequire } from \"module\";\nimport { fileURLToPath } from \"url\";\nimport { dirname, resolve } from \"path\";\nimport type { TSchema } from \"@sinclair/typebox\";\nimport { bigIntReplacer } from \"../encryption.js\";\nimport {\n isArray,\n isBigInt,\n isBoolean,\n isInteger,\n isLiteral,\n isNull,\n isNumber,\n isObject,\n isOptional,\n isString,\n isUnion,\n isSensitive,\n} from \"../type-guards.js\";\nimport type { AnyAction } from \"./types.js\";\nimport { executeAction } from \"./runner.js\";\n\n// Re-import Sprinkle as a type only to avoid circular deps\nimport type { Sprinkle } from \"../index.js\";\n\n// Load package version for MCP server identification\n// Robustly find package.json - works from both src and dist directories\nfunction loadPackageVersion(): string {\n const require = createRequire(import.meta.url);\n const __filename = fileURLToPath(import.meta.url);\n const __dirname = dirname(__filename);\n \n // Try multiple possible paths (handles src vs dist/esm vs dist/cjs)\n const candidates = [\n resolve(__dirname, \"../../../package.json\"), // from src/Sprinkle/actions\n resolve(__dirname, \"../../../../package.json\"), // from dist/*/Sprinkle/actions\n ];\n \n for (const candidate of candidates) {\n try {\n const pkg = require(candidate) as { version: string };\n return pkg.version;\n } catch {\n // Try next candidate\n }\n }\n \n return \"0.0.0\"; // Fallback if package.json not found\n}\n\nconst PACKAGE_VERSION: string = loadPackageVersion();\n\n// ---------------------------------------------------------------------------\n// TypeBox to JSON Schema conversion\n// ---------------------------------------------------------------------------\n\n/**\n * Convert a TypeBox schema to a plain JSON Schema object.\n *\n * Strips TypeBox-internal symbols and properties (Kind, OptionalKind, $id)\n * and maps TypeBox-specific types to their JSON Schema equivalents:\n * - BigInt -> { type: \"string\", pattern: \"^-?[0-9]+$\" } (JSON has no BigInt)\n * - Sensitive string fields -> add writeOnly: true\n * - Optional -> recurse on inner schema (Optional does not change Kind)\n * - Union -> { anyOf: [...] }\n * - Literal -> { type, const }\n * - Null -> { type: \"null\" }\n * - Unknown/unsupported -> {} (accepts anything)\n */\nexport function typeboxToJsonSchema(schema: TSchema): Record<string, unknown> {\n // Optional in TypeBox doesn't change the Kind, but marks the schema with\n // OptionalKind. We can check for it and recurse transparently since all\n // other guards still work correctly on optional-wrapped schemas.\n //\n // We intentionally do NOT strip optional here -- the containing Object\n // converter decides whether to include a property in `required` based\n // on the isOptional guard applied to the property schema.\n\n if (isBigInt(schema)) {\n return {\n type: \"string\",\n pattern: \"^-?[0-9]+$\",\n description: \"BigInt value as string\",\n };\n }\n\n if (isString(schema)) {\n const result: Record<string, unknown> = { type: \"string\" };\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.title !== undefined) result.title = schema.title;\n if (schema.examples !== undefined) result.examples = schema.examples;\n if (schema.pattern !== undefined) result.pattern = schema.pattern;\n if (schema.minLength !== undefined) result.minLength = schema.minLength;\n if (schema.maxLength !== undefined) result.maxLength = schema.maxLength;\n if (schema.default !== undefined) result.default = schema.default;\n // Sensitive fields are write-only (never returned in responses)\n if (isSensitive(schema)) result.writeOnly = true;\n return result;\n }\n\n if (isNumber(schema)) {\n const result: Record<string, unknown> = { type: \"number\" };\n if (schema.minimum !== undefined) result.minimum = schema.minimum;\n if (schema.maximum !== undefined) result.maximum = schema.maximum;\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isInteger(schema)) {\n const result: Record<string, unknown> = { type: \"integer\" };\n if (schema.minimum !== undefined) result.minimum = schema.minimum;\n if (schema.maximum !== undefined) result.maximum = schema.maximum;\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isBoolean(schema)) {\n const result: Record<string, unknown> = { type: \"boolean\" };\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isNull(schema)) {\n return { type: \"null\" };\n }\n\n if (isArray(schema)) {\n const result: Record<string, unknown> = { type: \"array\" };\n const itemSchema = (schema as { items?: TSchema }).items;\n if (itemSchema) {\n result.items = typeboxToJsonSchema(itemSchema);\n }\n if (schema.minItems !== undefined) result.minItems = schema.minItems;\n if (schema.maxItems !== undefined) result.maxItems = schema.maxItems;\n if (schema.description !== undefined) result.description = schema.description;\n return result;\n }\n\n if (isObject(schema)) {\n const properties = schema.properties as Record<string, TSchema>;\n const required: string[] = schema.required as string[] ?? [];\n const convertedProperties: Record<string, unknown> = {};\n\n for (const [propName, propSchema] of Object.entries(properties)) {\n convertedProperties[propName] = typeboxToJsonSchema(propSchema);\n }\n\n const result: Record<string, unknown> = {\n type: \"object\",\n properties: convertedProperties,\n };\n\n if (required.length > 0) {\n result.required = required;\n }\n\n if (schema.description !== undefined) result.description = schema.description;\n\n return result;\n }\n\n if (isUnion(schema)) {\n return {\n anyOf: schema.anyOf.map((member: TSchema) => typeboxToJsonSchema(member)),\n };\n }\n\n if (isLiteral(schema)) {\n const value = schema.const;\n let type: string;\n if (typeof value === \"string\") {\n type = \"string\";\n } else if (typeof value === \"number\") {\n type = \"number\";\n } else if (typeof value === \"boolean\") {\n type = \"boolean\";\n } else {\n // Fallback for unexpected literal types\n return {};\n }\n return { type, const: value };\n }\n\n // Unknown / unsupported TypeBox type: emit empty schema (accepts anything)\n // This is a safe fallback that allows the MCP tool to still receive input.\n console.warn(\n `[mcp-adapter] Unsupported TypeBox kind: ${(schema as Record<string, unknown>)[\"[Kind]\"] ?? \"(unknown)\"}. Emitting empty schema.`,\n );\n return {};\n}\n\n// ---------------------------------------------------------------------------\n// Input coercion: BigInt string -> BigInt\n// ---------------------------------------------------------------------------\n\n/**\n * Coerce MCP JSON input values to the types expected by a TypeBox schema.\n *\n * MCP transmits all values as JSON, which has no BigInt type. When the schema\n * expects a BigInt, the value arrives as a numeric string (e.g. \"12345678\").\n * This function recursively walks the schema and input, converting BigInt\n * string values to actual BigInt where needed.\n *\n * For all other types the value is passed through unchanged.\n */\nexport function coerceMcpInput(input: unknown, schema: TSchema): unknown {\n if (input === null || input === undefined) return input;\n\n if (isBigInt(schema)) {\n if (typeof input === \"bigint\") return input;\n if (typeof input === \"string\" && /^-?[0-9]+$/.test(input)) {\n try {\n return BigInt(input);\n } catch {\n return input;\n }\n }\n if (typeof input === \"number\") {\n try {\n return BigInt(Math.trunc(input));\n } catch {\n return input;\n }\n }\n return input;\n }\n\n if (isArray(schema)) {\n const itemSchema = (schema as { items?: TSchema }).items;\n if (Array.isArray(input) && itemSchema) {\n return input.map((item) => coerceMcpInput(item, itemSchema));\n }\n return input;\n }\n\n if (isObject(schema)) {\n if (typeof input !== \"object\" || Array.isArray(input)) return input;\n const properties = schema.properties as Record<string, TSchema>;\n const result: Record<string, unknown> = { ...(input as Record<string, unknown>) };\n for (const [propName, propSchema] of Object.entries(properties)) {\n if (propName in result) {\n result[propName] = coerceMcpInput(result[propName], propSchema);\n }\n }\n return result;\n }\n\n if (isUnion(schema)) {\n // For unions, we try each member schema in order and return the first\n // successful coercion. BigInt members take priority over string members\n // since they have a narrower, unambiguous pattern.\n for (const member of schema.anyOf) {\n if (isBigInt(member)) {\n if (typeof input === \"string\" && /^-?[0-9]+$/.test(input)) {\n return coerceMcpInput(input, member);\n }\n }\n }\n // No BigInt match; return as-is for other union members\n return input;\n }\n\n // For all other types (String, Number, Integer, Boolean, Literal, Null):\n // pass through unchanged -- MCP JSON already represents them correctly\n return input;\n}\n\n// ---------------------------------------------------------------------------\n// Lazy MCP SDK import\n// ---------------------------------------------------------------------------\n\n/**\n * MCP SDK module shape (the parts we use from @modelcontextprotocol/sdk).\n * Typed loosely to avoid a hard dependency at compile time.\n */\ninterface IMcpSdkModule {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n McpServer: new (opts: { name: string; version: string }) => any;\n}\n\ninterface IStdioTransportModule {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n StdioServerTransport: new () => any;\n}\n\n/**\n * Lazily import the MCP SDK server module.\n *\n * Throws a clear, actionable error if the SDK is not installed, rather than\n * a cryptic \"Cannot find module\" Node error.\n */\nexport async function getMcpSdk(): Promise<IMcpSdkModule> {\n try {\n return (await import(\n \"@modelcontextprotocol/sdk/server/mcp.js\"\n )) as IMcpSdkModule;\n } catch {\n throw new Error(\n \"MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk\",\n );\n }\n}\n\n/**\n * Lazily import the MCP SDK stdio transport module.\n */\nasync function getMcpStdioTransport(): Promise<IStdioTransportModule> {\n try {\n return (await import(\n \"@modelcontextprotocol/sdk/server/stdio.js\"\n )) as IStdioTransportModule;\n } catch {\n throw new Error(\n \"MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk\",\n );\n }\n}\n\n// ---------------------------------------------------------------------------\n// MCP server creation\n// ---------------------------------------------------------------------------\n\n/**\n * Create an MCP server and register all actions from a Sprinkle instance as\n * MCP tools.\n *\n * Each action becomes a tool with:\n * - name: action.name\n * - description: action.description\n * - input schema: typeboxToJsonSchema(action.inputSchema)\n * - handler: coerces input, executes action, returns JSON result\n *\n * The Sprinkle instance must already be initialized with a profile before\n * this function is called.\n *\n * @param sprinkle - Fully-initialized Sprinkle instance\n * @param serverName - Name to use for the MCP server\n * @returns The configured McpServer instance (not yet connected)\n */\nexport async function createMcpServer<S extends TSchema>(\n sprinkle: Sprinkle<S>,\n serverName: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Promise<any> {\n const { McpServer } = await getMcpSdk();\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const server = new McpServer({ name: serverName, version: PACKAGE_VERSION });\n\n const actions = sprinkle.listActions() as AnyAction<S>[];\n\n for (const action of actions) {\n const inputSchema = typeboxToJsonSchema(action.inputSchema);\n\n // Register the action as an MCP tool.\n // The high-level McpServer.tool() API accepts:\n // tool(name, description, schema, handler)\n server.tool(\n action.name,\n action.description,\n inputSchema,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n async (input: unknown): Promise<any> => {\n const coercedInput = coerceMcpInput(input, action.inputSchema);\n\n const context = {\n sprinkle,\n settings: sprinkle.settings,\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await executeAction(action as any, coercedInput, context as any);\n\n if (result.success) {\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify(result.data, bigIntReplacer),\n },\n ],\n };\n } else {\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify(\n {\n error: {\n code: result.error.code,\n message: result.error.message,\n details: result.error.details,\n },\n },\n bigIntReplacer,\n ),\n },\n ],\n isError: true,\n };\n }\n },\n );\n }\n\n return server;\n}\n\n// ---------------------------------------------------------------------------\n// MCP orchestrator\n// ---------------------------------------------------------------------------\n\n/**\n * Start an MCP server on stdio transport.\n *\n * This function:\n * 1. Creates the MCP server and registers all actions as tools\n * 2. Creates a StdioServerTransport\n * 3. Connects the server to the transport\n *\n * IMPORTANT: All logging in this function goes to stderr. stdout is reserved\n * for the MCP protocol messages.\n *\n * The Sprinkle instance must already be initialized with a profile. Profile\n * initialization is the responsibility of the caller (Sprinkle.Run()).\n *\n * @param sprinkle - Fully-initialized Sprinkle instance\n * @param serverName - Name to use for the MCP server\n */\nexport async function runMcp<S extends TSchema>(\n sprinkle: Sprinkle<S>,\n serverName: string,\n): Promise<void> {\n const { StdioServerTransport } = await getMcpStdioTransport();\n\n const server = await createMcpServer(sprinkle, serverName);\n const transport = new StdioServerTransport();\n\n try {\n await server.connect(transport);\n } catch (err) {\n // Log errors to stderr only -- stdout is the MCP transport channel\n process.stderr.write(\n `[sprinkle-mcp] Failed to start MCP server: ${err instanceof Error ? err.message : String(err)}\\n`,\n );\n throw err;\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,aAAa,QAAQ,QAAQ;AACtC,SAASC,aAAa,QAAQ,KAAK;AACnC,SAASC,OAAO,EAAEC,OAAO,QAAQ,MAAM;AAEvC,SAASC,cAAc,QAAQ,kBAAkB;AACjD,SACEC,OAAO,EACPC,QAAQ,EACRC,SAAS,EACTC,SAAS,EACTC,SAAS,EACTC,MAAM,EACNC,QAAQ,EACRC,QAAQ,EAERC,QAAQ,EACRC,OAAO,EACPC,WAAW,QACN,mBAAmB;AAE1B,SAASC,aAAa,QAAQ,aAAa;;AAE3C;;AAGA;AACA;AACA,SAASC,kBAAkBA,CAAA,EAAW;EACpC,MAAMC,OAAO,GAAGlB,aAAa,CAACmB,MAAM,CAACC,IAAI,CAACC,GAAG,CAAC;EAC9C,MAAMC,UAAU,GAAGrB,aAAa,CAACkB,MAAM,CAACC,IAAI,CAACC,GAAG,CAAC;EACjD,MAAME,SAAS,GAAGrB,OAAO,CAACoB,UAAU,CAAC;;EAErC;EACA,MAAME,UAAU,GAAG,CACjBrB,OAAO,CAACoB,SAAS,EAAE,uBAAuB,CAAC;EAAM;EACjDpB,OAAO,CAACoB,SAAS,EAAE,0BAA0B,CAAC,CAAG;EAAA,CAClD;EAED,KAAK,MAAME,SAAS,IAAID,UAAU,EAAE;IAClC,IAAI;MACF,MAAME,GAAG,GAAGR,OAAO,CAACO,SAAS,CAAwB;MACrD,OAAOC,GAAG,CAACC,OAAO;IACpB,CAA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;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeqB,MAAMA,CAC1BxB,QAAqB,EACrBC,UAAkB,EACH;EACf,MAAM;IAAEwB;EAAqB,CAAC,GAAG,MAAM3B,oBAAoB,CAAC,CAAC;EAE7D,MAAMK,MAAM,GAAG,MAAMJ,eAAe,CAACC,QAAQ,EAAEC,UAAU,CAAC;EAC1D,MAAMyB,SAAS,GAAG,IAAID,oBAAoB,CAAC,CAAC;EAE5C,IAAI;IACF,MAAMtB,MAAM,CAACwB,OAAO,CAACD,SAAS,CAAC;EACjC,CAAC,CAAC,OAAOE,GAAG,EAAE;IACZ;IACAC,OAAO,CAACC,MAAM,CAACC,KAAK,CAClB,8CAA8CH,GAAG,YAAY/B,KAAK,GAAG+B,GAAG,CAACP,OAAO,GAAGW,MAAM,CAACJ,GAAG,CAAC,IAChG,CAAC;IACD,MAAMA,GAAG;EACX;AACF","ignoreList":[]}
1
+ {"version":3,"file":"mcp-adapter.js","names":["createRequire","fileURLToPath","dirname","resolve","bigIntReplacer","isArray","isBigInt","isBoolean","isInteger","isLiteral","isNull","isNumber","isObject","isOptional","isString","isUnion","isSensitive","executeAction","loadPackageVersion","require","import","meta","url","__filename","__dirname","candidates","candidate","pkg","version","PACKAGE_VERSION","typeboxToJsonSchema","schema","type","pattern","description","result","undefined","title","examples","minLength","maxLength","default","writeOnly","minimum","maximum","itemSchema","items","minItems","maxItems","properties","required","convertedProperties","propName","propSchema","Object","entries","length","anyOf","map","member","value","const","console","warn","coerceMcpInput","input","test","BigInt","Math","trunc","Array","item","_zod","getZod","mod","string","number","boolean","literal","v","object","shape","array","union","members","nullType","any","Error","typeboxToZod","z","_typeboxToZod","regex","s","describe","n","b","zodProp","optional","typeboxToZodShape","getMcpSdk","getMcpStdioTransport","createMcpServer","sprinkle","serverName","McpServer","server","name","actions","listActions","action","zodShape","inputSchema","tool","coercedInput","context","settings","success","content","text","JSON","stringify","data","error","code","message","details","isError","runMcp","StdioServerTransport","transport","connect","err","process","stderr","write","String"],"sources":["../../../../src/Sprinkle/actions/mcp-adapter.ts"],"sourcesContent":["/**\n * MCP adapter for Sprinkles actions.\n *\n * Provides:\n * - TypeBox to JSON Schema conversion (typeboxToJsonSchema)\n * - TypeBox to Zod conversion for MCP SDK compatibility (typeboxToZod)\n * - BigInt string coercion for MCP input (coerceMcpInput)\n * - Lazy MCP SDK import with graceful error (getMcpSdk)\n * - MCP server creation from registered actions (createMcpServer)\n * - MCP orchestrator that starts a stdio server (runMcp)\n */\n\nimport { createRequire } from \"module\";\nimport { fileURLToPath } from \"url\";\nimport { dirname, resolve } from \"path\";\nimport type { TSchema } from \"@sinclair/typebox\";\nimport { bigIntReplacer } from \"../encryption.js\";\nimport {\n isArray,\n isBigInt,\n isBoolean,\n isInteger,\n isLiteral,\n isNull,\n isNumber,\n isObject,\n isOptional,\n isString,\n isUnion,\n isSensitive,\n} from \"../type-guards.js\";\nimport type { AnyAction } from \"./types.js\";\nimport { executeAction } from \"./runner.js\";\n\n// Re-import Sprinkle as a type only to avoid circular deps\nimport type { Sprinkle } from \"../index.js\";\n\n// Load package version for MCP server identification\n// Robustly find package.json - works from both src and dist directories\nfunction loadPackageVersion(): string {\n const require = createRequire(import.meta.url);\n const __filename = fileURLToPath(import.meta.url);\n const __dirname = dirname(__filename);\n \n // Try multiple possible paths (handles src vs dist/esm vs dist/cjs)\n const candidates = [\n resolve(__dirname, \"../../../package.json\"), // from src/Sprinkle/actions\n resolve(__dirname, \"../../../../package.json\"), // from dist/*/Sprinkle/actions\n ];\n \n for (const candidate of candidates) {\n try {\n const pkg = require(candidate) as { version: string };\n return pkg.version;\n } catch {\n // Try next candidate\n }\n }\n \n return \"0.0.0\"; // Fallback if package.json not found\n}\n\nconst PACKAGE_VERSION: string = loadPackageVersion();\n\n// ---------------------------------------------------------------------------\n// TypeBox to JSON Schema conversion\n// ---------------------------------------------------------------------------\n\n/**\n * Convert a TypeBox schema to a plain JSON Schema object.\n *\n * Strips TypeBox-internal symbols and properties (Kind, OptionalKind, $id)\n * and maps TypeBox-specific types to their JSON Schema equivalents:\n * - BigInt -> { type: \"string\", pattern: \"^-?[0-9]+$\" } (JSON has no BigInt)\n * - Sensitive string fields -> add writeOnly: true\n * - Optional -> recurse on inner schema (Optional does not change Kind)\n * - Union -> { anyOf: [...] }\n * - Literal -> { type, const }\n * - Null -> { type: \"null\" }\n * - Unknown/unsupported -> {} (accepts anything)\n */\nexport function typeboxToJsonSchema(schema: TSchema): Record<string, unknown> {\n // Optional in TypeBox doesn't change the Kind, but marks the schema with\n // OptionalKind. We can check for it and recurse transparently since all\n // other guards still work correctly on optional-wrapped schemas.\n //\n // We intentionally do NOT strip optional here -- the containing Object\n // converter decides whether to include a property in `required` based\n // on the isOptional guard applied to the property schema.\n\n if (isBigInt(schema)) {\n return {\n type: \"string\",\n pattern: \"^-?[0-9]+$\",\n description: \"BigInt value as string\",\n };\n }\n\n if (isString(schema)) {\n const result: Record<string, unknown> = { type: \"string\" };\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.title !== undefined) result.title = schema.title;\n if (schema.examples !== undefined) result.examples = schema.examples;\n if (schema.pattern !== undefined) result.pattern = schema.pattern;\n if (schema.minLength !== undefined) result.minLength = schema.minLength;\n if (schema.maxLength !== undefined) result.maxLength = schema.maxLength;\n if (schema.default !== undefined) result.default = schema.default;\n // Sensitive fields are write-only (never returned in responses)\n if (isSensitive(schema)) result.writeOnly = true;\n return result;\n }\n\n if (isNumber(schema)) {\n const result: Record<string, unknown> = { type: \"number\" };\n if (schema.minimum !== undefined) result.minimum = schema.minimum;\n if (schema.maximum !== undefined) result.maximum = schema.maximum;\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isInteger(schema)) {\n const result: Record<string, unknown> = { type: \"integer\" };\n if (schema.minimum !== undefined) result.minimum = schema.minimum;\n if (schema.maximum !== undefined) result.maximum = schema.maximum;\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isBoolean(schema)) {\n const result: Record<string, unknown> = { type: \"boolean\" };\n if (schema.description !== undefined) result.description = schema.description;\n if (schema.default !== undefined) result.default = schema.default;\n return result;\n }\n\n if (isNull(schema)) {\n return { type: \"null\" };\n }\n\n if (isArray(schema)) {\n const result: Record<string, unknown> = { type: \"array\" };\n const itemSchema = (schema as { items?: TSchema }).items;\n if (itemSchema) {\n result.items = typeboxToJsonSchema(itemSchema);\n }\n if (schema.minItems !== undefined) result.minItems = schema.minItems;\n if (schema.maxItems !== undefined) result.maxItems = schema.maxItems;\n if (schema.description !== undefined) result.description = schema.description;\n return result;\n }\n\n if (isObject(schema)) {\n const properties = schema.properties as Record<string, TSchema>;\n const required: string[] = schema.required as string[] ?? [];\n const convertedProperties: Record<string, unknown> = {};\n\n for (const [propName, propSchema] of Object.entries(properties)) {\n convertedProperties[propName] = typeboxToJsonSchema(propSchema);\n }\n\n const result: Record<string, unknown> = {\n type: \"object\",\n properties: convertedProperties,\n };\n\n if (required.length > 0) {\n result.required = required;\n }\n\n if (schema.description !== undefined) result.description = schema.description;\n\n return result;\n }\n\n if (isUnion(schema)) {\n return {\n anyOf: schema.anyOf.map((member: TSchema) => typeboxToJsonSchema(member)),\n };\n }\n\n if (isLiteral(schema)) {\n const value = schema.const;\n let type: string;\n if (typeof value === \"string\") {\n type = \"string\";\n } else if (typeof value === \"number\") {\n type = \"number\";\n } else if (typeof value === \"boolean\") {\n type = \"boolean\";\n } else {\n // Fallback for unexpected literal types\n return {};\n }\n return { type, const: value };\n }\n\n // Unknown / unsupported TypeBox type: emit empty schema (accepts anything)\n // This is a safe fallback that allows the MCP tool to still receive input.\n console.warn(\n `[mcp-adapter] Unsupported TypeBox kind: ${(schema as Record<string, unknown>)[\"[Kind]\"] ?? \"(unknown)\"}. Emitting empty schema.`,\n );\n return {};\n}\n\n// ---------------------------------------------------------------------------\n// Input coercion: BigInt string -> BigInt\n// ---------------------------------------------------------------------------\n\n/**\n * Coerce MCP JSON input values to the types expected by a TypeBox schema.\n *\n * MCP transmits all values as JSON, which has no BigInt type. When the schema\n * expects a BigInt, the value arrives as a numeric string (e.g. \"12345678\").\n * This function recursively walks the schema and input, converting BigInt\n * string values to actual BigInt where needed.\n *\n * For all other types the value is passed through unchanged.\n */\nexport function coerceMcpInput(input: unknown, schema: TSchema): unknown {\n if (input === null || input === undefined) return input;\n\n if (isBigInt(schema)) {\n if (typeof input === \"bigint\") return input;\n if (typeof input === \"string\" && /^-?[0-9]+$/.test(input)) {\n try {\n return BigInt(input);\n } catch {\n return input;\n }\n }\n if (typeof input === \"number\") {\n try {\n return BigInt(Math.trunc(input));\n } catch {\n return input;\n }\n }\n return input;\n }\n\n if (isArray(schema)) {\n const itemSchema = (schema as { items?: TSchema }).items;\n if (Array.isArray(input) && itemSchema) {\n return input.map((item) => coerceMcpInput(item, itemSchema));\n }\n return input;\n }\n\n if (isObject(schema)) {\n if (typeof input !== \"object\" || Array.isArray(input)) return input;\n const properties = schema.properties as Record<string, TSchema>;\n const result: Record<string, unknown> = { ...(input as Record<string, unknown>) };\n for (const [propName, propSchema] of Object.entries(properties)) {\n if (propName in result) {\n result[propName] = coerceMcpInput(result[propName], propSchema);\n }\n }\n return result;\n }\n\n if (isUnion(schema)) {\n // For unions, we try each member schema in order and return the first\n // successful coercion. BigInt members take priority over string members\n // since they have a narrower, unambiguous pattern.\n for (const member of schema.anyOf) {\n if (isBigInt(member)) {\n if (typeof input === \"string\" && /^-?[0-9]+$/.test(input)) {\n return coerceMcpInput(input, member);\n }\n }\n }\n // No BigInt match; return as-is for other union members\n return input;\n }\n\n // For all other types (String, Number, Integer, Boolean, Literal, Null):\n // pass through unchanged -- MCP JSON already represents them correctly\n return input;\n}\n\n// ---------------------------------------------------------------------------\n// TypeBox to Zod conversion\n// ---------------------------------------------------------------------------\n\n/**\n * Zod module shape (the parts we use from zod).\n * Typed loosely since zod is a transitive dependency of @modelcontextprotocol/sdk,\n * not a direct dependency of Sprinkles.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype ZodType = any;\ninterface IZodModule {\n string: () => ZodType;\n number: () => ZodType;\n boolean: () => ZodType;\n literal: (value: unknown) => ZodType;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n object: (shape: Record<string, any>) => ZodType;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n array: (item: any) => ZodType;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n union: (members: any[]) => ZodType;\n nullType: () => ZodType;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any: () => any;\n}\n\nlet _zod: IZodModule | undefined;\n\n/**\n * Lazily import the Zod module.\n * Zod is a transitive dependency of @modelcontextprotocol/sdk, so it's\n * guaranteed to be available when MCP mode is used.\n */\nasync function getZod(): Promise<IZodModule> {\n if (_zod) return _zod;\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const mod = (await import(\"zod\")) as any;\n _zod = {\n string: () => mod.string(),\n number: () => mod.number(),\n boolean: () => mod.boolean(),\n literal: (v: unknown) => mod.literal(v),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n object: (shape: Record<string, any>) => mod.object(shape),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n array: (item: any) => mod.array(item),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n union: (members: any[]) => mod.union(members),\n nullType: () => mod.nullType(),\n any: () => mod.any(),\n };\n return _zod;\n } catch {\n throw new Error(\n \"MCP mode requires zod (via @modelcontextprotocol/sdk). Install @modelcontextprotocol/sdk.\",\n );\n }\n}\n\n/**\n * Convert a TypeBox schema to a Zod schema for MCP SDK compatibility.\n *\n * The MCP SDK (v1.27+) expects Zod schemas for tool input validation,\n * not plain JSON Schema objects. This function converts TypeBox schemas\n * to their Zod equivalents so the SDK correctly registers tool parameters.\n *\n * BigInt fields are represented as z.string() with a regex pattern since\n * JSON (and thus MCP) has no BigInt type. The coerceMcpInput function\n * handles converting these string values back to BigInt at call time.\n */\nexport async function typeboxToZod(schema: TSchema): Promise<ZodType> {\n const z = await getZod();\n return _typeboxToZod(schema, z);\n}\n\nfunction _typeboxToZod(schema: TSchema, z: IZodModule): ZodType {\n if (isBigInt(schema)) {\n // BigInt -> string with regex pattern (JSON has no BigInt)\n return z.string().regex(/^-?[0-9]+$/);\n }\n\n if (isString(schema)) {\n let s = z.string();\n if (schema.description) s = s.describe(schema.description);\n return s;\n }\n\n if (isNumber(schema) || isInteger(schema)) {\n let n = z.number();\n if (schema.description) n = n.describe(schema.description);\n return n;\n }\n\n if (isBoolean(schema)) {\n let b = z.boolean();\n if (schema.description) b = b.describe(schema.description);\n return b;\n }\n\n if (isNull(schema)) {\n return z.nullType();\n }\n\n if (isLiteral(schema)) {\n return z.literal(schema.const);\n }\n\n if (isArray(schema)) {\n const itemSchema = (schema as { items?: TSchema }).items;\n if (itemSchema) {\n return z.array(_typeboxToZod(itemSchema, z));\n }\n return z.array(z.any());\n }\n\n if (isObject(schema)) {\n const properties = schema.properties as Record<string, TSchema>;\n const shape: Record<string, ZodType> = {};\n\n for (const [propName, propSchema] of Object.entries(properties)) {\n let zodProp = _typeboxToZod(propSchema, z);\n if (isOptional(propSchema)) {\n zodProp = zodProp.optional();\n }\n shape[propName] = zodProp;\n }\n\n return z.object(shape);\n }\n\n if (isUnion(schema)) {\n const members = schema.anyOf.map((member: TSchema) =>\n _typeboxToZod(member, z),\n );\n if (members.length >= 2) {\n return z.union(members);\n }\n return members[0] ?? z.any();\n }\n\n // Fallback: accept anything\n return z.any();\n}\n\n/**\n * Convert a TypeBox object schema to a Zod \"raw shape\" (Record<string, ZodType>).\n * This is the format expected by the MCP SDK's tool() API.\n */\nexport async function typeboxToZodShape(\n schema: TSchema,\n): Promise<Record<string, ZodType>> {\n const z = await getZod();\n\n if (!isObject(schema)) {\n // Non-object input schemas get wrapped as { input: zodSchema }\n return { input: _typeboxToZod(schema, z) };\n }\n\n const properties = schema.properties as Record<string, TSchema>;\n const shape: Record<string, ZodType> = {};\n\n for (const [propName, propSchema] of Object.entries(properties)) {\n let zodProp = _typeboxToZod(propSchema, z);\n if (isOptional(propSchema)) {\n zodProp = zodProp.optional();\n }\n shape[propName] = zodProp;\n }\n\n return shape;\n}\n\n// ---------------------------------------------------------------------------\n// Lazy MCP SDK import\n// ---------------------------------------------------------------------------\n\n/**\n * MCP SDK module shape (the parts we use from @modelcontextprotocol/sdk).\n * Typed loosely to avoid a hard dependency at compile time.\n */\ninterface IMcpSdkModule {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n McpServer: new (opts: { name: string; version: string }) => any;\n}\n\ninterface IStdioTransportModule {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n StdioServerTransport: new () => any;\n}\n\n/**\n * Lazily import the MCP SDK server module.\n *\n * Throws a clear, actionable error if the SDK is not installed, rather than\n * a cryptic \"Cannot find module\" Node error.\n */\nexport async function getMcpSdk(): Promise<IMcpSdkModule> {\n try {\n return (await import(\n \"@modelcontextprotocol/sdk/server/mcp.js\"\n )) as IMcpSdkModule;\n } catch {\n throw new Error(\n \"MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk\",\n );\n }\n}\n\n/**\n * Lazily import the MCP SDK stdio transport module.\n */\nasync function getMcpStdioTransport(): Promise<IStdioTransportModule> {\n try {\n return (await import(\n \"@modelcontextprotocol/sdk/server/stdio.js\"\n )) as IStdioTransportModule;\n } catch {\n throw new Error(\n \"MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk\",\n );\n }\n}\n\n// ---------------------------------------------------------------------------\n// MCP server creation\n// ---------------------------------------------------------------------------\n\n/**\n * Create an MCP server and register all actions from a Sprinkle instance as\n * MCP tools.\n *\n * Each action becomes a tool with:\n * - name: action.name\n * - description: action.description\n * - input schema: typeboxToJsonSchema(action.inputSchema)\n * - handler: coerces input, executes action, returns JSON result\n *\n * The Sprinkle instance must already be initialized with a profile before\n * this function is called.\n *\n * @param sprinkle - Fully-initialized Sprinkle instance\n * @param serverName - Name to use for the MCP server\n * @returns The configured McpServer instance (not yet connected)\n */\nexport async function createMcpServer<S extends TSchema>(\n sprinkle: Sprinkle<S>,\n serverName: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Promise<any> {\n const { McpServer } = await getMcpSdk();\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const server = new McpServer({ name: serverName, version: PACKAGE_VERSION });\n\n const actions = sprinkle.listActions() as AnyAction<S>[];\n\n for (const action of actions) {\n const zodShape = await typeboxToZodShape(action.inputSchema);\n\n // Register the action as an MCP tool.\n // The high-level McpServer.tool() API accepts a Zod raw shape\n // (Record<string, ZodType>) for input validation.\n server.tool(\n action.name,\n action.description,\n zodShape,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n async (input: unknown): Promise<any> => {\n const coercedInput = coerceMcpInput(input, action.inputSchema);\n\n const context = {\n sprinkle,\n settings: sprinkle.settings,\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const result = await executeAction(action as any, coercedInput, context as any);\n\n if (result.success) {\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify(result.data, bigIntReplacer),\n },\n ],\n };\n } else {\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify(\n {\n error: {\n code: result.error.code,\n message: result.error.message,\n details: result.error.details,\n },\n },\n bigIntReplacer,\n ),\n },\n ],\n isError: true,\n };\n }\n },\n );\n }\n\n return server;\n}\n\n// ---------------------------------------------------------------------------\n// MCP orchestrator\n// ---------------------------------------------------------------------------\n\n/**\n * Start an MCP server on stdio transport.\n *\n * This function:\n * 1. Creates the MCP server and registers all actions as tools\n * 2. Creates a StdioServerTransport\n * 3. Connects the server to the transport\n *\n * IMPORTANT: All logging in this function goes to stderr. stdout is reserved\n * for the MCP protocol messages.\n *\n * The Sprinkle instance must already be initialized with a profile. Profile\n * initialization is the responsibility of the caller (Sprinkle.Run()).\n *\n * @param sprinkle - Fully-initialized Sprinkle instance\n * @param serverName - Name to use for the MCP server\n */\nexport async function runMcp<S extends TSchema>(\n sprinkle: Sprinkle<S>,\n serverName: string,\n): Promise<void> {\n const { StdioServerTransport } = await getMcpStdioTransport();\n\n const server = await createMcpServer(sprinkle, serverName);\n const transport = new StdioServerTransport();\n\n try {\n await server.connect(transport);\n } catch (err) {\n // Log errors to stderr only -- stdout is the MCP transport channel\n process.stderr.write(\n `[sprinkle-mcp] Failed to start MCP server: ${err instanceof Error ? err.message : String(err)}\\n`,\n );\n throw err;\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,aAAa,QAAQ,QAAQ;AACtC,SAASC,aAAa,QAAQ,KAAK;AACnC,SAASC,OAAO,EAAEC,OAAO,QAAQ,MAAM;AAEvC,SAASC,cAAc,QAAQ,kBAAkB;AACjD,SACEC,OAAO,EACPC,QAAQ,EACRC,SAAS,EACTC,SAAS,EACTC,SAAS,EACTC,MAAM,EACNC,QAAQ,EACRC,QAAQ,EACRC,UAAU,EACVC,QAAQ,EACRC,OAAO,EACPC,WAAW,QACN,mBAAmB;AAE1B,SAASC,aAAa,QAAQ,aAAa;;AAE3C;;AAGA;AACA;AACA,SAASC,kBAAkBA,CAAA,EAAW;EACpC,MAAMC,OAAO,GAAGnB,aAAa,CAACoB,MAAM,CAACC,IAAI,CAACC,GAAG,CAAC;EAC9C,MAAMC,UAAU,GAAGtB,aAAa,CAACmB,MAAM,CAACC,IAAI,CAACC,GAAG,CAAC;EACjD,MAAME,SAAS,GAAGtB,OAAO,CAACqB,UAAU,CAAC;;EAErC;EACA,MAAME,UAAU,GAAG,CACjBtB,OAAO,CAACqB,SAAS,EAAE,uBAAuB,CAAC;EAAM;EACjDrB,OAAO,CAACqB,SAAS,EAAE,0BAA0B,CAAC,CAAG;EAAA,CAClD;EAED,KAAK,MAAME,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