@sundaeswap/sprinkles 0.6.0 → 0.7.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 (158) hide show
  1. package/dist/cjs/Sprinkle/__tests__/action-integration.test.js +590 -0
  2. package/dist/cjs/Sprinkle/__tests__/action-integration.test.js.map +1 -0
  3. package/dist/cjs/Sprinkle/__tests__/action-registry.test.js +193 -0
  4. package/dist/cjs/Sprinkle/__tests__/action-registry.test.js.map +1 -0
  5. package/dist/cjs/Sprinkle/__tests__/action-runner.test.js +304 -0
  6. package/dist/cjs/Sprinkle/__tests__/action-runner.test.js.map +1 -0
  7. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js +1110 -0
  8. package/dist/cjs/Sprinkle/__tests__/builtin-actions.test.js.map +1 -0
  9. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js +722 -0
  10. package/dist/cjs/Sprinkle/__tests__/cli-adapter.test.js.map +1 -0
  11. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js +138 -0
  12. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  13. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js +713 -0
  14. package/dist/cjs/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -0
  15. package/dist/cjs/Sprinkle/__tests__/tui-helpers.test.js +334 -0
  16. package/dist/cjs/Sprinkle/__tests__/tui-helpers.test.js.map +1 -0
  17. package/dist/cjs/Sprinkle/__tests__/wallet-transaction-actions.test.js +749 -0
  18. package/dist/cjs/Sprinkle/__tests__/wallet-transaction-actions.test.js.map +1 -0
  19. package/dist/cjs/Sprinkle/actions/builtin/blaze-helper.js +61 -0
  20. package/dist/cjs/Sprinkle/actions/builtin/blaze-helper.js.map +1 -0
  21. package/dist/cjs/Sprinkle/actions/builtin/index.js +117 -0
  22. package/dist/cjs/Sprinkle/actions/builtin/index.js.map +1 -0
  23. package/dist/cjs/Sprinkle/actions/builtin/profile-actions.js +202 -0
  24. package/dist/cjs/Sprinkle/actions/builtin/profile-actions.js.map +1 -0
  25. package/dist/cjs/Sprinkle/actions/builtin/settings-actions.js +87 -0
  26. package/dist/cjs/Sprinkle/actions/builtin/settings-actions.js.map +1 -0
  27. package/dist/cjs/Sprinkle/actions/builtin/transaction-actions.js +345 -0
  28. package/dist/cjs/Sprinkle/actions/builtin/transaction-actions.js.map +1 -0
  29. package/dist/cjs/Sprinkle/actions/builtin/wallet-actions.js +212 -0
  30. package/dist/cjs/Sprinkle/actions/builtin/wallet-actions.js.map +1 -0
  31. package/dist/cjs/Sprinkle/actions/cli-adapter.js +372 -0
  32. package/dist/cjs/Sprinkle/actions/cli-adapter.js.map +1 -0
  33. package/dist/cjs/Sprinkle/actions/index.js +127 -0
  34. package/dist/cjs/Sprinkle/actions/index.js.map +1 -0
  35. package/dist/cjs/Sprinkle/actions/mcp-adapter.js +415 -0
  36. package/dist/cjs/Sprinkle/actions/mcp-adapter.js.map +1 -0
  37. package/dist/cjs/Sprinkle/actions/registry.js +92 -0
  38. package/dist/cjs/Sprinkle/actions/registry.js.map +1 -0
  39. package/dist/cjs/Sprinkle/actions/runner.js +190 -0
  40. package/dist/cjs/Sprinkle/actions/runner.js.map +1 -0
  41. package/dist/cjs/Sprinkle/actions/tui-helpers.js +96 -0
  42. package/dist/cjs/Sprinkle/actions/tui-helpers.js.map +1 -0
  43. package/dist/cjs/Sprinkle/actions/types.js +68 -0
  44. package/dist/cjs/Sprinkle/actions/types.js.map +1 -0
  45. package/dist/cjs/Sprinkle/index.js +451 -4
  46. package/dist/cjs/Sprinkle/index.js.map +1 -1
  47. package/dist/cjs/Sprinkle/prompts.js +12 -7
  48. package/dist/cjs/Sprinkle/prompts.js.map +1 -1
  49. package/dist/cjs/Sprinkle/type-guards.js +7 -1
  50. package/dist/cjs/Sprinkle/type-guards.js.map +1 -1
  51. package/dist/esm/Sprinkle/__tests__/action-integration.test.js +588 -0
  52. package/dist/esm/Sprinkle/__tests__/action-integration.test.js.map +1 -0
  53. package/dist/esm/Sprinkle/__tests__/action-registry.test.js +192 -0
  54. package/dist/esm/Sprinkle/__tests__/action-registry.test.js.map +1 -0
  55. package/dist/esm/Sprinkle/__tests__/action-runner.test.js +302 -0
  56. package/dist/esm/Sprinkle/__tests__/action-runner.test.js.map +1 -0
  57. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js +1107 -0
  58. package/dist/esm/Sprinkle/__tests__/builtin-actions.test.js.map +1 -0
  59. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js +720 -0
  60. package/dist/esm/Sprinkle/__tests__/cli-adapter.test.js.map +1 -0
  61. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js +138 -0
  62. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -1
  63. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js +712 -0
  64. package/dist/esm/Sprinkle/__tests__/mcp-adapter.test.js.map +1 -0
  65. package/dist/esm/Sprinkle/__tests__/tui-helpers.test.js +332 -0
  66. package/dist/esm/Sprinkle/__tests__/tui-helpers.test.js.map +1 -0
  67. package/dist/esm/Sprinkle/__tests__/wallet-transaction-actions.test.js +747 -0
  68. package/dist/esm/Sprinkle/__tests__/wallet-transaction-actions.test.js.map +1 -0
  69. package/dist/esm/Sprinkle/actions/builtin/blaze-helper.js +55 -0
  70. package/dist/esm/Sprinkle/actions/builtin/blaze-helper.js.map +1 -0
  71. package/dist/esm/Sprinkle/actions/builtin/index.js +32 -0
  72. package/dist/esm/Sprinkle/actions/builtin/index.js.map +1 -0
  73. package/dist/esm/Sprinkle/actions/builtin/profile-actions.js +197 -0
  74. package/dist/esm/Sprinkle/actions/builtin/profile-actions.js.map +1 -0
  75. package/dist/esm/Sprinkle/actions/builtin/settings-actions.js +81 -0
  76. package/dist/esm/Sprinkle/actions/builtin/settings-actions.js.map +1 -0
  77. package/dist/esm/Sprinkle/actions/builtin/transaction-actions.js +340 -0
  78. package/dist/esm/Sprinkle/actions/builtin/transaction-actions.js.map +1 -0
  79. package/dist/esm/Sprinkle/actions/builtin/wallet-actions.js +207 -0
  80. package/dist/esm/Sprinkle/actions/builtin/wallet-actions.js.map +1 -0
  81. package/dist/esm/Sprinkle/actions/cli-adapter.js +361 -0
  82. package/dist/esm/Sprinkle/actions/cli-adapter.js.map +1 -0
  83. package/dist/esm/Sprinkle/actions/index.js +12 -0
  84. package/dist/esm/Sprinkle/actions/index.js.map +1 -0
  85. package/dist/esm/Sprinkle/actions/mcp-adapter.js +407 -0
  86. package/dist/esm/Sprinkle/actions/mcp-adapter.js.map +1 -0
  87. package/dist/esm/Sprinkle/actions/registry.js +85 -0
  88. package/dist/esm/Sprinkle/actions/registry.js.map +1 -0
  89. package/dist/esm/Sprinkle/actions/runner.js +182 -0
  90. package/dist/esm/Sprinkle/actions/runner.js.map +1 -0
  91. package/dist/esm/Sprinkle/actions/tui-helpers.js +91 -0
  92. package/dist/esm/Sprinkle/actions/tui-helpers.js.map +1 -0
  93. package/dist/esm/Sprinkle/actions/types.js +61 -0
  94. package/dist/esm/Sprinkle/actions/types.js.map +1 -0
  95. package/dist/esm/Sprinkle/index.js +299 -4
  96. package/dist/esm/Sprinkle/index.js.map +1 -1
  97. package/dist/esm/Sprinkle/prompts.js +12 -7
  98. package/dist/esm/Sprinkle/prompts.js.map +1 -1
  99. package/dist/esm/Sprinkle/type-guards.js +3 -0
  100. package/dist/esm/Sprinkle/type-guards.js.map +1 -1
  101. package/dist/types/Sprinkle/actions/builtin/blaze-helper.d.ts +39 -0
  102. package/dist/types/Sprinkle/actions/builtin/blaze-helper.d.ts.map +1 -0
  103. package/dist/types/Sprinkle/actions/builtin/index.d.ts +26 -0
  104. package/dist/types/Sprinkle/actions/builtin/index.d.ts.map +1 -0
  105. package/dist/types/Sprinkle/actions/builtin/profile-actions.d.ts +55 -0
  106. package/dist/types/Sprinkle/actions/builtin/profile-actions.d.ts.map +1 -0
  107. package/dist/types/Sprinkle/actions/builtin/settings-actions.d.ts +32 -0
  108. package/dist/types/Sprinkle/actions/builtin/settings-actions.d.ts.map +1 -0
  109. package/dist/types/Sprinkle/actions/builtin/transaction-actions.d.ts +70 -0
  110. package/dist/types/Sprinkle/actions/builtin/transaction-actions.d.ts.map +1 -0
  111. package/dist/types/Sprinkle/actions/builtin/wallet-actions.d.ts +50 -0
  112. package/dist/types/Sprinkle/actions/builtin/wallet-actions.d.ts.map +1 -0
  113. package/dist/types/Sprinkle/actions/cli-adapter.d.ts +104 -0
  114. package/dist/types/Sprinkle/actions/cli-adapter.d.ts.map +1 -0
  115. package/dist/types/Sprinkle/actions/index.d.ts +12 -0
  116. package/dist/types/Sprinkle/actions/index.d.ts.map +1 -0
  117. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts +92 -0
  118. package/dist/types/Sprinkle/actions/mcp-adapter.d.ts.map +1 -0
  119. package/dist/types/Sprinkle/actions/registry.d.ts +42 -0
  120. package/dist/types/Sprinkle/actions/registry.d.ts.map +1 -0
  121. package/dist/types/Sprinkle/actions/runner.d.ts +45 -0
  122. package/dist/types/Sprinkle/actions/runner.d.ts.map +1 -0
  123. package/dist/types/Sprinkle/actions/tui-helpers.d.ts +53 -0
  124. package/dist/types/Sprinkle/actions/tui-helpers.d.ts.map +1 -0
  125. package/dist/types/Sprinkle/actions/types.d.ts +76 -0
  126. package/dist/types/Sprinkle/actions/types.d.ts.map +1 -0
  127. package/dist/types/Sprinkle/index.d.ts +81 -1
  128. package/dist/types/Sprinkle/index.d.ts.map +1 -1
  129. package/dist/types/Sprinkle/prompts.d.ts.map +1 -1
  130. package/dist/types/Sprinkle/type-guards.d.ts +4 -1
  131. package/dist/types/Sprinkle/type-guards.d.ts.map +1 -1
  132. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  133. package/package.json +9 -2
  134. package/src/Sprinkle/__tests__/action-integration.test.ts +558 -0
  135. package/src/Sprinkle/__tests__/action-registry.test.ts +187 -0
  136. package/src/Sprinkle/__tests__/action-runner.test.ts +324 -0
  137. package/src/Sprinkle/__tests__/builtin-actions.test.ts +1022 -0
  138. package/src/Sprinkle/__tests__/cli-adapter.test.ts +715 -0
  139. package/src/Sprinkle/__tests__/fill-in-struct.test.ts +144 -0
  140. package/src/Sprinkle/__tests__/mcp-adapter.test.ts +718 -0
  141. package/src/Sprinkle/__tests__/tui-helpers.test.ts +325 -0
  142. package/src/Sprinkle/__tests__/wallet-transaction-actions.test.ts +695 -0
  143. package/src/Sprinkle/actions/builtin/blaze-helper.ts +89 -0
  144. package/src/Sprinkle/actions/builtin/index.ts +86 -0
  145. package/src/Sprinkle/actions/builtin/profile-actions.ts +229 -0
  146. package/src/Sprinkle/actions/builtin/settings-actions.ts +99 -0
  147. package/src/Sprinkle/actions/builtin/transaction-actions.ts +381 -0
  148. package/src/Sprinkle/actions/builtin/wallet-actions.ts +233 -0
  149. package/src/Sprinkle/actions/cli-adapter.ts +430 -0
  150. package/src/Sprinkle/actions/index.ts +32 -0
  151. package/src/Sprinkle/actions/mcp-adapter.ts +463 -0
  152. package/src/Sprinkle/actions/registry.ts +97 -0
  153. package/src/Sprinkle/actions/runner.ts +200 -0
  154. package/src/Sprinkle/actions/tui-helpers.ts +114 -0
  155. package/src/Sprinkle/actions/types.ts +91 -0
  156. package/src/Sprinkle/index.ts +395 -3
  157. package/src/Sprinkle/prompts.ts +118 -72
  158. package/src/Sprinkle/type-guards.ts +9 -0
@@ -0,0 +1,463 @@
1
+ /**
2
+ * MCP adapter for Sprinkles actions.
3
+ *
4
+ * Provides:
5
+ * - TypeBox to JSON Schema conversion (typeboxToJsonSchema)
6
+ * - BigInt string coercion for MCP input (coerceMcpInput)
7
+ * - Lazy MCP SDK import with graceful error (getMcpSdk)
8
+ * - MCP server creation from registered actions (createMcpServer)
9
+ * - MCP orchestrator that starts a stdio server (runMcp)
10
+ */
11
+
12
+ import { createRequire } from "module";
13
+ import { fileURLToPath } from "url";
14
+ import { dirname, resolve } from "path";
15
+ import type { TSchema } from "@sinclair/typebox";
16
+ import { bigIntReplacer } from "../encryption.js";
17
+ import {
18
+ isArray,
19
+ isBigInt,
20
+ isBoolean,
21
+ isInteger,
22
+ isLiteral,
23
+ isNull,
24
+ isNumber,
25
+ isObject,
26
+ isOptional,
27
+ isString,
28
+ isUnion,
29
+ isSensitive,
30
+ } from "../type-guards.js";
31
+ import type { AnyAction } from "./types.js";
32
+ import { executeAction } from "./runner.js";
33
+
34
+ // Re-import Sprinkle as a type only to avoid circular deps
35
+ import type { Sprinkle } from "../index.js";
36
+
37
+ // Load package version for MCP server identification
38
+ // Robustly find package.json - works from both src and dist directories
39
+ function loadPackageVersion(): string {
40
+ const require = createRequire(import.meta.url);
41
+ const __filename = fileURLToPath(import.meta.url);
42
+ const __dirname = dirname(__filename);
43
+
44
+ // Try multiple possible paths (handles src vs dist/esm vs dist/cjs)
45
+ const candidates = [
46
+ resolve(__dirname, "../../../package.json"), // from src/Sprinkle/actions
47
+ resolve(__dirname, "../../../../package.json"), // from dist/*/Sprinkle/actions
48
+ ];
49
+
50
+ for (const candidate of candidates) {
51
+ try {
52
+ const pkg = require(candidate) as { version: string };
53
+ return pkg.version;
54
+ } catch {
55
+ // Try next candidate
56
+ }
57
+ }
58
+
59
+ return "0.0.0"; // Fallback if package.json not found
60
+ }
61
+
62
+ const PACKAGE_VERSION: string = loadPackageVersion();
63
+
64
+ // ---------------------------------------------------------------------------
65
+ // TypeBox to JSON Schema conversion
66
+ // ---------------------------------------------------------------------------
67
+
68
+ /**
69
+ * Convert a TypeBox schema to a plain JSON Schema object.
70
+ *
71
+ * Strips TypeBox-internal symbols and properties (Kind, OptionalKind, $id)
72
+ * and maps TypeBox-specific types to their JSON Schema equivalents:
73
+ * - BigInt -> { type: "string", pattern: "^-?[0-9]+$" } (JSON has no BigInt)
74
+ * - Sensitive string fields -> add writeOnly: true
75
+ * - Optional -> recurse on inner schema (Optional does not change Kind)
76
+ * - Union -> { anyOf: [...] }
77
+ * - Literal -> { type, const }
78
+ * - Null -> { type: "null" }
79
+ * - Unknown/unsupported -> {} (accepts anything)
80
+ */
81
+ export function typeboxToJsonSchema(schema: TSchema): Record<string, unknown> {
82
+ // Optional in TypeBox doesn't change the Kind, but marks the schema with
83
+ // OptionalKind. We can check for it and recurse transparently since all
84
+ // other guards still work correctly on optional-wrapped schemas.
85
+ //
86
+ // We intentionally do NOT strip optional here -- the containing Object
87
+ // converter decides whether to include a property in `required` based
88
+ // on the isOptional guard applied to the property schema.
89
+
90
+ if (isBigInt(schema)) {
91
+ return {
92
+ type: "string",
93
+ pattern: "^-?[0-9]+$",
94
+ description: "BigInt value as string",
95
+ };
96
+ }
97
+
98
+ if (isString(schema)) {
99
+ const result: Record<string, unknown> = { type: "string" };
100
+ if (schema.description !== undefined) result.description = schema.description;
101
+ if (schema.title !== undefined) result.title = schema.title;
102
+ if (schema.examples !== undefined) result.examples = schema.examples;
103
+ if (schema.pattern !== undefined) result.pattern = schema.pattern;
104
+ if (schema.minLength !== undefined) result.minLength = schema.minLength;
105
+ if (schema.maxLength !== undefined) result.maxLength = schema.maxLength;
106
+ if (schema.default !== undefined) result.default = schema.default;
107
+ // Sensitive fields are write-only (never returned in responses)
108
+ if (isSensitive(schema)) result.writeOnly = true;
109
+ return result;
110
+ }
111
+
112
+ if (isNumber(schema)) {
113
+ const result: Record<string, unknown> = { type: "number" };
114
+ if (schema.minimum !== undefined) result.minimum = schema.minimum;
115
+ if (schema.maximum !== undefined) result.maximum = schema.maximum;
116
+ if (schema.description !== undefined) result.description = schema.description;
117
+ if (schema.default !== undefined) result.default = schema.default;
118
+ return result;
119
+ }
120
+
121
+ if (isInteger(schema)) {
122
+ const result: Record<string, unknown> = { type: "integer" };
123
+ if (schema.minimum !== undefined) result.minimum = schema.minimum;
124
+ if (schema.maximum !== undefined) result.maximum = schema.maximum;
125
+ if (schema.description !== undefined) result.description = schema.description;
126
+ if (schema.default !== undefined) result.default = schema.default;
127
+ return result;
128
+ }
129
+
130
+ if (isBoolean(schema)) {
131
+ const result: Record<string, unknown> = { type: "boolean" };
132
+ if (schema.description !== undefined) result.description = schema.description;
133
+ if (schema.default !== undefined) result.default = schema.default;
134
+ return result;
135
+ }
136
+
137
+ if (isNull(schema)) {
138
+ return { type: "null" };
139
+ }
140
+
141
+ if (isArray(schema)) {
142
+ const result: Record<string, unknown> = { type: "array" };
143
+ const itemSchema = (schema as { items?: TSchema }).items;
144
+ if (itemSchema) {
145
+ result.items = typeboxToJsonSchema(itemSchema);
146
+ }
147
+ if (schema.minItems !== undefined) result.minItems = schema.minItems;
148
+ if (schema.maxItems !== undefined) result.maxItems = schema.maxItems;
149
+ if (schema.description !== undefined) result.description = schema.description;
150
+ return result;
151
+ }
152
+
153
+ if (isObject(schema)) {
154
+ const properties = schema.properties as Record<string, TSchema>;
155
+ const required: string[] = schema.required as string[] ?? [];
156
+ const convertedProperties: Record<string, unknown> = {};
157
+
158
+ for (const [propName, propSchema] of Object.entries(properties)) {
159
+ convertedProperties[propName] = typeboxToJsonSchema(propSchema);
160
+ }
161
+
162
+ const result: Record<string, unknown> = {
163
+ type: "object",
164
+ properties: convertedProperties,
165
+ };
166
+
167
+ if (required.length > 0) {
168
+ result.required = required;
169
+ }
170
+
171
+ if (schema.description !== undefined) result.description = schema.description;
172
+
173
+ return result;
174
+ }
175
+
176
+ if (isUnion(schema)) {
177
+ return {
178
+ anyOf: schema.anyOf.map((member: TSchema) => typeboxToJsonSchema(member)),
179
+ };
180
+ }
181
+
182
+ if (isLiteral(schema)) {
183
+ const value = schema.const;
184
+ let type: string;
185
+ if (typeof value === "string") {
186
+ type = "string";
187
+ } else if (typeof value === "number") {
188
+ type = "number";
189
+ } else if (typeof value === "boolean") {
190
+ type = "boolean";
191
+ } else {
192
+ // Fallback for unexpected literal types
193
+ return {};
194
+ }
195
+ return { type, const: value };
196
+ }
197
+
198
+ // Unknown / unsupported TypeBox type: emit empty schema (accepts anything)
199
+ // This is a safe fallback that allows the MCP tool to still receive input.
200
+ console.warn(
201
+ `[mcp-adapter] Unsupported TypeBox kind: ${(schema as Record<string, unknown>)["[Kind]"] ?? "(unknown)"}. Emitting empty schema.`,
202
+ );
203
+ return {};
204
+ }
205
+
206
+ // ---------------------------------------------------------------------------
207
+ // Input coercion: BigInt string -> BigInt
208
+ // ---------------------------------------------------------------------------
209
+
210
+ /**
211
+ * Coerce MCP JSON input values to the types expected by a TypeBox schema.
212
+ *
213
+ * MCP transmits all values as JSON, which has no BigInt type. When the schema
214
+ * expects a BigInt, the value arrives as a numeric string (e.g. "12345678").
215
+ * This function recursively walks the schema and input, converting BigInt
216
+ * string values to actual BigInt where needed.
217
+ *
218
+ * For all other types the value is passed through unchanged.
219
+ */
220
+ export function coerceMcpInput(input: unknown, schema: TSchema): unknown {
221
+ if (input === null || input === undefined) return input;
222
+
223
+ if (isBigInt(schema)) {
224
+ if (typeof input === "bigint") return input;
225
+ if (typeof input === "string" && /^-?[0-9]+$/.test(input)) {
226
+ try {
227
+ return BigInt(input);
228
+ } catch {
229
+ return input;
230
+ }
231
+ }
232
+ if (typeof input === "number") {
233
+ try {
234
+ return BigInt(Math.trunc(input));
235
+ } catch {
236
+ return input;
237
+ }
238
+ }
239
+ return input;
240
+ }
241
+
242
+ if (isArray(schema)) {
243
+ const itemSchema = (schema as { items?: TSchema }).items;
244
+ if (Array.isArray(input) && itemSchema) {
245
+ return input.map((item) => coerceMcpInput(item, itemSchema));
246
+ }
247
+ return input;
248
+ }
249
+
250
+ if (isObject(schema)) {
251
+ if (typeof input !== "object" || Array.isArray(input)) return input;
252
+ const properties = schema.properties as Record<string, TSchema>;
253
+ const result: Record<string, unknown> = { ...(input as Record<string, unknown>) };
254
+ for (const [propName, propSchema] of Object.entries(properties)) {
255
+ if (propName in result) {
256
+ result[propName] = coerceMcpInput(result[propName], propSchema);
257
+ }
258
+ }
259
+ return result;
260
+ }
261
+
262
+ if (isUnion(schema)) {
263
+ // For unions, we try each member schema in order and return the first
264
+ // successful coercion. BigInt members take priority over string members
265
+ // since they have a narrower, unambiguous pattern.
266
+ for (const member of schema.anyOf) {
267
+ if (isBigInt(member)) {
268
+ if (typeof input === "string" && /^-?[0-9]+$/.test(input)) {
269
+ return coerceMcpInput(input, member);
270
+ }
271
+ }
272
+ }
273
+ // No BigInt match; return as-is for other union members
274
+ return input;
275
+ }
276
+
277
+ // For all other types (String, Number, Integer, Boolean, Literal, Null):
278
+ // pass through unchanged -- MCP JSON already represents them correctly
279
+ return input;
280
+ }
281
+
282
+ // ---------------------------------------------------------------------------
283
+ // Lazy MCP SDK import
284
+ // ---------------------------------------------------------------------------
285
+
286
+ /**
287
+ * MCP SDK module shape (the parts we use from @modelcontextprotocol/sdk).
288
+ * Typed loosely to avoid a hard dependency at compile time.
289
+ */
290
+ interface IMcpSdkModule {
291
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
292
+ McpServer: new (opts: { name: string; version: string }) => any;
293
+ }
294
+
295
+ interface IStdioTransportModule {
296
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
297
+ StdioServerTransport: new () => any;
298
+ }
299
+
300
+ /**
301
+ * Lazily import the MCP SDK server module.
302
+ *
303
+ * Throws a clear, actionable error if the SDK is not installed, rather than
304
+ * a cryptic "Cannot find module" Node error.
305
+ */
306
+ export async function getMcpSdk(): Promise<IMcpSdkModule> {
307
+ try {
308
+ return (await import(
309
+ "@modelcontextprotocol/sdk/server/mcp.js"
310
+ )) as IMcpSdkModule;
311
+ } catch {
312
+ throw new Error(
313
+ "MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk",
314
+ );
315
+ }
316
+ }
317
+
318
+ /**
319
+ * Lazily import the MCP SDK stdio transport module.
320
+ */
321
+ async function getMcpStdioTransport(): Promise<IStdioTransportModule> {
322
+ try {
323
+ return (await import(
324
+ "@modelcontextprotocol/sdk/server/stdio.js"
325
+ )) as IStdioTransportModule;
326
+ } catch {
327
+ throw new Error(
328
+ "MCP mode requires @modelcontextprotocol/sdk. Install it with: npm install @modelcontextprotocol/sdk",
329
+ );
330
+ }
331
+ }
332
+
333
+ // ---------------------------------------------------------------------------
334
+ // MCP server creation
335
+ // ---------------------------------------------------------------------------
336
+
337
+ /**
338
+ * Create an MCP server and register all actions from a Sprinkle instance as
339
+ * MCP tools.
340
+ *
341
+ * Each action becomes a tool with:
342
+ * - name: action.name
343
+ * - description: action.description
344
+ * - input schema: typeboxToJsonSchema(action.inputSchema)
345
+ * - handler: coerces input, executes action, returns JSON result
346
+ *
347
+ * The Sprinkle instance must already be initialized with a profile before
348
+ * this function is called.
349
+ *
350
+ * @param sprinkle - Fully-initialized Sprinkle instance
351
+ * @param serverName - Name to use for the MCP server
352
+ * @returns The configured McpServer instance (not yet connected)
353
+ */
354
+ export async function createMcpServer<S extends TSchema>(
355
+ sprinkle: Sprinkle<S>,
356
+ serverName: string,
357
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
358
+ ): Promise<any> {
359
+ const { McpServer } = await getMcpSdk();
360
+
361
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
362
+ const server = new McpServer({ name: serverName, version: PACKAGE_VERSION });
363
+
364
+ const actions = sprinkle.listActions() as AnyAction<S>[];
365
+
366
+ for (const action of actions) {
367
+ const inputSchema = typeboxToJsonSchema(action.inputSchema);
368
+
369
+ // Register the action as an MCP tool.
370
+ // The high-level McpServer.tool() API accepts:
371
+ // tool(name, description, schema, handler)
372
+ server.tool(
373
+ action.name,
374
+ action.description,
375
+ inputSchema,
376
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
377
+ async (input: unknown): Promise<any> => {
378
+ const coercedInput = coerceMcpInput(input, action.inputSchema);
379
+
380
+ const context = {
381
+ sprinkle,
382
+ settings: sprinkle.settings,
383
+ };
384
+
385
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
386
+ const result = await executeAction(action as any, coercedInput, context as any);
387
+
388
+ if (result.success) {
389
+ return {
390
+ content: [
391
+ {
392
+ type: "text",
393
+ text: JSON.stringify(result.data, bigIntReplacer),
394
+ },
395
+ ],
396
+ };
397
+ } else {
398
+ return {
399
+ content: [
400
+ {
401
+ type: "text",
402
+ text: JSON.stringify(
403
+ {
404
+ error: {
405
+ code: result.error.code,
406
+ message: result.error.message,
407
+ details: result.error.details,
408
+ },
409
+ },
410
+ bigIntReplacer,
411
+ ),
412
+ },
413
+ ],
414
+ isError: true,
415
+ };
416
+ }
417
+ },
418
+ );
419
+ }
420
+
421
+ return server;
422
+ }
423
+
424
+ // ---------------------------------------------------------------------------
425
+ // MCP orchestrator
426
+ // ---------------------------------------------------------------------------
427
+
428
+ /**
429
+ * Start an MCP server on stdio transport.
430
+ *
431
+ * This function:
432
+ * 1. Creates the MCP server and registers all actions as tools
433
+ * 2. Creates a StdioServerTransport
434
+ * 3. Connects the server to the transport
435
+ *
436
+ * IMPORTANT: All logging in this function goes to stderr. stdout is reserved
437
+ * for the MCP protocol messages.
438
+ *
439
+ * The Sprinkle instance must already be initialized with a profile. Profile
440
+ * initialization is the responsibility of the caller (Sprinkle.Run()).
441
+ *
442
+ * @param sprinkle - Fully-initialized Sprinkle instance
443
+ * @param serverName - Name to use for the MCP server
444
+ */
445
+ export async function runMcp<S extends TSchema>(
446
+ sprinkle: Sprinkle<S>,
447
+ serverName: string,
448
+ ): Promise<void> {
449
+ const { StdioServerTransport } = await getMcpStdioTransport();
450
+
451
+ const server = await createMcpServer(sprinkle, serverName);
452
+ const transport = new StdioServerTransport();
453
+
454
+ try {
455
+ await server.connect(transport);
456
+ } catch (err) {
457
+ // Log errors to stderr only -- stdout is the MCP transport channel
458
+ process.stderr.write(
459
+ `[sprinkle-mcp] Failed to start MCP server: ${err instanceof Error ? err.message : String(err)}\n`,
460
+ );
461
+ throw err;
462
+ }
463
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Action registry for storing and retrieving registered actions.
3
+ */
4
+
5
+ import type { TSchema } from "@sinclair/typebox";
6
+ import type { AnyAction } from "./types.js";
7
+
8
+ /** Regex for valid kebab-case action names */
9
+ const KEBAB_CASE_REGEX = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
10
+
11
+ /**
12
+ * Registry for managing actions registered on a Sprinkle app.
13
+ * Enforces name uniqueness and schema validation at registration time.
14
+ */
15
+ export class ActionRegistry<S extends TSchema> {
16
+ private actions: Map<string, AnyAction<S>> = new Map();
17
+
18
+ /**
19
+ * Register an action with the registry.
20
+ *
21
+ * Validates:
22
+ * - Name is kebab-case (lowercase letters, digits, hyphens)
23
+ * - Name is unique within this registry
24
+ * - inputSchema and outputSchema are present
25
+ *
26
+ * @throws Error if validation fails
27
+ */
28
+ register(action: AnyAction<S>): void {
29
+ if (!KEBAB_CASE_REGEX.test(action.name)) {
30
+ throw new Error(
31
+ `Invalid action name "${action.name}": must be kebab-case (e.g. "my-action", "get-balance")`,
32
+ );
33
+ }
34
+
35
+ if (this.actions.has(action.name)) {
36
+ throw new Error(
37
+ `Action "${action.name}" is already registered. Action names must be unique.`,
38
+ );
39
+ }
40
+
41
+ if (!action.inputSchema) {
42
+ throw new Error(
43
+ `Action "${action.name}" is missing inputSchema. All actions must define an inputSchema.`,
44
+ );
45
+ }
46
+
47
+ if (!action.outputSchema) {
48
+ throw new Error(
49
+ `Action "${action.name}" is missing outputSchema. All actions must define an outputSchema.`,
50
+ );
51
+ }
52
+
53
+ this.actions.set(action.name, action);
54
+ }
55
+
56
+ /**
57
+ * Retrieve an action by name.
58
+ * @returns The action, or undefined if not registered
59
+ */
60
+ get(name: string): AnyAction<S> | undefined {
61
+ return this.actions.get(name);
62
+ }
63
+
64
+ /**
65
+ * Check if an action with the given name is registered.
66
+ */
67
+ has(name: string): boolean {
68
+ return this.actions.has(name);
69
+ }
70
+
71
+ /**
72
+ * List all registered actions.
73
+ */
74
+ list(): AnyAction<S>[] {
75
+ return Array.from(this.actions.values());
76
+ }
77
+
78
+ /**
79
+ * Group all registered actions by their category.
80
+ * Actions without a category are placed under "default".
81
+ */
82
+ listByCategory(): Map<string, AnyAction<S>[]> {
83
+ const result = new Map<string, AnyAction<S>[]>();
84
+
85
+ for (const action of this.actions.values()) {
86
+ const category = action.category ?? "default";
87
+ const bucket = result.get(category);
88
+ if (bucket) {
89
+ bucket.push(action);
90
+ } else {
91
+ result.set(category, [action]);
92
+ }
93
+ }
94
+
95
+ return result;
96
+ }
97
+ }