@unifiedcommerce/core 0.1.0 → 0.2.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 (245) hide show
  1. package/dist/auth/setup.d.ts.map +1 -1
  2. package/dist/auth/setup.js +8 -3
  3. package/dist/config/types.d.ts +3 -1
  4. package/dist/config/types.d.ts.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/interfaces/mcp/server.d.ts +3 -5
  9. package/dist/interfaces/mcp/server.d.ts.map +1 -1
  10. package/dist/interfaces/mcp/server.js +25 -510
  11. package/dist/interfaces/mcp/tool-builder.d.ts +120 -0
  12. package/dist/interfaces/mcp/tool-builder.d.ts.map +1 -0
  13. package/dist/interfaces/mcp/tool-builder.js +224 -0
  14. package/dist/interfaces/mcp/tools/analytics.d.ts +42 -0
  15. package/dist/interfaces/mcp/tools/analytics.d.ts.map +1 -0
  16. package/dist/interfaces/mcp/tools/analytics.js +70 -0
  17. package/dist/interfaces/mcp/tools/cart.d.ts +14 -0
  18. package/dist/interfaces/mcp/tools/cart.d.ts.map +1 -0
  19. package/dist/interfaces/mcp/tools/cart.js +47 -0
  20. package/dist/interfaces/mcp/tools/catalog.d.ts +53 -0
  21. package/dist/interfaces/mcp/tools/catalog.d.ts.map +1 -0
  22. package/dist/interfaces/mcp/tools/catalog.js +284 -0
  23. package/dist/interfaces/mcp/tools/index.d.ts +3 -0
  24. package/dist/interfaces/mcp/tools/index.d.ts.map +1 -0
  25. package/dist/interfaces/mcp/tools/index.js +20 -0
  26. package/dist/interfaces/mcp/tools/inventory.d.ts +27 -0
  27. package/dist/interfaces/mcp/tools/inventory.d.ts.map +1 -0
  28. package/dist/interfaces/mcp/tools/inventory.js +143 -0
  29. package/dist/interfaces/mcp/tools/orders.d.ts +18 -0
  30. package/dist/interfaces/mcp/tools/orders.d.ts.map +1 -0
  31. package/dist/interfaces/mcp/tools/orders.js +82 -0
  32. package/dist/interfaces/mcp/tools/pricing.d.ts +29 -0
  33. package/dist/interfaces/mcp/tools/pricing.d.ts.map +1 -0
  34. package/dist/interfaces/mcp/tools/pricing.js +90 -0
  35. package/dist/interfaces/mcp/tools/promotions.d.ts +44 -0
  36. package/dist/interfaces/mcp/tools/promotions.d.ts.map +1 -0
  37. package/dist/interfaces/mcp/tools/promotions.js +109 -0
  38. package/dist/interfaces/mcp/tools/registry.d.ts +32 -0
  39. package/dist/interfaces/mcp/tools/registry.d.ts.map +1 -0
  40. package/dist/interfaces/mcp/tools/registry.js +55 -0
  41. package/dist/interfaces/mcp/tools/search.d.ts +14 -0
  42. package/dist/interfaces/mcp/tools/search.d.ts.map +1 -0
  43. package/dist/interfaces/mcp/tools/search.js +39 -0
  44. package/dist/interfaces/mcp/tools/webhooks.d.ts +15 -0
  45. package/dist/interfaces/mcp/tools/webhooks.d.ts.map +1 -0
  46. package/dist/interfaces/mcp/tools/webhooks.js +48 -0
  47. package/dist/interfaces/mcp/transport.d.ts +17 -2
  48. package/dist/interfaces/mcp/transport.d.ts.map +1 -1
  49. package/dist/interfaces/mcp/transport.js +91 -44
  50. package/dist/interfaces/rest/router.d.ts.map +1 -1
  51. package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -1
  52. package/dist/interfaces/rest/routes/checkout.js +1 -1
  53. package/dist/interfaces/rest/routes/promotions.d.ts.map +1 -1
  54. package/dist/interfaces/rest/routes/promotions.js +3 -2
  55. package/dist/kernel/database/adapter.d.ts +8 -0
  56. package/dist/kernel/database/adapter.d.ts.map +1 -1
  57. package/dist/kernel/factory/repository-factory.d.ts.map +1 -1
  58. package/dist/kernel/factory/repository-factory.js +3 -1
  59. package/dist/kernel/local-api.d.ts.map +1 -1
  60. package/dist/kernel/local-api.js +2 -0
  61. package/dist/kernel/plugin/manifest.d.ts +3 -3
  62. package/dist/kernel/plugin/manifest.d.ts.map +1 -1
  63. package/dist/kernel/plugin/manifest.js +36 -7
  64. package/dist/runtime/kernel.d.ts +1 -2
  65. package/dist/runtime/kernel.d.ts.map +1 -1
  66. package/dist/runtime/kernel.js +16 -8
  67. package/dist/runtime/server.d.ts.map +1 -1
  68. package/dist/runtime/server.js +8 -3
  69. package/dist/test-utils/create-pglite-adapter.d.ts.map +1 -1
  70. package/dist/test-utils/create-pglite-adapter.js +7 -6
  71. package/dist/tsconfig.tsbuildinfo +1 -0
  72. package/package.json +2 -2
  73. package/src/adapters/console-email.ts +0 -43
  74. package/src/auth/access.ts +0 -187
  75. package/src/auth/auth-schema.ts +0 -139
  76. package/src/auth/middleware.ts +0 -161
  77. package/src/auth/org.ts +0 -41
  78. package/src/auth/permissions.ts +0 -28
  79. package/src/auth/setup.ts +0 -169
  80. package/src/auth/system-actor.ts +0 -19
  81. package/src/auth/types.ts +0 -10
  82. package/src/config/defaults.ts +0 -82
  83. package/src/config/define-config.ts +0 -53
  84. package/src/config/types.ts +0 -299
  85. package/src/generated/plugin-capabilities.d.ts +0 -20
  86. package/src/generated/plugin-manifest.ts +0 -23
  87. package/src/generated/plugin-repositories.d.ts +0 -20
  88. package/src/hooks/checkout-completion.ts +0 -262
  89. package/src/hooks/checkout.ts +0 -677
  90. package/src/hooks/order-emails.ts +0 -62
  91. package/src/index.ts +0 -214
  92. package/src/interfaces/mcp/agent-prompt.ts +0 -174
  93. package/src/interfaces/mcp/context-enrichment.ts +0 -177
  94. package/src/interfaces/mcp/server.ts +0 -617
  95. package/src/interfaces/mcp/transport.ts +0 -68
  96. package/src/interfaces/rest/customer-portal.ts +0 -299
  97. package/src/interfaces/rest/index.ts +0 -74
  98. package/src/interfaces/rest/router.ts +0 -334
  99. package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
  100. package/src/interfaces/rest/routes/audit.ts +0 -50
  101. package/src/interfaces/rest/routes/carts.ts +0 -89
  102. package/src/interfaces/rest/routes/catalog.ts +0 -493
  103. package/src/interfaces/rest/routes/checkout.ts +0 -283
  104. package/src/interfaces/rest/routes/inventory.ts +0 -70
  105. package/src/interfaces/rest/routes/media.ts +0 -86
  106. package/src/interfaces/rest/routes/orders.ts +0 -78
  107. package/src/interfaces/rest/routes/payments.ts +0 -60
  108. package/src/interfaces/rest/routes/pricing.ts +0 -57
  109. package/src/interfaces/rest/routes/promotions.ts +0 -92
  110. package/src/interfaces/rest/routes/search.ts +0 -71
  111. package/src/interfaces/rest/routes/webhooks.ts +0 -46
  112. package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
  113. package/src/interfaces/rest/schemas/audit.ts +0 -46
  114. package/src/interfaces/rest/schemas/carts.ts +0 -125
  115. package/src/interfaces/rest/schemas/catalog.ts +0 -450
  116. package/src/interfaces/rest/schemas/checkout.ts +0 -66
  117. package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
  118. package/src/interfaces/rest/schemas/inventory.ts +0 -138
  119. package/src/interfaces/rest/schemas/media.ts +0 -75
  120. package/src/interfaces/rest/schemas/orders.ts +0 -104
  121. package/src/interfaces/rest/schemas/pricing.ts +0 -80
  122. package/src/interfaces/rest/schemas/promotions.ts +0 -110
  123. package/src/interfaces/rest/schemas/responses.ts +0 -85
  124. package/src/interfaces/rest/schemas/search.ts +0 -58
  125. package/src/interfaces/rest/schemas/shared.ts +0 -62
  126. package/src/interfaces/rest/schemas/webhooks.ts +0 -68
  127. package/src/interfaces/rest/utils.ts +0 -104
  128. package/src/interfaces/rest/webhook-router.ts +0 -50
  129. package/src/kernel/compensation/executor.ts +0 -61
  130. package/src/kernel/compensation/types.ts +0 -26
  131. package/src/kernel/database/adapter.ts +0 -13
  132. package/src/kernel/database/drizzle-db.ts +0 -56
  133. package/src/kernel/database/migrate.ts +0 -76
  134. package/src/kernel/database/plugin-types.ts +0 -34
  135. package/src/kernel/database/schema.ts +0 -49
  136. package/src/kernel/database/scoped-db.ts +0 -68
  137. package/src/kernel/database/tx-context.ts +0 -46
  138. package/src/kernel/error-mapper.ts +0 -15
  139. package/src/kernel/errors.ts +0 -89
  140. package/src/kernel/factory/repository-factory.ts +0 -242
  141. package/src/kernel/hooks/create-context.ts +0 -43
  142. package/src/kernel/hooks/executor.ts +0 -88
  143. package/src/kernel/hooks/registry.ts +0 -74
  144. package/src/kernel/hooks/types.ts +0 -52
  145. package/src/kernel/http-error.ts +0 -44
  146. package/src/kernel/jobs/adapter.ts +0 -36
  147. package/src/kernel/jobs/drizzle-adapter.ts +0 -58
  148. package/src/kernel/jobs/runner.ts +0 -153
  149. package/src/kernel/jobs/schema.ts +0 -46
  150. package/src/kernel/jobs/types.ts +0 -30
  151. package/src/kernel/local-api.ts +0 -185
  152. package/src/kernel/plugin/manifest.ts +0 -253
  153. package/src/kernel/query/executor.ts +0 -184
  154. package/src/kernel/query/registry.ts +0 -46
  155. package/src/kernel/result.ts +0 -33
  156. package/src/kernel/schema/extra-columns.ts +0 -37
  157. package/src/kernel/service-registry.ts +0 -76
  158. package/src/kernel/service-timing.ts +0 -89
  159. package/src/kernel/state-machine/machine.ts +0 -101
  160. package/src/modules/analytics/drizzle-adapter.ts +0 -426
  161. package/src/modules/analytics/hooks.ts +0 -11
  162. package/src/modules/analytics/models.ts +0 -125
  163. package/src/modules/analytics/repository/index.ts +0 -6
  164. package/src/modules/analytics/service.ts +0 -245
  165. package/src/modules/analytics/types.ts +0 -180
  166. package/src/modules/audit/hooks.ts +0 -78
  167. package/src/modules/audit/schema.ts +0 -33
  168. package/src/modules/audit/service.ts +0 -151
  169. package/src/modules/cart/access.ts +0 -27
  170. package/src/modules/cart/matcher.ts +0 -26
  171. package/src/modules/cart/repository/index.ts +0 -234
  172. package/src/modules/cart/schema.ts +0 -42
  173. package/src/modules/cart/schemas.ts +0 -38
  174. package/src/modules/cart/service.ts +0 -541
  175. package/src/modules/catalog/repository/index.ts +0 -772
  176. package/src/modules/catalog/schema.ts +0 -203
  177. package/src/modules/catalog/schemas.ts +0 -104
  178. package/src/modules/catalog/service.ts +0 -1544
  179. package/src/modules/customers/repository/index.ts +0 -327
  180. package/src/modules/customers/schema.ts +0 -64
  181. package/src/modules/customers/service.ts +0 -171
  182. package/src/modules/fulfillment/repository/index.ts +0 -426
  183. package/src/modules/fulfillment/schema.ts +0 -101
  184. package/src/modules/fulfillment/service.ts +0 -555
  185. package/src/modules/fulfillment/types.ts +0 -59
  186. package/src/modules/inventory/repository/index.ts +0 -509
  187. package/src/modules/inventory/schema.ts +0 -94
  188. package/src/modules/inventory/schemas.ts +0 -38
  189. package/src/modules/inventory/service.ts +0 -490
  190. package/src/modules/media/adapter.ts +0 -17
  191. package/src/modules/media/repository/index.ts +0 -274
  192. package/src/modules/media/schema.ts +0 -41
  193. package/src/modules/media/service.ts +0 -151
  194. package/src/modules/orders/repository/index.ts +0 -287
  195. package/src/modules/orders/schema.ts +0 -66
  196. package/src/modules/orders/service.ts +0 -619
  197. package/src/modules/orders/stale-order-cleanup.ts +0 -76
  198. package/src/modules/organization/service.ts +0 -191
  199. package/src/modules/payments/adapter.ts +0 -47
  200. package/src/modules/payments/repository/index.ts +0 -6
  201. package/src/modules/payments/service.ts +0 -107
  202. package/src/modules/pricing/repository/index.ts +0 -291
  203. package/src/modules/pricing/schema.ts +0 -71
  204. package/src/modules/pricing/schemas.ts +0 -38
  205. package/src/modules/pricing/service.ts +0 -494
  206. package/src/modules/promotions/repository/index.ts +0 -325
  207. package/src/modules/promotions/schema.ts +0 -62
  208. package/src/modules/promotions/schemas.ts +0 -38
  209. package/src/modules/promotions/service.ts +0 -598
  210. package/src/modules/search/adapter.ts +0 -57
  211. package/src/modules/search/hooks.ts +0 -12
  212. package/src/modules/search/repository/index.ts +0 -6
  213. package/src/modules/search/service.ts +0 -315
  214. package/src/modules/shipping/calculator.ts +0 -188
  215. package/src/modules/shipping/repository/index.ts +0 -6
  216. package/src/modules/shipping/service.ts +0 -51
  217. package/src/modules/tax/adapter.ts +0 -60
  218. package/src/modules/tax/repository/index.ts +0 -6
  219. package/src/modules/tax/service.ts +0 -53
  220. package/src/modules/webhooks/hook.ts +0 -34
  221. package/src/modules/webhooks/repository/index.ts +0 -278
  222. package/src/modules/webhooks/schema.ts +0 -56
  223. package/src/modules/webhooks/service.ts +0 -117
  224. package/src/modules/webhooks/signing.ts +0 -6
  225. package/src/modules/webhooks/ssrf-guard.ts +0 -71
  226. package/src/modules/webhooks/tasks.ts +0 -52
  227. package/src/modules/webhooks/worker.ts +0 -134
  228. package/src/runtime/commerce.ts +0 -145
  229. package/src/runtime/kernel.ts +0 -419
  230. package/src/runtime/logger.ts +0 -36
  231. package/src/runtime/server.ts +0 -349
  232. package/src/runtime/shutdown.ts +0 -43
  233. package/src/test-utils/create-pglite-adapter.ts +0 -129
  234. package/src/test-utils/create-plugin-test-app.ts +0 -128
  235. package/src/test-utils/create-repository-test-harness.ts +0 -16
  236. package/src/test-utils/create-test-config.ts +0 -190
  237. package/src/test-utils/create-test-kernel.ts +0 -7
  238. package/src/test-utils/create-test-plugin-context.ts +0 -75
  239. package/src/test-utils/rest-api-test-utils.ts +0 -265
  240. package/src/test-utils/test-actors.ts +0 -62
  241. package/src/test-utils/typed-hooks.ts +0 -54
  242. package/src/types/commerce-types.ts +0 -34
  243. package/src/utils/id.ts +0 -3
  244. package/src/utils/logger.ts +0 -18
  245. package/src/utils/pagination.ts +0 -22
@@ -1,617 +0,0 @@
1
- import type { MCPResource, MCPTool } from "../../config/types.js";
2
- import type { AnalyticsQueryParams } from "../../modules/analytics/service.js";
3
- import { buildAnalyticsScope } from "../../modules/analytics/types.js";
4
- import type { Kernel } from "../../runtime/kernel.js";
5
- import { orderStateMachine } from "../../kernel/state-machine/machine.js";
6
- import {
7
- enrichEntityForAgent,
8
- enrichInventoryForAgent,
9
- enrichOrderForAgent,
10
- } from "./context-enrichment.js";
11
-
12
- function textContent(value: unknown) {
13
- return {
14
- content: [{ type: "text", text: JSON.stringify(value, null, 2) }],
15
- };
16
- }
17
-
18
- function asObject(params: unknown): Record<string, unknown> {
19
- if (!params || typeof params !== "object" || Array.isArray(params)) return {};
20
- return params as Record<string, unknown>;
21
- }
22
-
23
- function readString(
24
- params: Record<string, unknown>,
25
- key: string,
26
- ): string | undefined {
27
- const value = params[key];
28
- return typeof value === "string" ? value : undefined;
29
- }
30
-
31
- function readNumber(
32
- params: Record<string, unknown>,
33
- key: string,
34
- ): number | undefined {
35
- const value = params[key];
36
- return typeof value === "number" && Number.isFinite(value)
37
- ? value
38
- : undefined;
39
- }
40
-
41
- function readStringArray(
42
- params: Record<string, unknown>,
43
- key: string,
44
- ): string[] {
45
- const value = params[key];
46
- if (!Array.isArray(value)) return [];
47
- return value.filter((item): item is string => typeof item === "string");
48
- }
49
-
50
- function validationError(message: string): {
51
- error: { code: string; message: string };
52
- } {
53
- return {
54
- error: {
55
- code: "VALIDATION_FAILED",
56
- message,
57
- },
58
- };
59
- }
60
-
61
- export function registerMCPCapabilities(kernel: Kernel): {
62
- tools: MCPTool[];
63
- resources: MCPResource[];
64
- } {
65
- return {
66
- tools: [
67
- {
68
- name: "catalog_search",
69
- description:
70
- "Search the catalog. Filter by type, status, category, brand, price range, free-text. Returns paginated results with pricing and availability.",
71
- inputSchema: {
72
- type: "object",
73
- properties: {
74
- query: { type: "string" },
75
- type: { type: "string" },
76
- status: { type: "string" },
77
- categorySlug: { type: "string" },
78
- brandSlug: { type: "string" },
79
- page: { type: "number", default: 1 },
80
- limit: { type: "number", default: 20 },
81
- },
82
- },
83
- handler: async (params: unknown) => {
84
- const input = asObject(params);
85
- const query = (readString(input, "query") ?? "").trim().toLowerCase();
86
- const page = readNumber(input, "page") ?? 1;
87
- const limit = readNumber(input, "limit") ?? 20;
88
- const type = readString(input, "type");
89
- const status = readString(input, "status");
90
- const categorySlug = readString(input, "categorySlug");
91
- const brandSlug = readString(input, "brandSlug");
92
- if (query.length > 0) {
93
- const searchResult = await kernel.services.search.query({
94
- query,
95
- page,
96
- limit,
97
- filters: {
98
- ...(type ? { type } : {}),
99
- ...(status ? { status } : {}),
100
- ...(categorySlug ? { category: categorySlug } : {}),
101
- ...(brandSlug ? { brand: brandSlug } : {}),
102
- },
103
- });
104
-
105
- if (!searchResult.ok) return { error: searchResult.error };
106
-
107
- const enrichedItems = await Promise.all(
108
- searchResult.value.hits.map(async (hit) => {
109
- const entityResult = await kernel.services.catalog.getById(
110
- hit.id,
111
- );
112
- if (entityResult.ok) {
113
- return enrichEntityForAgent(entityResult.value, kernel);
114
- }
115
- return hit.document;
116
- }),
117
- );
118
-
119
- return textContent({
120
- items: await Promise.all(enrichedItems),
121
- pagination: {
122
- page: searchResult.value.page,
123
- limit: searchResult.value.limit,
124
- total: searchResult.value.total,
125
- totalPages: Math.max(
126
- 1,
127
- Math.ceil(
128
- searchResult.value.total / searchResult.value.limit,
129
- ),
130
- ),
131
- },
132
- facets: searchResult.value.facets,
133
- });
134
- }
135
-
136
- const result = await kernel.services.catalog.list({
137
- filter: {
138
- ...(type ? { type } : {}),
139
- ...(status ? { status } : {}),
140
- ...(categorySlug ? { category: categorySlug } : {}),
141
- ...(brandSlug ? { brand: brandSlug } : {}),
142
- },
143
- pagination: {
144
- page,
145
- limit,
146
- },
147
- });
148
- if (!result.ok) return { error: result.error };
149
-
150
- return textContent({
151
- ...result.value,
152
- items: await Promise.all(
153
- result.value.items.map((item) =>
154
- enrichEntityForAgent(item, kernel),
155
- ),
156
- ),
157
- });
158
- },
159
- },
160
- {
161
- name: "catalog_create_entity",
162
- description:
163
- "Create a new catalog entity. Requires type, slug, title. Created in draft status.",
164
- inputSchema: {
165
- type: "object",
166
- required: ["type", "slug", "title"],
167
- properties: {
168
- type: { type: "string" },
169
- slug: { type: "string" },
170
- title: { type: "string" },
171
- description: { type: "string" },
172
- metadata: { type: "object" },
173
- },
174
- },
175
- handler: async (params: unknown) => {
176
- const input = asObject(params);
177
- const type = readString(input, "type");
178
- const slug = readString(input, "slug");
179
- const title = readString(input, "title");
180
- const description = readString(input, "description");
181
- if (!type || !slug || !title) {
182
- return validationError("type, slug, and title are required.");
183
- }
184
- const result = await kernel.services.catalog.create(
185
- {
186
- type,
187
- slug,
188
- attributes: {
189
- locale: "en",
190
- title,
191
- ...(description !== undefined ? { description } : {}),
192
- },
193
- ...(input.metadata &&
194
- typeof input.metadata === "object" &&
195
- !Array.isArray(input.metadata)
196
- ? { metadata: input.metadata as Record<string, unknown> }
197
- : {}),
198
- },
199
- kernel.getMCPActor(),
200
- );
201
-
202
- if (!result.ok) return { error: result.error };
203
- return textContent(await enrichEntityForAgent(result.value, kernel));
204
- },
205
- },
206
- {
207
- name: "inventory_check",
208
- description: "Check stock levels for one or more entities/variants.",
209
- inputSchema: {
210
- type: "object",
211
- required: ["entityIds"],
212
- properties: {
213
- entityIds: {
214
- type: "array",
215
- items: { type: "string" },
216
- },
217
- },
218
- },
219
- handler: async (params: unknown) => {
220
- const input = asObject(params);
221
- const entityIds = readStringArray(input, "entityIds");
222
- if (entityIds.length === 0) {
223
- return validationError("entityIds is required.");
224
- }
225
- const result =
226
- await kernel.services.inventory.checkMultiple(entityIds);
227
- if (!result.ok) return { error: result.error };
228
-
229
- const enriched = await Promise.all(
230
- Object.entries(result.value).map(async ([entityId, available]) => {
231
- const levelsResult =
232
- await kernel.services.inventory.getLevelsByEntityId(entityId);
233
- const levels = levelsResult.ok ? levelsResult.value : [];
234
- const reorderThreshold = levels[0]?.reorderThreshold;
235
- const reorderQuantity = levels[0]?.reorderQuantity;
236
-
237
- return enrichInventoryForAgent({
238
- entityId,
239
- available,
240
- ...(reorderThreshold !== undefined ? { reorderThreshold } : {}),
241
- ...(reorderQuantity !== undefined ? { reorderQuantity } : {}),
242
- });
243
- }),
244
- );
245
-
246
- return textContent(enriched);
247
- },
248
- },
249
- {
250
- name: "inventory_adjust",
251
- description:
252
- "Adjust inventory. Positive adds stock, negative removes. Requires reason.",
253
- inputSchema: {
254
- type: "object",
255
- required: ["entityId", "adjustment", "reason"],
256
- properties: {
257
- entityId: { type: "string" },
258
- variantId: { type: "string" },
259
- adjustment: { type: "number" },
260
- reason: { type: "string" },
261
- },
262
- },
263
- handler: async (params: unknown) => {
264
- const input = asObject(params);
265
- const entityId = readString(input, "entityId");
266
- const adjustment = readNumber(input, "adjustment");
267
- const reason = readString(input, "reason");
268
- const variantId = readString(input, "variantId");
269
- if (!entityId || adjustment === undefined || !reason) {
270
- return validationError(
271
- "entityId, adjustment, and reason are required.",
272
- );
273
- }
274
-
275
- const result = await kernel.services.inventory.adjust(
276
- {
277
- entityId,
278
- adjustment,
279
- reason,
280
- ...(variantId !== undefined ? { variantId } : {}),
281
- },
282
- kernel.getMCPActor(),
283
- );
284
- if (!result.ok) return { error: result.error };
285
-
286
- const availableResult = await kernel.services.inventory.getAvailable(
287
- entityId,
288
- variantId,
289
- );
290
-
291
- return textContent(
292
- enrichInventoryForAgent({
293
- entityId,
294
- available: availableResult.ok ? availableResult.value : 0,
295
- ...(result.value.reorderThreshold !== undefined
296
- ? { reorderThreshold: result.value.reorderThreshold }
297
- : {}),
298
- ...(result.value.reorderQuantity !== undefined
299
- ? { reorderQuantity: result.value.reorderQuantity }
300
- : {}),
301
- }),
302
- );
303
- },
304
- },
305
- {
306
- name: "cart_create",
307
- description:
308
- "Create a new shopping cart. Returns cart ID for subsequent operations.",
309
- inputSchema: {
310
- type: "object",
311
- properties: {
312
- customerId: { type: "string" },
313
- currency: { type: "string", default: "USD" },
314
- },
315
- },
316
- handler: async (params: unknown) => {
317
- const input = asObject(params);
318
- const customerId = readString(input, "customerId");
319
- const currency = readString(input, "currency");
320
- const result = await kernel.services.cart.create(
321
- {
322
- ...(customerId !== undefined ? { customerId } : {}),
323
- ...(currency !== undefined ? { currency } : {}),
324
- },
325
- kernel.getMCPActor(),
326
- );
327
- if (!result.ok) return { error: result.error };
328
- return textContent(result.value);
329
- },
330
- },
331
- {
332
- name: "cart_add_item",
333
- description:
334
- "Add item to cart. If entity has variants and no variantId provided, fails with available variants.",
335
- inputSchema: {
336
- type: "object",
337
- required: ["cartId", "entityId"],
338
- properties: {
339
- cartId: { type: "string" },
340
- entityId: { type: "string" },
341
- variantId: { type: "string" },
342
- quantity: { type: "number", default: 1 },
343
- },
344
- },
345
- handler: async (params: unknown) => {
346
- const input = asObject(params);
347
- const cartId = readString(input, "cartId");
348
- const entityId = readString(input, "entityId");
349
- const variantId = readString(input, "variantId");
350
- const quantity = readNumber(input, "quantity");
351
- if (!cartId || !entityId) {
352
- return validationError("cartId and entityId are required.");
353
- }
354
- const result = await kernel.services.cart.addItem(
355
- {
356
- cartId,
357
- entityId,
358
- ...(variantId !== undefined ? { variantId } : {}),
359
- quantity: quantity ?? 1,
360
- },
361
- kernel.getMCPActor(),
362
- );
363
- if (!result.ok) return { error: result.error };
364
- return textContent(result.value);
365
- },
366
- },
367
- {
368
- name: "order_get",
369
- description:
370
- "Get order details by ID or order number. Includes line items, payment, fulfillment, and AI context.",
371
- inputSchema: {
372
- type: "object",
373
- properties: {
374
- orderId: { type: "string" },
375
- orderNumber: { type: "string" },
376
- },
377
- },
378
- handler: async (params: unknown) => {
379
- const input = asObject(params);
380
- const orderId = readString(input, "orderId");
381
- const orderNumber = readString(input, "orderNumber");
382
- if (!orderId && !orderNumber) {
383
- return validationError(
384
- "Either orderId or orderNumber is required.",
385
- );
386
- }
387
-
388
- const result = orderId
389
- ? await kernel.services.orders.getById(
390
- orderId,
391
- kernel.getMCPActor(),
392
- )
393
- : await kernel.services.orders.getByNumber(
394
- orderNumber ?? "",
395
- kernel.getMCPActor(),
396
- );
397
-
398
- if (!result.ok) return { error: result.error };
399
- const enriched = enrichOrderForAgent(
400
- result.value,
401
- kernel.getMCPActor().permissions,
402
- );
403
- return textContent(enriched);
404
- },
405
- },
406
- {
407
- name: "order_list",
408
- description: "List orders with pagination and optional status filter.",
409
- inputSchema: {
410
- type: "object",
411
- properties: {
412
- page: { type: "number", default: 1 },
413
- limit: { type: "number", default: 20 },
414
- status: { type: "string" },
415
- },
416
- },
417
- handler: async (params: unknown) => {
418
- const input = asObject(params);
419
- const page = readNumber(input, "page");
420
- const limit = readNumber(input, "limit");
421
- const status = readString(input, "status");
422
- const result = await kernel.services.orders.list(
423
- {
424
- ...(page !== undefined ? { page } : {}),
425
- ...(limit !== undefined ? { limit } : {}),
426
- ...(status !== undefined ? { status } : {}),
427
- },
428
- kernel.getMCPActor(),
429
- );
430
-
431
- if (!result.ok) return { error: result.error };
432
- return textContent({
433
- ...result.value,
434
- items: result.value.items.map((order) =>
435
- enrichOrderForAgent(order, kernel.getMCPActor().permissions),
436
- ),
437
- });
438
- },
439
- },
440
- {
441
- name: "analytics_query",
442
- description:
443
- "Query analytics using semantic measures, dimensions, time dimensions, and filters.",
444
- inputSchema: {
445
- type: "object",
446
- required: ["measures"],
447
- properties: {
448
- measures: {
449
- type: "array",
450
- items: { type: "string" },
451
- },
452
- dimensions: {
453
- type: "array",
454
- items: { type: "string" },
455
- },
456
- timeDimensions: {
457
- type: "array",
458
- items: {
459
- type: "object",
460
- properties: {
461
- dimension: { type: "string" },
462
- granularity: {
463
- type: "string",
464
- enum: ["day", "week", "month", "year"],
465
- },
466
- dateRange: {
467
- oneOf: [
468
- { type: "string" },
469
- {
470
- type: "array",
471
- items: { type: "string" },
472
- minItems: 2,
473
- maxItems: 2,
474
- },
475
- ],
476
- },
477
- },
478
- required: ["dimension"],
479
- },
480
- },
481
- filters: {
482
- type: "array",
483
- items: {
484
- type: "object",
485
- properties: {
486
- member: { type: "string" },
487
- operator: {
488
- type: "string",
489
- enum: [
490
- "equals",
491
- "notEquals",
492
- "contains",
493
- "in",
494
- "notIn",
495
- "gt",
496
- "gte",
497
- "lt",
498
- "lte",
499
- "beforeDate",
500
- "afterDate",
501
- "inDateRange",
502
- ],
503
- },
504
- values: {
505
- type: "array",
506
- items: { type: "string" },
507
- },
508
- },
509
- required: ["member", "operator"],
510
- },
511
- },
512
- order: {
513
- type: "object",
514
- additionalProperties: {
515
- type: "string",
516
- enum: ["asc", "desc"],
517
- },
518
- },
519
- limit: { type: "number", default: 100 },
520
- },
521
- },
522
- handler: async (params: unknown) => {
523
- const input = asObject(params);
524
- const measures = readStringArray(input, "measures");
525
- const limit = readNumber(input, "limit");
526
- if (measures.length === 0) {
527
- return validationError("measures is required.");
528
- }
529
-
530
- const queryParams: AnalyticsQueryParams = {
531
- measures,
532
- ...(Array.isArray(input.dimensions)
533
- ? {
534
- dimensions: input.dimensions.filter(
535
- (item): item is string => typeof item === "string",
536
- ),
537
- }
538
- : {}),
539
- ...(Array.isArray(input.timeDimensions)
540
- ? {
541
- timeDimensions: input.timeDimensions as NonNullable<
542
- AnalyticsQueryParams["timeDimensions"]
543
- >,
544
- }
545
- : {}),
546
- ...(Array.isArray(input.filters)
547
- ? {
548
- filters: input.filters as NonNullable<
549
- AnalyticsQueryParams["filters"]
550
- >,
551
- }
552
- : {}),
553
- ...(input.order &&
554
- typeof input.order === "object" &&
555
- !Array.isArray(input.order)
556
- ? {
557
- order: input.order as NonNullable<
558
- AnalyticsQueryParams["order"]
559
- >,
560
- }
561
- : {}),
562
- ...(limit !== undefined ? { limit } : {}),
563
- };
564
-
565
- // Scope is always derived from the actor — never manually constructed.
566
- const analyticsScope = buildAnalyticsScope(kernel.getMCPActor());
567
-
568
- const result = await kernel.services.analytics.query(queryParams, analyticsScope);
569
- if (!result.ok) return { error: result.error };
570
- return textContent(result.value);
571
- },
572
- },
573
- {
574
- name: "analytics_meta",
575
- description:
576
- "List available analytics measures, dimensions, segments, and models.",
577
- inputSchema: {
578
- type: "object",
579
- },
580
- handler: async () => {
581
- const metaScope = buildAnalyticsScope(kernel.getMCPActor());
582
- const result = await kernel.services.analytics.getMeta();
583
- if (!result.ok) return { error: result.error };
584
- return textContent(result.value);
585
- },
586
- },
587
- ],
588
- resources: [
589
- {
590
- uri: "commerce://schema/entity-types",
591
- name: "Entity Type Schema",
592
- description:
593
- "Complete schema of all entity types, fields, variants, fulfillment strategies.",
594
- mimeType: "application/json",
595
- handler: async () => ({
596
- content: [
597
- {
598
- type: "text",
599
- text: JSON.stringify(kernel.config.entities ?? {}, null, 2),
600
- },
601
- ],
602
- }),
603
- },
604
- {
605
- uri: "commerce://schema/order-states",
606
- name: "Order State Machine",
607
- description: "All order states and valid transitions.",
608
- mimeType: "application/json",
609
- handler: async () => ({
610
- content: [
611
- { type: "text", text: JSON.stringify(orderStateMachine, null, 2) },
612
- ],
613
- }),
614
- },
615
- ],
616
- };
617
- }
@@ -1,68 +0,0 @@
1
- import { Hono } from "hono";
2
- import { streamSSE, type SSEStreamingApi } from "hono/streaming";
3
- import type { MCPTool } from "../../config/types.js";
4
- import type { Kernel } from "../../runtime/kernel.js";
5
- import { registerMCPCapabilities } from "./server.js";
6
-
7
- async function handleMCPSession(
8
- stream: SSEStreamingApi,
9
- capabilities: ReturnType<typeof registerMCPCapabilities>,
10
- ): Promise<void> {
11
- await stream.writeSSE({
12
- event: "ready",
13
- data: JSON.stringify({
14
- tools: capabilities.tools.map((tool) => ({ name: tool.name, description: tool.description })),
15
- resources: capabilities.resources.map((resource) => ({ uri: resource.uri, name: resource.name })),
16
- }),
17
- });
18
- }
19
-
20
- export function createMCPHandler(kernel: Kernel, customTools?: (kernel: Kernel) => MCPTool[]) {
21
- const router = new Hono();
22
- const capabilities = registerMCPCapabilities(kernel);
23
-
24
- if (customTools) {
25
- capabilities.tools.push(...customTools(kernel));
26
- }
27
-
28
- if (kernel.mcpTools.length > 0) {
29
- capabilities.tools.push(...kernel.mcpTools);
30
- }
31
-
32
- if (kernel.mcpResources.length > 0) {
33
- capabilities.resources.push(...kernel.mcpResources);
34
- }
35
-
36
- router.get("/sse", async (c) =>
37
- streamSSE(c, async (stream) => {
38
- await handleMCPSession(stream, capabilities);
39
- }),
40
- );
41
-
42
- router.post("/tools/:toolName", async (c) => {
43
- const tool = capabilities.tools.find((item) => item.name === c.req.param("toolName"));
44
- if (!tool) return c.json({ error: "Tool not found" }, 404);
45
-
46
- return c.json(await tool.handler(await c.req.json<unknown>()));
47
- });
48
-
49
- router.get("/resources", async (c) => {
50
- return c.json({
51
- data: capabilities.resources.map((resource) => ({
52
- uri: resource.uri,
53
- name: resource.name,
54
- description: resource.description,
55
- mimeType: resource.mimeType,
56
- })),
57
- });
58
- });
59
-
60
- router.get("/resources/:resourceId", async (c) => {
61
- const uri = decodeURIComponent(c.req.param("resourceId"));
62
- const resource = capabilities.resources.find((item) => item.uri === uri);
63
- if (!resource) return c.json({ error: "Resource not found" }, 404);
64
- return c.json(await resource.handler());
65
- });
66
-
67
- return router;
68
- }