@pikku/core 0.7.8 → 0.8.1

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 (165) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/errors/error-handler.d.ts +2 -0
  3. package/dist/errors/errors.js +2 -0
  4. package/dist/{channel → events/channel}/channel-handler.d.ts +1 -1
  5. package/dist/{channel → events/channel}/channel-handler.js +4 -2
  6. package/dist/{channel → events/channel}/channel-runner.d.ts +1 -1
  7. package/dist/{channel → events/channel}/channel-runner.js +4 -4
  8. package/dist/{channel → events/channel}/channel-store.d.ts +1 -1
  9. package/dist/{channel → events/channel}/channel.types.d.ts +3 -3
  10. package/dist/{channel → events/channel}/local/local-channel-runner.js +6 -6
  11. package/dist/{channel → events/channel}/log-channels.d.ts +1 -1
  12. package/dist/{channel → events/channel}/log-channels.js +1 -1
  13. package/dist/{channel → events/channel}/serverless/serverless-channel-runner.js +7 -7
  14. package/dist/{http → events/http}/http-runner.d.ts +1 -1
  15. package/dist/{http → events/http}/http-runner.js +8 -8
  16. package/dist/{http → events/http}/http.types.d.ts +3 -3
  17. package/dist/{http → events/http}/index.d.ts +0 -1
  18. package/dist/{http → events/http}/index.js +0 -1
  19. package/dist/{http → events/http}/log-http-routes.d.ts +1 -1
  20. package/dist/{http → events/http}/log-http-routes.js +1 -1
  21. package/dist/{http → events/http}/pikku-fetch-http-request.js +1 -1
  22. package/dist/events/mcp/index.d.ts +3 -0
  23. package/dist/events/mcp/index.js +3 -0
  24. package/dist/events/mcp/mcp-endpoint-registry.d.ts +48 -0
  25. package/dist/events/mcp/mcp-endpoint-registry.js +122 -0
  26. package/dist/events/mcp/mcp-runner.d.ts +30 -0
  27. package/dist/events/mcp/mcp-runner.js +192 -0
  28. package/dist/events/mcp/mcp.types.d.ts +117 -0
  29. package/dist/events/queue/index.d.ts +6 -0
  30. package/dist/events/queue/index.js +6 -0
  31. package/dist/events/queue/queue-runner.d.ts +24 -0
  32. package/dist/events/queue/queue-runner.js +83 -0
  33. package/dist/events/queue/queue.types.d.ts +154 -0
  34. package/dist/events/queue/register-queue-helper.d.ts +17 -0
  35. package/dist/events/queue/register-queue-helper.js +34 -0
  36. package/dist/events/queue/validate-worker-config.d.ts +41 -0
  37. package/dist/events/queue/validate-worker-config.js +50 -0
  38. package/dist/events/rpc/index.d.ts +2 -0
  39. package/dist/events/rpc/rpc-runner.d.ts +25 -0
  40. package/dist/{rpc → events/rpc}/rpc-runner.js +2 -2
  41. package/dist/events/scheduler/index.d.ts +3 -0
  42. package/dist/events/scheduler/index.js +3 -0
  43. package/dist/{scheduler → events/scheduler}/log-schedulers.d.ts +1 -1
  44. package/dist/{scheduler → events/scheduler}/log-schedulers.js +1 -1
  45. package/dist/{scheduler → events/scheduler}/scheduler-runner.d.ts +4 -3
  46. package/dist/{scheduler → events/scheduler}/scheduler-runner.js +7 -4
  47. package/dist/{scheduler → events/scheduler}/scheduler.types.d.ts +3 -4
  48. package/dist/function/function-runner.d.ts +3 -2
  49. package/dist/function/function-runner.js +16 -40
  50. package/dist/function/functions.types.d.ts +3 -2
  51. package/dist/handle-error.d.ts +2 -2
  52. package/dist/handle-error.js +1 -1
  53. package/dist/index.d.ts +15 -7
  54. package/dist/index.js +15 -7
  55. package/dist/middleware-runner.d.ts +4 -4
  56. package/dist/middleware-runner.js +3 -1
  57. package/dist/pikku-state.d.ts +18 -4
  58. package/dist/pikku-state.js +12 -0
  59. package/dist/services/index.d.ts +2 -2
  60. package/dist/services/index.js +2 -2
  61. package/dist/services/jwt-service.d.ts +1 -1
  62. package/dist/services/user-session-service.d.ts +1 -1
  63. package/dist/types/core.types.d.ts +17 -14
  64. package/dist/types/core.types.js +3 -1
  65. package/lcov.info +3947 -3184
  66. package/package.json +9 -7
  67. package/src/errors/error-handler.ts +2 -1
  68. package/src/errors/errors.ts +2 -0
  69. package/src/{channel → events/channel}/channel-handler.ts +7 -3
  70. package/src/{channel → events/channel}/channel-runner.ts +5 -5
  71. package/src/{channel → events/channel}/channel-store.ts +1 -1
  72. package/src/{channel → events/channel}/channel.types.ts +3 -3
  73. package/src/{channel → events/channel}/local/local-channel-runner.test.ts +1 -1
  74. package/src/{channel → events/channel}/local/local-channel-runner.ts +7 -7
  75. package/src/{channel → events/channel}/local/local-eventhub-service.test.ts +1 -1
  76. package/src/{channel → events/channel}/log-channels.ts +2 -2
  77. package/src/{channel → events/channel}/serverless/serverless-channel-runner.ts +8 -8
  78. package/src/{http → events/http}/http-runner.test.ts +6 -7
  79. package/src/{http → events/http}/http-runner.ts +9 -9
  80. package/src/{http → events/http}/http.types.ts +3 -3
  81. package/src/{http → events/http}/index.ts +0 -1
  82. package/src/{http → events/http}/log-http-routes.ts +2 -2
  83. package/src/{http → events/http}/pikku-fetch-http-request.ts +1 -1
  84. package/src/events/mcp/index.ts +3 -0
  85. package/src/events/mcp/mcp-endpoint-registry.test.d.ts +1 -0
  86. package/src/events/mcp/mcp-endpoint-registry.test.ts +389 -0
  87. package/src/events/mcp/mcp-endpoint-registry.ts +165 -0
  88. package/src/events/mcp/mcp-runner.ts +321 -0
  89. package/src/events/mcp/mcp.types.ts +150 -0
  90. package/src/events/queue/index.ts +18 -0
  91. package/src/events/queue/queue-runner.ts +125 -0
  92. package/src/events/queue/queue.types.ts +176 -0
  93. package/src/events/queue/register-queue-helper.ts +62 -0
  94. package/src/events/queue/validate-worker-config.ts +113 -0
  95. package/src/events/rpc/index.ts +2 -0
  96. package/src/{rpc → events/rpc}/rpc-runner.ts +3 -3
  97. package/src/events/scheduler/index.ts +9 -0
  98. package/src/{scheduler → events/scheduler}/log-schedulers.ts +2 -2
  99. package/src/{scheduler → events/scheduler}/scheduler-runner.ts +11 -10
  100. package/src/{scheduler → events/scheduler}/scheduler.types.ts +2 -4
  101. package/src/function/function-runner.ts +38 -65
  102. package/src/function/functions.types.ts +3 -1
  103. package/src/handle-error.ts +2 -2
  104. package/src/index.ts +28 -7
  105. package/src/middleware/auth-apikey.ts +1 -1
  106. package/src/middleware/auth-bearer.ts +1 -1
  107. package/src/middleware/auth-cookie.ts +1 -1
  108. package/src/middleware-runner.ts +14 -7
  109. package/src/pikku-state.ts +40 -4
  110. package/src/services/index.ts +2 -2
  111. package/src/services/jwt-service.ts +1 -1
  112. package/src/services/user-session-service.ts +1 -1
  113. package/src/types/core.types.ts +24 -13
  114. package/tsconfig.json +3 -2
  115. package/tsconfig.tsbuildinfo +1 -1
  116. package/dist/rpc/index.d.ts +0 -2
  117. package/dist/rpc/rpc-runner.d.ts +0 -27
  118. package/dist/scheduler/index.d.ts +0 -3
  119. package/dist/scheduler/index.js +0 -3
  120. package/src/http/incomingmessage-to-request-convertor.ts +0 -0
  121. package/src/rpc/index.ts +0 -2
  122. package/src/scheduler/index.ts +0 -5
  123. /package/dist/{channel → events/channel}/channel-store.js +0 -0
  124. /package/dist/{channel → events/channel}/channel.types.js +0 -0
  125. /package/dist/{channel → events/channel}/eventhub-service.d.ts +0 -0
  126. /package/dist/{channel → events/channel}/eventhub-service.js +0 -0
  127. /package/dist/{channel → events/channel}/eventhub-store.d.ts +0 -0
  128. /package/dist/{channel → events/channel}/eventhub-store.js +0 -0
  129. /package/dist/{channel → events/channel}/index.d.ts +0 -0
  130. /package/dist/{channel → events/channel}/index.js +0 -0
  131. /package/dist/{channel → events/channel}/local/index.d.ts +0 -0
  132. /package/dist/{channel → events/channel}/local/index.js +0 -0
  133. /package/dist/{channel → events/channel}/local/local-channel-handler.d.ts +0 -0
  134. /package/dist/{channel → events/channel}/local/local-channel-handler.js +0 -0
  135. /package/dist/{channel → events/channel}/local/local-channel-runner.d.ts +0 -0
  136. /package/dist/{channel → events/channel}/local/local-eventhub-service.d.ts +0 -0
  137. /package/dist/{channel → events/channel}/local/local-eventhub-service.js +0 -0
  138. /package/dist/{channel → events/channel}/pikku-abstract-channel-handler.d.ts +0 -0
  139. /package/dist/{channel → events/channel}/pikku-abstract-channel-handler.js +0 -0
  140. /package/dist/{channel → events/channel}/serverless/index.d.ts +0 -0
  141. /package/dist/{channel → events/channel}/serverless/index.js +0 -0
  142. /package/dist/{channel → events/channel}/serverless/serverless-channel-runner.d.ts +0 -0
  143. /package/dist/{http → events/http}/http.types.js +0 -0
  144. /package/dist/{http → events/http}/pikku-fetch-http-request.d.ts +0 -0
  145. /package/dist/{http → events/http}/pikku-fetch-http-response.d.ts +0 -0
  146. /package/dist/{http → events/http}/pikku-fetch-http-response.js +0 -0
  147. /package/dist/{http/incomingmessage-to-request-convertor.d.ts → events/mcp/mcp.types.js} +0 -0
  148. /package/dist/{http/incomingmessage-to-request-convertor.js → events/queue/queue.types.js} +0 -0
  149. /package/dist/{rpc → events/rpc}/index.js +0 -0
  150. /package/dist/{rpc → events/rpc}/rpc-types.d.ts +0 -0
  151. /package/dist/{rpc → events/rpc}/rpc-types.js +0 -0
  152. /package/dist/{scheduler → events/scheduler}/scheduler.types.js +0 -0
  153. /package/src/{channel → events/channel}/eventhub-service.ts +0 -0
  154. /package/src/{channel → events/channel}/eventhub-store.ts +0 -0
  155. /package/src/{channel → events/channel}/index.ts +0 -0
  156. /package/src/{channel → events/channel}/local/index.ts +0 -0
  157. /package/src/{channel → events/channel}/local/local-channel-handler.ts +0 -0
  158. /package/src/{channel → events/channel}/local/local-eventhub-service.ts +0 -0
  159. /package/src/{channel → events/channel}/pikku-abstract-channel-handler.test.ts +0 -0
  160. /package/src/{channel → events/channel}/pikku-abstract-channel-handler.ts +0 -0
  161. /package/src/{channel → events/channel}/serverless/index.ts +0 -0
  162. /package/src/{http → events/http}/pikku-fetch-http-request.test.ts +0 -0
  163. /package/src/{http → events/http}/pikku-fetch-http-response.test.ts +0 -0
  164. /package/src/{http → events/http}/pikku-fetch-http-response.ts +0 -0
  165. /package/src/{rpc → events/rpc}/rpc-types.ts +0 -0
package/CHANGELOG.md CHANGED
@@ -1,4 +1,29 @@
1
- ## 0.7.0
1
+ ## 0.8.0
2
+
3
+ ## 0.8.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 3261090: refactor: moving mcp endpoints into core
8
+ - 7c592b8: feat: support for required services and improved service configuration
9
+
10
+ This release includes several enhancements to service management and configuration:
11
+
12
+ - Added support for required services configuration
13
+ - Improved service discovery and registration
14
+ - Added typed RPC clients for service communication
15
+ - Updated middleware to run per function
16
+
17
+ - 30a082f: refactor: moving service implementations out of pikku/core since they aren't all edge compatible
18
+
19
+ ### Major Features
20
+
21
+ - **Model Context Protocol (MCP) Support**: Added MCP implementation with resources, tools, and prompts
22
+ - **Queue System**: Added queue support with runners and workers
23
+ - **RPC (Remote Procedure Calls)**: Added typed RPC calls inside functions with local and remote support
24
+ - **Middleware Runner**: Added middleware runner to functions for enhanced request processing
25
+ - **Multiple Bootstrap Files**: Added support for different transport bootstrap files
26
+ - **Middleware Runner**: Run middleware on any function
2
27
 
3
28
  ## 0.7.8
4
29
 
@@ -15,6 +15,7 @@ export declare class PikkuError extends Error {
15
15
  export interface ErrorDetails {
16
16
  status: number;
17
17
  message: string;
18
+ mcpCode?: number;
18
19
  }
19
20
  /**
20
21
  * Adds an error to the API errors map.
@@ -35,4 +36,5 @@ export declare const addErrors: (errors: Array<[error: any, details: ErrorDetail
35
36
  export declare const getErrorResponse: (error: Error) => {
36
37
  status: number;
37
38
  message: string;
39
+ mcpCode?: number;
38
40
  } | undefined;
@@ -10,6 +10,7 @@ export class BadRequestError extends PikkuError {
10
10
  }
11
11
  addError(BadRequestError, {
12
12
  status: 400,
13
+ mcpCode: -32600,
13
14
  message: 'The server cannot or will not process the request due to client error (e.g., malformed request syntax).',
14
15
  });
15
16
  /**
@@ -81,6 +82,7 @@ export class NotFoundError extends PikkuError {
81
82
  */
82
83
  addError(NotFoundError, {
83
84
  status: 404,
85
+ mcpCode: -32601,
84
86
  message: 'The server cannot find the requested resource.',
85
87
  });
86
88
  /**
@@ -1,3 +1,3 @@
1
- import { CoreServices, CoreUserSession } from '../types/core.types.js';
1
+ import { CoreServices, CoreUserSession } from '../../types/core.types.js';
2
2
  import { CoreAPIChannel, PikkuChannelHandler } from './channel.types.js';
3
3
  export declare const processMessageHandlers: (services: CoreServices, session: CoreUserSession | undefined, channelConfig: CoreAPIChannel<any, any>, channelHandler: PikkuChannelHandler) => (rawData: any) => Promise<unknown>;
@@ -1,5 +1,5 @@
1
- import { pikkuState } from '../pikku-state.js';
2
- import { runPikkuFunc } from '../function/function-runner.js';
1
+ import { pikkuState } from '../../pikku-state.js';
2
+ import { runPikkuFunc } from '../../function/function-runner.js';
3
3
  const getRouteMeta = (channelName, routingProperty, routerValue) => {
4
4
  const channelMeta = pikkuState('channel', 'meta')[channelName];
5
5
  if (!channelMeta) {
@@ -46,6 +46,7 @@ export const processMessageHandlers = (services, session, channelConfig, channel
46
46
  }
47
47
  const { pikkuFuncName } = getRouteMeta(channelConfig.name, routingProperty, routerValue);
48
48
  const permissions = typeof onMessage === 'function' ? {} : onMessage.permissions;
49
+ const middleware = typeof onMessage === 'function' ? [] : onMessage.middleware;
49
50
  return await runPikkuFunc(pikkuFuncName, {
50
51
  getAllServices: () => ({
51
52
  ...services,
@@ -54,6 +55,7 @@ export const processMessageHandlers = (services, session, channelConfig, channel
54
55
  data,
55
56
  session,
56
57
  permissions,
58
+ middleware,
57
59
  });
58
60
  };
59
61
  const onMessage = async (rawData) => {
@@ -1,4 +1,4 @@
1
- import { UserSessionService } from '../services/user-session-service.js';
1
+ import { UserSessionService } from '../../services/user-session-service.js';
2
2
  import { ChannelMeta, CoreAPIChannel, RunChannelOptions, RunChannelParams } from './channel.types.js';
3
3
  /**
4
4
  * Adds a channel and registers all functions referenced in it using the
@@ -1,7 +1,7 @@
1
- import { NotFoundError } from '../errors/errors.js';
2
- import { addFunction } from '../function/function-runner.js';
3
- import { pikkuState } from '../pikku-state.js';
4
- import { coerceTopLevelDataFromSchema, validateSchema } from '../schema.js';
1
+ import { NotFoundError } from '../../errors/errors.js';
2
+ import { addFunction } from '../../function/function-runner.js';
3
+ import { pikkuState } from '../../pikku-state.js';
4
+ import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js';
5
5
  import { match } from 'path-to-regexp';
6
6
  /**
7
7
  * Adds a channel and registers all functions referenced in it using the
@@ -1,4 +1,4 @@
1
- import { CoreUserSession } from '../types/core.types.js';
1
+ import { CoreUserSession } from '../../types/core.types.js';
2
2
  export type Channel<ChannelType = unknown, OpeningData = unknown> = {
3
3
  channelId: string;
4
4
  channelName: string;
@@ -1,7 +1,7 @@
1
- import { PikkuError } from '../errors/error-handler.js';
1
+ import { PikkuError } from '../../errors/error-handler.js';
2
2
  import { HTTPFunctionMetaInputTypes, PikkuHTTPRequest, PikkuHTTPResponse } from '../http/http.types.js';
3
- import { APIDocs, CoreSingletonServices, CreateSessionServices, PikkuMiddleware } from '../types/core.types.js';
4
- import { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission } from '../function/functions.types.js';
3
+ import { APIDocs, CoreSingletonServices, CreateSessionServices, PikkuMiddleware } from '../../types/core.types.js';
4
+ import { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission } from '../../function/functions.types.js';
5
5
  export type RunChannelOptions = Partial<{
6
6
  skipUserSession: boolean;
7
7
  respondWith404: boolean;
@@ -1,12 +1,12 @@
1
1
  import { openChannel } from '../channel-runner.js';
2
2
  import { createHTTPInteraction } from '../../http/http-runner.js';
3
- import { closeSessionServices } from '../../utils.js';
3
+ import { closeSessionServices } from '../../../utils.js';
4
4
  import { processMessageHandlers } from '../channel-handler.js';
5
5
  import { PikkuLocalChannelHandler } from './local-channel-handler.js';
6
- import { handleError } from '../../handle-error.js';
7
- import { runMiddleware } from '../../middleware-runner.js';
8
- import { PikkuUserSessionService } from '../../services/user-session-service.js';
9
- import { runPikkuFuncDirectly } from '../../function/function-runner.js';
6
+ import { handleHTTPError } from '../../../handle-error.js';
7
+ import { runMiddleware } from '../../../middleware-runner.js';
8
+ import { PikkuUserSessionService } from '../../../services/user-session-service.js';
9
+ import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
10
10
  import { rpcService } from '../../rpc/rpc-runner.js';
11
11
  export const runLocalChannel = async ({ singletonServices, channelId, request, response, route, createSessionServices, skipUserSession = false, respondWith404 = true, coerceDataFromSchema = true, logWarningsForStatusCodes = [], bubbleErrors = false, }) => {
12
12
  let sessionServices;
@@ -58,7 +58,7 @@ export const runLocalChannel = async ({ singletonServices, channelId, request, r
58
58
  channelHandler.registerOnMessage(processMessageHandlers(allServices, session, channelConfig, channelHandler));
59
59
  }
60
60
  catch (e) {
61
- handleError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
61
+ handleHTTPError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
62
62
  }
63
63
  finally {
64
64
  if (sessionServices) {
@@ -1,4 +1,4 @@
1
- import { Logger } from '../services/index.js';
1
+ import { Logger } from '../../services/index.js';
2
2
  /**
3
3
  * Logs all the loaded channels.
4
4
  * @param logger - A logger for logging information.
@@ -1,4 +1,4 @@
1
- import { pikkuState } from '../pikku-state.js';
1
+ import { pikkuState } from '../../pikku-state.js';
2
2
  /**
3
3
  * Logs all the loaded channels.
4
4
  * @param logger - A logger for logging information.
@@ -1,13 +1,13 @@
1
- import { closeSessionServices } from '../../utils.js';
1
+ import { closeSessionServices } from '../../../utils.js';
2
2
  import { processMessageHandlers } from '../channel-handler.js';
3
3
  import { openChannel } from '../channel-runner.js';
4
4
  import { createHTTPInteraction } from '../../http/http-runner.js';
5
- import { handleError } from '../../handle-error.js';
6
- import { PikkuUserSessionService } from '../../services/user-session-service.js';
7
- import { runMiddleware } from '../../middleware-runner.js';
8
- import { pikkuState } from '../../pikku-state.js';
5
+ import { handleHTTPError } from '../../../handle-error.js';
6
+ import { PikkuUserSessionService } from '../../../services/user-session-service.js';
7
+ import { runMiddleware } from '../../../middleware-runner.js';
8
+ import { pikkuState } from '../../../pikku-state.js';
9
9
  import { PikkuFetchHTTPRequest } from '../../http/pikku-fetch-http-request.js';
10
- import { runPikkuFuncDirectly } from '../../function/function-runner.js';
10
+ import { runPikkuFuncDirectly } from '../../../function/function-runner.js';
11
11
  const getVariablesForChannel = ({ channelId, channelName, channelHandlerFactory, openingData, }) => {
12
12
  const channels = pikkuState('channel', 'channels');
13
13
  const channelConfig = channels[channelName];
@@ -65,7 +65,7 @@ export const runChannelConnect = async ({ singletonServices, channelId, channelO
65
65
  http?.response?.status(101);
66
66
  }
67
67
  catch (e) {
68
- handleError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
68
+ handleHTTPError(e, http, channelId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
69
69
  }
70
70
  finally {
71
71
  if (sessionServices) {
@@ -1,5 +1,5 @@
1
1
  import { CoreHTTPFunctionRoute, RunRouteOptions, RunRouteParams, PikkuHTTP, PikkuHTTPRequest, PikkuHTTPResponse } from './http.types.js';
2
- import { PikkuMiddleware } from '../types/core.types.js';
2
+ import { PikkuMiddleware } from '../../types/core.types.js';
3
3
  import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
4
4
  /**
5
5
  * Registers middleware either globally or for a specific route.
@@ -1,13 +1,13 @@
1
1
  import { match } from 'path-to-regexp';
2
- import { MissingSessionError, NotFoundError } from '../errors/errors.js';
3
- import { closeSessionServices, createWeakUID, isSerializable, } from '../utils.js';
4
- import { PikkuUserSessionService, } from '../services/user-session-service.js';
5
- import { runMiddleware } from '../middleware-runner.js';
6
- import { handleError } from '../handle-error.js';
7
- import { pikkuState } from '../pikku-state.js';
2
+ import { MissingSessionError, NotFoundError } from '../../errors/errors.js';
3
+ import { closeSessionServices, createWeakUID, isSerializable, } from '../../utils.js';
4
+ import { PikkuUserSessionService, } from '../../services/user-session-service.js';
5
+ import { runMiddleware } from '../../middleware-runner.js';
6
+ import { handleHTTPError } from '../../handle-error.js';
7
+ import { pikkuState } from '../../pikku-state.js';
8
8
  import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
9
9
  import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
10
- import { addFunction, runPikkuFunc } from '../function/function-runner.js';
10
+ import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
11
11
  import { rpcService } from '../rpc/rpc-runner.js';
12
12
  /**
13
13
  * Registers middleware either globally or for a specific route.
@@ -342,7 +342,7 @@ export const fetchData = async (request, response, { singletonServices, createSe
342
342
  }
343
343
  catch (e) {
344
344
  // Handle errors and, depending on configuration, bubble them up or respond with an error
345
- handleError(e, http, requestId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
345
+ handleHTTPError(e, http, requestId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors);
346
346
  }
347
347
  finally {
348
348
  // Clean up any session-specific services created during processing
@@ -1,7 +1,7 @@
1
1
  import type { SerializeOptions } from 'cookie';
2
- import type { PikkuError } from '../errors/error-handler.js';
3
- import type { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '../types/core.types.js';
4
- import type { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission, CorePermissionGroup } from '../function/functions.types.js';
2
+ import type { PikkuError } from '../../errors/error-handler.js';
3
+ import type { APIDocs, CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices, PikkuMiddleware } from '../../types/core.types.js';
4
+ import type { CoreAPIFunction, CoreAPIFunctionSessionless, CoreAPIPermission, CorePermissionGroup } from '../../function/functions.types.js';
5
5
  type ExtractRouteParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
6
6
  export type AssertRouteParams<In, Route extends string> = ExtractRouteParams<Route> extends keyof In ? unknown : ['Error: Route parameters', ExtractRouteParams<Route>, 'not in', keyof In];
7
7
  export type RunRouteOptions = Partial<{
@@ -1,6 +1,5 @@
1
1
  export * from './pikku-fetch-http-request.js';
2
2
  export * from './pikku-fetch-http-response.js';
3
- export * from './incomingmessage-to-request-convertor.js';
4
3
  export * from './log-http-routes.js';
5
4
  export { fetch, fetchData, addHTTPRoute } from './http-runner.js';
6
5
  export type * from './http.types.js';
@@ -1,5 +1,4 @@
1
1
  export * from './pikku-fetch-http-request.js';
2
2
  export * from './pikku-fetch-http-response.js';
3
- export * from './incomingmessage-to-request-convertor.js';
4
3
  export * from './log-http-routes.js';
5
4
  export { fetch, fetchData, addHTTPRoute } from './http-runner.js';
@@ -1,4 +1,4 @@
1
- import { Logger } from '../services/index.js';
1
+ import { Logger } from '../../services/index.js';
2
2
  /**
3
3
  * Logs all the loaded routes.
4
4
  * @param logger - A logger for logging information.
@@ -1,4 +1,4 @@
1
- import { pikkuState } from '../pikku-state.js';
1
+ import { pikkuState } from '../../pikku-state.js';
2
2
  /**
3
3
  * Logs all the loaded routes.
4
4
  * @param logger - A logger for logging information.
@@ -1,6 +1,6 @@
1
1
  import { parse as parseQuery } from 'picoquery';
2
2
  import { parse as parseCookie } from 'cookie';
3
- import { UnprocessableContentError } from '../errors/errors.js';
3
+ import { UnprocessableContentError } from '../../errors/errors.js';
4
4
  /**
5
5
  * Abstract class representing a pikku request.
6
6
  * @template In - The type of the request body.
@@ -0,0 +1,3 @@
1
+ export * from './mcp.types.js';
2
+ export * from './mcp-runner.js';
3
+ export * from './mcp-endpoint-registry.js';
@@ -0,0 +1,3 @@
1
+ export * from './mcp.types.js';
2
+ export * from './mcp-runner.js';
3
+ export * from './mcp-endpoint-registry.js';
@@ -0,0 +1,48 @@
1
+ import { MCPResourceMeta, MCPToolMeta, MCPPromptMeta } from '@pikku/core';
2
+ export interface MCPToolEndpoint {
3
+ name: string;
4
+ title: string;
5
+ description: string;
6
+ inputSchema?: any;
7
+ outputSchema?: any;
8
+ enabled: boolean;
9
+ }
10
+ export interface MCPResourceEndpoint {
11
+ uri: string;
12
+ title: string;
13
+ description: string;
14
+ inputSchema?: any;
15
+ mimeType?: string;
16
+ enabled: boolean;
17
+ }
18
+ export interface MCPPromptEndpoint {
19
+ name: string;
20
+ description: string;
21
+ inputSchema?: any;
22
+ enabled: boolean;
23
+ }
24
+ export declare class MCPEndpointRegistry {
25
+ private tools;
26
+ private resources;
27
+ private prompts;
28
+ private resourcesMeta;
29
+ private toolsMeta;
30
+ private promptsMeta;
31
+ loadFromMCPJsonFile(mcpJsonPath: string): Promise<void>;
32
+ loadFromMCPJson(mcpData: any): Promise<void>;
33
+ setResourcesMeta(meta: MCPResourceMeta): void;
34
+ setToolsMeta(meta: MCPToolMeta): void;
35
+ setPromptsMeta(meta: MCPPromptMeta): void;
36
+ getTools(): MCPToolEndpoint[];
37
+ getResources(): MCPResourceEndpoint[];
38
+ getPrompts(): MCPPromptEndpoint[];
39
+ getTool(name: string): MCPToolEndpoint | undefined;
40
+ getResource(name: string): MCPResourceEndpoint | undefined;
41
+ getPrompt(name: string): MCPPromptEndpoint | undefined;
42
+ hasResource(name: string): boolean;
43
+ hasTool(name: string): boolean;
44
+ hasPrompt(name: string): boolean;
45
+ enableTools(tools: Record<any, boolean>): boolean;
46
+ enablePrompts(prompts: Record<any, boolean>): boolean;
47
+ enableResources(resources: Record<any, boolean>): boolean;
48
+ }
@@ -0,0 +1,122 @@
1
+ import { readFile } from 'fs/promises';
2
+ export class MCPEndpointRegistry {
3
+ tools = new Map();
4
+ resources = new Map();
5
+ prompts = new Map();
6
+ resourcesMeta = {};
7
+ toolsMeta = {};
8
+ promptsMeta = {};
9
+ async loadFromMCPJsonFile(mcpJsonPath) {
10
+ try {
11
+ const mcpJsonContent = await readFile(mcpJsonPath, 'utf-8');
12
+ await this.loadFromMCPJson(JSON.parse(mcpJsonContent));
13
+ }
14
+ catch (error) {
15
+ throw new Error(`Failed to load MCP JSON from ${mcpJsonPath}: ${error}`);
16
+ }
17
+ }
18
+ async loadFromMCPJson(mcpData) {
19
+ if (mcpData.tools && Array.isArray(mcpData.tools)) {
20
+ for (const tool of mcpData.tools) {
21
+ this.tools.set(tool.name, {
22
+ name: tool.name,
23
+ title: tool.title,
24
+ description: tool.description,
25
+ inputSchema: tool.parameters,
26
+ outputSchema: tool.returns,
27
+ enabled: tool.enabled !== undefined ? tool.enabled : true,
28
+ });
29
+ }
30
+ }
31
+ if (mcpData.resources && Array.isArray(mcpData.resources)) {
32
+ for (const resource of mcpData.resources) {
33
+ this.resources.set(resource.name, {
34
+ title: resource.name,
35
+ uri: resource.uri,
36
+ description: resource.description,
37
+ inputSchema: resource.parameters,
38
+ enabled: resource.enabled !== undefined ? resource.enabled : true,
39
+ });
40
+ }
41
+ }
42
+ if (mcpData.prompts && Array.isArray(mcpData.prompts)) {
43
+ for (const prompt of mcpData.prompts) {
44
+ this.prompts.set(prompt.name, {
45
+ name: prompt.name,
46
+ description: prompt.description,
47
+ inputSchema: prompt.arguments,
48
+ enabled: prompt.enabled !== undefined ? prompt.enabled : true,
49
+ });
50
+ }
51
+ }
52
+ }
53
+ setResourcesMeta(meta) {
54
+ this.resourcesMeta = meta;
55
+ }
56
+ setToolsMeta(meta) {
57
+ this.toolsMeta = meta;
58
+ }
59
+ setPromptsMeta(meta) {
60
+ this.promptsMeta = meta;
61
+ }
62
+ getTools() {
63
+ return Array.from(this.tools.values()).filter((tool) => tool.enabled);
64
+ }
65
+ getResources() {
66
+ return Array.from(this.resources.values());
67
+ }
68
+ getPrompts() {
69
+ return Array.from(this.prompts.values());
70
+ }
71
+ getTool(name) {
72
+ return this.tools.get(name);
73
+ }
74
+ getResource(name) {
75
+ return this.resources.get(name);
76
+ }
77
+ getPrompt(name) {
78
+ return this.prompts.get(name);
79
+ }
80
+ hasResource(name) {
81
+ return name in this.resourcesMeta;
82
+ }
83
+ hasTool(name) {
84
+ return name in this.toolsMeta;
85
+ }
86
+ hasPrompt(name) {
87
+ return name in this.promptsMeta;
88
+ }
89
+ enableTools(tools) {
90
+ let changed = false;
91
+ for (const [name, enabled] of Object.entries(tools)) {
92
+ const tool = this.tools.get(name);
93
+ if (tool && tool.enabled !== enabled) {
94
+ tool.enabled = enabled;
95
+ changed = true;
96
+ }
97
+ }
98
+ return changed;
99
+ }
100
+ enablePrompts(prompts) {
101
+ let changed = false;
102
+ for (const [name, enabled] of Object.entries(prompts)) {
103
+ const prompt = this.prompts.get(name);
104
+ if (prompt && prompt.enabled !== enabled) {
105
+ prompt.enabled = enabled;
106
+ changed = true;
107
+ }
108
+ }
109
+ return changed;
110
+ }
111
+ enableResources(resources) {
112
+ let changed = false;
113
+ for (const [name, enabled] of Object.entries(resources)) {
114
+ const resource = this.resources.get(name);
115
+ if (resource && resource.enabled !== enabled) {
116
+ resource.enabled = enabled;
117
+ changed = true;
118
+ }
119
+ }
120
+ return changed;
121
+ }
122
+ }
@@ -0,0 +1,30 @@
1
+ import type { CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices } from '../../types/core.types.js';
2
+ import type { CoreMCPResource, CoreMCPTool, CoreMCPPrompt, JsonRpcRequest, JsonRpcResponse, JsonRpcErrorResponse, PikkuMCP } from './mcp.types.js';
3
+ import type { CoreAPIFunctionSessionless } from '../../function/functions.types.js';
4
+ export declare class MCPError extends Error {
5
+ readonly error: JsonRpcErrorResponse;
6
+ constructor(error: JsonRpcErrorResponse);
7
+ }
8
+ export type RunMCPEndpointParams<Tools extends string = any> = {
9
+ session?: CoreUserSession;
10
+ singletonServices: CoreSingletonServices;
11
+ mcp?: PikkuMCP<Tools>;
12
+ createSessionServices?: CreateSessionServices<CoreSingletonServices, CoreServices<CoreSingletonServices>, CoreUserSession>;
13
+ };
14
+ export type JsonRpcError = {
15
+ code: number;
16
+ message: string;
17
+ data?: any;
18
+ };
19
+ export declare const addMCPResource: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpResource: CoreMCPResource<APIFunction>) => void;
20
+ export declare const addMCPTool: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpTool: CoreMCPTool<APIFunction>) => void;
21
+ export declare const addMCPPrompt: <APIFunction extends CoreAPIFunctionSessionless<any, any>>(mcpPrompt: CoreMCPPrompt<APIFunction>) => void;
22
+ export declare function runMCPResource(request: JsonRpcRequest, params: RunMCPEndpointParams, uri: string): Promise<JsonRpcResponse>;
23
+ export declare function runMCPTool(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
24
+ export declare function runMCPPrompt(request: JsonRpcRequest, params: RunMCPEndpointParams, name: string): Promise<JsonRpcResponse>;
25
+ export declare const getMCPTools: () => Map<string, CoreMCPTool>;
26
+ export declare const getMCPResources: () => Map<string, CoreMCPResource>;
27
+ export declare const getMCPResourcesMeta: () => import("./mcp.types.js").MCPResourceMeta;
28
+ export declare const getMCPToolsMeta: () => import("./mcp.types.js").MCPToolMeta;
29
+ export declare const getMCPPrompts: () => Map<string, CoreMCPPrompt>;
30
+ export declare const getMCPPromptsMeta: () => import("./mcp.types.js").MCPPromptMeta;