@epicdm/flowstate-mcp-gateway 1.0.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.
package/dist/index.js ADDED
@@ -0,0 +1,335 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ MCPGatewayServer: () => MCPGatewayServer
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+
37
+ // src/server.ts
38
+ var import_express = __toESM(require("express"));
39
+ var import_cors = __toESM(require("cors"));
40
+ var import_flowstate_mcp = require("@epicdm/flowstate-mcp");
41
+
42
+ // src/InMemoryTransport.ts
43
+ var import_events = require("events");
44
+ var InMemoryTransport = class {
45
+ sessionId;
46
+ onclose;
47
+ onerror;
48
+ onmessage;
49
+ started = false;
50
+ emitter = new import_events.EventEmitter();
51
+ responseHandlers = /* @__PURE__ */ new Map();
52
+ async start() {
53
+ this.started = true;
54
+ this.sessionId = `inmemory-${Date.now()}-${Math.random().toString(36).substring(7)}`;
55
+ }
56
+ async send(message) {
57
+ if (!this.started) {
58
+ throw new Error("Transport not started");
59
+ }
60
+ if ("result" in message || "error" in message) {
61
+ const response = message;
62
+ const handler = this.responseHandlers.get(response.id);
63
+ if (handler) {
64
+ handler(message);
65
+ this.responseHandlers.delete(response.id);
66
+ }
67
+ }
68
+ }
69
+ async close() {
70
+ this.started = false;
71
+ this.responseHandlers.clear();
72
+ this.onclose?.();
73
+ }
74
+ setProtocolVersion(version) {
75
+ }
76
+ /**
77
+ * Send a request and wait for response
78
+ * Used by the gateway to make programmatic requests
79
+ */
80
+ async sendRequest(request) {
81
+ if (!this.started) {
82
+ throw new Error("Transport not started");
83
+ }
84
+ return new Promise((resolve, reject) => {
85
+ const req = request;
86
+ const timeout = setTimeout(() => {
87
+ this.responseHandlers.delete(req.id);
88
+ reject(new Error("Request timeout"));
89
+ }, 6e4);
90
+ this.responseHandlers.set(req.id, (response) => {
91
+ clearTimeout(timeout);
92
+ resolve(response);
93
+ });
94
+ if (this.onmessage) {
95
+ this.onmessage(request);
96
+ } else {
97
+ reject(new Error("Transport not connected to server"));
98
+ }
99
+ });
100
+ }
101
+ };
102
+
103
+ // src/server.ts
104
+ var MCPGatewayServer = class {
105
+ app;
106
+ mcpServer;
107
+ config;
108
+ isInitialized = false;
109
+ httpServer;
110
+ transport;
111
+ constructor(config) {
112
+ this.config = config;
113
+ this.app = (0, import_express.default)();
114
+ this.mcpServer = new import_flowstate_mcp.FlowStateMCPServer({
115
+ rxdbServerUrl: config.rxdbServerUrl,
116
+ domainId: config.domainId,
117
+ projectPath: config.projectPath || process.cwd(),
118
+ ...config.userId && { userId: config.userId },
119
+ ...config.orgId && { orgId: config.orgId }
120
+ });
121
+ this.setupMiddleware();
122
+ this.setupRoutes();
123
+ }
124
+ /**
125
+ * Get list of tools from MCP server
126
+ *
127
+ * WARNING: This method accesses internal MCP server API via type casting.
128
+ * This is necessary because FlowStateMCPServer doesn't expose a public method
129
+ * for listing tools. The underlying server.request() method is used directly.
130
+ *
131
+ * RISKS:
132
+ * - Internal API may change without notice in future versions
133
+ * - Type casting bypasses TypeScript safety checks
134
+ * - May break if FlowStateMCPServer implementation changes
135
+ *
136
+ * TODO: Consider requesting a public API method in FlowStateMCPServer
137
+ * for listing and calling tools to avoid internal API access.
138
+ */
139
+ async listTools() {
140
+ if (!this.transport) {
141
+ return [];
142
+ }
143
+ const request = {
144
+ jsonrpc: "2.0",
145
+ id: Date.now(),
146
+ method: "tools/list"
147
+ };
148
+ const response = await this.transport.sendRequest(request);
149
+ if ("error" in response) {
150
+ throw new Error(response.error.message || "Failed to list tools");
151
+ }
152
+ return response.result?.tools || [];
153
+ }
154
+ /**
155
+ * Call a tool on the MCP server
156
+ *
157
+ * WARNING: This method accesses internal MCP server API via type casting.
158
+ * This is necessary because FlowStateMCPServer doesn't expose a public method
159
+ * for calling tools. The underlying server.request() method is used directly.
160
+ *
161
+ * RISKS:
162
+ * - Internal API may change without notice in future versions
163
+ * - Type casting bypasses TypeScript safety checks
164
+ * - May break if FlowStateMCPServer implementation changes
165
+ *
166
+ * TODO: Consider requesting a public API method in FlowStateMCPServer
167
+ * for listing and calling tools to avoid internal API access.
168
+ *
169
+ * NOTE: The MCP SDK's server.request() method appears to require the request
170
+ * object to be passed twice (once as the request, once as params). This seems
171
+ * to be how the underlying MCP SDK expects the call to be structured based on
172
+ * the protocol specification.
173
+ */
174
+ async callTool(toolName, args) {
175
+ if (!this.transport) {
176
+ throw new Error("MCP server not available");
177
+ }
178
+ const request = {
179
+ jsonrpc: "2.0",
180
+ id: Date.now(),
181
+ method: "tools/call",
182
+ params: {
183
+ name: toolName,
184
+ arguments: args
185
+ }
186
+ };
187
+ const response = await this.transport.sendRequest(request);
188
+ if ("error" in response) {
189
+ throw new Error(response.error.message || "Failed to call tool");
190
+ }
191
+ return response.result?.content?.[0]?.text || response.result;
192
+ }
193
+ setupMiddleware() {
194
+ this.app.use(
195
+ (0, import_cors.default)({
196
+ origin: this.config.corsOrigins || "*",
197
+ methods: ["GET", "POST", "OPTIONS"],
198
+ allowedHeaders: ["Content-Type", "Authorization"]
199
+ })
200
+ );
201
+ this.app.use(import_express.default.json());
202
+ this.app.use(import_express.default.urlencoded({ extended: true }));
203
+ this.app.use((req, res, next) => {
204
+ console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${req.method} ${req.path}`);
205
+ next();
206
+ });
207
+ }
208
+ /**
209
+ * Extract and forward auth token from request to MCP server
210
+ *
211
+ * This enables per-request auth so each API call can use its own token,
212
+ * rather than relying on the initial config token.
213
+ */
214
+ updateAuthFromRequest(req) {
215
+ const authHeader = req.headers["authorization"];
216
+ if (!authHeader) {
217
+ console.log("[MCP Gateway] No Authorization header in request");
218
+ return;
219
+ }
220
+ const token = authHeader.toString().replace("Bearer ", "");
221
+ if (!token) {
222
+ console.log("[MCP Gateway] Empty token after Bearer prefix removal");
223
+ return;
224
+ }
225
+ if (!token.startsWith("eyJ") && !token.startsWith("epic_")) {
226
+ console.log(`[MCP Gateway] Invalid token format (starts with: ${token.substring(0, 10)}...)`);
227
+ return;
228
+ }
229
+ try {
230
+ ;
231
+ this.mcpServer.config.authToken = token;
232
+ console.log("[MCP Gateway] Auth token updated from request");
233
+ } catch (error) {
234
+ console.error("[MCP Gateway] Failed to update auth config:", error);
235
+ }
236
+ }
237
+ setupRoutes() {
238
+ this.app.get("/health", (req, res) => {
239
+ res.json({
240
+ status: "ok",
241
+ initialized: this.isInitialized,
242
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
243
+ });
244
+ });
245
+ this.app.get("/mcp/tools", async (req, res) => {
246
+ try {
247
+ if (!this.isInitialized) {
248
+ return res.status(503).json({
249
+ error: "Server not initialized"
250
+ });
251
+ }
252
+ this.updateAuthFromRequest(req);
253
+ const tools = await this.listTools();
254
+ res.json({ tools });
255
+ } catch (error) {
256
+ console.error("Error listing tools:", error);
257
+ res.status(500).json({
258
+ error: error instanceof Error ? error.message : "Unknown error"
259
+ });
260
+ }
261
+ });
262
+ this.app.post("/mcp/tools/call", async (req, res) => {
263
+ try {
264
+ if (!this.isInitialized) {
265
+ return res.status(503).json({
266
+ success: false,
267
+ error: "Server not initialized"
268
+ });
269
+ }
270
+ this.updateAuthFromRequest(req);
271
+ const { toolName, arguments: args } = req.body;
272
+ if (!toolName) {
273
+ return res.status(400).json({
274
+ success: false,
275
+ error: "toolName is required"
276
+ });
277
+ }
278
+ const result = await this.callTool(toolName, args || {});
279
+ const response = {
280
+ success: true,
281
+ result
282
+ };
283
+ res.json(response);
284
+ } catch (error) {
285
+ console.error("Error calling tool:", error);
286
+ const response = {
287
+ success: false,
288
+ error: error instanceof Error ? error.message : "Unknown error"
289
+ };
290
+ res.status(500).json(response);
291
+ }
292
+ });
293
+ }
294
+ async initialize() {
295
+ await this.mcpServer.initialize();
296
+ this.transport = new InMemoryTransport();
297
+ const server = this.mcpServer.server;
298
+ if (server) {
299
+ await server.connect(this.transport);
300
+ }
301
+ this.isInitialized = true;
302
+ console.log("MCP Gateway initialized");
303
+ }
304
+ async start() {
305
+ await this.initialize();
306
+ return new Promise((resolve) => {
307
+ this.httpServer = this.app.listen(this.config.port, this.config.host, () => {
308
+ console.log(`MCP Gateway listening on http://${this.config.host}:${this.config.port}`);
309
+ resolve();
310
+ });
311
+ });
312
+ }
313
+ async close() {
314
+ if (this.httpServer) {
315
+ await new Promise((resolve, reject) => {
316
+ this.httpServer.close((err) => {
317
+ if (err) {
318
+ console.error("Error closing HTTP server:", err);
319
+ reject(err);
320
+ } else {
321
+ console.log("HTTP server closed");
322
+ resolve();
323
+ }
324
+ });
325
+ });
326
+ }
327
+ await this.mcpServer.close();
328
+ console.log("MCP Gateway closed");
329
+ }
330
+ };
331
+ // Annotate the CommonJS export names for ESM import in node:
332
+ 0 && (module.exports = {
333
+ MCPGatewayServer
334
+ });
335
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/server.ts","../src/InMemoryTransport.ts"],"sourcesContent":["// Copyright 2026 Epic Digital Interactive Media LLC\n// SPDX-License-Identifier: Apache-2.0\n\nexport { MCPGatewayServer } from './server';\nexport type {\n MCPGatewayConfig,\n ToolDefinition,\n ToolCallRequest,\n ToolCallResponse,\n} from './types';\n","// Copyright 2026 Epic Digital Interactive Media LLC\n// SPDX-License-Identifier: Apache-2.0\n\nimport express, { Express, Request, Response } from 'express'\nimport cors from 'cors'\nimport { FlowStateMCPServer } from '@epicdm/flowstate-mcp'\nimport { InMemoryTransport } from './InMemoryTransport'\nimport type { MCPGatewayConfig, ToolCallRequest, ToolCallResponse } from './types'\n\nexport class MCPGatewayServer {\n private app: Express\n private mcpServer: FlowStateMCPServer\n private config: MCPGatewayConfig\n private isInitialized = false\n private httpServer?: ReturnType<Express['listen']>\n private transport?: InMemoryTransport\n\n constructor(config: MCPGatewayConfig) {\n this.config = config\n this.app = express()\n\n // Initialize MCP Server\n this.mcpServer = new FlowStateMCPServer({\n rxdbServerUrl: config.rxdbServerUrl,\n domainId: config.domainId,\n projectPath: config.projectPath || process.cwd(),\n ...(config.userId && { userId: config.userId }),\n ...(config.orgId && { orgId: config.orgId }),\n } as any)\n\n this.setupMiddleware()\n this.setupRoutes()\n }\n\n /**\n * Get list of tools from MCP server\n *\n * WARNING: This method accesses internal MCP server API via type casting.\n * This is necessary because FlowStateMCPServer doesn't expose a public method\n * for listing tools. The underlying server.request() method is used directly.\n *\n * RISKS:\n * - Internal API may change without notice in future versions\n * - Type casting bypasses TypeScript safety checks\n * - May break if FlowStateMCPServer implementation changes\n *\n * TODO: Consider requesting a public API method in FlowStateMCPServer\n * for listing and calling tools to avoid internal API access.\n */\n private async listTools(): Promise<any[]> {\n if (!this.transport) {\n return []\n }\n\n // Send request through transport\n const request = {\n jsonrpc: '2.0' as const,\n id: Date.now(),\n method: 'tools/list' as const,\n }\n\n const response: any = await this.transport.sendRequest(request)\n\n if ('error' in response) {\n throw new Error(response.error.message || 'Failed to list tools')\n }\n\n return response.result?.tools || []\n }\n\n /**\n * Call a tool on the MCP server\n *\n * WARNING: This method accesses internal MCP server API via type casting.\n * This is necessary because FlowStateMCPServer doesn't expose a public method\n * for calling tools. The underlying server.request() method is used directly.\n *\n * RISKS:\n * - Internal API may change without notice in future versions\n * - Type casting bypasses TypeScript safety checks\n * - May break if FlowStateMCPServer implementation changes\n *\n * TODO: Consider requesting a public API method in FlowStateMCPServer\n * for listing and calling tools to avoid internal API access.\n *\n * NOTE: The MCP SDK's server.request() method appears to require the request\n * object to be passed twice (once as the request, once as params). This seems\n * to be how the underlying MCP SDK expects the call to be structured based on\n * the protocol specification.\n */\n private async callTool(toolName: string, args: Record<string, any>): Promise<any> {\n if (!this.transport) {\n throw new Error('MCP server not available')\n }\n\n // Send request through transport\n const request = {\n jsonrpc: '2.0' as const,\n id: Date.now(),\n method: 'tools/call' as const,\n params: {\n name: toolName,\n arguments: args,\n },\n }\n\n const response: any = await this.transport.sendRequest(request)\n\n if ('error' in response) {\n throw new Error(response.error.message || 'Failed to call tool')\n }\n\n return response.result?.content?.[0]?.text || response.result\n }\n\n private setupMiddleware(): void {\n // CORS\n this.app.use(\n cors({\n origin: this.config.corsOrigins || '*',\n methods: ['GET', 'POST', 'OPTIONS'],\n allowedHeaders: ['Content-Type', 'Authorization'],\n })\n )\n\n // Body parsing\n this.app.use(express.json())\n this.app.use(express.urlencoded({ extended: true }))\n\n // Request logging\n this.app.use((req, res, next) => {\n console.log(`${new Date().toISOString()} ${req.method} ${req.path}`)\n next()\n })\n }\n\n /**\n * Extract and forward auth token from request to MCP server\n *\n * This enables per-request auth so each API call can use its own token,\n * rather than relying on the initial config token.\n */\n private updateAuthFromRequest(req: Request): void {\n const authHeader = req.headers['authorization']\n if (!authHeader) {\n console.log('[MCP Gateway] No Authorization header in request')\n return\n }\n\n const token = authHeader.toString().replace('Bearer ', '')\n if (!token) {\n console.log('[MCP Gateway] Empty token after Bearer prefix removal')\n return\n }\n\n // Only accept valid-looking tokens (JWT or API tokens)\n if (!token.startsWith('eyJ') && !token.startsWith('epic_')) {\n console.log(`[MCP Gateway] Invalid token format (starts with: ${token.substring(0, 10)}...)`)\n return\n }\n\n try {\n ;(this.mcpServer as any).config.authToken = token\n console.log('[MCP Gateway] Auth token updated from request')\n } catch (error) {\n console.error('[MCP Gateway] Failed to update auth config:', error)\n }\n }\n\n private setupRoutes(): void {\n // Health check\n this.app.get('/health', (req: Request, res: Response) => {\n res.json({\n status: 'ok',\n initialized: this.isInitialized,\n timestamp: new Date().toISOString(),\n })\n })\n\n // List available tools\n this.app.get('/mcp/tools', async (req: Request, res: Response) => {\n try {\n if (!this.isInitialized) {\n return res.status(503).json({\n error: 'Server not initialized',\n })\n }\n\n // Forward per-request auth to MCP server\n this.updateAuthFromRequest(req)\n\n const tools = await this.listTools()\n res.json({ tools })\n } catch (error) {\n console.error('Error listing tools:', error)\n res.status(500).json({\n error: error instanceof Error ? error.message : 'Unknown error',\n })\n }\n })\n\n // Execute a tool\n this.app.post('/mcp/tools/call', async (req: Request, res: Response) => {\n try {\n if (!this.isInitialized) {\n return res.status(503).json({\n success: false,\n error: 'Server not initialized',\n })\n }\n\n // Forward per-request auth to MCP server\n this.updateAuthFromRequest(req)\n\n const { toolName, arguments: args } = req.body as ToolCallRequest\n\n if (!toolName) {\n return res.status(400).json({\n success: false,\n error: 'toolName is required',\n })\n }\n\n const result = await this.callTool(toolName, args || {})\n\n const response: ToolCallResponse = {\n success: true,\n result,\n }\n\n res.json(response)\n } catch (error) {\n console.error('Error calling tool:', error)\n const response: ToolCallResponse = {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error',\n }\n res.status(500).json(response)\n }\n })\n }\n\n async initialize(): Promise<void> {\n await this.mcpServer.initialize()\n\n // Connect the MCP server to an in-memory transport\n // This is required for the MCP SDK to handle requests\n this.transport = new InMemoryTransport()\n const server = (this.mcpServer as any).server\n if (server) {\n await server.connect(this.transport)\n }\n\n this.isInitialized = true\n console.log('MCP Gateway initialized')\n }\n\n async start(): Promise<void> {\n await this.initialize()\n\n return new Promise(resolve => {\n this.httpServer = this.app.listen(this.config.port, this.config.host, () => {\n console.log(`MCP Gateway listening on http://${this.config.host}:${this.config.port}`)\n resolve()\n })\n })\n }\n\n async close(): Promise<void> {\n // Close HTTP server first\n if (this.httpServer) {\n await new Promise<void>((resolve, reject) => {\n this.httpServer!.close(err => {\n if (err) {\n console.error('Error closing HTTP server:', err)\n reject(err)\n } else {\n console.log('HTTP server closed')\n resolve()\n }\n })\n })\n }\n\n // Then close MCP server\n await this.mcpServer.close()\n console.log('MCP Gateway closed')\n }\n}\n","// Copyright 2026 Epic Digital Interactive Media LLC\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';\nimport type { JSONRPCMessage, JSONRPCRequest, JSONRPCResponse, JSONRPCErrorResponse } from '@modelcontextprotocol/sdk/types.js';\nimport { EventEmitter } from 'events';\n\n/**\n * In-Memory Transport for programmatic MCP server usage\n *\n * This transport creates a bidirectional message channel that allows\n * the MCP server to be used programmatically without stdio/SSE.\n */\nexport class InMemoryTransport implements Transport {\n sessionId?: string;\n onclose?: () => void;\n onerror?: (error: Error) => void;\n onmessage?: (message: JSONRPCMessage) => void;\n\n private started = false;\n private emitter = new EventEmitter();\n private responseHandlers = new Map<string | number, (response: JSONRPCMessage) => void>();\n\n async start(): Promise<void> {\n this.started = true;\n this.sessionId = `inmemory-${Date.now()}-${Math.random().toString(36).substring(7)}`;\n }\n\n async send(message: JSONRPCMessage): Promise<void> {\n if (!this.started) {\n throw new Error('Transport not started');\n }\n\n // If this is a response (has 'result' or 'error' and 'id')\n if ('result' in message || 'error' in message) {\n const response = message as JSONRPCResponse | JSONRPCErrorResponse;\n const handler = this.responseHandlers.get(response.id!);\n if (handler) {\n handler(message);\n this.responseHandlers.delete(response.id!);\n }\n }\n }\n\n async close(): Promise<void> {\n this.started = false;\n this.responseHandlers.clear();\n this.onclose?.();\n }\n\n setProtocolVersion?(version: string): void {\n // No-op for in-memory transport\n }\n\n /**\n * Send a request and wait for response\n * Used by the gateway to make programmatic requests\n */\n async sendRequest(request: JSONRPCMessage): Promise<JSONRPCMessage> {\n if (!this.started) {\n throw new Error('Transport not started');\n }\n\n return new Promise((resolve, reject) => {\n const req = request as JSONRPCRequest;\n const timeout = setTimeout(() => {\n this.responseHandlers.delete(req.id);\n reject(new Error('Request timeout'));\n }, 60000);\n\n this.responseHandlers.set(req.id, (response) => {\n clearTimeout(timeout);\n resolve(response);\n });\n\n // Route request to server's message handler\n if (this.onmessage) {\n this.onmessage(request);\n } else {\n reject(new Error('Transport not connected to server'));\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,qBAAoD;AACpD,kBAAiB;AACjB,2BAAmC;;;ACAnC,oBAA6B;AAQtB,IAAM,oBAAN,MAA6C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,UAAU;AAAA,EACV,UAAU,IAAI,2BAAa;AAAA,EAC3B,mBAAmB,oBAAI,IAAyD;AAAA,EAExF,MAAM,QAAuB;AAC3B,SAAK,UAAU;AACf,SAAK,YAAY,YAAY,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EACpF;AAAA,EAEA,MAAM,KAAK,SAAwC;AACjD,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI,MAAM,uBAAuB;AAAA,IACzC;AAGA,QAAI,YAAY,WAAW,WAAW,SAAS;AAC7C,YAAM,WAAW;AACjB,YAAM,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,QAClD;AACA,YAAI,OAAO,GAAG,EAAE,KAAK,QAAQ;AAAA,MAC/B;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,KAAK,UAAU,WAAW;AAIhC,SAAK,YAAY,IAAI,kBAAkB;AACvC,UAAM,SAAU,KAAK,UAAkB;AACvC,QAAI,QAAQ;AACV,YAAM,OAAO,QAAQ,KAAK,SAAS;AAAA,IACrC;AAEA,SAAK,gBAAgB;AACrB,YAAQ,IAAI,yBAAyB;AAAA,EACvC;AAAA,EAEA,MAAM,QAAuB;AAC3B,UAAM,KAAK,WAAW;AAEtB,WAAO,IAAI,QAAQ,aAAW;AAC5B,WAAK,aAAa,KAAK,IAAI,OAAO,KAAK,OAAO,MAAM,KAAK,OAAO,MAAM,MAAM;AAC1E,gBAAQ,IAAI,mCAAmC,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,IAAI,EAAE;AACrF,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,QAAuB;AAE3B,QAAI,KAAK,YAAY;AACnB,YAAM,IAAI,QAAc,CAAC,SAAS,WAAW;AAC3C,aAAK,WAAY,MAAM,SAAO;AAC5B,cAAI,KAAK;AACP,oBAAQ,MAAM,8BAA8B,GAAG;AAC/C,mBAAO,GAAG;AAAA,UACZ,OAAO;AACL,oBAAQ,IAAI,oBAAoB;AAChC,oBAAQ;AAAA,UACV;AAAA,QACF,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAGA,UAAM,KAAK,UAAU,MAAM;AAC3B,YAAQ,IAAI,oBAAoB;AAAA,EAClC;AACF;","names":["express","cors"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,298 @@
1
+ // src/server.ts
2
+ import express from "express";
3
+ import cors from "cors";
4
+ import { FlowStateMCPServer } from "@epicdm/flowstate-mcp";
5
+
6
+ // src/InMemoryTransport.ts
7
+ import { EventEmitter } from "events";
8
+ var InMemoryTransport = class {
9
+ sessionId;
10
+ onclose;
11
+ onerror;
12
+ onmessage;
13
+ started = false;
14
+ emitter = new EventEmitter();
15
+ responseHandlers = /* @__PURE__ */ new Map();
16
+ async start() {
17
+ this.started = true;
18
+ this.sessionId = `inmemory-${Date.now()}-${Math.random().toString(36).substring(7)}`;
19
+ }
20
+ async send(message) {
21
+ if (!this.started) {
22
+ throw new Error("Transport not started");
23
+ }
24
+ if ("result" in message || "error" in message) {
25
+ const response = message;
26
+ const handler = this.responseHandlers.get(response.id);
27
+ if (handler) {
28
+ handler(message);
29
+ this.responseHandlers.delete(response.id);
30
+ }
31
+ }
32
+ }
33
+ async close() {
34
+ this.started = false;
35
+ this.responseHandlers.clear();
36
+ this.onclose?.();
37
+ }
38
+ setProtocolVersion(version) {
39
+ }
40
+ /**
41
+ * Send a request and wait for response
42
+ * Used by the gateway to make programmatic requests
43
+ */
44
+ async sendRequest(request) {
45
+ if (!this.started) {
46
+ throw new Error("Transport not started");
47
+ }
48
+ return new Promise((resolve, reject) => {
49
+ const req = request;
50
+ const timeout = setTimeout(() => {
51
+ this.responseHandlers.delete(req.id);
52
+ reject(new Error("Request timeout"));
53
+ }, 6e4);
54
+ this.responseHandlers.set(req.id, (response) => {
55
+ clearTimeout(timeout);
56
+ resolve(response);
57
+ });
58
+ if (this.onmessage) {
59
+ this.onmessage(request);
60
+ } else {
61
+ reject(new Error("Transport not connected to server"));
62
+ }
63
+ });
64
+ }
65
+ };
66
+
67
+ // src/server.ts
68
+ var MCPGatewayServer = class {
69
+ app;
70
+ mcpServer;
71
+ config;
72
+ isInitialized = false;
73
+ httpServer;
74
+ transport;
75
+ constructor(config) {
76
+ this.config = config;
77
+ this.app = express();
78
+ this.mcpServer = new FlowStateMCPServer({
79
+ rxdbServerUrl: config.rxdbServerUrl,
80
+ domainId: config.domainId,
81
+ projectPath: config.projectPath || process.cwd(),
82
+ ...config.userId && { userId: config.userId },
83
+ ...config.orgId && { orgId: config.orgId }
84
+ });
85
+ this.setupMiddleware();
86
+ this.setupRoutes();
87
+ }
88
+ /**
89
+ * Get list of tools from MCP server
90
+ *
91
+ * WARNING: This method accesses internal MCP server API via type casting.
92
+ * This is necessary because FlowStateMCPServer doesn't expose a public method
93
+ * for listing tools. The underlying server.request() method is used directly.
94
+ *
95
+ * RISKS:
96
+ * - Internal API may change without notice in future versions
97
+ * - Type casting bypasses TypeScript safety checks
98
+ * - May break if FlowStateMCPServer implementation changes
99
+ *
100
+ * TODO: Consider requesting a public API method in FlowStateMCPServer
101
+ * for listing and calling tools to avoid internal API access.
102
+ */
103
+ async listTools() {
104
+ if (!this.transport) {
105
+ return [];
106
+ }
107
+ const request = {
108
+ jsonrpc: "2.0",
109
+ id: Date.now(),
110
+ method: "tools/list"
111
+ };
112
+ const response = await this.transport.sendRequest(request);
113
+ if ("error" in response) {
114
+ throw new Error(response.error.message || "Failed to list tools");
115
+ }
116
+ return response.result?.tools || [];
117
+ }
118
+ /**
119
+ * Call a tool on the MCP server
120
+ *
121
+ * WARNING: This method accesses internal MCP server API via type casting.
122
+ * This is necessary because FlowStateMCPServer doesn't expose a public method
123
+ * for calling tools. The underlying server.request() method is used directly.
124
+ *
125
+ * RISKS:
126
+ * - Internal API may change without notice in future versions
127
+ * - Type casting bypasses TypeScript safety checks
128
+ * - May break if FlowStateMCPServer implementation changes
129
+ *
130
+ * TODO: Consider requesting a public API method in FlowStateMCPServer
131
+ * for listing and calling tools to avoid internal API access.
132
+ *
133
+ * NOTE: The MCP SDK's server.request() method appears to require the request
134
+ * object to be passed twice (once as the request, once as params). This seems
135
+ * to be how the underlying MCP SDK expects the call to be structured based on
136
+ * the protocol specification.
137
+ */
138
+ async callTool(toolName, args) {
139
+ if (!this.transport) {
140
+ throw new Error("MCP server not available");
141
+ }
142
+ const request = {
143
+ jsonrpc: "2.0",
144
+ id: Date.now(),
145
+ method: "tools/call",
146
+ params: {
147
+ name: toolName,
148
+ arguments: args
149
+ }
150
+ };
151
+ const response = await this.transport.sendRequest(request);
152
+ if ("error" in response) {
153
+ throw new Error(response.error.message || "Failed to call tool");
154
+ }
155
+ return response.result?.content?.[0]?.text || response.result;
156
+ }
157
+ setupMiddleware() {
158
+ this.app.use(
159
+ cors({
160
+ origin: this.config.corsOrigins || "*",
161
+ methods: ["GET", "POST", "OPTIONS"],
162
+ allowedHeaders: ["Content-Type", "Authorization"]
163
+ })
164
+ );
165
+ this.app.use(express.json());
166
+ this.app.use(express.urlencoded({ extended: true }));
167
+ this.app.use((req, res, next) => {
168
+ console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${req.method} ${req.path}`);
169
+ next();
170
+ });
171
+ }
172
+ /**
173
+ * Extract and forward auth token from request to MCP server
174
+ *
175
+ * This enables per-request auth so each API call can use its own token,
176
+ * rather than relying on the initial config token.
177
+ */
178
+ updateAuthFromRequest(req) {
179
+ const authHeader = req.headers["authorization"];
180
+ if (!authHeader) {
181
+ console.log("[MCP Gateway] No Authorization header in request");
182
+ return;
183
+ }
184
+ const token = authHeader.toString().replace("Bearer ", "");
185
+ if (!token) {
186
+ console.log("[MCP Gateway] Empty token after Bearer prefix removal");
187
+ return;
188
+ }
189
+ if (!token.startsWith("eyJ") && !token.startsWith("epic_")) {
190
+ console.log(`[MCP Gateway] Invalid token format (starts with: ${token.substring(0, 10)}...)`);
191
+ return;
192
+ }
193
+ try {
194
+ ;
195
+ this.mcpServer.config.authToken = token;
196
+ console.log("[MCP Gateway] Auth token updated from request");
197
+ } catch (error) {
198
+ console.error("[MCP Gateway] Failed to update auth config:", error);
199
+ }
200
+ }
201
+ setupRoutes() {
202
+ this.app.get("/health", (req, res) => {
203
+ res.json({
204
+ status: "ok",
205
+ initialized: this.isInitialized,
206
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
207
+ });
208
+ });
209
+ this.app.get("/mcp/tools", async (req, res) => {
210
+ try {
211
+ if (!this.isInitialized) {
212
+ return res.status(503).json({
213
+ error: "Server not initialized"
214
+ });
215
+ }
216
+ this.updateAuthFromRequest(req);
217
+ const tools = await this.listTools();
218
+ res.json({ tools });
219
+ } catch (error) {
220
+ console.error("Error listing tools:", error);
221
+ res.status(500).json({
222
+ error: error instanceof Error ? error.message : "Unknown error"
223
+ });
224
+ }
225
+ });
226
+ this.app.post("/mcp/tools/call", async (req, res) => {
227
+ try {
228
+ if (!this.isInitialized) {
229
+ return res.status(503).json({
230
+ success: false,
231
+ error: "Server not initialized"
232
+ });
233
+ }
234
+ this.updateAuthFromRequest(req);
235
+ const { toolName, arguments: args } = req.body;
236
+ if (!toolName) {
237
+ return res.status(400).json({
238
+ success: false,
239
+ error: "toolName is required"
240
+ });
241
+ }
242
+ const result = await this.callTool(toolName, args || {});
243
+ const response = {
244
+ success: true,
245
+ result
246
+ };
247
+ res.json(response);
248
+ } catch (error) {
249
+ console.error("Error calling tool:", error);
250
+ const response = {
251
+ success: false,
252
+ error: error instanceof Error ? error.message : "Unknown error"
253
+ };
254
+ res.status(500).json(response);
255
+ }
256
+ });
257
+ }
258
+ async initialize() {
259
+ await this.mcpServer.initialize();
260
+ this.transport = new InMemoryTransport();
261
+ const server = this.mcpServer.server;
262
+ if (server) {
263
+ await server.connect(this.transport);
264
+ }
265
+ this.isInitialized = true;
266
+ console.log("MCP Gateway initialized");
267
+ }
268
+ async start() {
269
+ await this.initialize();
270
+ return new Promise((resolve) => {
271
+ this.httpServer = this.app.listen(this.config.port, this.config.host, () => {
272
+ console.log(`MCP Gateway listening on http://${this.config.host}:${this.config.port}`);
273
+ resolve();
274
+ });
275
+ });
276
+ }
277
+ async close() {
278
+ if (this.httpServer) {
279
+ await new Promise((resolve, reject) => {
280
+ this.httpServer.close((err) => {
281
+ if (err) {
282
+ console.error("Error closing HTTP server:", err);
283
+ reject(err);
284
+ } else {
285
+ console.log("HTTP server closed");
286
+ resolve();
287
+ }
288
+ });
289
+ });
290
+ }
291
+ await this.mcpServer.close();
292
+ console.log("MCP Gateway closed");
293
+ }
294
+ };
295
+ export {
296
+ MCPGatewayServer
297
+ };
298
+ //# sourceMappingURL=index.mjs.map