@elastic.io/mcp 1.0.0-beta

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 (174) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +173 -0
  3. package/README.npm.md +173 -0
  4. package/dist/api/client.d.ts +12 -0
  5. package/dist/api/client.d.ts.map +1 -0
  6. package/dist/api/client.js +129 -0
  7. package/dist/api/client.js.map +1 -0
  8. package/dist/api/jsonapi.d.ts +21 -0
  9. package/dist/api/jsonapi.d.ts.map +1 -0
  10. package/dist/api/jsonapi.js +32 -0
  11. package/dist/api/jsonapi.js.map +1 -0
  12. package/dist/api/logger.d.ts +8 -0
  13. package/dist/api/logger.d.ts.map +1 -0
  14. package/dist/api/logger.js +43 -0
  15. package/dist/api/logger.js.map +1 -0
  16. package/dist/api/samples.d.ts +15 -0
  17. package/dist/api/samples.d.ts.map +1 -0
  18. package/dist/api/samples.js +465 -0
  19. package/dist/api/samples.js.map +1 -0
  20. package/dist/api/verification.d.ts +6 -0
  21. package/dist/api/verification.d.ts.map +1 -0
  22. package/dist/api/verification.js +71 -0
  23. package/dist/api/verification.js.map +1 -0
  24. package/dist/cli.d.ts +3 -0
  25. package/dist/cli.d.ts.map +1 -0
  26. package/dist/cli.js +9 -0
  27. package/dist/cli.js.map +1 -0
  28. package/dist/config.d.ts +15 -0
  29. package/dist/config.d.ts.map +1 -0
  30. package/dist/config.js +44 -0
  31. package/dist/config.js.map +1 -0
  32. package/dist/embeddings/factory.d.ts +7 -0
  33. package/dist/embeddings/factory.d.ts.map +1 -0
  34. package/dist/embeddings/factory.js +25 -0
  35. package/dist/embeddings/factory.js.map +1 -0
  36. package/dist/embeddings/google.d.ts +19 -0
  37. package/dist/embeddings/google.d.ts.map +1 -0
  38. package/dist/embeddings/google.js +132 -0
  39. package/dist/embeddings/google.js.map +1 -0
  40. package/dist/embeddings/types.d.ts +27 -0
  41. package/dist/embeddings/types.d.ts.map +1 -0
  42. package/dist/embeddings/types.js +2 -0
  43. package/dist/embeddings/types.js.map +1 -0
  44. package/dist/index.d.ts +3 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +227 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/paths.d.ts +2 -0
  49. package/dist/paths.d.ts.map +1 -0
  50. package/dist/paths.js +10 -0
  51. package/dist/paths.js.map +1 -0
  52. package/dist/server.d.ts +29 -0
  53. package/dist/server.d.ts.map +1 -0
  54. package/dist/server.js +385 -0
  55. package/dist/server.js.map +1 -0
  56. package/dist/setup.d.ts +3 -0
  57. package/dist/setup.d.ts.map +1 -0
  58. package/dist/setup.js +338 -0
  59. package/dist/setup.js.map +1 -0
  60. package/dist/tools/confirmation.d.ts +37 -0
  61. package/dist/tools/confirmation.d.ts.map +1 -0
  62. package/dist/tools/confirmation.js +120 -0
  63. package/dist/tools/confirmation.js.map +1 -0
  64. package/dist/tools/constants.d.ts +28 -0
  65. package/dist/tools/constants.d.ts.map +1 -0
  66. package/dist/tools/constants.js +92 -0
  67. package/dist/tools/constants.js.map +1 -0
  68. package/dist/tools/definitions.d.ts +3 -0
  69. package/dist/tools/definitions.d.ts.map +1 -0
  70. package/dist/tools/definitions.js +915 -0
  71. package/dist/tools/definitions.js.map +1 -0
  72. package/dist/tools/flow-build.d.ts +18 -0
  73. package/dist/tools/flow-build.d.ts.map +1 -0
  74. package/dist/tools/flow-build.js +295 -0
  75. package/dist/tools/flow-build.js.map +1 -0
  76. package/dist/tools/flow-evidence.d.ts +62 -0
  77. package/dist/tools/flow-evidence.d.ts.map +1 -0
  78. package/dist/tools/flow-evidence.js +128 -0
  79. package/dist/tools/flow-evidence.js.map +1 -0
  80. package/dist/tools/flow-state-machine.d.ts +69 -0
  81. package/dist/tools/flow-state-machine.d.ts.map +1 -0
  82. package/dist/tools/flow-state-machine.js +292 -0
  83. package/dist/tools/flow-state-machine.js.map +1 -0
  84. package/dist/tools/handlers/auth.d.ts +3 -0
  85. package/dist/tools/handlers/auth.d.ts.map +1 -0
  86. package/dist/tools/handlers/auth.js +410 -0
  87. package/dist/tools/handlers/auth.js.map +1 -0
  88. package/dist/tools/handlers/component.d.ts +3 -0
  89. package/dist/tools/handlers/component.d.ts.map +1 -0
  90. package/dist/tools/handlers/component.js +165 -0
  91. package/dist/tools/handlers/component.js.map +1 -0
  92. package/dist/tools/handlers/data-sample.d.ts +3 -0
  93. package/dist/tools/handlers/data-sample.d.ts.map +1 -0
  94. package/dist/tools/handlers/data-sample.js +83 -0
  95. package/dist/tools/handlers/data-sample.js.map +1 -0
  96. package/dist/tools/handlers/dynamic.d.ts +3 -0
  97. package/dist/tools/handlers/dynamic.d.ts.map +1 -0
  98. package/dist/tools/handlers/dynamic.js +139 -0
  99. package/dist/tools/handlers/dynamic.js.map +1 -0
  100. package/dist/tools/handlers/flow.d.ts +9 -0
  101. package/dist/tools/handlers/flow.d.ts.map +1 -0
  102. package/dist/tools/handlers/flow.js +427 -0
  103. package/dist/tools/handlers/flow.js.map +1 -0
  104. package/dist/tools/handlers/log.d.ts +3 -0
  105. package/dist/tools/handlers/log.d.ts.map +1 -0
  106. package/dist/tools/handlers/log.js +92 -0
  107. package/dist/tools/handlers/log.js.map +1 -0
  108. package/dist/tools/handlers/organization.d.ts +3 -0
  109. package/dist/tools/handlers/organization.d.ts.map +1 -0
  110. package/dist/tools/handlers/organization.js +39 -0
  111. package/dist/tools/handlers/organization.js.map +1 -0
  112. package/dist/tools/handlers/raw.d.ts +3 -0
  113. package/dist/tools/handlers/raw.d.ts.map +1 -0
  114. package/dist/tools/handlers/raw.js +80 -0
  115. package/dist/tools/handlers/raw.js.map +1 -0
  116. package/dist/tools/handlers/search.d.ts +3 -0
  117. package/dist/tools/handlers/search.d.ts.map +1 -0
  118. package/dist/tools/handlers/search.js +105 -0
  119. package/dist/tools/handlers/search.js.map +1 -0
  120. package/dist/tools/handlers/session.d.ts +3 -0
  121. package/dist/tools/handlers/session.d.ts.map +1 -0
  122. package/dist/tools/handlers/session.js +22 -0
  123. package/dist/tools/handlers/session.js.map +1 -0
  124. package/dist/tools/handlers.d.ts +12 -0
  125. package/dist/tools/handlers.d.ts.map +1 -0
  126. package/dist/tools/handlers.js +202 -0
  127. package/dist/tools/handlers.js.map +1 -0
  128. package/dist/tools/helpers.d.ts +44 -0
  129. package/dist/tools/helpers.d.ts.map +1 -0
  130. package/dist/tools/helpers.js +195 -0
  131. package/dist/tools/helpers.js.map +1 -0
  132. package/dist/tools/types.d.ts +39 -0
  133. package/dist/tools/types.d.ts.map +1 -0
  134. package/dist/tools/types.js +2 -0
  135. package/dist/tools/types.js.map +1 -0
  136. package/dist/tools/validation.d.ts +35 -0
  137. package/dist/tools/validation.d.ts.map +1 -0
  138. package/dist/tools/validation.js +800 -0
  139. package/dist/tools/validation.js.map +1 -0
  140. package/dist/types/index.d.ts +47 -0
  141. package/dist/types/index.d.ts.map +1 -0
  142. package/dist/types/index.js +2 -0
  143. package/dist/types/index.js.map +1 -0
  144. package/dist/vectorstore/factory.d.ts +7 -0
  145. package/dist/vectorstore/factory.d.ts.map +1 -0
  146. package/dist/vectorstore/factory.js +25 -0
  147. package/dist/vectorstore/factory.js.map +1 -0
  148. package/dist/vectorstore/qdrant.d.ts +11 -0
  149. package/dist/vectorstore/qdrant.d.ts.map +1 -0
  150. package/dist/vectorstore/qdrant.js +107 -0
  151. package/dist/vectorstore/qdrant.js.map +1 -0
  152. package/dist/vectorstore/types.d.ts +28 -0
  153. package/dist/vectorstore/types.d.ts.map +1 -0
  154. package/dist/vectorstore/types.js +2 -0
  155. package/dist/vectorstore/types.js.map +1 -0
  156. package/package.json +66 -0
  157. package/resources/docs/flow-building-guide.md +581 -0
  158. package/resources/docs/rest-api-pagination.md +58 -0
  159. package/resources/docs/standardized-components-guide.md +66 -0
  160. package/resources/docs/system-instructions.md +207 -0
  161. package/resources/flow-examples/batch-flow-1-collect.json +51 -0
  162. package/resources/flow-examples/batch-flow-2-process.json +70 -0
  163. package/resources/flow-examples/code-component-transform.json +65 -0
  164. package/resources/flow-examples/code-component-trigger.json +52 -0
  165. package/resources/flow-examples/code-component-with-credentials-trigger.json +53 -0
  166. package/resources/flow-examples/code-component-with-credentials.json +67 -0
  167. package/resources/flow-examples/filter-step.json +63 -0
  168. package/resources/flow-examples/polling-sync-trigger.json +50 -0
  169. package/resources/flow-examples/router-branching.json +94 -0
  170. package/resources/flow-examples/splitter-re-assemble.json +72 -0
  171. package/resources/flow-examples/splitter-split.json +68 -0
  172. package/resources/flow-examples/timer-query-action-sync.json +68 -0
  173. package/resources/flow-examples/webhook-async-sync.json +49 -0
  174. package/resources/flow-examples/webhook-request-reply.json +63 -0
package/dist/server.js ADDED
@@ -0,0 +1,385 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { CallToolRequestSchema, ListToolsRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, ListPromptsRequestSchema, GetPromptRequestSchema } from '@modelcontextprotocol/sdk/types.js';
3
+ import { TOOLS } from './tools/definitions.js';
4
+ import { handleToolCall, getDiscoveryState } from './tools/handlers.js';
5
+ import { sanitizeInput } from './tools/helpers.js';
6
+ import { logger } from './api/logger.js';
7
+ import fs from 'fs';
8
+ import { resolveFromRoot } from './paths.js';
9
+ /**
10
+ * Instantiates and configures a new MCP Server instance.
11
+ * By passing the scoped AxiosInstance, we achieve absolute credential isolation.
12
+ */
13
+ export function createMcpServer(client) {
14
+ const mcpServer = new McpServer({
15
+ name: 'elasticio-mcp-server',
16
+ version: '1.0.0'
17
+ }, {
18
+ capabilities: {
19
+ tools: {},
20
+ resources: {},
21
+ prompts: {}
22
+ },
23
+ instructions: fs.readFileSync(resolveFromRoot('resources/docs/system-instructions.md'), 'utf8')
24
+ });
25
+ const server = mcpServer.server;
26
+ // eslint-disable-next-line @typescript-eslint/require-await
27
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
28
+ return { tools: TOOLS };
29
+ });
30
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
31
+ const { name, arguments: args } = request.params;
32
+ return await handleToolCall(name, args || {}, client);
33
+ });
34
+ // eslint-disable-next-line @typescript-eslint/require-await
35
+ server.setRequestHandler(ListResourcesRequestSchema, async () => {
36
+ return {
37
+ resources: [
38
+ {
39
+ uri: 'elasticio://docs/flow-building-guide',
40
+ name: 'elastic.io Flow Building Guide',
41
+ mimeType: 'text/markdown',
42
+ description: 'Guidelines, schemas, and best practices for creating and editing elastic.io integration flows.'
43
+ },
44
+ {
45
+ uri: 'elasticio://docs/standardized-components-guide',
46
+ name: 'elastic.io Standardized Actions and Triggers Guide',
47
+ mimeType: 'text/markdown',
48
+ description: 'Specifications and usage guidelines for standardized actions and triggers across components.'
49
+ },
50
+ {
51
+ uri: 'elasticio://docs/rest-api-pagination',
52
+ name: 'Implementing Pagination with the REST API Component and Any Component That Uses the makeRawRequest Action',
53
+ mimeType: 'text/markdown',
54
+ description: 'How to implement pagination with the REST API component and any component that uses the makeRawRequest action, using a recursive Flow Linking pattern, since the httpRequestAxios action does not support automatic pagination.'
55
+ }
56
+ ]
57
+ };
58
+ });
59
+ // eslint-disable-next-line @typescript-eslint/require-await
60
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
61
+ const { uri } = request.params;
62
+ if (uri === 'elasticio://docs/flow-building-guide') {
63
+ const discoveryState = getDiscoveryState(client);
64
+ discoveryState.flowBuildingGuideRead = true;
65
+ const filePath = resolveFromRoot('resources/docs/flow-building-guide.md');
66
+ const text = fs.readFileSync(filePath, 'utf8');
67
+ return {
68
+ contents: [
69
+ {
70
+ uri,
71
+ mimeType: 'text/markdown',
72
+ text
73
+ }
74
+ ]
75
+ };
76
+ }
77
+ if (uri === 'elasticio://docs/standardized-components-guide') {
78
+ const filePath = resolveFromRoot('resources/docs/standardized-components-guide.md');
79
+ const text = fs.readFileSync(filePath, 'utf8');
80
+ return {
81
+ contents: [
82
+ {
83
+ uri,
84
+ mimeType: 'text/markdown',
85
+ text
86
+ }
87
+ ]
88
+ };
89
+ }
90
+ if (uri === 'elasticio://docs/rest-api-pagination') {
91
+ const filePath = resolveFromRoot('resources/docs/rest-api-pagination.md');
92
+ const text = fs.readFileSync(filePath, 'utf8');
93
+ return {
94
+ contents: [
95
+ {
96
+ uri,
97
+ mimeType: 'text/markdown',
98
+ text
99
+ }
100
+ ]
101
+ };
102
+ }
103
+ throw new Error(`Resource not found: ${uri}`);
104
+ });
105
+ // eslint-disable-next-line @typescript-eslint/require-await
106
+ server.setRequestHandler(ListPromptsRequestSchema, async () => {
107
+ return {
108
+ prompts: [
109
+ {
110
+ name: 'hello-world-placeholder',
111
+ description: 'A simple testing prompt that welcomes a user by name.',
112
+ arguments: [
113
+ {
114
+ name: 'name',
115
+ description: 'The name of the user to welcome',
116
+ required: true
117
+ }
118
+ ]
119
+ },
120
+ {
121
+ name: 'create-new-flow',
122
+ description: 'Step-by-step assistant to create a brand new integration flow using Playbook A.',
123
+ arguments: [
124
+ {
125
+ name: 'description',
126
+ description: 'A description of what the integration flow should do',
127
+ required: true
128
+ }
129
+ ]
130
+ },
131
+ {
132
+ name: 'update-existing-flow',
133
+ description: 'Modify or update steps and configurations of an existing integration flow using Playbook B.',
134
+ arguments: [
135
+ {
136
+ name: 'flowId',
137
+ description: 'The unique ID of the integration flow to modify',
138
+ required: true
139
+ },
140
+ {
141
+ name: 'modification',
142
+ description: 'Details of what modifications to apply to the flow',
143
+ required: true
144
+ }
145
+ ]
146
+ },
147
+ {
148
+ name: 'debug-flow-failure',
149
+ description: 'Diagnose and debug a failing elastic.io flow using its structure and logs.',
150
+ arguments: [
151
+ {
152
+ name: 'flowId',
153
+ description: 'The unique ID of the failing integration flow',
154
+ required: true
155
+ },
156
+ {
157
+ name: 'limit',
158
+ description: 'Maximum number of log entries to retrieve (default: 20)',
159
+ required: false
160
+ }
161
+ ]
162
+ },
163
+ {
164
+ name: 'create-jsonata-mapping',
165
+ description: 'Help construct a JSONata mapping expression between two flow steps.',
166
+ arguments: [
167
+ {
168
+ name: 'sourceSchema',
169
+ description: 'JSON schema representing the preceding step\'s output payload',
170
+ required: true
171
+ },
172
+ {
173
+ name: 'targetSchema',
174
+ description: 'JSON schema representing the current step\'s input payload requirements',
175
+ required: true
176
+ }
177
+ ]
178
+ }
179
+ ]
180
+ };
181
+ });
182
+ // eslint-disable-next-line @typescript-eslint/require-await
183
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
184
+ const { name, arguments: rawArgs } = request.params;
185
+ const args = rawArgs ? sanitizeInput(rawArgs) : rawArgs;
186
+ if (name === 'hello-world-placeholder') {
187
+ const targetName = args?.name;
188
+ if (!targetName) {
189
+ throw new Error('name argument is required');
190
+ }
191
+ return {
192
+ description: 'A simple testing prompt that welcomes a user by name.',
193
+ messages: [
194
+ {
195
+ role: 'user',
196
+ content: {
197
+ type: 'text',
198
+ text: `Hello, ${targetName}! Welcome to the elastic.io MCP server. This is a testing placeholder template to verify that prompts are working successfully.`
199
+ }
200
+ }
201
+ ]
202
+ };
203
+ }
204
+ if (name === 'create-new-flow') {
205
+ const desc = args?.description || '';
206
+ return {
207
+ description: 'Interactive wizard to create a new integration flow — guides the user step by step.',
208
+ messages: [
209
+ {
210
+ role: 'user',
211
+ content: {
212
+ type: 'text',
213
+ text: `I want to create a new integration flow: "${desc}".
214
+
215
+ You MUST act as an interactive wizard and follow Playbook A strictly. Key rules:
216
+
217
+ **NEVER build the entire flow silently.** Stop and ask me at every decision point:
218
+ - Which component to use (if multiple matches)
219
+ - Which credentials/secrets to use (if multiple or none exist)
220
+ - Which action/trigger to use
221
+ - Which values for dropdown fields (resolve dynamic select models and present options)
222
+ - How to map fields between steps (resolve dynamic metadata and present the schema)
223
+
224
+ **NEVER use Code components as substitutes for real connectors.** If I say "Salesforce", use the Salesforce component with proper credentials. If credentials don't exist, stop and help me create them.
225
+
226
+ **Build the flow incrementally, step by step:**
227
+ 1. Ask me what the first step should do → discover component → check credentials → select action → configure fields
228
+ 2. Create the flow with just the first step using \`create_flow\`
229
+ 3. Retrieve a real data sample for this step using \`debug_flow_step\` (pass flowId and componentId so the sample is saved to the draft)
230
+ 4. Ask about the next step → discover component → configure → add it via \`update_flow_draft\`
231
+ 5. Retrieve its data sample using \`debug_flow_step\` — use the previous step's real output to build accurate mappers
232
+ 6. Repeat steps 4-5 for each subsequent step
233
+ 7. After all steps are added, show a summary and ask for final confirmation
234
+
235
+ Start by asking me clarifying questions about what this flow should do, if my description above is not detailed enough.
236
+
237
+ **Example of correct behavior (follow this pattern):**
238
+ User: "I want to sync Salesforce contacts to a Google Sheet"
239
+ Assistant: "What type of trigger should this flow use? Should it poll for new/updated contacts on a schedule, or should it run once manually?"
240
+ User: "Poll every 15 minutes"
241
+ Assistant: [calls list_components(workspaceId, search="salesforce")] → gets multiple results → "I found these Salesforce components: 1) salesforce-v2 (id: abc123) 2) salesforce (id: def456, deprecated). Which one would you like to use?"
242
+ User: "salesforce-v2"
243
+ Assistant: [calls list_credentials + list_secrets for abc123] → "I found 1 secret for Salesforce v2: 'My SF Connection' (id: sec789). Should I use this one?"
244
+ User: "Yes"
245
+ Assistant: [calls get_component(abc123)] → "Salesforce v2 has these triggers: Get New and Updated Objects Polling, Bulk Query, Subscribe to PubSub... Which trigger do you need?"
246
+ ...and so on, one question at a time.`
247
+ }
248
+ }
249
+ ]
250
+ };
251
+ }
252
+ if (name === 'update-existing-flow') {
253
+ const flowId = args?.flowId;
254
+ const modification = args?.modification || '';
255
+ if (!flowId) {
256
+ throw new Error('flowId argument is required');
257
+ }
258
+ return {
259
+ description: 'Modify or update steps and configurations of an existing integration flow using Playbook B.',
260
+ messages: [
261
+ {
262
+ role: 'user',
263
+ content: {
264
+ type: 'text',
265
+ text: `I want to update the integration flow with ID "${flowId}". The modifications needed are: "${modification}".
266
+ Please strictly follow Playbook B:
267
+ 1. **Inspect Flow**: Call \`get_flow\` to read the current graph.
268
+ 2. **Deactivate**: If active, call \`stop_flow\` and wait/poll until inactive.
269
+ 3. **Draft Check**: Check if a draft exists with \`get_flow_draft\`.
270
+ 4. **Patch**: Construct modifications conforming strictly to the rules in \`elasticio://docs/flow-building-guide\` (especially using \`$getPassthrough().step_id.body.field\` for all mappers and applying correct JSONata double-quoting (escaped as \\") without outer double-quoting of the whole expression) and update using \`update_flow\`.
271
+ 5. **Activate**: Call \`start_flow\` to restart.`
272
+ }
273
+ }
274
+ ]
275
+ };
276
+ }
277
+ if (name === 'debug-flow-failure') {
278
+ const flowId = args?.flowId;
279
+ if (!flowId) {
280
+ throw new Error('flowId argument is required');
281
+ }
282
+ const limit = args?.limit || '20';
283
+ return {
284
+ description: 'Diagnose and debug a failing elastic.io flow using its structure and logs using Playbook C.',
285
+ messages: [
286
+ {
287
+ role: 'user',
288
+ content: {
289
+ type: 'text',
290
+ text: `Please debug the failing elastic.io flow with ID "${flowId}".
291
+ Please follow Playbook C:
292
+ 1. Call \`get_flow\` and \`get_logs\` with limit=${limit}.
293
+ 2. Compare the flow graph architecture, step inputs, and error logs.
294
+ 3. Suggest a resolution plan and patch/fix the configuration.`
295
+ }
296
+ }
297
+ ]
298
+ };
299
+ }
300
+ if (name === 'create-jsonata-mapping') {
301
+ const sourceSchema = args?.sourceSchema || '';
302
+ const targetSchema = args?.targetSchema || '';
303
+ return {
304
+ description: 'Help construct a JSONata mapping expression between two flow steps.',
305
+ messages: [
306
+ {
307
+ role: 'user',
308
+ content: {
309
+ type: 'text',
310
+ text: `I need to create a JSONata mapping to transform data between two steps in an integration flow.
311
+ Here is the JSON schema of the source output:
312
+ \`\`\`json
313
+ ${sourceSchema}
314
+ \`\`\`
315
+
316
+ Here is the JSON schema of the target input requirement:
317
+ \`\`\`json
318
+ ${targetSchema}
319
+ \`\`\`
320
+
321
+ Please:
322
+ 1. Analyze both schemas to map corresponding fields.
323
+ 2. Formulate a valid JSONata transformation object mapping source properties to target properties.
324
+ 3. Keep the JSONata expression clean, correct, and handle typical edge cases (e.g., check if arrays or optional properties are defined using conditionals).
325
+ 4. Provide the final mapping JSON string.`
326
+ }
327
+ }
328
+ ]
329
+ };
330
+ }
331
+ throw new Error(`Prompt not found: ${name}`);
332
+ });
333
+ server.onerror = (error) => {
334
+ logger.error('[MCP Server Error]', error);
335
+ };
336
+ return mcpServer;
337
+ }
338
+ /**
339
+ * Manage active sessions for SSE Transport (multi-tenant environment)
340
+ */
341
+ export class SessionManager {
342
+ sessions = new Map();
343
+ cleanupInterval;
344
+ registerSession(sessionId, session) {
345
+ this.sessions.set(sessionId, session);
346
+ }
347
+ getSession(sessionId) {
348
+ return this.sessions.get(sessionId);
349
+ }
350
+ removeSession(sessionId) {
351
+ const session = this.sessions.get(sessionId);
352
+ if (session) {
353
+ session.server.close().catch((err) => {
354
+ logger.error(`Error closing server for session ${sessionId}:`, err.message || err, err.stack || '');
355
+ });
356
+ this.sessions.delete(sessionId);
357
+ }
358
+ }
359
+ activeSessionCount() {
360
+ return this.sessions.size;
361
+ }
362
+ startReaper(maxIdleMs = 30 * 60 * 1000) {
363
+ if (this.cleanupInterval)
364
+ return;
365
+ this.cleanupInterval = setInterval(() => {
366
+ const now = Date.now();
367
+ for (const [sessionId, session] of this.sessions.entries()) {
368
+ if (now - session.lastActive > maxIdleMs) {
369
+ logger.info(`[Session Reaper] Cleaning up idle session: ${sessionId}`);
370
+ this.removeSession(sessionId);
371
+ }
372
+ }
373
+ }, 60 * 1000);
374
+ if (this.cleanupInterval && typeof this.cleanupInterval.unref === 'function') {
375
+ this.cleanupInterval.unref();
376
+ }
377
+ }
378
+ stopReaper() {
379
+ if (this.cleanupInterval) {
380
+ clearInterval(this.cleanupInterval);
381
+ this.cleanupInterval = undefined;
382
+ }
383
+ }
384
+ }
385
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,s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,CAAC;YACzB,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;YACpC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACnC,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export declare function runSetup(): Promise<void>;
3
+ //# sourceMappingURL=setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":";AA+SA,wBAAsB,QAAQ,kBA0E7B"}