@pikku/core 0.12.2 → 0.12.4

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 (282) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/crypto-utils.d.ts +10 -0
  3. package/dist/crypto-utils.js +62 -0
  4. package/dist/function/function-runner.js +4 -4
  5. package/dist/function/functions.types.d.ts +26 -1
  6. package/dist/function/functions.types.js +16 -0
  7. package/dist/handle-error.d.ts +1 -1
  8. package/dist/handle-error.js +7 -3
  9. package/dist/index.d.ts +14 -2
  10. package/dist/index.js +1 -1
  11. package/dist/pikku-state.js +3 -1
  12. package/dist/schema.js +1 -1
  13. package/dist/services/in-memory-workflow-service.d.ts +22 -3
  14. package/dist/services/in-memory-workflow-service.js +77 -1
  15. package/dist/services/workflow-service.d.ts +8 -2
  16. package/dist/testing/index.d.ts +2 -0
  17. package/dist/testing/index.js +1 -0
  18. package/dist/testing/service-tests.d.ts +31 -0
  19. package/dist/testing/service-tests.js +598 -0
  20. package/dist/types/core.types.d.ts +9 -1
  21. package/dist/types/state.types.d.ts +4 -2
  22. package/dist/utils/hash.d.ts +2 -0
  23. package/dist/utils/hash.js +23 -0
  24. package/dist/wirings/ai-agent/agent-dynamic-workflow.d.ts +6 -0
  25. package/dist/wirings/ai-agent/agent-dynamic-workflow.js +348 -0
  26. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +1 -1
  27. package/dist/wirings/ai-agent/ai-agent-memory.js +6 -5
  28. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +10 -7
  29. package/dist/wirings/ai-agent/ai-agent-prepare.js +127 -47
  30. package/dist/wirings/ai-agent/ai-agent-runner.d.ts +4 -0
  31. package/dist/wirings/ai-agent/ai-agent-runner.js +381 -27
  32. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +7 -2
  33. package/dist/wirings/ai-agent/ai-agent-stream.js +109 -46
  34. package/dist/wirings/ai-agent/ai-agent.types.d.ts +22 -1
  35. package/dist/wirings/ai-agent/index.d.ts +1 -2
  36. package/dist/wirings/ai-agent/index.js +1 -2
  37. package/dist/wirings/channel/channel-handler.js +5 -1
  38. package/dist/wirings/channel/channel.types.d.ts +1 -7
  39. package/dist/wirings/channel/local/local-channel-handler.d.ts +1 -1
  40. package/dist/wirings/channel/local/local-channel-handler.js +5 -3
  41. package/dist/wirings/channel/local/local-channel-runner.js +14 -4
  42. package/dist/wirings/channel/serverless/serverless-channel-runner.js +4 -1
  43. package/dist/wirings/cli/cli-runner.js +10 -2
  44. package/dist/wirings/cli/cli.types.d.ts +1 -0
  45. package/dist/wirings/http/http-routes.js +0 -1
  46. package/dist/wirings/http/http-runner.d.ts +1 -1
  47. package/dist/wirings/http/http-runner.js +25 -3
  48. package/dist/wirings/http/http.types.d.ts +3 -11
  49. package/dist/wirings/http/index.d.ts +2 -1
  50. package/dist/wirings/http/index.js +1 -0
  51. package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -0
  52. package/dist/wirings/http/pikku-fetch-http-request.js +3 -0
  53. package/dist/wirings/http/pikku-fetch-http-response.d.ts +1 -0
  54. package/dist/wirings/http/pikku-fetch-http-response.js +4 -4
  55. package/dist/wirings/http/web-request.d.ts +12 -0
  56. package/dist/wirings/http/web-request.js +85 -0
  57. package/dist/wirings/mcp/mcp-endpoint-registry.js +1 -1
  58. package/dist/wirings/mcp/mcp-runner.js +13 -1
  59. package/dist/wirings/rpc/rpc-runner.d.ts +30 -6
  60. package/dist/wirings/rpc/rpc-runner.js +36 -14
  61. package/dist/wirings/rpc/rpc-types.d.ts +6 -1
  62. package/dist/wirings/rpc/wire-addon.d.ts +1 -0
  63. package/dist/wirings/rpc/wire-addon.js +1 -1
  64. package/dist/wirings/scheduler/scheduler-runner.js +1 -0
  65. package/dist/wirings/workflow/graph/graph-validation.d.ts +3 -0
  66. package/dist/wirings/workflow/graph/graph-validation.js +175 -0
  67. package/dist/wirings/workflow/graph/index.d.ts +1 -0
  68. package/dist/wirings/workflow/graph/index.js +1 -0
  69. package/dist/wirings/workflow/index.d.ts +1 -1
  70. package/dist/wirings/workflow/pikku-workflow-service.d.ts +8 -2
  71. package/dist/wirings/workflow/pikku-workflow-service.js +1 -2
  72. package/dist/wirings/workflow/workflow.types.d.ts +10 -1
  73. package/lcov.info +18891 -0
  74. package/package.json +4 -2
  75. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +0 -5
  76. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +0 -168
  77. package/src/crypto-utils.test.ts +0 -116
  78. package/src/crypto-utils.ts +0 -99
  79. package/src/errors/error-handler.ts +0 -62
  80. package/src/errors/error.test.ts +0 -195
  81. package/src/errors/errors.ts +0 -374
  82. package/src/errors/index.ts +0 -2
  83. package/src/factory-functions.test.ts +0 -109
  84. package/src/function/function-runner.test.ts +0 -536
  85. package/src/function/function-runner.ts +0 -365
  86. package/src/function/functions.types.ts +0 -267
  87. package/src/function/index.ts +0 -5
  88. package/src/handle-error.test.ts +0 -361
  89. package/src/handle-error.ts +0 -65
  90. package/src/index.ts +0 -104
  91. package/src/internal.ts +0 -6
  92. package/src/middleware/auth-apikey.test.ts +0 -363
  93. package/src/middleware/auth-apikey.ts +0 -47
  94. package/src/middleware/auth-bearer.test.ts +0 -450
  95. package/src/middleware/auth-bearer.ts +0 -72
  96. package/src/middleware/auth-cookie.test.ts +0 -528
  97. package/src/middleware/auth-cookie.ts +0 -80
  98. package/src/middleware/cors.test.ts +0 -424
  99. package/src/middleware/cors.ts +0 -104
  100. package/src/middleware/index.ts +0 -5
  101. package/src/middleware/remote-auth.test.ts +0 -488
  102. package/src/middleware/remote-auth.ts +0 -68
  103. package/src/middleware/timeout.ts +0 -15
  104. package/src/middleware-runner.test.ts +0 -418
  105. package/src/middleware-runner.ts +0 -234
  106. package/src/permissions.test.ts +0 -434
  107. package/src/permissions.ts +0 -319
  108. package/src/pikku-request.ts +0 -23
  109. package/src/pikku-response.ts +0 -5
  110. package/src/pikku-state.test.ts +0 -224
  111. package/src/pikku-state.ts +0 -214
  112. package/src/run-tests-script.test.ts +0 -49
  113. package/src/schema.test.ts +0 -249
  114. package/src/schema.ts +0 -151
  115. package/src/services/ai-agent-runner-service.ts +0 -41
  116. package/src/services/ai-run-state-service.ts +0 -20
  117. package/src/services/ai-storage-service.ts +0 -39
  118. package/src/services/content-service.ts +0 -76
  119. package/src/services/deployment-service.ts +0 -22
  120. package/src/services/gateway-service.ts +0 -20
  121. package/src/services/gopass-secrets.ts +0 -98
  122. package/src/services/in-memory-ai-run-state-service.ts +0 -73
  123. package/src/services/in-memory-trigger-service.ts +0 -64
  124. package/src/services/in-memory-workflow-service.test.ts +0 -351
  125. package/src/services/in-memory-workflow-service.ts +0 -395
  126. package/src/services/index.ts +0 -56
  127. package/src/services/jwt-service.ts +0 -30
  128. package/src/services/local-content.ts +0 -120
  129. package/src/services/local-gateway-service.ts +0 -62
  130. package/src/services/local-secrets.test.ts +0 -80
  131. package/src/services/local-secrets.ts +0 -94
  132. package/src/services/local-variables.test.ts +0 -61
  133. package/src/services/local-variables.ts +0 -39
  134. package/src/services/logger-console.test.ts +0 -118
  135. package/src/services/logger-console.ts +0 -98
  136. package/src/services/logger.ts +0 -57
  137. package/src/services/scheduler-service.ts +0 -86
  138. package/src/services/schema-service.ts +0 -33
  139. package/src/services/scoped-secret-service.test.ts +0 -74
  140. package/src/services/scoped-secret-service.ts +0 -41
  141. package/src/services/secret-service.ts +0 -38
  142. package/src/services/trigger-service.ts +0 -17
  143. package/src/services/typed-secret-service.test.ts +0 -93
  144. package/src/services/typed-secret-service.ts +0 -70
  145. package/src/services/typed-variables-service.test.ts +0 -73
  146. package/src/services/typed-variables-service.ts +0 -81
  147. package/src/services/user-session-service.test.ts +0 -113
  148. package/src/services/user-session-service.ts +0 -86
  149. package/src/services/variables-service.ts +0 -11
  150. package/src/services/workflow-service.ts +0 -84
  151. package/src/time-utils.test.ts +0 -56
  152. package/src/time-utils.ts +0 -107
  153. package/src/types/core.types.ts +0 -470
  154. package/src/types/state.types.ts +0 -186
  155. package/src/utils.test.ts +0 -350
  156. package/src/utils.ts +0 -129
  157. package/src/version.test.ts +0 -80
  158. package/src/version.ts +0 -25
  159. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +0 -363
  160. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +0 -238
  161. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +0 -152
  162. package/src/wirings/ai-agent/ai-agent-helpers.ts +0 -76
  163. package/src/wirings/ai-agent/ai-agent-memory.ts +0 -309
  164. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +0 -115
  165. package/src/wirings/ai-agent/ai-agent-model-config.ts +0 -43
  166. package/src/wirings/ai-agent/ai-agent-prepare.ts +0 -522
  167. package/src/wirings/ai-agent/ai-agent-registry.test.ts +0 -37
  168. package/src/wirings/ai-agent/ai-agent-registry.ts +0 -82
  169. package/src/wirings/ai-agent/ai-agent-runner.test.ts +0 -317
  170. package/src/wirings/ai-agent/ai-agent-runner.ts +0 -251
  171. package/src/wirings/ai-agent/ai-agent-stream.test.ts +0 -652
  172. package/src/wirings/ai-agent/ai-agent-stream.ts +0 -1060
  173. package/src/wirings/ai-agent/ai-agent.types.ts +0 -360
  174. package/src/wirings/ai-agent/index.ts +0 -41
  175. package/src/wirings/channel/channel-common.ts +0 -90
  176. package/src/wirings/channel/channel-handler.ts +0 -241
  177. package/src/wirings/channel/channel-middleware-runner.ts +0 -120
  178. package/src/wirings/channel/channel-runner.ts +0 -166
  179. package/src/wirings/channel/channel-store.ts +0 -29
  180. package/src/wirings/channel/channel.types.ts +0 -178
  181. package/src/wirings/channel/define-channel-routes.ts +0 -25
  182. package/src/wirings/channel/eventhub-service.ts +0 -34
  183. package/src/wirings/channel/eventhub-store.ts +0 -13
  184. package/src/wirings/channel/index.ts +0 -24
  185. package/src/wirings/channel/local/index.ts +0 -3
  186. package/src/wirings/channel/local/local-channel-handler.ts +0 -79
  187. package/src/wirings/channel/local/local-channel-runner.test.ts +0 -215
  188. package/src/wirings/channel/local/local-channel-runner.ts +0 -200
  189. package/src/wirings/channel/local/local-eventhub-service.test.ts +0 -95
  190. package/src/wirings/channel/local/local-eventhub-service.ts +0 -101
  191. package/src/wirings/channel/log-channels.ts +0 -20
  192. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +0 -54
  193. package/src/wirings/channel/pikku-abstract-channel-handler.ts +0 -45
  194. package/src/wirings/channel/serverless/index.ts +0 -5
  195. package/src/wirings/channel/serverless/serverless-channel-runner.ts +0 -286
  196. package/src/wirings/cli/channel/cli-channel-runner.ts +0 -136
  197. package/src/wirings/cli/channel/index.ts +0 -1
  198. package/src/wirings/cli/cli-runner.test.ts +0 -403
  199. package/src/wirings/cli/cli-runner.ts +0 -519
  200. package/src/wirings/cli/cli.types.ts +0 -357
  201. package/src/wirings/cli/command-parser.test.ts +0 -445
  202. package/src/wirings/cli/command-parser.ts +0 -504
  203. package/src/wirings/cli/define-cli-commands.ts +0 -24
  204. package/src/wirings/cli/index.ts +0 -19
  205. package/src/wirings/gateway/gateway-runner.test.ts +0 -474
  206. package/src/wirings/gateway/gateway-runner.ts +0 -411
  207. package/src/wirings/gateway/gateway.types.ts +0 -149
  208. package/src/wirings/gateway/index.ts +0 -13
  209. package/src/wirings/http/http-routes.test.ts +0 -322
  210. package/src/wirings/http/http-routes.ts +0 -198
  211. package/src/wirings/http/http-runner.test.ts +0 -144
  212. package/src/wirings/http/http-runner.ts +0 -563
  213. package/src/wirings/http/http.types.ts +0 -378
  214. package/src/wirings/http/index.ts +0 -25
  215. package/src/wirings/http/log-http-routes.ts +0 -22
  216. package/src/wirings/http/pikku-fetch-http-request.test.ts +0 -237
  217. package/src/wirings/http/pikku-fetch-http-request.ts +0 -166
  218. package/src/wirings/http/pikku-fetch-http-response.test.ts +0 -82
  219. package/src/wirings/http/pikku-fetch-http-response.ts +0 -147
  220. package/src/wirings/http/routers/http-router.ts +0 -13
  221. package/src/wirings/http/routers/path-to-regex.test.ts +0 -319
  222. package/src/wirings/http/routers/path-to-regex.ts +0 -126
  223. package/src/wirings/mcp/index.ts +0 -27
  224. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +0 -389
  225. package/src/wirings/mcp/mcp-endpoint-registry.ts +0 -159
  226. package/src/wirings/mcp/mcp-runner.ts +0 -305
  227. package/src/wirings/mcp/mcp.types.ts +0 -216
  228. package/src/wirings/node/index.ts +0 -2
  229. package/src/wirings/node/node.types.ts +0 -23
  230. package/src/wirings/oauth2/index.ts +0 -3
  231. package/src/wirings/oauth2/oauth2-client.test.ts +0 -929
  232. package/src/wirings/oauth2/oauth2-client.ts +0 -335
  233. package/src/wirings/oauth2/oauth2.types.ts +0 -69
  234. package/src/wirings/oauth2/wire-oauth2-credential.ts +0 -23
  235. package/src/wirings/queue/index.ts +0 -31
  236. package/src/wirings/queue/queue-runner.test.ts +0 -710
  237. package/src/wirings/queue/queue-runner.ts +0 -192
  238. package/src/wirings/queue/queue.types.ts +0 -189
  239. package/src/wirings/queue/register-queue-helper.ts +0 -60
  240. package/src/wirings/queue/validate-worker-config.test.ts +0 -108
  241. package/src/wirings/queue/validate-worker-config.ts +0 -116
  242. package/src/wirings/rpc/index.ts +0 -4
  243. package/src/wirings/rpc/rpc-runner.ts +0 -439
  244. package/src/wirings/rpc/rpc-types.ts +0 -51
  245. package/src/wirings/rpc/wire-addon.ts +0 -20
  246. package/src/wirings/scheduler/index.ts +0 -11
  247. package/src/wirings/scheduler/log-schedulers.ts +0 -20
  248. package/src/wirings/scheduler/scheduler-runner.test.ts +0 -660
  249. package/src/wirings/scheduler/scheduler-runner.ts +0 -132
  250. package/src/wirings/scheduler/scheduler.types.ts +0 -53
  251. package/src/wirings/secret/index.ts +0 -9
  252. package/src/wirings/secret/secret.types.ts +0 -32
  253. package/src/wirings/secret/validate-secret-definitions.test.ts +0 -140
  254. package/src/wirings/secret/validate-secret-definitions.ts +0 -82
  255. package/src/wirings/trigger/index.ts +0 -10
  256. package/src/wirings/trigger/pikku-trigger-service.ts +0 -112
  257. package/src/wirings/trigger/trigger-runner.test.ts +0 -79
  258. package/src/wirings/trigger/trigger-runner.ts +0 -135
  259. package/src/wirings/trigger/trigger.types.ts +0 -178
  260. package/src/wirings/variable/index.ts +0 -8
  261. package/src/wirings/variable/validate-variable-definitions.test.ts +0 -91
  262. package/src/wirings/variable/validate-variable-definitions.ts +0 -69
  263. package/src/wirings/variable/variable.types.ts +0 -22
  264. package/src/wirings/workflow/dsl/index.ts +0 -31
  265. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +0 -320
  266. package/src/wirings/workflow/dsl/workflow-runner.ts +0 -28
  267. package/src/wirings/workflow/graph/graph-node.ts +0 -222
  268. package/src/wirings/workflow/graph/graph-runner.test.ts +0 -487
  269. package/src/wirings/workflow/graph/graph-runner.ts +0 -816
  270. package/src/wirings/workflow/graph/index.ts +0 -14
  271. package/src/wirings/workflow/graph/template.test.ts +0 -49
  272. package/src/wirings/workflow/graph/template.ts +0 -42
  273. package/src/wirings/workflow/graph/wire-workflow-graph.ts +0 -29
  274. package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -104
  275. package/src/wirings/workflow/index.ts +0 -81
  276. package/src/wirings/workflow/pikku-workflow-service.test.ts +0 -200
  277. package/src/wirings/workflow/pikku-workflow-service.ts +0 -1165
  278. package/src/wirings/workflow/workflow-helpers.test.ts +0 -129
  279. package/src/wirings/workflow/workflow-helpers.ts +0 -79
  280. package/src/wirings/workflow/workflow.types.ts +0 -266
  281. package/tsconfig.json +0 -14
  282. package/tsconfig.tsbuildinfo +0 -1
@@ -1,305 +0,0 @@
1
- import type { PikkuWire } from '../../types/core.types.js'
2
- import type {
3
- CoreMCPResource,
4
- CoreMCPPrompt,
5
- JsonRpcRequest,
6
- JsonRpcResponse,
7
- JsonRpcErrorResponse,
8
- PikkuMCP,
9
- } from './mcp.types.js'
10
- import type {
11
- CorePikkuFunctionConfig,
12
- CorePikkuFunctionSessionless,
13
- } from '../../function/functions.types.js'
14
- import { getErrorResponse } from '../../errors/error-handler.js'
15
- import { closeWireServices } from '../../utils.js'
16
- import {
17
- pikkuState,
18
- getSingletonServices,
19
- getCreateWireServices,
20
- } from '../../pikku-state.js'
21
- import { addFunction, runPikkuFunc } from '../../function/function-runner.js'
22
- import {
23
- BadRequestError,
24
- NotFoundError,
25
- PikkuMissingMetaError,
26
- } from '../../errors/errors.js'
27
- import {
28
- PikkuSessionService,
29
- createMiddlewareSessionWireProps,
30
- } from '../../services/user-session-service.js'
31
-
32
- export class MCPError extends Error {
33
- constructor(public readonly error: JsonRpcErrorResponse) {
34
- super(error?.message || 'MCP Error')
35
- this.name = 'MCPError'
36
- this.stack = new Error().stack
37
- }
38
- }
39
-
40
- export type RunMCPEndpointParams<Tools extends string = any> = {
41
- mcp?: PikkuMCP<Tools>
42
- }
43
-
44
- export type JsonRpcError = {
45
- code: number
46
- message: string
47
- data?: any
48
- }
49
-
50
- export const wireMCPResource = <
51
- PikkuFunctionConfig extends CorePikkuFunctionConfig<
52
- CorePikkuFunctionSessionless<any, any>
53
- > = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>,
54
- >(
55
- mcpResource: CoreMCPResource<PikkuFunctionConfig>
56
- ) => {
57
- const resourcesMeta = pikkuState(null, 'mcp', 'resourcesMeta')
58
- const mcpResourceMeta = resourcesMeta[mcpResource.uri]
59
- if (!mcpResourceMeta) {
60
- throw new PikkuMissingMetaError(
61
- `Missing generated metadata for MCP resource '${mcpResource.uri}'`
62
- )
63
- }
64
- addFunction(mcpResourceMeta.pikkuFuncId, mcpResource.func as any)
65
- const resources = pikkuState(null, 'mcp', 'resources')
66
- if (resources.has(mcpResource.uri)) {
67
- throw new Error(`MCP resource already exists: ${mcpResource.uri}`)
68
- }
69
- resources.set(mcpResource.uri, mcpResource)
70
- }
71
-
72
- export const wireMCPPrompt = <
73
- PikkuFunctionConfig extends CorePikkuFunctionConfig<
74
- CorePikkuFunctionSessionless<any, any>
75
- > = CorePikkuFunctionConfig<CorePikkuFunctionSessionless<any, any>>,
76
- >(
77
- mcpPrompt: CoreMCPPrompt<PikkuFunctionConfig>
78
- ) => {
79
- const promptsMeta = pikkuState(null, 'mcp', 'promptsMeta')
80
- const mcpPromptMeta = promptsMeta[mcpPrompt.name]
81
- if (!mcpPromptMeta) {
82
- throw new PikkuMissingMetaError(
83
- `Missing generated metadata for MCP prompt '${mcpPrompt.name}'`
84
- )
85
- }
86
- addFunction(mcpPromptMeta.pikkuFuncId, mcpPrompt.func as any)
87
- const prompts = pikkuState(null, 'mcp', 'prompts')
88
- if (prompts.has(mcpPrompt.name)) {
89
- throw new Error(`MCP prompt already exists: ${mcpPrompt.name}`)
90
- }
91
- prompts.set(mcpPrompt.name, mcpPrompt)
92
- }
93
-
94
- export async function runMCPResource(
95
- request: JsonRpcRequest,
96
- params: RunMCPEndpointParams,
97
- uri: string
98
- ) {
99
- let endpoint: CoreMCPResource | undefined
100
- let pikkuFuncId: string | undefined
101
- let extractedParams: Record<string, string> = {}
102
-
103
- const metas = pikkuState(null, 'mcp', 'resourcesMeta')
104
- const endpoints = pikkuState(null, 'mcp', 'resources')
105
-
106
- if (endpoints.has(uri)) {
107
- endpoint = endpoints.get(uri)
108
- pikkuFuncId = metas[uri]?.pikkuFuncId
109
- } else {
110
- for (const [uriTemplate, value] of endpoints.entries()) {
111
- // Extract parameter names from the template
112
- const paramNames = Array.from(
113
- uriTemplate.matchAll(/\{([^}]+)\}/g),
114
- (m) => m[1]
115
- )
116
-
117
- // Create regex pattern to match and capture parameter values
118
- const regexPattern = uriTemplate.replace(/\{[^}]+\}/g, '([^/]+)')
119
- const regex = new RegExp(`^${regexPattern}$`)
120
- const match = uri.match(regex)
121
-
122
- if (match) {
123
- endpoint = value
124
- pikkuFuncId = metas[uriTemplate]?.pikkuFuncId
125
-
126
- // Extract parameter values and create params object
127
- for (let i = 0; i < paramNames.length; i++) {
128
- extractedParams[paramNames[i]!] = match[i + 1]! // match[0] is the full match
129
- }
130
- break
131
- }
132
- }
133
- }
134
-
135
- return await runMCPPikkuFunc(
136
- {
137
- ...request,
138
- params: { ...request.params, ...extractedParams },
139
- },
140
- 'resource',
141
- uri,
142
- endpoint,
143
- pikkuFuncId,
144
- { ...params, mcp: { ...params.mcp, uri } } as RunMCPEndpointParams<
145
- keyof CoreMCPResource
146
- >
147
- )
148
- }
149
-
150
- export async function runMCPTool(
151
- request: JsonRpcRequest,
152
- params: RunMCPEndpointParams,
153
- name: string
154
- ) {
155
- const meta = pikkuState(null, 'mcp', 'toolsMeta')[name]
156
- return await runMCPPikkuFunc(
157
- request,
158
- 'tool',
159
- name,
160
- undefined,
161
- meta?.pikkuFuncId,
162
- params
163
- )
164
- }
165
-
166
- export async function runMCPPrompt(
167
- request: JsonRpcRequest,
168
- params: RunMCPEndpointParams,
169
- name: string
170
- ) {
171
- const endpoint = pikkuState(null, 'mcp', 'prompts').get(name)
172
- const meta = pikkuState(null, 'mcp', 'promptsMeta')[name]
173
- return await runMCPPikkuFunc(
174
- request,
175
- 'prompt',
176
- name,
177
- endpoint,
178
- meta?.pikkuFuncId,
179
- params
180
- )
181
- }
182
-
183
- /**
184
- * JSON-RPC 2.0 compatible MCP endpoint runner
185
- */
186
- async function runMCPPikkuFunc(
187
- request: JsonRpcRequest,
188
- type: 'resource' | 'tool' | 'prompt',
189
- name: string,
190
- mcp: CoreMCPResource | CoreMCPPrompt | undefined,
191
- pikkuFuncId: string | undefined,
192
- { mcp: mcpWire }: RunMCPEndpointParams
193
- ): Promise<JsonRpcResponse> {
194
- const singletonServices = getSingletonServices()
195
- const createWireServices = getCreateWireServices()
196
- let wireServices: any
197
-
198
- try {
199
- if (request.jsonrpc !== '2.0') {
200
- throw new BadRequestError(
201
- 'Invalid JSON-RPC version, only supoorted version is 2.0'
202
- )
203
- }
204
-
205
- if (mcp === undefined && type !== 'tool') {
206
- throw new NotFoundError(
207
- `MCP '${type}' registration not found for '${name}'`
208
- )
209
- }
210
-
211
- if (!pikkuFuncId) {
212
- throw new NotFoundError(
213
- `MCP '${type}' PikkuFunction Mapping not found for '${name}'`
214
- )
215
- }
216
-
217
- singletonServices.logger.debug(`Running MCP ${type}: ${name}`)
218
-
219
- const mcpSessionService = new PikkuSessionService()
220
- const wire: PikkuWire = {
221
- mcp: mcpWire,
222
- ...createMiddlewareSessionWireProps(mcpSessionService),
223
- }
224
-
225
- let meta: any
226
- if (type === 'resource') {
227
- meta = pikkuState(null, 'mcp', 'resourcesMeta')[name]
228
- } else if (type === 'tool') {
229
- meta = pikkuState(null, 'mcp', 'toolsMeta')[name]
230
- } else if (type === 'prompt') {
231
- meta = pikkuState(null, 'mcp', 'promptsMeta')[name]
232
- }
233
-
234
- let result = await runPikkuFunc('mcp', `${type}:${name}`, pikkuFuncId, {
235
- singletonServices,
236
- createWireServices,
237
- data: () => request.params,
238
- inheritedMiddleware: meta?.middleware,
239
- wireMiddleware: mcp?.middleware,
240
- inheritedPermissions: meta?.permissions,
241
- wirePermissions: mcp?.permissions,
242
- tags: mcp?.tags,
243
- wire,
244
- sessionService: mcpSessionService,
245
- })
246
-
247
- if (type === 'tool' && meta?.outputSchema !== 'MCPToolResponse') {
248
- result = [{ type: 'text', text: JSON.stringify(result) }]
249
- }
250
-
251
- return {
252
- id: request.id,
253
- result,
254
- }
255
- } catch (e: any) {
256
- singletonServices.logger.error(
257
- `Error running MCP ${type} '${name}':`,
258
- e.constructor
259
- )
260
- const errorResponse = getErrorResponse(e)
261
- if (errorResponse?.mcpCode) {
262
- throw new MCPError({
263
- id: request.id,
264
- code: errorResponse.mcpCode,
265
- message: errorResponse.message,
266
- })
267
- } else {
268
- if (errorResponse) {
269
- singletonServices.logger.warn(
270
- `Got error without a mapping: ${errorResponse.message}`
271
- )
272
- }
273
- throw new MCPError({
274
- id: request.id,
275
- code: -32603,
276
- message: 'Internal error',
277
- data: { message: e.message, stack: e.stack },
278
- })
279
- }
280
- } finally {
281
- if (wireServices) {
282
- await closeWireServices(singletonServices.logger, wireServices)
283
- }
284
- }
285
- }
286
-
287
- export const getMCPResources = () => {
288
- return pikkuState(null, 'mcp', 'resources')
289
- }
290
-
291
- export const getMCPResourcesMeta = () => {
292
- return pikkuState(null, 'mcp', 'resourcesMeta')
293
- }
294
-
295
- export const getMCPToolsMeta = () => {
296
- return pikkuState(null, 'mcp', 'toolsMeta')
297
- }
298
-
299
- export const getMCPPrompts = () => {
300
- return pikkuState(null, 'mcp', 'prompts')
301
- }
302
-
303
- export const getMCPPromptsMeta = () => {
304
- return pikkuState(null, 'mcp', 'promptsMeta')
305
- }
@@ -1,216 +0,0 @@
1
- import type {
2
- CorePermissionGroup,
3
- CorePikkuFunctionConfig,
4
- CorePikkuFunctionSessionless,
5
- CorePikkuPermission,
6
- } from '../../function/functions.types.js'
7
- import type {
8
- CorePikkuMiddleware,
9
- MiddlewareMetadata,
10
- PermissionMetadata,
11
- } from '../../types/core.types.js'
12
-
13
- /**
14
- * Extract URI parameters from MCP resource URI template.
15
- * E.g., "user/{userId}/post/{postId}" => "userId" | "postId"
16
- */
17
- type ExtractMCPURIParams<S extends string> =
18
- S extends `${string}{${infer Param}}/${infer Rest}`
19
- ? Param | ExtractMCPURIParams<Rest>
20
- : S extends `${string}{${infer Param}}`
21
- ? Param
22
- : never
23
-
24
- /**
25
- * Type-level assertion that MCP resource URI parameters are present in the input type.
26
- * This ensures compile-time safety for URI parameter validation.
27
- */
28
- export type AssertMCPResourceURIParams<In, URI extends string> =
29
- ExtractMCPURIParams<URI> extends keyof In
30
- ? unknown
31
- : [
32
- 'Error: MCP Resource URI parameters',
33
- ExtractMCPURIParams<URI>,
34
- 'not in input type',
35
- keyof In,
36
- ]
37
-
38
- export type PikkuMCP<Tools extends string = any> = {
39
- // elicitInput: <Input>(message: string) => Promise<{ action: string, content: Input }>
40
- uri?: string
41
- sendResourceUpdated: (uri: string) => void
42
- enableResources: (resources: Record<string, boolean>) => Promise<boolean>
43
- enablePrompts: (prompts: Record<string, boolean>) => Promise<boolean>
44
- enableTools: (tools: Record<Tools, boolean>) => Promise<boolean>
45
- }
46
-
47
- /**
48
- * Represents metadata for MCP resources, including name, description, and documentation.
49
- */
50
- export type MCPResourceMeta = Record<
51
- string,
52
- Omit<CoreMCPResource, 'func' | 'middleware' | 'permissions'> & {
53
- pikkuFuncId: string
54
- inputSchema: string | null
55
- outputSchema: string | null
56
- middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
57
- permissions?: PermissionMetadata[] // Pre-resolved permission chain (tag + explicit)
58
- }
59
- >
60
-
61
- /**
62
- * Represents metadata for MCP tools, including name, description, and documentation.
63
- */
64
- export type MCPToolMeta = Record<
65
- string,
66
- Omit<CoreMCPTool, 'func' | 'middleware' | 'permissions'> & {
67
- pikkuFuncId: string
68
- inputSchema: string | null
69
- outputSchema: string | null
70
- middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
71
- permissions?: PermissionMetadata[] // Pre-resolved permission chain (tag + explicit)
72
- }
73
- >
74
-
75
- /**
76
- * Represents metadata for MCP prompts, including name, description, and arguments.
77
- */
78
- export type MCPPromptMeta = Record<
79
- string,
80
- Omit<CoreMCPPrompt, 'func' | 'middleware' | 'permissions'> & {
81
- pikkuFuncId: string
82
- inputSchema: string | null
83
- outputSchema: string | null
84
- arguments: Array<{
85
- name: string
86
- description: string
87
- required: boolean
88
- }>
89
- middleware?: MiddlewareMetadata[] // Pre-resolved middleware chain (tag + explicit)
90
- permissions?: PermissionMetadata[] // Pre-resolved permission chain (tag + explicit)
91
- }
92
- >
93
-
94
- /**
95
- * Represents an MCP resource with specific properties.
96
- */
97
- export type CoreMCPResource<
98
- PikkuFunctionConfig = CorePikkuFunctionConfig<
99
- CorePikkuFunctionSessionless<any, any>
100
- >,
101
- PikkuPermission = CorePikkuPermission<any, any>,
102
- PikkuMiddleware = CorePikkuMiddleware<any>,
103
- > = {
104
- uri: string
105
- title: string
106
- description: string
107
- summary?: string
108
- errors?: string[]
109
- mimeType?: string
110
- size?: number
111
- streaming?: boolean
112
- func: PikkuFunctionConfig
113
- tags?: string[]
114
- middleware?: PikkuMiddleware[]
115
- permissions?: CorePermissionGroup<PikkuPermission>
116
- }
117
-
118
- /**
119
- * Represents an MCP tool with specific properties.
120
- */
121
- export type CoreMCPTool<
122
- PikkuFunctionConfig = CorePikkuFunctionConfig<
123
- CorePikkuFunctionSessionless<any, any>
124
- >,
125
- PikkuPermission = CorePikkuPermission<any, any>,
126
- PikkuMiddleware = CorePikkuMiddleware<any>,
127
- > = {
128
- name: string
129
- title?: string
130
- description: string
131
- summary?: string
132
- errors?: string[]
133
- func: PikkuFunctionConfig
134
- tags?: string[]
135
- streaming?: boolean
136
- middleware?: PikkuMiddleware[]
137
- permissions?: CorePermissionGroup<PikkuPermission>
138
- }
139
-
140
- /**
141
- * Represents an MCP prompt with specific properties.
142
- */
143
- export type CoreMCPPrompt<
144
- PikkuFunctionConfig = CorePikkuFunctionConfig<
145
- CorePikkuFunctionSessionless<any, MCPPromptResponse>
146
- >,
147
- PikkuPermission = CorePikkuPermission<any, any>,
148
- PikkuMiddleware = CorePikkuMiddleware<any>,
149
- > = {
150
- name: string
151
- description: string
152
- summary?: string
153
- errors?: string[]
154
- func: PikkuFunctionConfig
155
- tags?: string[]
156
- middleware?: PikkuMiddleware[]
157
- permissions?: CorePermissionGroup<PikkuPermission>
158
- }
159
-
160
- export type JsonRpcRequest = {
161
- jsonrpc: string
162
- id?: string | number | null
163
- params?: any
164
- }
165
-
166
- export type JsonRpcResponse = {
167
- id?: string | number | null
168
- result?: any
169
- }
170
-
171
- export type JsonRpcErrorResponse = {
172
- id?: string | number | null
173
- code: number
174
- message: string
175
- data?: any
176
- }
177
-
178
- /**
179
- * Represents a message in an MCP prompt response
180
- */
181
- export type MCPPromptMessage = {
182
- role: 'user' | 'assistant' | 'system'
183
- content: {
184
- type: 'text' | 'image'
185
- text: string
186
- data?: string // for image content
187
- }
188
- }
189
-
190
- /**
191
- * Standard response type for MCP prompts - array of messages
192
- */
193
- export type MCPPromptResponse = MCPPromptMessage[]
194
-
195
- export type MCPResourceMessage = {
196
- uri: string
197
- text: string
198
- }
199
-
200
- export type MCPResourceResponse = MCPResourceMessage[]
201
-
202
- /**
203
- * Standard response type for MCP prompts - array of messages
204
- */
205
-
206
- export type MCPToolMessage =
207
- | {
208
- type: 'text'
209
- text: string
210
- }
211
- | {
212
- type: 'image'
213
- data: string // base64 encoded image data
214
- }
215
-
216
- export type MCPToolResponse = MCPToolMessage[]
@@ -1,2 +0,0 @@
1
- export type { CoreNodeConfig, NodeType } from './node.types.js'
2
- export type { NodesMeta } from './node.types.js'
@@ -1,23 +0,0 @@
1
- export type NodeType = 'trigger' | 'action' | 'end'
2
-
3
- export type CoreNodeConfig = {
4
- displayName: string
5
- category: string
6
- type: NodeType
7
- errorOutput?: boolean
8
- }
9
-
10
- export type NodeMeta = {
11
- name: string
12
- displayName: string
13
- category: string
14
- type: NodeType
15
- rpc: string
16
- description?: string
17
- errorOutput: boolean
18
- inputSchemaName: string | null
19
- outputSchemaName: string | null
20
- tags?: string[]
21
- }
22
-
23
- export type NodesMeta = Record<string, NodeMeta>
@@ -1,3 +0,0 @@
1
- export { OAuth2Client } from './oauth2-client.js'
2
- export { wireOAuth2Credential } from './wire-oauth2-credential.js'
3
- export type { OAuth2AppCredential, OAuth2Token } from './oauth2.types.js'