@sundaeswap/sprinkles 0.6.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/README.md +178 -181
  2. package/dist/cjs/Sprinkle/__tests__/action-integration.test.js +590 -0
  3. package/dist/cjs/Sprinkle/__tests__/action-integration.test.js.map +1 -0
  4. package/dist/cjs/Sprinkle/__tests__/action-registry.test.js +193 -0
  5. package/dist/cjs/Sprinkle/__tests__/action-registry.test.js.map +1 -0
  6. package/dist/cjs/Sprinkle/__tests__/action-runner.test.js +304 -0
  7. package/dist/cjs/Sprinkle/__tests__/action-runner.test.js.map +1 -0
  8. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js +1110 -0
  9. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js.map +1 -0
  10. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js +744 -0
  11. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js.map +1 -0
  12. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js +15 -1
  13. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  14. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js +711 -0
  15. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -0
  16. package/dist/cjs/Sprinkle/__tests__/native-script.test.js +390 -0
  17. package/dist/cjs/Sprinkle/__tests__/native-script.test.js.map +1 -0
  18. package/dist/cjs/Sprinkle/__tests__/tui-helpers.test.js +334 -0
  19. package/dist/cjs/Sprinkle/__tests__/tui-helpers.test.js.map +1 -0
  20. package/dist/cjs/Sprinkle/__tests__/utility-actions.test.js +367 -0
  21. package/dist/cjs/Sprinkle/__tests__/utility-actions.test.js.map +1 -0
  22. package/dist/cjs/Sprinkle/__tests__/wallet-transaction-actions.test.js +749 -0
  23. package/dist/cjs/Sprinkle/__tests__/wallet-transaction-actions.test.js.map +1 -0
  24. package/dist/cjs/Sprinkle/actions/builtin/addressbook-actions.js +164 -0
  25. package/dist/cjs/Sprinkle/actions/builtin/addressbook-actions.js.map +1 -0
  26. package/dist/cjs/Sprinkle/actions/builtin/blaze-helper.js +61 -0
  27. package/dist/cjs/Sprinkle/actions/builtin/blaze-helper.js.map +1 -0
  28. package/dist/cjs/Sprinkle/actions/builtin/index.js +174 -0
  29. package/dist/cjs/Sprinkle/actions/builtin/index.js.map +1 -0
  30. package/dist/cjs/Sprinkle/actions/builtin/native-script.js +139 -0
  31. package/dist/cjs/Sprinkle/actions/builtin/native-script.js.map +1 -0
  32. package/dist/cjs/Sprinkle/actions/builtin/profile-actions.js +202 -0
  33. package/dist/cjs/Sprinkle/actions/builtin/profile-actions.js.map +1 -0
  34. package/dist/cjs/Sprinkle/actions/builtin/settings-actions.js +87 -0
  35. package/dist/cjs/Sprinkle/actions/builtin/settings-actions.js.map +1 -0
  36. package/dist/cjs/Sprinkle/actions/builtin/transaction-actions.js +345 -0
  37. package/dist/cjs/Sprinkle/actions/builtin/transaction-actions.js.map +1 -0
  38. package/dist/cjs/Sprinkle/actions/builtin/utility-actions.js +218 -0
  39. package/dist/cjs/Sprinkle/actions/builtin/utility-actions.js.map +1 -0
  40. package/dist/cjs/Sprinkle/actions/builtin/wallet-actions.js +212 -0
  41. package/dist/cjs/Sprinkle/actions/builtin/wallet-actions.js.map +1 -0
  42. package/dist/cjs/Sprinkle/actions/cli-adapter.js +390 -0
  43. package/dist/cjs/Sprinkle/actions/cli-adapter.js.map +1 -0
  44. package/dist/cjs/Sprinkle/actions/index.js +139 -0
  45. package/dist/cjs/Sprinkle/actions/index.js.map +1 -0
  46. package/dist/cjs/Sprinkle/actions/mcp-adapter.js +557 -0
  47. package/dist/cjs/Sprinkle/actions/mcp-adapter.js.map +1 -0
  48. package/dist/cjs/Sprinkle/actions/registry.js +92 -0
  49. package/dist/cjs/Sprinkle/actions/registry.js.map +1 -0
  50. package/dist/cjs/Sprinkle/actions/runner.js +190 -0
  51. package/dist/cjs/Sprinkle/actions/runner.js.map +1 -0
  52. package/dist/cjs/Sprinkle/actions/tui-helpers.js +96 -0
  53. package/dist/cjs/Sprinkle/actions/tui-helpers.js.map +1 -0
  54. package/dist/cjs/Sprinkle/actions/types.js +68 -0
  55. package/dist/cjs/Sprinkle/actions/types.js.map +1 -0
  56. package/dist/cjs/Sprinkle/index.js +678 -5
  57. package/dist/cjs/Sprinkle/index.js.map +1 -1
  58. package/dist/cjs/Sprinkle/prompts.js +12 -7
  59. package/dist/cjs/Sprinkle/prompts.js.map +1 -1
  60. package/dist/cjs/Sprinkle/schemas.js +17 -1
  61. package/dist/cjs/Sprinkle/schemas.js.map +1 -1
  62. package/dist/cjs/Sprinkle/type-guards.js +7 -1
  63. package/dist/cjs/Sprinkle/type-guards.js.map +1 -1
  64. package/dist/esm/Sprinkle/__tests__/action-integration.test.js +588 -0
  65. package/dist/esm/Sprinkle/__tests__/action-integration.test.js.map +1 -0
  66. package/dist/esm/Sprinkle/__tests__/action-registry.test.js +192 -0
  67. package/dist/esm/Sprinkle/__tests__/action-registry.test.js.map +1 -0
  68. package/dist/esm/Sprinkle/__tests__/action-runner.test.js +302 -0
  69. package/dist/esm/Sprinkle/__tests__/action-runner.test.js.map +1 -0
  70. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js +1107 -0
  71. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js.map +1 -0
  72. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js +742 -0
  73. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js.map +1 -0
  74. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js +15 -1
  75. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  76. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js +710 -0
  77. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -0
  78. package/dist/esm/Sprinkle/__tests__/native-script.test.js +388 -0
  79. package/dist/esm/Sprinkle/__tests__/native-script.test.js.map +1 -0
  80. package/dist/esm/Sprinkle/__tests__/tui-helpers.test.js +332 -0
  81. package/dist/esm/Sprinkle/__tests__/tui-helpers.test.js.map +1 -0
  82. package/dist/esm/Sprinkle/__tests__/utility-actions.test.js +365 -0
  83. package/dist/esm/Sprinkle/__tests__/utility-actions.test.js.map +1 -0
  84. package/dist/esm/Sprinkle/__tests__/wallet-transaction-actions.test.js +747 -0
  85. package/dist/esm/Sprinkle/__tests__/wallet-transaction-actions.test.js.map +1 -0
  86. package/dist/esm/Sprinkle/actions/builtin/addressbook-actions.js +159 -0
  87. package/dist/esm/Sprinkle/actions/builtin/addressbook-actions.js.map +1 -0
  88. package/dist/esm/Sprinkle/actions/builtin/blaze-helper.js +55 -0
  89. package/dist/esm/Sprinkle/actions/builtin/blaze-helper.js.map +1 -0
  90. package/dist/esm/Sprinkle/actions/builtin/index.js +37 -0
  91. package/dist/esm/Sprinkle/actions/builtin/index.js.map +1 -0
  92. package/dist/esm/Sprinkle/actions/builtin/native-script.js +133 -0
  93. package/dist/esm/Sprinkle/actions/builtin/native-script.js.map +1 -0
  94. package/dist/esm/Sprinkle/actions/builtin/profile-actions.js +197 -0
  95. package/dist/esm/Sprinkle/actions/builtin/profile-actions.js.map +1 -0
  96. package/dist/esm/Sprinkle/actions/builtin/settings-actions.js +81 -0
  97. package/dist/esm/Sprinkle/actions/builtin/settings-actions.js.map +1 -0
  98. package/dist/esm/Sprinkle/actions/builtin/transaction-actions.js +340 -0
  99. package/dist/esm/Sprinkle/actions/builtin/transaction-actions.js.map +1 -0
  100. package/dist/esm/Sprinkle/actions/builtin/utility-actions.js +213 -0
  101. package/dist/esm/Sprinkle/actions/builtin/utility-actions.js.map +1 -0
  102. package/dist/esm/Sprinkle/actions/builtin/wallet-actions.js +207 -0
  103. package/dist/esm/Sprinkle/actions/builtin/wallet-actions.js.map +1 -0
  104. package/dist/esm/Sprinkle/actions/cli-adapter.js +379 -0
  105. package/dist/esm/Sprinkle/actions/cli-adapter.js.map +1 -0
  106. package/dist/esm/Sprinkle/actions/index.js +12 -0
  107. package/dist/esm/Sprinkle/actions/index.js.map +1 -0
  108. package/dist/esm/Sprinkle/actions/mcp-adapter.js +547 -0
  109. package/dist/esm/Sprinkle/actions/mcp-adapter.js.map +1 -0
  110. package/dist/esm/Sprinkle/actions/registry.js +85 -0
  111. package/dist/esm/Sprinkle/actions/registry.js.map +1 -0
  112. package/dist/esm/Sprinkle/actions/runner.js +182 -0
  113. package/dist/esm/Sprinkle/actions/runner.js.map +1 -0
  114. package/dist/esm/Sprinkle/actions/tui-helpers.js +91 -0
  115. package/dist/esm/Sprinkle/actions/tui-helpers.js.map +1 -0
  116. package/dist/esm/Sprinkle/actions/types.js +61 -0
  117. package/dist/esm/Sprinkle/actions/types.js.map +1 -0
  118. package/dist/esm/Sprinkle/index.js +517 -7
  119. package/dist/esm/Sprinkle/index.js.map +1 -1
  120. package/dist/esm/Sprinkle/prompts.js +12 -7
  121. package/dist/esm/Sprinkle/prompts.js.map +1 -1
  122. package/dist/esm/Sprinkle/schemas.js +16 -0
  123. package/dist/esm/Sprinkle/schemas.js.map +1 -1
  124. package/dist/esm/Sprinkle/type-guards.js +3 -0
  125. package/dist/esm/Sprinkle/type-guards.js.map +1 -1
  126. package/dist/types/Sprinkle/actions/builtin/addressbook-actions.d.ts +50 -0
  127. package/dist/types/Sprinkle/actions/builtin/addressbook-actions.d.ts.map +1 -0
  128. package/dist/types/Sprinkle/actions/builtin/blaze-helper.d.ts +39 -0
  129. package/dist/types/Sprinkle/actions/builtin/blaze-helper.d.ts.map +1 -0
  130. package/dist/types/Sprinkle/actions/builtin/index.d.ts +30 -0
  131. package/dist/types/Sprinkle/actions/builtin/index.d.ts.map +1 -0
  132. package/dist/types/Sprinkle/actions/builtin/native-script.d.ts +27 -0
  133. package/dist/types/Sprinkle/actions/builtin/native-script.d.ts.map +1 -0
  134. package/dist/types/Sprinkle/actions/builtin/profile-actions.d.ts +55 -0
  135. package/dist/types/Sprinkle/actions/builtin/profile-actions.d.ts.map +1 -0
  136. package/dist/types/Sprinkle/actions/builtin/settings-actions.d.ts +32 -0
  137. package/dist/types/Sprinkle/actions/builtin/settings-actions.d.ts.map +1 -0
  138. package/dist/types/Sprinkle/actions/builtin/transaction-actions.d.ts +70 -0
  139. package/dist/types/Sprinkle/actions/builtin/transaction-actions.d.ts.map +1 -0
  140. package/dist/types/Sprinkle/actions/builtin/utility-actions.d.ts +48 -0
  141. package/dist/types/Sprinkle/actions/builtin/utility-actions.d.ts.map +1 -0
  142. package/dist/types/Sprinkle/actions/builtin/wallet-actions.d.ts +50 -0
  143. package/dist/types/Sprinkle/actions/builtin/wallet-actions.d.ts.map +1 -0
  144. package/dist/types/Sprinkle/actions/cli-adapter.d.ts +104 -0
  145. package/dist/types/Sprinkle/actions/cli-adapter.d.ts.map +1 -0
  146. package/dist/types/Sprinkle/actions/index.d.ts +13 -0
  147. package/dist/types/Sprinkle/actions/index.d.ts.map +1 -0
  148. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts +116 -0
  149. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts.map +1 -0
  150. package/dist/types/Sprinkle/actions/registry.d.ts +42 -0
  151. package/dist/types/Sprinkle/actions/registry.d.ts.map +1 -0
  152. package/dist/types/Sprinkle/actions/runner.d.ts +45 -0
  153. package/dist/types/Sprinkle/actions/runner.d.ts.map +1 -0
  154. package/dist/types/Sprinkle/actions/tui-helpers.d.ts +53 -0
  155. package/dist/types/Sprinkle/actions/tui-helpers.d.ts.map +1 -0
  156. package/dist/types/Sprinkle/actions/types.d.ts +76 -0
  157. package/dist/types/Sprinkle/actions/types.d.ts.map +1 -0
  158. package/dist/types/Sprinkle/index.d.ts +84 -2
  159. package/dist/types/Sprinkle/index.d.ts.map +1 -1
  160. package/dist/types/Sprinkle/prompts.d.ts.map +1 -1
  161. package/dist/types/Sprinkle/schemas.d.ts +72 -0
  162. package/dist/types/Sprinkle/schemas.d.ts.map +1 -1
  163. package/dist/types/Sprinkle/type-guards.d.ts +4 -1
  164. package/dist/types/Sprinkle/type-guards.d.ts.map +1 -1
  165. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  166. package/package.json +9 -2
  167. package/src/Sprinkle/__tests__/action-integration.test.ts +558 -0
  168. package/src/Sprinkle/__tests__/action-registry.test.ts +187 -0
  169. package/src/Sprinkle/__tests__/action-runner.test.ts +324 -0
  170. package/src/Sprinkle/__tests__/builtin-actions.test.ts +1022 -0
  171. package/src/Sprinkle/__tests__/cli-adapter.test.ts +736 -0
  172. package/src/Sprinkle/__tests__/fill-in-struct.test.ts +23 -1
  173. package/src/Sprinkle/__tests__/mcp-adapter.test.ts +720 -0
  174. package/src/Sprinkle/__tests__/native-script.test.ts +341 -0
  175. package/src/Sprinkle/__tests__/tui-helpers.test.ts +325 -0
  176. package/src/Sprinkle/__tests__/utility-actions.test.ts +348 -0
  177. package/src/Sprinkle/__tests__/wallet-transaction-actions.test.ts +695 -0
  178. package/src/Sprinkle/actions/builtin/addressbook-actions.ts +168 -0
  179. package/src/Sprinkle/actions/builtin/blaze-helper.ts +89 -0
  180. package/src/Sprinkle/actions/builtin/index.ts +125 -0
  181. package/src/Sprinkle/actions/builtin/native-script.ts +165 -0
  182. package/src/Sprinkle/actions/builtin/profile-actions.ts +229 -0
  183. package/src/Sprinkle/actions/builtin/settings-actions.ts +99 -0
  184. package/src/Sprinkle/actions/builtin/transaction-actions.ts +381 -0
  185. package/src/Sprinkle/actions/builtin/utility-actions.ts +285 -0
  186. package/src/Sprinkle/actions/builtin/wallet-actions.ts +233 -0
  187. package/src/Sprinkle/actions/cli-adapter.ts +446 -0
  188. package/src/Sprinkle/actions/index.ts +33 -0
  189. package/src/Sprinkle/actions/mcp-adapter.ts +638 -0
  190. package/src/Sprinkle/actions/registry.ts +97 -0
  191. package/src/Sprinkle/actions/runner.ts +200 -0
  192. package/src/Sprinkle/actions/tui-helpers.ts +114 -0
  193. package/src/Sprinkle/actions/types.ts +91 -0
  194. package/src/Sprinkle/index.ts +612 -3
  195. package/src/Sprinkle/prompts.ts +118 -72
  196. package/src/Sprinkle/schemas.ts +20 -0
  197. package/src/Sprinkle/type-guards.ts +9 -0
@@ -0,0 +1,557 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.coerceMcpInput = coerceMcpInput;
7
+ exports.createMcpServer = createMcpServer;
8
+ exports.getMcpSdk = getMcpSdk;
9
+ exports.runMcp = runMcp;
10
+ exports.typeboxToJsonSchema = typeboxToJsonSchema;
11
+ exports.typeboxToZod = typeboxToZod;
12
+ exports.typeboxToZodShape = typeboxToZodShape;
13
+ var _module = require("module");
14
+ var _url = require("url");
15
+ var _path = require("path");
16
+ var _encryption = require("../encryption.js");
17
+ var _typeGuards = require("../type-guards.js");
18
+ var _runner = require("./runner.js");
19
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } /**
20
+ * MCP adapter for Sprinkles actions.
21
+ *
22
+ * Provides:
23
+ * - TypeBox to JSON Schema conversion (typeboxToJsonSchema)
24
+ * - TypeBox to Zod conversion for MCP SDK compatibility (typeboxToZod)
25
+ * - BigInt string coercion for MCP input (coerceMcpInput)
26
+ * - Lazy MCP SDK import with graceful error (getMcpSdk)
27
+ * - MCP server creation from registered actions (createMcpServer)
28
+ * - MCP orchestrator that starts a stdio server (runMcp)
29
+ */
30
+ // Re-import Sprinkle as a type only to avoid circular deps
31
+
32
+ // Load package version for MCP server identification
33
+ // Robustly find package.json - works from both src and dist directories
34
+ function loadPackageVersion() {
35
+ const require = (0, _module.createRequire)(import.meta.url);
36
+ const __filename = (0, _url.fileURLToPath)(import.meta.url);
37
+ const __dirname = (0, _path.dirname)(__filename);
38
+
39
+ // Try multiple possible paths (handles src vs dist/esm vs dist/cjs)
40
+ const candidates = [(0, _path.resolve)(__dirname, "../../../package.json"),
41
+ // from src/Sprinkle/actions
42
+ (0, _path.resolve)(__dirname, "../../../../package.json") // from dist/*/Sprinkle/actions
43
+ ];
44
+ for (const candidate of candidates) {
45
+ try {
46
+ const pkg = require(candidate);
47
+ return pkg.version;
48
+ } catch {
49
+ // Try next candidate
50
+ }
51
+ }
52
+ return "0.0.0"; // Fallback if package.json not found
53
+ }
54
+ const PACKAGE_VERSION = loadPackageVersion();
55
+
56
+ // ---------------------------------------------------------------------------
57
+ // TypeBox to JSON Schema conversion
58
+ // ---------------------------------------------------------------------------
59
+
60
+ /**
61
+ * Convert a TypeBox schema to a plain JSON Schema object.
62
+ *
63
+ * Strips TypeBox-internal symbols and properties (Kind, OptionalKind, $id)
64
+ * and maps TypeBox-specific types to their JSON Schema equivalents:
65
+ * - BigInt -> { type: "string", pattern: "^-?[0-9]+$" } (JSON has no BigInt)
66
+ * - Sensitive string fields -> add writeOnly: true
67
+ * - Optional -> recurse on inner schema (Optional does not change Kind)
68
+ * - Union -> { anyOf: [...] }
69
+ * - Literal -> { type, const }
70
+ * - Null -> { type: "null" }
71
+ * - Unknown/unsupported -> {} (accepts anything)
72
+ */
73
+ function typeboxToJsonSchema(schema) {
74
+ // Optional in TypeBox doesn't change the Kind, but marks the schema with
75
+ // OptionalKind. We can check for it and recurse transparently since all
76
+ // other guards still work correctly on optional-wrapped schemas.
77
+ //
78
+ // We intentionally do NOT strip optional here -- the containing Object
79
+ // converter decides whether to include a property in `required` based
80
+ // on the isOptional guard applied to the property schema.
81
+
82
+ if ((0, _typeGuards.isBigInt)(schema)) {
83
+ return {
84
+ type: "string",
85
+ pattern: "^-?[0-9]+$",
86
+ description: "BigInt value as string"
87
+ };
88
+ }
89
+ if ((0, _typeGuards.isString)(schema)) {
90
+ const result = {
91
+ type: "string"
92
+ };
93
+ if (schema.description !== undefined) result.description = schema.description;
94
+ if (schema.title !== undefined) result.title = schema.title;
95
+ if (schema.examples !== undefined) result.examples = schema.examples;
96
+ if (schema.pattern !== undefined) result.pattern = schema.pattern;
97
+ if (schema.minLength !== undefined) result.minLength = schema.minLength;
98
+ if (schema.maxLength !== undefined) result.maxLength = schema.maxLength;
99
+ if (schema.default !== undefined) result.default = schema.default;
100
+ // Sensitive fields are write-only (never returned in responses)
101
+ if ((0, _typeGuards.isSensitive)(schema)) result.writeOnly = true;
102
+ return result;
103
+ }
104
+ if ((0, _typeGuards.isNumber)(schema)) {
105
+ const result = {
106
+ type: "number"
107
+ };
108
+ if (schema.minimum !== undefined) result.minimum = schema.minimum;
109
+ if (schema.maximum !== undefined) result.maximum = schema.maximum;
110
+ if (schema.description !== undefined) result.description = schema.description;
111
+ if (schema.default !== undefined) result.default = schema.default;
112
+ return result;
113
+ }
114
+ if ((0, _typeGuards.isInteger)(schema)) {
115
+ const result = {
116
+ type: "integer"
117
+ };
118
+ if (schema.minimum !== undefined) result.minimum = schema.minimum;
119
+ if (schema.maximum !== undefined) result.maximum = schema.maximum;
120
+ if (schema.description !== undefined) result.description = schema.description;
121
+ if (schema.default !== undefined) result.default = schema.default;
122
+ return result;
123
+ }
124
+ if ((0, _typeGuards.isBoolean)(schema)) {
125
+ const result = {
126
+ type: "boolean"
127
+ };
128
+ if (schema.description !== undefined) result.description = schema.description;
129
+ if (schema.default !== undefined) result.default = schema.default;
130
+ return result;
131
+ }
132
+ if ((0, _typeGuards.isNull)(schema)) {
133
+ return {
134
+ type: "null"
135
+ };
136
+ }
137
+ if ((0, _typeGuards.isArray)(schema)) {
138
+ const result = {
139
+ type: "array"
140
+ };
141
+ const itemSchema = schema.items;
142
+ if (itemSchema) {
143
+ result.items = typeboxToJsonSchema(itemSchema);
144
+ }
145
+ if (schema.minItems !== undefined) result.minItems = schema.minItems;
146
+ if (schema.maxItems !== undefined) result.maxItems = schema.maxItems;
147
+ if (schema.description !== undefined) result.description = schema.description;
148
+ return result;
149
+ }
150
+ if ((0, _typeGuards.isObject)(schema)) {
151
+ const properties = schema.properties;
152
+ const required = schema.required ?? [];
153
+ const convertedProperties = {};
154
+ for (const [propName, propSchema] of Object.entries(properties)) {
155
+ convertedProperties[propName] = typeboxToJsonSchema(propSchema);
156
+ }
157
+ const result = {
158
+ type: "object",
159
+ properties: convertedProperties
160
+ };
161
+ if (required.length > 0) {
162
+ result.required = required;
163
+ }
164
+ if (schema.description !== undefined) result.description = schema.description;
165
+ return result;
166
+ }
167
+ if ((0, _typeGuards.isUnion)(schema)) {
168
+ return {
169
+ anyOf: schema.anyOf.map(member => typeboxToJsonSchema(member))
170
+ };
171
+ }
172
+ if ((0, _typeGuards.isLiteral)(schema)) {
173
+ const value = schema.const;
174
+ let type;
175
+ if (typeof value === "string") {
176
+ type = "string";
177
+ } else if (typeof value === "number") {
178
+ type = "number";
179
+ } else if (typeof value === "boolean") {
180
+ type = "boolean";
181
+ } else {
182
+ // Fallback for unexpected literal types
183
+ return {};
184
+ }
185
+ return {
186
+ type,
187
+ const: value
188
+ };
189
+ }
190
+
191
+ // Unknown / unsupported TypeBox type: emit empty schema (accepts anything)
192
+ // This is a safe fallback that allows the MCP tool to still receive input.
193
+ console.warn(`[mcp-adapter] Unsupported TypeBox kind: ${schema["[Kind]"] ?? "(unknown)"}. Emitting empty schema.`);
194
+ return {};
195
+ }
196
+
197
+ // ---------------------------------------------------------------------------
198
+ // Input coercion: BigInt string -> BigInt
199
+ // ---------------------------------------------------------------------------
200
+
201
+ /**
202
+ * Coerce MCP JSON input values to the types expected by a TypeBox schema.
203
+ *
204
+ * MCP transmits all values as JSON, which has no BigInt type. When the schema
205
+ * expects a BigInt, the value arrives as a numeric string (e.g. "12345678").
206
+ * This function recursively walks the schema and input, converting BigInt
207
+ * string values to actual BigInt where needed.
208
+ *
209
+ * For all other types the value is passed through unchanged.
210
+ */
211
+ function coerceMcpInput(input, schema) {
212
+ if (input === null || input === undefined) return input;
213
+ if ((0, _typeGuards.isBigInt)(schema)) {
214
+ if (typeof input === "bigint") return input;
215
+ if (typeof input === "string" && /^-?[0-9]+$/.test(input)) {
216
+ try {
217
+ return BigInt(input);
218
+ } catch {
219
+ return input;
220
+ }
221
+ }
222
+ if (typeof input === "number") {
223
+ try {
224
+ return BigInt(Math.trunc(input));
225
+ } catch {
226
+ return input;
227
+ }
228
+ }
229
+ return input;
230
+ }
231
+ if ((0, _typeGuards.isArray)(schema)) {
232
+ const itemSchema = schema.items;
233
+ if (Array.isArray(input) && itemSchema) {
234
+ return input.map(item => coerceMcpInput(item, itemSchema));
235
+ }
236
+ return input;
237
+ }
238
+ if ((0, _typeGuards.isObject)(schema)) {
239
+ if (typeof input !== "object" || Array.isArray(input)) return input;
240
+ const properties = schema.properties;
241
+ const result = {
242
+ ...input
243
+ };
244
+ for (const [propName, propSchema] of Object.entries(properties)) {
245
+ if (propName in result) {
246
+ result[propName] = coerceMcpInput(result[propName], propSchema);
247
+ }
248
+ }
249
+ return result;
250
+ }
251
+ if ((0, _typeGuards.isUnion)(schema)) {
252
+ // For unions, we try each member schema in order and return the first
253
+ // successful coercion. BigInt members take priority over string members
254
+ // since they have a narrower, unambiguous pattern.
255
+ for (const member of schema.anyOf) {
256
+ if ((0, _typeGuards.isBigInt)(member)) {
257
+ if (typeof input === "string" && /^-?[0-9]+$/.test(input)) {
258
+ return coerceMcpInput(input, member);
259
+ }
260
+ }
261
+ }
262
+ // No BigInt match; return as-is for other union members
263
+ return input;
264
+ }
265
+
266
+ // For all other types (String, Number, Integer, Boolean, Literal, Null):
267
+ // pass through unchanged -- MCP JSON already represents them correctly
268
+ return input;
269
+ }
270
+
271
+ // ---------------------------------------------------------------------------
272
+ // TypeBox to Zod conversion
273
+ // ---------------------------------------------------------------------------
274
+
275
+ /**
276
+ * Zod module shape (the parts we use from zod).
277
+ * Typed loosely since zod is a transitive dependency of @modelcontextprotocol/sdk,
278
+ * not a direct dependency of Sprinkles.
279
+ */
280
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
281
+
282
+ let _zod;
283
+
284
+ /**
285
+ * Lazily import the Zod module.
286
+ * Zod is a transitive dependency of @modelcontextprotocol/sdk, so it's
287
+ * guaranteed to be available when MCP mode is used.
288
+ */
289
+ async function getZod() {
290
+ if (_zod) return _zod;
291
+ try {
292
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
293
+ const mod = await Promise.resolve().then(() => _interopRequireWildcard(require("zod")));
294
+ _zod = {
295
+ string: () => mod.string(),
296
+ number: () => mod.number(),
297
+ boolean: () => mod.boolean(),
298
+ literal: v => mod.literal(v),
299
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
300
+ object: shape => mod.object(shape),
301
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
302
+ array: item => mod.array(item),
303
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
304
+ union: members => mod.union(members),
305
+ nullType: () => mod.nullType(),
306
+ any: () => mod.any()
307
+ };
308
+ return _zod;
309
+ } catch {
310
+ throw new Error("MCP mode requires zod (via @modelcontextprotocol/sdk). Install @modelcontextprotocol/sdk.");
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Convert a TypeBox schema to a Zod schema for MCP SDK compatibility.
316
+ *
317
+ * The MCP SDK (v1.27+) expects Zod schemas for tool input validation,
318
+ * not plain JSON Schema objects. This function converts TypeBox schemas
319
+ * to their Zod equivalents so the SDK correctly registers tool parameters.
320
+ *
321
+ * BigInt fields are represented as z.string() with a regex pattern since
322
+ * JSON (and thus MCP) has no BigInt type. The coerceMcpInput function
323
+ * handles converting these string values back to BigInt at call time.
324
+ */
325
+ async function typeboxToZod(schema) {
326
+ const z = await getZod();
327
+ return _typeboxToZod(schema, z);
328
+ }
329
+ function _typeboxToZod(schema, z) {
330
+ if ((0, _typeGuards.isBigInt)(schema)) {
331
+ // BigInt -> string with regex pattern (JSON has no BigInt)
332
+ return z.string().regex(/^-?[0-9]+$/);
333
+ }
334
+ if ((0, _typeGuards.isString)(schema)) {
335
+ let s = z.string();
336
+ if (schema.description) s = s.describe(schema.description);
337
+ return s;
338
+ }
339
+ if ((0, _typeGuards.isNumber)(schema) || (0, _typeGuards.isInteger)(schema)) {
340
+ let n = z.number();
341
+ if (schema.description) n = n.describe(schema.description);
342
+ return n;
343
+ }
344
+ if ((0, _typeGuards.isBoolean)(schema)) {
345
+ let b = z.boolean();
346
+ if (schema.description) b = b.describe(schema.description);
347
+ return b;
348
+ }
349
+ if ((0, _typeGuards.isNull)(schema)) {
350
+ return z.nullType();
351
+ }
352
+ if ((0, _typeGuards.isLiteral)(schema)) {
353
+ return z.literal(schema.const);
354
+ }
355
+ if ((0, _typeGuards.isArray)(schema)) {
356
+ const itemSchema = schema.items;
357
+ if (itemSchema) {
358
+ return z.array(_typeboxToZod(itemSchema, z));
359
+ }
360
+ return z.array(z.any());
361
+ }
362
+ if ((0, _typeGuards.isObject)(schema)) {
363
+ const properties = schema.properties;
364
+ const shape = {};
365
+ for (const [propName, propSchema] of Object.entries(properties)) {
366
+ let zodProp = _typeboxToZod(propSchema, z);
367
+ if ((0, _typeGuards.isOptional)(propSchema)) {
368
+ zodProp = zodProp.optional();
369
+ }
370
+ shape[propName] = zodProp;
371
+ }
372
+ return z.object(shape);
373
+ }
374
+ if ((0, _typeGuards.isUnion)(schema)) {
375
+ const members = schema.anyOf.map(member => _typeboxToZod(member, z));
376
+ if (members.length >= 2) {
377
+ return z.union(members);
378
+ }
379
+ return members[0] ?? z.any();
380
+ }
381
+
382
+ // Fallback: accept anything
383
+ return z.any();
384
+ }
385
+
386
+ /**
387
+ * Convert a TypeBox object schema to a Zod "raw shape" (Record<string, ZodType>).
388
+ * This is the format expected by the MCP SDK's tool() API.
389
+ */
390
+ async function typeboxToZodShape(schema) {
391
+ const z = await getZod();
392
+ if (!(0, _typeGuards.isObject)(schema)) {
393
+ // Non-object input schemas get wrapped as { input: zodSchema }
394
+ return {
395
+ input: _typeboxToZod(schema, z)
396
+ };
397
+ }
398
+ const properties = schema.properties;
399
+ const shape = {};
400
+ for (const [propName, propSchema] of Object.entries(properties)) {
401
+ let zodProp = _typeboxToZod(propSchema, z);
402
+ if ((0, _typeGuards.isOptional)(propSchema)) {
403
+ zodProp = zodProp.optional();
404
+ }
405
+ shape[propName] = zodProp;
406
+ }
407
+ return shape;
408
+ }
409
+
410
+ // ---------------------------------------------------------------------------
411
+ // Lazy MCP SDK import
412
+ // ---------------------------------------------------------------------------
413
+
414
+ /**
415
+ * MCP SDK module shape (the parts we use from @modelcontextprotocol/sdk).
416
+ * Typed loosely to avoid a hard dependency at compile time.
417
+ */
418
+
419
+ /**
420
+ * Lazily import the MCP SDK server module.
421
+ *
422
+ * Throws a clear, actionable error if the SDK is not installed, rather than
423
+ * a cryptic "Cannot find module" Node error.
424
+ */
425
+ async function getMcpSdk() {
426
+ try {
427
+ return await Promise.resolve().then(() => _interopRequireWildcard(require("@modelcontextprotocol/sdk/server/mcp.js")));
428
+ } catch {
429
+ throw new Error("MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk");
430
+ }
431
+ }
432
+
433
+ /**
434
+ * Lazily import the MCP SDK stdio transport module.
435
+ */
436
+ async function getMcpStdioTransport() {
437
+ try {
438
+ return await Promise.resolve().then(() => _interopRequireWildcard(require("@modelcontextprotocol/sdk/server/stdio.js")));
439
+ } catch {
440
+ throw new Error("MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk");
441
+ }
442
+ }
443
+
444
+ // ---------------------------------------------------------------------------
445
+ // MCP server creation
446
+ // ---------------------------------------------------------------------------
447
+
448
+ /**
449
+ * Create an MCP server and register all actions from a Sprinkle instance as
450
+ * MCP tools.
451
+ *
452
+ * Each action becomes a tool with:
453
+ * - name: action.name
454
+ * - description: action.description
455
+ * - input schema: typeboxToJsonSchema(action.inputSchema)
456
+ * - handler: coerces input, executes action, returns JSON result
457
+ *
458
+ * The Sprinkle instance must already be initialized with a profile before
459
+ * this function is called.
460
+ *
461
+ * @param sprinkle - Fully-initialized Sprinkle instance
462
+ * @param serverName - Name to use for the MCP server
463
+ * @returns The configured McpServer instance (not yet connected)
464
+ */
465
+ async function createMcpServer(sprinkle, serverName
466
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
467
+ ) {
468
+ const {
469
+ McpServer
470
+ } = await getMcpSdk();
471
+
472
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
473
+ const server = new McpServer({
474
+ name: serverName,
475
+ version: PACKAGE_VERSION
476
+ });
477
+ const actions = sprinkle.listActions();
478
+ for (const action of actions) {
479
+ const zodShape = await typeboxToZodShape(action.inputSchema);
480
+
481
+ // Register the action as an MCP tool.
482
+ // The high-level McpServer.tool() API accepts a Zod raw shape
483
+ // (Record<string, ZodType>) for input validation.
484
+ server.tool(action.name, action.description, zodShape,
485
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
486
+ async input => {
487
+ const coercedInput = coerceMcpInput(input, action.inputSchema);
488
+ const context = {
489
+ sprinkle,
490
+ settings: sprinkle.settings
491
+ };
492
+
493
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
494
+ const result = await (0, _runner.executeAction)(action, coercedInput, context);
495
+ if (result.success) {
496
+ return {
497
+ content: [{
498
+ type: "text",
499
+ text: JSON.stringify(result.data, _encryption.bigIntReplacer)
500
+ }]
501
+ };
502
+ } else {
503
+ return {
504
+ content: [{
505
+ type: "text",
506
+ text: JSON.stringify({
507
+ error: {
508
+ code: result.error.code,
509
+ message: result.error.message,
510
+ details: result.error.details
511
+ }
512
+ }, _encryption.bigIntReplacer)
513
+ }],
514
+ isError: true
515
+ };
516
+ }
517
+ });
518
+ }
519
+ return server;
520
+ }
521
+
522
+ // ---------------------------------------------------------------------------
523
+ // MCP orchestrator
524
+ // ---------------------------------------------------------------------------
525
+
526
+ /**
527
+ * Start an MCP server on stdio transport.
528
+ *
529
+ * This function:
530
+ * 1. Creates the MCP server and registers all actions as tools
531
+ * 2. Creates a StdioServerTransport
532
+ * 3. Connects the server to the transport
533
+ *
534
+ * IMPORTANT: All logging in this function goes to stderr. stdout is reserved
535
+ * for the MCP protocol messages.
536
+ *
537
+ * The Sprinkle instance must already be initialized with a profile. Profile
538
+ * initialization is the responsibility of the caller (Sprinkle.Run()).
539
+ *
540
+ * @param sprinkle - Fully-initialized Sprinkle instance
541
+ * @param serverName - Name to use for the MCP server
542
+ */
543
+ async function runMcp(sprinkle, serverName) {
544
+ const {
545
+ StdioServerTransport
546
+ } = await getMcpStdioTransport();
547
+ const server = await createMcpServer(sprinkle, serverName);
548
+ const transport = new StdioServerTransport();
549
+ try {
550
+ await server.connect(transport);
551
+ } catch (err) {
552
+ // Log errors to stderr only -- stdout is the MCP transport channel
553
+ process.stderr.write(`[sprinkle-mcp] Failed to start MCP server: ${err instanceof Error ? err.message : String(err)}\n`);
554
+ throw err;
555
+ }
556
+ }
557
+ //# sourceMappingURL=mcp-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-adapter.js","names":["_module","require","_url","_path","_encryption","_typeGuards","_runner","_interopRequireWildcard","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","loadPackageVersion","createRequire","import","meta","url","__filename","fileURLToPath","__dirname","dirname","candidates","resolve","candidate","pkg","version","PACKAGE_VERSION","typeboxToJsonSchema","schema","isBigInt","type","pattern","description","isString","result","undefined","title","examples","minLength","maxLength","isSensitive","writeOnly","isNumber","minimum","maximum","isInteger","isBoolean","isNull","isArray","itemSchema","items","minItems","maxItems","isObject","properties","required","convertedProperties","propName","propSchema","entries","length","isUnion","anyOf","map","member","isLiteral","value","const","console","warn","coerceMcpInput","input","test","BigInt","Math","trunc","Array","item","_zod","getZod","mod","Promise","then","string","number","boolean","literal","v","object","shape","array","union","members","nullType","any","Error","typeboxToZod","z","_typeboxToZod","regex","s","describe","b","zodProp","isOptional","optional","typeboxToZodShape","getMcpSdk","getMcpStdioTransport","createMcpServer","sprinkle","serverName","McpServer","server","name","actions","listActions","action","zodShape","inputSchema","tool","coercedInput","context","settings","executeAction","success","content","text","JSON","stringify","data","bigIntReplacer","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":";;;;;;;;;;;;AAYA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,IAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AAEA,IAAAG,WAAA,GAAAH,OAAA;AACA,IAAAI,WAAA,GAAAJ,OAAA;AAeA,IAAAK,OAAA,GAAAL,OAAA;AAA4C,SAAAM,wBAAAC,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAH,uBAAA,YAAAA,CAAAC,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAA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