@jadchene/mcp-gateway-service 0.3.3 → 0.4.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 CHANGED
@@ -3,10 +3,16 @@ import { existsSync } from "node:fs";
3
3
  import { resolve } from "node:path";
4
4
  import { ConfigLoader } from "./config.js";
5
5
  import { ConfigFileWatcher } from "./file-watcher.js";
6
+ import { McpGatewayEngine } from "./gateway-engine.js";
6
7
  import { GatewayServer } from "./gateway-server.js";
8
+ import { StreamableHttpGatewayServer } from "./http-server.js";
7
9
  import { Logger } from "./logger.js";
8
10
  import { ServiceRegistry } from "./service-registry.js";
9
11
  import { VERSION } from "./version.js";
12
+ /**
13
+ * Lists OS signals that should trigger a graceful gateway shutdown.
14
+ */
15
+ export const SHUTDOWN_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
10
16
  /**
11
17
  * Boots the MCP gateway process and wires config reload handling.
12
18
  */
@@ -18,15 +24,27 @@ class Application {
18
24
  /**
19
25
  * Stores the config file path in use for the current process.
20
26
  */
21
- configPath = resolve(getConfigPath());
27
+ cliOptions = parseCliArgs(process.argv.slice(2));
28
+ /**
29
+ * Stores the config file path in use for the current process.
30
+ */
31
+ configPath = resolve(this.cliOptions.configPath);
22
32
  /**
23
33
  * Stores the runtime service registry.
24
34
  */
25
35
  registry = new ServiceRegistry(this.configPath, new ConfigLoader(), this.logger);
36
+ /**
37
+ * Stores the transport-neutral request engine shared by all inbound transports.
38
+ */
39
+ engine = new McpGatewayEngine(this.registry, this.logger);
26
40
  /**
27
41
  * Stores the gateway server bound to stdin/stdout.
28
42
  */
29
- server = new GatewayServer(this.registry, this.logger);
43
+ server = new GatewayServer(this.registry, this.logger, this.engine);
44
+ /**
45
+ * Stores the optional Streamable HTTP gateway server.
46
+ */
47
+ httpServer = null;
30
48
  /**
31
49
  * Stores the file watcher used for config hot reload.
32
50
  */
@@ -45,10 +63,15 @@ class Application {
45
63
  throw new Error(`Config file was not found: ${this.configPath}`);
46
64
  }
47
65
  const startup = this.registry.initialize();
48
- this.server.setStartupBarrier(startup);
66
+ this.engine.setStartupBarrier(startup);
49
67
  this.server.start();
50
- this.registerSignals();
51
68
  await startup;
69
+ const serverConfig = this.cliOptions.server;
70
+ this.registerSignals(Boolean(serverConfig));
71
+ if (serverConfig) {
72
+ this.httpServer = new StreamableHttpGatewayServer(serverConfig, this.engine, this.logger);
73
+ await this.httpServer.start();
74
+ }
52
75
  this.watcher.start();
53
76
  this.logger.info("gateway.started", {
54
77
  configPath: this.configPath
@@ -57,7 +80,7 @@ class Application {
57
80
  /**
58
81
  * Registers signal handlers for graceful shutdown.
59
82
  */
60
- registerSignals() {
83
+ registerSignals(httpEnabled) {
61
84
  const shutdown = async (signal) => {
62
85
  if (this.shuttingDown) {
63
86
  return;
@@ -65,30 +88,85 @@ class Application {
65
88
  this.shuttingDown = true;
66
89
  this.logger.info("gateway.stopping", { signal });
67
90
  this.watcher.stop();
91
+ await this.httpServer?.stop();
68
92
  await this.registry.dispose();
69
93
  process.exit(0);
70
94
  };
71
- process.on("SIGINT", () => void shutdown("SIGINT"));
72
- process.on("SIGTERM", () => void shutdown("SIGTERM"));
73
- process.stdin.on("end", () => void shutdown("stdin-end"));
74
- process.stdin.on("close", () => void shutdown("stdin-close"));
95
+ for (const signal of SHUTDOWN_SIGNALS) {
96
+ process.on(signal, () => void shutdown(signal));
97
+ }
98
+ if (!httpEnabled) {
99
+ process.stdin.on("end", () => void shutdown("stdin-end"));
100
+ process.stdin.on("close", () => void shutdown("stdin-close"));
101
+ }
102
+ }
103
+ }
104
+ if (import.meta.main) {
105
+ const application = new Application();
106
+ if (process.argv.includes("-v") || process.argv.includes("--version")) {
107
+ process.stdout.write(`${VERSION}\n`);
108
+ process.exit(0);
75
109
  }
110
+ void application.start().catch((error) => {
111
+ const message = error instanceof Error ? error.message : String(error);
112
+ process.stderr.write(`${message}\n`);
113
+ process.exit(1);
114
+ });
115
+ }
116
+ export function parseCliArgs(args) {
117
+ const configPath = readOption(args, "--config") ?? process.env.MCP_GATEWAY_CONFIG ?? "config.json";
118
+ const port = readIntegerOption(args, "--port");
119
+ const host = readOption(args, "--host");
120
+ const path = readHttpPathOption(args);
121
+ const enableHttp = args.includes("--http");
122
+ const enableJsonResponse = args.includes("--json-response");
123
+ return {
124
+ configPath,
125
+ server: enableHttp
126
+ ? {
127
+ enable: true,
128
+ host: host ?? "127.0.0.1",
129
+ port: port ?? 3000,
130
+ path: path ?? "/mcp",
131
+ enableJsonResponse
132
+ }
133
+ : undefined
134
+ };
135
+ }
136
+ /**
137
+ * Reads a string option from argv.
138
+ */
139
+ function readOption(args, name) {
140
+ const index = args.indexOf(name);
141
+ if (index !== -1 && args[index + 1]) {
142
+ return args[index + 1];
143
+ }
144
+ return undefined;
76
145
  }
77
- const application = new Application();
78
- if (process.argv.includes("-v") || process.argv.includes("--version")) {
79
- process.stdout.write(`${VERSION}\n`);
80
- process.exit(0);
146
+ /**
147
+ * Reads an integer option from argv.
148
+ */
149
+ function readIntegerOption(args, name) {
150
+ const value = readOption(args, name);
151
+ if (value === undefined) {
152
+ return undefined;
153
+ }
154
+ const parsed = Number(value);
155
+ if (!Number.isInteger(parsed) || parsed < 1 || parsed > 65_535) {
156
+ throw new Error(`${name} must be an integer from 1 to 65535.`);
157
+ }
158
+ return parsed;
81
159
  }
82
- void application.start().catch((error) => {
83
- const message = error instanceof Error ? error.message : String(error);
84
- process.stderr.write(`${message}\n`);
85
- process.exit(1);
86
- });
87
- function getConfigPath() {
88
- const args = process.argv.slice(2);
89
- const configIndex = args.indexOf("--config");
90
- if (configIndex !== -1 && args[configIndex + 1]) {
91
- return args[configIndex + 1];
160
+ /**
161
+ * Reads and validates the inbound HTTP endpoint path option.
162
+ */
163
+ function readHttpPathOption(args) {
164
+ const path = readOption(args, "--path");
165
+ if (path === undefined) {
166
+ return undefined;
167
+ }
168
+ if (!path.startsWith("/")) {
169
+ throw new Error("--path must start with '/'.");
92
170
  }
93
- return process.env.MCP_GATEWAY_CONFIG ?? "config.json";
171
+ return path;
94
172
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,6 @@
1
1
  import { execFileSync, spawn } from "node:child_process";
2
2
  import { resolve } from "node:path";
3
+ import { VERSION } from "../version.js";
3
4
  import { createMessageReader, createMessageWriter } from "./protocol.js";
4
5
  /**
5
6
  * Defines the default timeout applied to downstream JSON-RPC requests.
@@ -108,8 +109,9 @@ export class StdioMcpClient {
108
109
  if (this.initialized) {
109
110
  return;
110
111
  }
111
- const framingCandidates = this.service.transport.framing
112
- ? [this.service.transport.framing]
112
+ const transport = requireStdioTransport(this.service);
113
+ const framingCandidates = transport.framing
114
+ ? [transport.framing]
113
115
  : ["line", "content-length"];
114
116
  let lastError = null;
115
117
  for (const framing of framingCandidates) {
@@ -121,7 +123,7 @@ export class StdioMcpClient {
121
123
  catch (error) {
122
124
  lastError = normalizeError(error);
123
125
  await this.disposeProcess();
124
- if (this.service.transport.framing) {
126
+ if (transport.framing) {
125
127
  break;
126
128
  }
127
129
  this.logger.warn("downstream.framing_probe_failed", {
@@ -240,10 +242,7 @@ export class StdioMcpClient {
240
242
  if (this.child) {
241
243
  return;
242
244
  }
243
- if (this.service.transport.type !== "stdio") {
244
- throw new Error(`Unsupported transport '${String(this.service.transport.type)}'.`);
245
- }
246
- const transport = this.service.transport;
245
+ const transport = requireStdioTransport(this.service);
247
246
  const cwd = transport.cwd ? resolve(transport.cwd) : process.cwd();
248
247
  const commandSpec = resolveCommandSpec(transport.command, transport.args ?? []);
249
248
  const child = spawn(commandSpec.command, commandSpec.args, {
@@ -310,7 +309,7 @@ export class StdioMcpClient {
310
309
  },
311
310
  clientInfo: {
312
311
  name: "mcp-gateway",
313
- version: "0.3.3"
312
+ version: VERSION
314
313
  }
315
314
  });
316
315
  if (isRecord(result)) {
@@ -511,6 +510,15 @@ function resolveViaPowerShell(command) {
511
510
  return null;
512
511
  }
513
512
  }
513
+ /**
514
+ * Narrows a service config to its stdio transport.
515
+ */
516
+ function requireStdioTransport(service) {
517
+ if (service.transport.type !== "stdio") {
518
+ throw new Error(`Unsupported transport '${String(service.transport.type)}'.`);
519
+ }
520
+ return service.transport;
521
+ }
514
522
  /**
515
523
  * Resolves the preferred PowerShell host on Windows.
516
524
  */