@pikku/core 0.11.2 → 0.12.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 (344) hide show
  1. package/CHANGELOG.md +33 -2
  2. package/dist/crypto-utils.d.ts +2 -0
  3. package/dist/crypto-utils.js +78 -0
  4. package/dist/errors/errors.d.ts +8 -0
  5. package/dist/errors/errors.js +16 -0
  6. package/dist/errors/index.d.ts +0 -1
  7. package/dist/errors/index.js +0 -1
  8. package/dist/function/function-runner.d.ts +7 -1
  9. package/dist/function/function-runner.js +82 -23
  10. package/dist/function/functions.types.d.ts +31 -87
  11. package/dist/function/functions.types.js +16 -46
  12. package/dist/function/index.d.ts +2 -2
  13. package/dist/function/index.js +1 -2
  14. package/dist/handle-error.js +1 -10
  15. package/dist/index.d.ts +28 -26
  16. package/dist/index.js +18 -29
  17. package/dist/middleware/auth-apikey.d.ts +2 -2
  18. package/dist/middleware/auth-apikey.js +3 -3
  19. package/dist/middleware/auth-bearer.d.ts +2 -2
  20. package/dist/middleware/auth-bearer.js +3 -6
  21. package/dist/middleware/auth-cookie.d.ts +2 -2
  22. package/dist/middleware/auth-cookie.js +6 -8
  23. package/dist/middleware/cors.d.ts +46 -0
  24. package/dist/middleware/cors.js +73 -0
  25. package/dist/middleware/index.d.ts +5 -3
  26. package/dist/middleware/index.js +5 -3
  27. package/dist/middleware/remote-auth.d.ts +5 -0
  28. package/dist/middleware/remote-auth.js +55 -0
  29. package/dist/middleware/timeout.d.ts +2 -2
  30. package/dist/middleware-runner.js +8 -10
  31. package/dist/permissions.js +11 -15
  32. package/dist/pikku-state.d.ts +1 -0
  33. package/dist/pikku-state.js +18 -8
  34. package/dist/schema.js +12 -11
  35. package/dist/services/ai-agent-runner-service.d.ts +24 -0
  36. package/dist/services/ai-agent-runner-service.js +1 -0
  37. package/dist/services/ai-run-state-service.d.ts +13 -0
  38. package/dist/services/ai-run-state-service.js +1 -0
  39. package/dist/services/ai-storage-service.d.ts +18 -0
  40. package/dist/services/ai-storage-service.js +1 -0
  41. package/dist/services/deployment-service.d.ts +19 -0
  42. package/dist/services/deployment-service.js +1 -0
  43. package/dist/services/gopass-secrets.d.ts +47 -0
  44. package/dist/services/gopass-secrets.js +94 -0
  45. package/dist/services/in-memory-trigger-service.d.ts +23 -0
  46. package/dist/services/in-memory-trigger-service.js +50 -0
  47. package/dist/services/in-memory-workflow-service.d.ts +59 -0
  48. package/dist/services/in-memory-workflow-service.js +287 -0
  49. package/dist/services/index.d.ts +25 -11
  50. package/dist/services/index.js +10 -13
  51. package/dist/services/local-secrets.d.ts +23 -0
  52. package/dist/services/local-secrets.js +45 -0
  53. package/dist/services/local-variables.d.ts +5 -0
  54. package/dist/services/local-variables.js +18 -0
  55. package/dist/services/scheduler-service.d.ts +21 -1
  56. package/dist/services/scheduler-service.js +14 -0
  57. package/dist/services/schema-service.d.ts +6 -0
  58. package/dist/services/scoped-secret-service.d.ts +16 -0
  59. package/dist/services/scoped-secret-service.js +35 -0
  60. package/dist/services/secret-service.d.ts +25 -4
  61. package/dist/services/trigger-service.d.ts +23 -0
  62. package/dist/services/trigger-service.js +1 -0
  63. package/dist/services/typed-secret-service.d.ts +30 -0
  64. package/dist/services/typed-secret-service.js +40 -0
  65. package/dist/services/typed-variables-service.d.ts +27 -0
  66. package/dist/services/typed-variables-service.js +46 -0
  67. package/dist/services/user-session-service.d.ts +13 -0
  68. package/dist/services/user-session-service.js +17 -0
  69. package/dist/services/variables-service.d.ts +5 -0
  70. package/dist/services/workflow-service.d.ts +9 -4
  71. package/dist/types/core.types.d.ts +48 -12
  72. package/dist/types/core.types.js +8 -1
  73. package/dist/types/state.types.d.ts +43 -51
  74. package/dist/utils.d.ts +3 -2
  75. package/dist/utils.js +15 -3
  76. package/dist/version.d.ts +6 -0
  77. package/dist/version.js +19 -0
  78. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +29 -0
  79. package/dist/wirings/ai-agent/ai-agent-memory.js +199 -0
  80. package/dist/wirings/ai-agent/ai-agent-model-config.d.ts +9 -0
  81. package/dist/wirings/ai-agent/ai-agent-model-config.js +36 -0
  82. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +60 -0
  83. package/dist/wirings/ai-agent/ai-agent-prepare.js +312 -0
  84. package/dist/wirings/ai-agent/ai-agent-registry.d.ts +15 -0
  85. package/dist/wirings/ai-agent/ai-agent-registry.js +47 -0
  86. package/dist/wirings/ai-agent/ai-agent-runner.d.ts +3 -0
  87. package/dist/wirings/ai-agent/ai-agent-runner.js +96 -0
  88. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +11 -0
  89. package/dist/wirings/ai-agent/ai-agent-stream.js +503 -0
  90. package/dist/wirings/ai-agent/ai-agent.types.d.ts +254 -0
  91. package/dist/wirings/ai-agent/ai-agent.types.js +1 -0
  92. package/dist/wirings/ai-agent/index.d.ts +5 -0
  93. package/dist/wirings/ai-agent/index.js +4 -0
  94. package/dist/wirings/channel/channel-common.d.ts +7 -4
  95. package/dist/wirings/channel/channel-common.js +15 -8
  96. package/dist/wirings/channel/channel-handler.js +12 -3
  97. package/dist/wirings/channel/channel-middleware-runner.d.ts +9 -0
  98. package/dist/wirings/channel/channel-middleware-runner.js +73 -0
  99. package/dist/wirings/channel/channel-runner.js +7 -7
  100. package/dist/wirings/channel/channel.types.d.ts +5 -1
  101. package/dist/wirings/channel/define-channel-routes.d.ts +21 -0
  102. package/dist/wirings/channel/define-channel-routes.js +23 -0
  103. package/dist/wirings/channel/index.d.ts +10 -8
  104. package/dist/wirings/channel/index.js +7 -8
  105. package/dist/wirings/channel/local/index.d.ts +3 -3
  106. package/dist/wirings/channel/local/index.js +3 -3
  107. package/dist/wirings/channel/local/local-channel-runner.js +18 -6
  108. package/dist/wirings/channel/serverless/index.d.ts +1 -1
  109. package/dist/wirings/channel/serverless/index.js +1 -1
  110. package/dist/wirings/channel/serverless/serverless-channel-runner.js +15 -4
  111. package/dist/wirings/cli/channel/index.d.ts +1 -1
  112. package/dist/wirings/cli/channel/index.js +1 -1
  113. package/dist/wirings/cli/cli-runner.d.ts +1 -1
  114. package/dist/wirings/cli/cli-runner.js +12 -8
  115. package/dist/wirings/cli/cli.types.d.ts +6 -6
  116. package/dist/wirings/cli/define-cli-commands.d.ts +19 -0
  117. package/dist/wirings/cli/define-cli-commands.js +20 -0
  118. package/dist/wirings/cli/index.d.ts +2 -3
  119. package/dist/wirings/cli/index.js +1 -3
  120. package/dist/wirings/http/http-routes.d.ts +69 -0
  121. package/dist/wirings/http/http-routes.js +139 -0
  122. package/dist/wirings/http/http-runner.js +49 -23
  123. package/dist/wirings/http/http.types.d.ts +78 -38
  124. package/dist/wirings/http/index.d.ts +5 -4
  125. package/dist/wirings/http/index.js +4 -3
  126. package/dist/wirings/mcp/index.d.ts +4 -3
  127. package/dist/wirings/mcp/index.js +3 -3
  128. package/dist/wirings/mcp/mcp-runner.d.ts +1 -3
  129. package/dist/wirings/mcp/mcp-runner.js +25 -39
  130. package/dist/wirings/mcp/mcp.types.d.ts +3 -3
  131. package/dist/wirings/node/index.d.ts +2 -0
  132. package/dist/wirings/node/index.js +1 -0
  133. package/dist/wirings/node/node.types.d.ts +20 -0
  134. package/dist/wirings/node/node.types.js +1 -0
  135. package/dist/wirings/oauth2/index.d.ts +3 -0
  136. package/dist/wirings/oauth2/index.js +2 -0
  137. package/dist/wirings/oauth2/oauth2-client.d.ts +47 -0
  138. package/dist/wirings/oauth2/oauth2-client.js +267 -0
  139. package/dist/wirings/oauth2/oauth2.types.d.ts +65 -0
  140. package/dist/wirings/oauth2/oauth2.types.js +4 -0
  141. package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +20 -0
  142. package/dist/wirings/oauth2/wire-oauth2-credential.js +21 -0
  143. package/dist/wirings/queue/index.d.ts +2 -2
  144. package/dist/wirings/queue/index.js +1 -1
  145. package/dist/wirings/queue/queue-runner.d.ts +7 -0
  146. package/dist/wirings/queue/queue-runner.js +17 -6
  147. package/dist/wirings/queue/queue.types.d.ts +8 -2
  148. package/dist/wirings/rpc/index.d.ts +1 -1
  149. package/dist/wirings/rpc/index.js +1 -1
  150. package/dist/wirings/rpc/rpc-runner.d.ts +33 -5
  151. package/dist/wirings/rpc/rpc-runner.js +134 -16
  152. package/dist/wirings/rpc/rpc-types.d.ts +8 -4
  153. package/dist/wirings/scheduler/index.d.ts +3 -3
  154. package/dist/wirings/scheduler/index.js +2 -3
  155. package/dist/wirings/scheduler/scheduler-runner.d.ts +5 -0
  156. package/dist/wirings/scheduler/scheduler-runner.js +23 -5
  157. package/dist/wirings/secret/index.d.ts +3 -0
  158. package/dist/wirings/secret/index.js +2 -0
  159. package/dist/wirings/secret/secret.types.d.ts +27 -0
  160. package/dist/wirings/secret/secret.types.js +1 -0
  161. package/dist/wirings/secret/validate-secret-definitions.d.ts +6 -0
  162. package/dist/wirings/secret/validate-secret-definitions.js +55 -0
  163. package/dist/wirings/trigger/index.d.ts +3 -2
  164. package/dist/wirings/trigger/index.js +2 -2
  165. package/dist/wirings/trigger/pikku-trigger-service.d.ts +26 -0
  166. package/dist/wirings/trigger/pikku-trigger-service.js +75 -0
  167. package/dist/wirings/trigger/trigger-runner.d.ts +21 -8
  168. package/dist/wirings/trigger/trigger-runner.js +46 -18
  169. package/dist/wirings/trigger/trigger.types.d.ts +99 -11
  170. package/dist/wirings/trigger/trigger.types.js +40 -1
  171. package/dist/wirings/variable/index.d.ts +3 -0
  172. package/dist/wirings/variable/index.js +2 -0
  173. package/dist/wirings/variable/validate-variable-definitions.d.ts +6 -0
  174. package/dist/wirings/variable/validate-variable-definitions.js +45 -0
  175. package/dist/wirings/variable/variable.types.d.ts +18 -0
  176. package/dist/wirings/variable/variable.types.js +1 -0
  177. package/dist/wirings/workflow/dsl/index.d.ts +1 -1
  178. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +7 -1
  179. package/dist/wirings/workflow/dsl/workflow-runner.js +3 -2
  180. package/dist/wirings/workflow/graph/graph-node.d.ts +1 -6
  181. package/dist/wirings/workflow/graph/graph-runner.d.ts +5 -31
  182. package/dist/wirings/workflow/graph/graph-runner.js +384 -296
  183. package/dist/wirings/workflow/graph/index.d.ts +4 -3
  184. package/dist/wirings/workflow/graph/index.js +3 -3
  185. package/dist/wirings/workflow/graph/template.d.ts +15 -0
  186. package/dist/wirings/workflow/graph/template.js +27 -0
  187. package/dist/wirings/workflow/graph/wire-workflow-graph.d.ts +15 -0
  188. package/dist/wirings/workflow/graph/wire-workflow-graph.js +8 -0
  189. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +0 -13
  190. package/dist/wirings/workflow/index.d.ts +6 -6
  191. package/dist/wirings/workflow/index.js +6 -4
  192. package/dist/wirings/workflow/pikku-workflow-service.d.ts +28 -14
  193. package/dist/wirings/workflow/pikku-workflow-service.js +200 -56
  194. package/dist/wirings/workflow/workflow-helpers.d.ts +36 -0
  195. package/dist/wirings/workflow/workflow-helpers.js +38 -0
  196. package/dist/wirings/workflow/workflow-utils.d.ts +1 -23
  197. package/dist/wirings/workflow/workflow-utils.js +1 -66
  198. package/dist/wirings/workflow/workflow.types.d.ts +40 -93
  199. package/package.json +13 -15
  200. package/run-tests.sh +4 -1
  201. package/src/crypto-utils.ts +99 -0
  202. package/src/errors/errors.ts +17 -0
  203. package/src/errors/index.ts +0 -1
  204. package/src/factory-functions.test.ts +42 -2
  205. package/src/function/function-runner.test.ts +4 -11
  206. package/src/function/function-runner.ts +114 -29
  207. package/src/function/functions.types.ts +62 -132
  208. package/src/function/index.ts +5 -2
  209. package/src/handle-error.ts +2 -10
  210. package/src/index.ts +99 -25
  211. package/src/middleware/auth-apikey.test.ts +15 -12
  212. package/src/middleware/auth-apikey.ts +3 -3
  213. package/src/middleware/auth-bearer.test.ts +17 -14
  214. package/src/middleware/auth-bearer.ts +25 -27
  215. package/src/middleware/auth-cookie.test.ts +16 -13
  216. package/src/middleware/auth-cookie.ts +6 -8
  217. package/src/middleware/cors.ts +96 -0
  218. package/src/middleware/index.ts +5 -3
  219. package/src/middleware/remote-auth.ts +68 -0
  220. package/src/middleware-runner.test.ts +122 -0
  221. package/src/middleware-runner.ts +12 -12
  222. package/src/permissions.test.ts +81 -7
  223. package/src/permissions.ts +15 -17
  224. package/src/pikku-state.ts +20 -8
  225. package/src/run-tests-script.test.ts +49 -0
  226. package/src/schema.ts +18 -15
  227. package/src/services/ai-agent-runner-service.ts +29 -0
  228. package/src/services/ai-run-state-service.ts +20 -0
  229. package/src/services/ai-storage-service.ts +39 -0
  230. package/src/services/deployment-service.ts +22 -0
  231. package/src/services/gopass-secrets.ts +95 -0
  232. package/src/services/in-memory-trigger-service.ts +68 -0
  233. package/src/services/in-memory-workflow-service.ts +395 -0
  234. package/src/services/index.ts +49 -14
  235. package/src/services/local-secrets.ts +51 -0
  236. package/src/services/local-variables.ts +22 -0
  237. package/src/services/scheduler-service.ts +29 -1
  238. package/src/services/schema-service.ts +7 -0
  239. package/src/services/scoped-secret-service.ts +41 -0
  240. package/src/services/secret-service.ts +25 -4
  241. package/src/services/trigger-service.ts +38 -0
  242. package/src/services/typed-secret-service.ts +70 -0
  243. package/src/services/typed-variables-service.ts +81 -0
  244. package/src/services/user-session-service.ts +23 -0
  245. package/src/services/variables-service.ts +5 -0
  246. package/src/services/workflow-service.ts +22 -2
  247. package/src/types/core.types.ts +76 -11
  248. package/src/types/state.types.ts +37 -63
  249. package/src/utils.ts +20 -6
  250. package/src/version.test.ts +80 -0
  251. package/src/version.ts +25 -0
  252. package/src/wirings/ai-agent/ai-agent-memory.ts +300 -0
  253. package/src/wirings/ai-agent/ai-agent-model-config.ts +43 -0
  254. package/src/wirings/ai-agent/ai-agent-prepare.ts +470 -0
  255. package/src/wirings/ai-agent/ai-agent-registry.test.ts +37 -0
  256. package/src/wirings/ai-agent/ai-agent-registry.ts +82 -0
  257. package/src/wirings/ai-agent/ai-agent-runner.test.ts +75 -0
  258. package/src/wirings/ai-agent/ai-agent-runner.ts +137 -0
  259. package/src/wirings/ai-agent/ai-agent-stream.test.ts +246 -0
  260. package/src/wirings/ai-agent/ai-agent-stream.ts +774 -0
  261. package/src/wirings/ai-agent/ai-agent.types.ts +273 -0
  262. package/src/wirings/ai-agent/index.ts +28 -0
  263. package/src/wirings/channel/channel-common.ts +32 -19
  264. package/src/wirings/channel/channel-handler.ts +17 -4
  265. package/src/wirings/channel/channel-middleware-runner.ts +119 -0
  266. package/src/wirings/channel/channel-runner.ts +9 -10
  267. package/src/wirings/channel/channel.types.ts +17 -1
  268. package/src/wirings/channel/define-channel-routes.ts +25 -0
  269. package/src/wirings/channel/index.ts +23 -8
  270. package/src/wirings/channel/local/index.ts +3 -3
  271. package/src/wirings/channel/local/local-channel-runner.test.ts +44 -1
  272. package/src/wirings/channel/local/local-channel-runner.ts +22 -6
  273. package/src/wirings/channel/serverless/index.ts +5 -1
  274. package/src/wirings/channel/serverless/serverless-channel-runner.ts +20 -4
  275. package/src/wirings/cli/channel/index.ts +1 -1
  276. package/src/wirings/cli/cli-runner.test.ts +9 -9
  277. package/src/wirings/cli/cli-runner.ts +17 -10
  278. package/src/wirings/cli/cli.types.ts +11 -11
  279. package/src/wirings/cli/command-parser.test.ts +8 -8
  280. package/src/wirings/cli/define-cli-commands.ts +24 -0
  281. package/src/wirings/cli/index.ts +10 -4
  282. package/src/wirings/http/http-routes.test.ts +322 -0
  283. package/src/wirings/http/http-routes.ts +198 -0
  284. package/src/wirings/http/http-runner.test.ts +3 -3
  285. package/src/wirings/http/http-runner.ts +63 -29
  286. package/src/wirings/http/http.types.ts +121 -37
  287. package/src/wirings/http/index.ts +16 -4
  288. package/src/wirings/mcp/index.ts +27 -3
  289. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +3 -3
  290. package/src/wirings/mcp/mcp-runner.ts +39 -50
  291. package/src/wirings/mcp/mcp.types.ts +3 -3
  292. package/src/wirings/node/index.ts +2 -0
  293. package/src/wirings/node/node.types.ts +23 -0
  294. package/src/wirings/oauth2/index.ts +3 -0
  295. package/src/wirings/oauth2/oauth2-client.test.ts +929 -0
  296. package/src/wirings/oauth2/oauth2-client.ts +335 -0
  297. package/src/wirings/oauth2/oauth2.types.ts +69 -0
  298. package/src/wirings/oauth2/wire-oauth2-credential.ts +23 -0
  299. package/src/wirings/queue/index.ts +14 -2
  300. package/src/wirings/queue/queue-runner.test.ts +97 -55
  301. package/src/wirings/queue/queue-runner.ts +33 -7
  302. package/src/wirings/queue/queue.types.ts +12 -2
  303. package/src/wirings/rpc/index.ts +1 -1
  304. package/src/wirings/rpc/rpc-runner.ts +187 -17
  305. package/src/wirings/rpc/rpc-types.ts +13 -4
  306. package/src/wirings/scheduler/index.ts +7 -4
  307. package/src/wirings/scheduler/scheduler-runner.test.ts +70 -26
  308. package/src/wirings/scheduler/scheduler-runner.ts +45 -5
  309. package/src/wirings/secret/index.ts +9 -0
  310. package/src/wirings/secret/secret.types.ts +32 -0
  311. package/src/wirings/secret/validate-secret-definitions.ts +82 -0
  312. package/src/wirings/trigger/index.ts +10 -2
  313. package/src/wirings/trigger/pikku-trigger-service.ts +134 -0
  314. package/src/wirings/trigger/trigger-runner.ts +80 -38
  315. package/src/wirings/trigger/trigger.types.ts +144 -22
  316. package/src/wirings/variable/index.ts +8 -0
  317. package/src/wirings/variable/validate-variable-definitions.ts +69 -0
  318. package/src/wirings/variable/variable.types.ts +22 -0
  319. package/src/wirings/workflow/dsl/index.ts +1 -0
  320. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +9 -1
  321. package/src/wirings/workflow/dsl/workflow-runner.ts +4 -3
  322. package/src/wirings/workflow/graph/graph-node.ts +1 -6
  323. package/src/wirings/workflow/graph/graph-runner.test.ts +495 -0
  324. package/src/wirings/workflow/graph/graph-runner.ts +496 -374
  325. package/src/wirings/workflow/graph/index.ts +14 -3
  326. package/src/wirings/workflow/graph/template.ts +42 -0
  327. package/src/wirings/workflow/graph/wire-workflow-graph.ts +29 -0
  328. package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -22
  329. package/src/wirings/workflow/index.ts +21 -10
  330. package/src/wirings/workflow/pikku-workflow-service.test.ts +212 -0
  331. package/src/wirings/workflow/pikku-workflow-service.ts +285 -66
  332. package/src/wirings/workflow/workflow-helpers.ts +79 -0
  333. package/src/wirings/workflow/workflow-utils.ts +0 -120
  334. package/src/wirings/workflow/workflow.types.ts +45 -90
  335. package/tsconfig.tsbuildinfo +1 -1
  336. package/dist/wirings/forge-node/forge-node.types.d.ts +0 -120
  337. package/dist/wirings/forge-node/forge-node.types.js +0 -38
  338. package/dist/wirings/forge-node/index.d.ts +0 -1
  339. package/dist/wirings/forge-node/index.js +0 -1
  340. package/dist/wirings/workflow/wire-workflow.d.ts +0 -42
  341. package/dist/wirings/workflow/wire-workflow.js +0 -53
  342. package/src/wirings/forge-node/forge-node.types.ts +0 -135
  343. package/src/wirings/forge-node/index.ts +0 -1
  344. package/src/wirings/workflow/wire-workflow.ts +0 -94
@@ -1,13 +1,26 @@
1
- import { NotFoundError } from '../../errors/errors.js';
1
+ import { NotFoundError, PikkuMissingMetaError } from '../../errors/errors.js';
2
2
  import { closeWireServices, createWeakUID, isSerializable, } from '../../utils.js';
3
- import { PikkuSessionService } from '../../services/user-session-service.js';
3
+ import { PikkuSessionService, createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
4
4
  import { handleHTTPError } from '../../handle-error.js';
5
5
  import { pikkuState } from '../../pikku-state.js';
6
6
  import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
7
7
  import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
8
8
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
9
9
  import { httpRouter } from './routers/http-router.js';
10
- import { startWorkflowByHttpWire } from '../workflow/workflow-utils.js';
10
+ import { validateSchema } from '../../schema.js';
11
+ /**
12
+ * Extract headers from a PikkuHTTPRequest based on the schema keys
13
+ */
14
+ function extractHeadersFromRequest(request, headerKeys) {
15
+ const headers = {};
16
+ for (const headerName of headerKeys) {
17
+ const value = request.header(headerName);
18
+ if (value !== null) {
19
+ headers[headerName] = value;
20
+ }
21
+ }
22
+ return headers;
23
+ }
11
24
  /**
12
25
  * Registers HTTP middleware for a specific route pattern.
13
26
  *
@@ -95,10 +108,10 @@ export const wireHTTP = (httpWiring) => {
95
108
  const httpMeta = pikkuState(null, 'http', 'meta');
96
109
  const routeMeta = httpMeta[httpWiring.method][httpWiring.route];
97
110
  if (!routeMeta) {
98
- throw new Error('Route metadata not found');
111
+ throw new PikkuMissingMetaError(`Missing generated metadata for HTTP route '${httpWiring.method.toUpperCase()} ${httpWiring.route}'`);
99
112
  }
100
113
  if (httpWiring.func) {
101
- addFunction(routeMeta.pikkuFuncName, httpWiring.func);
114
+ addFunction(routeMeta.pikkuFuncId, httpWiring.func);
102
115
  }
103
116
  const routes = pikkuState(null, 'http', 'routes');
104
117
  if (!routes.has(httpWiring.method)) {
@@ -184,6 +197,12 @@ const executeRoute = async (services, matchedRoute, http, options) => {
184
197
  const { singletonServices, createWireServices, skipUserSession, requestId } = services;
185
198
  // Attach URL parameters to the request object
186
199
  http?.request?.setParams(params);
200
+ // Validate request headers if schema is defined
201
+ if (meta.headersSchemaName && http.request && singletonServices.schema) {
202
+ const headerKeys = singletonServices.schema.getSchemaKeys(meta.headersSchemaName);
203
+ const rawHeaders = extractHeadersFromRequest(http.request, headerKeys);
204
+ await validateSchema(singletonServices.logger, singletonServices.schema, meta.headersSchemaName, rawHeaders);
205
+ }
187
206
  const requiresSession = route.auth !== false;
188
207
  let wireServices;
189
208
  let result;
@@ -192,7 +211,13 @@ const executeRoute = async (services, matchedRoute, http, options) => {
192
211
  if (skipUserSession && requiresSession) {
193
212
  throw new Error("Can't skip trying to get user session if auth is required");
194
213
  }
195
- const data = () => http.request.data();
214
+ let cachedData;
215
+ const data = async () => {
216
+ if (cachedData === undefined) {
217
+ cachedData = await http.request.data();
218
+ }
219
+ return cachedData;
220
+ };
196
221
  let channel;
197
222
  if (matchedRoute.route.sse) {
198
223
  const response = http?.response;
@@ -220,14 +245,12 @@ const executeRoute = async (services, matchedRoute, http, options) => {
220
245
  state: 'open',
221
246
  };
222
247
  }
223
- const wire = { http, channel, session: userSession };
224
- if (matchedRoute.meta.workflow === true) {
225
- await startWorkflowByHttpWire(singletonServices, createWireServices, matchedRoute, wire);
226
- return wireServices
227
- ? { wireServices, result: http.response }
228
- : { result: http.response };
229
- }
230
- result = await runPikkuFunc('http', `${meta.method}:${meta.route}`, meta.pikkuFuncName, {
248
+ const wire = {
249
+ http,
250
+ channel,
251
+ ...createMiddlewareSessionWireProps(userSession),
252
+ };
253
+ result = await runPikkuFunc('http', `${meta.method}:${meta.route}`, meta.pikkuFuncId, {
231
254
  singletonServices,
232
255
  createWireServices,
233
256
  auth: route.auth !== false,
@@ -239,17 +262,20 @@ const executeRoute = async (services, matchedRoute, http, options) => {
239
262
  coerceDataFromSchema: options.coerceDataFromSchema,
240
263
  tags: route.tags,
241
264
  wire,
265
+ sessionService: userSession,
242
266
  });
243
- // Respond with either a binary or JSON response based on configuration
244
- if (route.returnsJSON === false) {
245
- http?.response?.arrayBuffer(result);
246
- }
247
- else {
248
- http?.response?.json(result);
267
+ if (!matchedRoute.route.sse) {
268
+ if (result === undefined || result === null) {
269
+ http?.response?.status(204);
270
+ }
271
+ else if (route.returnsJSON === false) {
272
+ http?.response?.arrayBuffer(result);
273
+ }
274
+ else {
275
+ http?.response?.status(200);
276
+ http?.response?.json(result);
277
+ }
249
278
  }
250
- http?.response?.status(200);
251
- // TODO: Evaluate if the response stream should be explicitly ended.
252
- // http?.response?.end()
253
279
  return wireServices ? { result, wireServices } : { result };
254
280
  };
255
281
  /**
@@ -1,5 +1,6 @@
1
1
  import type { SerializeOptions } from 'cookie';
2
- import type { PikkuError } from '../../errors/error-handler.js';
2
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
3
+ import { PikkuError } from '../../errors/error-handler.js';
3
4
  import type { CoreServices, CoreSingletonServices, CoreUserSession, CreateWireServices, CorePikkuMiddleware, CommonWireMeta } from '../../types/core.types.js';
4
5
  import type { CorePikkuFunction, CorePikkuFunctionSessionless, CorePikkuPermission, CorePermissionGroup, CorePikkuFunctionConfig } from '../../function/functions.types.js';
5
6
  type ExtractHTTPWiringParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractHTTPWiringParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
@@ -19,28 +20,43 @@ export type RunHTTPWiringOptions = Partial<{
19
20
  }>;
20
21
  export type RunHTTPWiringParams = {
21
22
  singletonServices: CoreSingletonServices;
22
- createWireServices: CreateWireServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
23
+ createWireServices?: CreateWireServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
23
24
  };
24
25
  /**
25
26
  * Represents the HTTP methods supported for API HTTP wirings.
26
- t */
27
+ */
27
28
  export type HTTPMethod = 'post' | 'get' | 'delete' | 'patch' | 'head' | 'put' | 'options';
28
29
  /**
29
- * Represents an API HTTP wiring without a function, including metadata such as content type, route, and timeout settings.
30
+ * Header schema type for request header validation.
31
+ * Uses Standard Schema interface - works with Zod, Valibot, ArkType, Effect Schema, etc.
32
+ */
33
+ export type HTTPHeadersSchema = StandardSchemaV1<Record<string, string | string[] | undefined>>;
34
+ /**
35
+ * Documentation config for OpenAPI generation
36
+ */
37
+ export type HTTPRouteDocsConfig = {
38
+ description?: string;
39
+ response?: string;
40
+ errors?: Array<typeof PikkuError>;
41
+ tags?: string[];
42
+ };
43
+ /**
44
+ * Common HTTP route configuration shared between wireHTTP and wireHTTPRoutes
30
45
  */
31
- export type CoreHTTPFunction = {
46
+ export type HTTPRouteBaseConfig = {
32
47
  contentType?: 'xml' | 'json';
48
+ timeout?: number;
49
+ tags?: string[];
50
+ headers?: HTTPHeadersSchema;
51
+ docs?: HTTPRouteDocsConfig;
52
+ };
53
+ /**
54
+ * Represents an API HTTP wiring without a function, including metadata such as content type, route, and timeout settings.
55
+ */
56
+ export type CoreHTTPFunction = HTTPRouteBaseConfig & {
33
57
  route: string;
34
58
  eventChannel?: false;
35
59
  returnsJSON?: false;
36
- timeout?: number;
37
- tags?: string[];
38
- docs?: Partial<{
39
- description: string;
40
- response: string;
41
- errors: Array<typeof PikkuError>;
42
- tags: string[];
43
- }>;
44
60
  };
45
61
  /**
46
62
  * Represents a http wire within Pikku, including a request and response.
@@ -67,13 +83,12 @@ export type PikkuQuery<T = Record<string, string | undefined>> = Record<string,
67
83
  * @template PikkuFunctionSessionless - The sessionless API function type, defaults to `CorePikkuFunctionSessionless`.
68
84
  * @template PikkuPermission - The permission function type, defaults to `CorePikkuPermission`.
69
85
  */
70
- export type CoreHTTPFunctionWiring<In, Out, R extends string, PikkuFunction extends CorePikkuFunction<In, Out, any, any> = CorePikkuFunction<In, Out>, PikkuFunctionSessionless extends CorePikkuFunctionSessionless<In, Out, any, any> = CorePikkuFunctionSessionless<In, Out>, PikkuPermission extends CorePikkuPermission<In, any, any> = CorePikkuPermission<In, any, any>, PikkuMiddleware extends CorePikkuMiddleware<any, any> = CorePikkuMiddleware<any>> = (CoreHTTPFunction & {
86
+ export type CoreHTTPFunctionWiring<In, Out, R extends string, PikkuFunction extends CorePikkuFunction<In, Out, any, any, any> = CorePikkuFunction<In, Out>, PikkuFunctionSessionless extends CorePikkuFunctionSessionless<In, Out, any, any, any> = CorePikkuFunctionSessionless<In, Out>, PikkuPermission extends CorePikkuPermission<In, any, any> = CorePikkuPermission<In, any, any>, PikkuMiddleware extends CorePikkuMiddleware<any, any> = CorePikkuMiddleware<any>> = (CoreHTTPFunction & {
71
87
  route: R;
72
88
  method: HTTPMethod;
73
89
  func: CorePikkuFunctionConfig<PikkuFunction, PikkuPermission, PikkuMiddleware>;
74
90
  permissions?: CorePermissionGroup<PikkuPermission>;
75
91
  auth?: true;
76
- tags?: string[];
77
92
  middleware?: PikkuMiddleware[];
78
93
  sse?: undefined;
79
94
  }) | (CoreHTTPFunction & {
@@ -82,7 +97,6 @@ export type CoreHTTPFunctionWiring<In, Out, R extends string, PikkuFunction exte
82
97
  func: CorePikkuFunctionConfig<PikkuFunctionSessionless, PikkuPermission, PikkuMiddleware>;
83
98
  permissions?: undefined;
84
99
  auth?: false;
85
- tags?: string[];
86
100
  middleware?: PikkuMiddleware[];
87
101
  sse?: undefined;
88
102
  }) | (CoreHTTPFunction & {
@@ -91,27 +105,24 @@ export type CoreHTTPFunctionWiring<In, Out, R extends string, PikkuFunction exte
91
105
  func: CorePikkuFunctionConfig<PikkuFunction, PikkuPermission, PikkuMiddleware>;
92
106
  permissions?: CorePermissionGroup<PikkuPermission>;
93
107
  auth?: true;
94
- sse?: boolean;
95
- tags?: string[];
96
108
  middleware?: PikkuMiddleware[];
109
+ sse?: boolean;
97
110
  }) | (CoreHTTPFunction & {
98
111
  route: R;
99
112
  method: 'get';
100
113
  func: CorePikkuFunctionConfig<PikkuFunctionSessionless, PikkuPermission, PikkuMiddleware>;
101
114
  permissions?: undefined;
102
115
  auth?: false;
103
- sse?: boolean;
104
- tags?: string[];
105
116
  middleware?: PikkuMiddleware[];
117
+ sse?: boolean;
106
118
  }) | (CoreHTTPFunction & {
107
119
  route: R;
108
120
  method: 'post';
109
121
  func: CorePikkuFunctionConfig<PikkuFunction, PikkuPermission, PikkuMiddleware>;
110
122
  permissions?: CorePermissionGroup<PikkuPermission>;
111
123
  auth?: true;
112
- query?: Array<keyof In>;
113
- tags?: string[];
114
124
  middleware?: PikkuMiddleware[];
125
+ query?: Array<keyof In>;
115
126
  sse?: undefined;
116
127
  }) | (CoreHTTPFunction & {
117
128
  route: R;
@@ -119,23 +130,10 @@ export type CoreHTTPFunctionWiring<In, Out, R extends string, PikkuFunction exte
119
130
  func: CorePikkuFunctionConfig<PikkuFunctionSessionless, PikkuPermission, PikkuMiddleware>;
120
131
  permissions?: undefined;
121
132
  auth?: false;
122
- query?: Array<keyof In>;
123
- tags?: string[];
124
133
  middleware?: PikkuMiddleware[];
134
+ query?: Array<keyof In>;
125
135
  sse?: undefined;
126
- }) | {
127
- /** Route triggers a workflow matched by route/method from workflow wires.http */
128
- route: R;
129
- method: HTTPMethod;
130
- workflow: true;
131
- func?: undefined;
132
- permissions?: undefined;
133
- auth?: undefined;
134
- tags?: undefined;
135
- middleware?: undefined;
136
- returnsJSON?: undefined;
137
- sse?: boolean;
138
- };
136
+ });
139
137
  /**
140
138
  * Represents the input types for HTTP wiring metadata, including parameters, query, and body types.
141
139
  */
@@ -153,8 +151,9 @@ export type HTTPWiringMeta = CommonWireMeta & {
153
151
  params?: string[];
154
152
  query?: string[];
155
153
  inputTypes?: HTTPFunctionMetaInputTypes;
154
+ headersSchemaName?: string;
156
155
  sse?: true;
157
- workflow?: true;
156
+ groupBasePath?: string;
158
157
  };
159
158
  export type HTTPWiringsMeta = Record<HTTPMethod, Record<string, HTTPWiringMeta>>;
160
159
  export type HTTPFunctionsMeta = Array<{
@@ -188,4 +187,45 @@ export interface PikkuHTTPResponse<Out = unknown> {
188
187
  close?: () => void;
189
188
  setMode?: (mode: 'stream') => void;
190
189
  }
190
+ /**
191
+ * Single route configuration - supports all wireHTTP options
192
+ */
193
+ export type HTTPRouteConfig<PikkuFunction extends CorePikkuFunction<any, any, any, any, any> | CorePikkuFunctionSessionless<any, any, any, any, any> = CorePikkuFunction<any, any, any, any, any> | CorePikkuFunctionSessionless<any, any, any, any, any>, PikkuPermission extends CorePikkuPermission<any, any, any> = CorePikkuPermission<any>, PikkuMiddleware extends CorePikkuMiddleware<any, any> = CorePikkuMiddleware<any, any>> = HTTPRouteBaseConfig & {
194
+ method: HTTPMethod;
195
+ route: string;
196
+ func: CorePikkuFunctionConfig<PikkuFunction, PikkuPermission, PikkuMiddleware>;
197
+ auth?: boolean;
198
+ permissions?: CorePermissionGroup<PikkuPermission>;
199
+ middleware?: PikkuMiddleware[];
200
+ sse?: boolean;
201
+ };
202
+ /**
203
+ * Group-level configuration applied to all routes
204
+ */
205
+ export type HTTPRoutesGroupConfig<PikkuPermission extends CorePikkuPermission<any, any, any> = CorePikkuPermission<any>, PikkuMiddleware extends CorePikkuMiddleware<any, any> = CorePikkuMiddleware<any, any>> = {
206
+ basePath?: string;
207
+ tags?: string[];
208
+ auth?: boolean;
209
+ middleware?: PikkuMiddleware[];
210
+ permissions?: CorePermissionGroup<PikkuPermission>;
211
+ };
212
+ /**
213
+ * Nested route map - allows hierarchical organization
214
+ * Can contain individual routes, nested maps, or contracts with config
215
+ */
216
+ export type HTTPRouteMap = {
217
+ [key: string]: HTTPRouteConfig | HTTPRouteMap | HTTPRouteContract;
218
+ };
219
+ /**
220
+ * A route contract with optional group config (returned by defineRoutes)
221
+ */
222
+ export type HTTPRouteContract<T extends HTTPRouteMap = HTTPRouteMap> = HTTPRoutesGroupConfig & {
223
+ routes: T;
224
+ };
225
+ /**
226
+ * Full configuration for wireHTTPRoutes
227
+ */
228
+ export type WireHTTPRoutesConfig = HTTPRoutesGroupConfig & {
229
+ routes: HTTPRouteMap | HTTPRouteConfig[];
230
+ };
191
231
  export {};
@@ -1,5 +1,6 @@
1
- export * from './pikku-fetch-http-request.js';
2
- export * from './pikku-fetch-http-response.js';
3
- export * from './log-http-routes.js';
1
+ export { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
2
+ export { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
3
+ export { logRoutes } from './log-http-routes.js';
4
4
  export { fetch, fetchData, wireHTTP, addHTTPMiddleware, addHTTPPermission, } from './http-runner.js';
5
- export type * from './http.types.js';
5
+ export { wireHTTPRoutes, defineHTTPRoutes } from './http-routes.js';
6
+ export type { AssertHTTPWiringParams, CoreHTTPFunctionWiring, HTTPMethod, HTTPRouteBaseConfig, HTTPWiringsMeta, PikkuHTTPRequest, PikkuHTTPResponse, PikkuQuery, RunHTTPWiringOptions, } from './http.types.js';
@@ -1,4 +1,5 @@
1
- export * from './pikku-fetch-http-request.js';
2
- export * from './pikku-fetch-http-response.js';
3
- export * from './log-http-routes.js';
1
+ export { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
2
+ export { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
3
+ export { logRoutes } from './log-http-routes.js';
4
4
  export { fetch, fetchData, wireHTTP, addHTTPMiddleware, addHTTPPermission, } from './http-runner.js';
5
+ export { wireHTTPRoutes, defineHTTPRoutes } from './http-routes.js';
@@ -1,3 +1,4 @@
1
- export * from './mcp.types.js';
2
- export * from './mcp-runner.js';
3
- export * from './mcp-endpoint-registry.js';
1
+ export { MCPEndpointRegistry } from './mcp-endpoint-registry.js';
2
+ export { MCPError, wireMCPResource, wireMCPPrompt, runMCPResource, runMCPTool, runMCPPrompt, } from './mcp-runner.js';
3
+ export { getMCPResourcesMeta, getMCPToolsMeta, getMCPPromptsMeta, } from './mcp-runner.js';
4
+ export type { AssertMCPResourceURIParams, CoreMCPPrompt, CoreMCPResource, CoreMCPTool, MCPPromptResponse, MCPResourceMeta, MCPResourceResponse, MCPToolMeta, MCPToolResponse, MCPPromptMeta, PikkuMCP, } from './mcp.types.js';
@@ -1,3 +1,3 @@
1
- export * from './mcp.types.js';
2
- export * from './mcp-runner.js';
3
- export * from './mcp-endpoint-registry.js';
1
+ export { MCPEndpointRegistry } from './mcp-endpoint-registry.js';
2
+ export { MCPError, wireMCPResource, wireMCPPrompt, runMCPResource, runMCPTool, runMCPPrompt, } from './mcp-runner.js';
3
+ export { getMCPResourcesMeta, getMCPToolsMeta, getMCPPromptsMeta, } from './mcp-runner.js';
@@ -1,5 +1,5 @@
1
1
  import { type CoreServices, type CoreSingletonServices, type CoreUserSession, type CreateWireServices } from '../../types/core.types.js';
2
- import type { CoreMCPResource, CoreMCPTool, CoreMCPPrompt, JsonRpcRequest, JsonRpcResponse, JsonRpcErrorResponse, PikkuMCP } from './mcp.types.js';
2
+ import type { CoreMCPResource, CoreMCPPrompt, JsonRpcRequest, JsonRpcResponse, JsonRpcErrorResponse, PikkuMCP } from './mcp.types.js';
3
3
  import type { CorePikkuFunctionConfig, CorePikkuFunctionSessionless } from '../../function/functions.types.js';
4
4
  export declare class MCPError extends Error {
5
5
  readonly error: JsonRpcErrorResponse;
@@ -16,12 +16,10 @@ export type JsonRpcError = {
16
16
  data?: any;
17
17
  };
18
18
  export declare const wireMCPResource: <PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>> = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>>(mcpResource: CoreMCPResource<PikkuFunctionConfig>) => void;
19
- export declare const wireMCPTool: <PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>> = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>>(mcpTool: CoreMCPTool<PikkuFunctionConfig>) => void;
20
19
  export declare const wireMCPPrompt: <PikkuFunctionConfig extends CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>> = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>>(mcpPrompt: CoreMCPPrompt<PikkuFunctionConfig>) => void;
21
20
  export declare function runMCPResource(request: JsonRpcRequest, params: RunMCPEndpointParams, uri: string): Promise<JsonRpcResponse>;
22
21
  export declare function runMCPTool(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
23
22
  export declare function runMCPPrompt(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
24
- export declare const getMCPTools: () => Map<string, CoreMCPTool>;
25
23
  export declare const getMCPResources: () => Map<string, CoreMCPResource>;
26
24
  export declare const getMCPResourcesMeta: () => import("./mcp.types.js").MCPResourceMeta;
27
25
  export declare const getMCPToolsMeta: () => import("./mcp.types.js").MCPToolMeta;
@@ -2,8 +2,8 @@ import { getErrorResponse } from '../../errors/error-handler.js';
2
2
  import { closeWireServices } from '../../utils.js';
3
3
  import { pikkuState } from '../../pikku-state.js';
4
4
  import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
5
- import { BadRequestError, NotFoundError } from '../../errors/errors.js';
6
- import { PikkuSessionService } from '../../services/user-session-service.js';
5
+ import { BadRequestError, NotFoundError, PikkuMissingMetaError, } from '../../errors/errors.js';
6
+ import { PikkuSessionService, createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
7
7
  export class MCPError extends Error {
8
8
  error;
9
9
  constructor(error) {
@@ -17,35 +17,22 @@ export const wireMCPResource = (mcpResource) => {
17
17
  const resourcesMeta = pikkuState(null, 'mcp', 'resourcesMeta');
18
18
  const mcpResourceMeta = resourcesMeta[mcpResource.uri];
19
19
  if (!mcpResourceMeta) {
20
- throw new Error(`MCP resource metadata not found for '${mcpResource.uri}'`);
20
+ throw new PikkuMissingMetaError(`Missing generated metadata for MCP resource '${mcpResource.uri}'`);
21
21
  }
22
- addFunction(mcpResourceMeta.pikkuFuncName, mcpResource.func);
22
+ addFunction(mcpResourceMeta.pikkuFuncId, mcpResource.func);
23
23
  const resources = pikkuState(null, 'mcp', 'resources');
24
24
  if (resources.has(mcpResource.uri)) {
25
25
  throw new Error(`MCP resource already exists: ${mcpResource.uri}`);
26
26
  }
27
27
  resources.set(mcpResource.uri, mcpResource);
28
28
  };
29
- export const wireMCPTool = (mcpTool) => {
30
- const toolsMeta = pikkuState(null, 'mcp', 'toolsMeta');
31
- const mcpToolMeta = toolsMeta[mcpTool.name];
32
- if (!mcpToolMeta) {
33
- throw new Error(`MCP tool metadata not found for '${mcpTool.name}'`);
34
- }
35
- addFunction(mcpToolMeta.pikkuFuncName, mcpTool.func);
36
- const tools = pikkuState(null, 'mcp', 'tools');
37
- if (tools.has(mcpTool.name)) {
38
- throw new Error(`MCP tool already exists: ${mcpTool.name}`);
39
- }
40
- tools.set(mcpTool.name, mcpTool);
41
- };
42
29
  export const wireMCPPrompt = (mcpPrompt) => {
43
30
  const promptsMeta = pikkuState(null, 'mcp', 'promptsMeta');
44
31
  const mcpPromptMeta = promptsMeta[mcpPrompt.name];
45
32
  if (!mcpPromptMeta) {
46
- throw new Error(`MCP prompt metadata not found for '${mcpPrompt.name}'`);
33
+ throw new PikkuMissingMetaError(`Missing generated metadata for MCP prompt '${mcpPrompt.name}'`);
47
34
  }
48
- addFunction(mcpPromptMeta.pikkuFuncName, mcpPrompt.func);
35
+ addFunction(mcpPromptMeta.pikkuFuncId, mcpPrompt.func);
49
36
  const prompts = pikkuState(null, 'mcp', 'prompts');
50
37
  if (prompts.has(mcpPrompt.name)) {
51
38
  throw new Error(`MCP prompt already exists: ${mcpPrompt.name}`);
@@ -54,13 +41,13 @@ export const wireMCPPrompt = (mcpPrompt) => {
54
41
  };
55
42
  export async function runMCPResource(request, params, uri) {
56
43
  let endpoint;
57
- let pikkuFuncName;
44
+ let pikkuFuncId;
58
45
  let extractedParams = {};
59
46
  const metas = pikkuState(null, 'mcp', 'resourcesMeta');
60
47
  const endpoints = pikkuState(null, 'mcp', 'resources');
61
48
  if (endpoints.has(uri)) {
62
49
  endpoint = endpoints.get(uri);
63
- pikkuFuncName = metas[uri]?.pikkuFuncName;
50
+ pikkuFuncId = metas[uri]?.pikkuFuncId;
64
51
  }
65
52
  else {
66
53
  for (const [uriTemplate, value] of endpoints.entries()) {
@@ -72,7 +59,7 @@ export async function runMCPResource(request, params, uri) {
72
59
  const match = uri.match(regex);
73
60
  if (match) {
74
61
  endpoint = value;
75
- pikkuFuncName = metas[uriTemplate]?.pikkuFuncName;
62
+ pikkuFuncId = metas[uriTemplate]?.pikkuFuncId;
76
63
  // Extract parameter values and create params object
77
64
  for (let i = 0; i < paramNames.length; i++) {
78
65
  extractedParams[paramNames[i]] = match[i + 1]; // match[0] is the full match
@@ -84,40 +71,38 @@ export async function runMCPResource(request, params, uri) {
84
71
  return await runMCPPikkuFunc({
85
72
  ...request,
86
73
  params: { ...request.params, ...extractedParams },
87
- }, 'resource', uri, endpoint, pikkuFuncName, { ...params, mcp: { ...params.mcp, uri } });
74
+ }, 'resource', uri, endpoint, pikkuFuncId, { ...params, mcp: { ...params.mcp, uri } });
88
75
  }
89
76
  export async function runMCPTool(request, params, name) {
90
- const endpoint = pikkuState(null, 'mcp', 'tools').get(name);
91
77
  const meta = pikkuState(null, 'mcp', 'toolsMeta')[name];
92
- return await runMCPPikkuFunc(request, 'tool', name, endpoint, meta?.pikkuFuncName, params);
78
+ return await runMCPPikkuFunc(request, 'tool', name, undefined, meta?.pikkuFuncId, params);
93
79
  }
94
80
  export async function runMCPPrompt(request, params, name) {
95
81
  const endpoint = pikkuState(null, 'mcp', 'prompts').get(name);
96
82
  const meta = pikkuState(null, 'mcp', 'promptsMeta')[name];
97
- return await runMCPPikkuFunc(request, 'prompt', name, endpoint, meta?.pikkuFuncName, params);
83
+ return await runMCPPikkuFunc(request, 'prompt', name, endpoint, meta?.pikkuFuncId, params);
98
84
  }
99
85
  /**
100
86
  * JSON-RPC 2.0 compatible MCP endpoint runner
101
87
  */
102
- async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncName, { singletonServices, createWireServices, mcp: mcpWire }) {
88
+ async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncId, { singletonServices, createWireServices, mcp: mcpWire }) {
103
89
  let wireServices;
104
90
  try {
105
- // Validate JSON-RPC request structure
106
91
  if (request.jsonrpc !== '2.0') {
107
92
  throw new BadRequestError('Invalid JSON-RPC version, only supoorted version is 2.0');
108
93
  }
109
- if (!mcp) {
94
+ if (mcp === undefined && type !== 'tool') {
110
95
  throw new NotFoundError(`MCP '${type}' registration not found for '${name}'`);
111
96
  }
112
- if (!pikkuFuncName) {
97
+ if (!pikkuFuncId) {
113
98
  throw new NotFoundError(`MCP '${type}' PikkuFunction Mapping not found for '${name}'`);
114
99
  }
115
100
  singletonServices.logger.debug(`Running MCP ${type}: ${name}`);
101
+ const mcpSessionService = new PikkuSessionService();
116
102
  const wire = {
117
103
  mcp: mcpWire,
118
- session: new PikkuSessionService(),
104
+ ...createMiddlewareSessionWireProps(mcpSessionService),
119
105
  };
120
- // Get metadata for the MCP endpoint to access pre-resolved middleware
121
106
  let meta;
122
107
  if (type === 'resource') {
123
108
  meta = pikkuState(null, 'mcp', 'resourcesMeta')[name];
@@ -128,17 +113,21 @@ async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncName, { single
128
113
  else if (type === 'prompt') {
129
114
  meta = pikkuState(null, 'mcp', 'promptsMeta')[name];
130
115
  }
131
- const result = await runPikkuFunc('mcp', `${type}:${name}`, pikkuFuncName, {
116
+ let result = await runPikkuFunc('mcp', `${type}:${name}`, pikkuFuncId, {
132
117
  singletonServices,
133
118
  createWireServices,
134
119
  data: () => request.params,
135
120
  inheritedMiddleware: meta?.middleware,
136
- wireMiddleware: mcp.middleware,
121
+ wireMiddleware: mcp?.middleware,
137
122
  inheritedPermissions: meta?.permissions,
138
- wirePermissions: mcp.permissions,
139
- tags: mcp.tags,
123
+ wirePermissions: mcp?.permissions,
124
+ tags: mcp?.tags,
140
125
  wire,
126
+ sessionService: mcpSessionService,
141
127
  });
128
+ if (type === 'tool' && meta?.outputSchema !== 'MCPToolResponse') {
129
+ result = [{ type: 'text', text: JSON.stringify(result) }];
130
+ }
142
131
  return {
143
132
  id: request.id,
144
133
  result,
@@ -172,9 +161,6 @@ async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncName, { single
172
161
  }
173
162
  }
174
163
  }
175
- export const getMCPTools = () => {
176
- return pikkuState(null, 'mcp', 'tools');
177
- };
178
164
  export const getMCPResources = () => {
179
165
  return pikkuState(null, 'mcp', 'resources');
180
166
  };
@@ -26,7 +26,7 @@ export type PikkuMCP<Tools extends string = any> = {
26
26
  * Represents metadata for MCP resources, including name, description, and documentation.
27
27
  */
28
28
  export type MCPResourceMeta = Record<string, Omit<CoreMCPResource, 'func' | 'middleware' | 'permissions'> & {
29
- pikkuFuncName: string;
29
+ pikkuFuncId: string;
30
30
  inputSchema: string | null;
31
31
  outputSchema: string | null;
32
32
  middleware?: MiddlewareMetadata[];
@@ -36,7 +36,7 @@ export type MCPResourceMeta = Record<string, Omit<CoreMCPResource, 'func' | 'mid
36
36
  * Represents metadata for MCP tools, including name, description, and documentation.
37
37
  */
38
38
  export type MCPToolMeta = Record<string, Omit<CoreMCPTool, 'func' | 'middleware' | 'permissions'> & {
39
- pikkuFuncName: string;
39
+ pikkuFuncId: string;
40
40
  inputSchema: string | null;
41
41
  outputSchema: string | null;
42
42
  middleware?: MiddlewareMetadata[];
@@ -46,7 +46,7 @@ export type MCPToolMeta = Record<string, Omit<CoreMCPTool, 'func' | 'middleware'
46
46
  * Represents metadata for MCP prompts, including name, description, and arguments.
47
47
  */
48
48
  export type MCPPromptMeta = Record<string, Omit<CoreMCPPrompt, 'func' | 'middleware' | 'permissions'> & {
49
- pikkuFuncName: string;
49
+ pikkuFuncId: string;
50
50
  inputSchema: string | null;
51
51
  outputSchema: string | null;
52
52
  arguments: Array<{
@@ -0,0 +1,2 @@
1
+ export type { CoreNodeConfig, NodeType } from './node.types.js';
2
+ export type { NodesMeta } from './node.types.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ export type NodeType = 'trigger' | 'action' | 'end';
2
+ export type CoreNodeConfig = {
3
+ displayName: string;
4
+ category: string;
5
+ type: NodeType;
6
+ errorOutput?: boolean;
7
+ };
8
+ export type NodeMeta = {
9
+ name: string;
10
+ displayName: string;
11
+ category: string;
12
+ type: NodeType;
13
+ rpc: string;
14
+ description?: string;
15
+ errorOutput: boolean;
16
+ inputSchemaName: string | null;
17
+ outputSchemaName: string | null;
18
+ tags?: string[];
19
+ };
20
+ export type NodesMeta = Record<string, NodeMeta>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { OAuth2Client } from './oauth2-client.js';
2
+ export { wireOAuth2Credential } from './wire-oauth2-credential.js';
3
+ export type { OAuth2AppCredential, OAuth2Token } from './oauth2.types.js';
@@ -0,0 +1,2 @@
1
+ export { OAuth2Client } from './oauth2-client.js';
2
+ export { wireOAuth2Credential } from './wire-oauth2-credential.js';