@jadchene/mcp-gateway-service 0.1.0 → 0.1.2
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/README.md +5 -1
- package/README_zh.md +5 -1
- package/config.example.json +2 -1
- package/dist/config.js +2 -0
- package/dist/gateway-server.js +12 -1
- package/dist/index.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/mcp/client.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -140,8 +140,11 @@ mcp-gateway-service -v
|
|
|
140
140
|
|
|
141
141
|
The gateway currently supports `stdio` downstream transports only.
|
|
142
142
|
|
|
143
|
+
A gateway log file is written to `mcp-gateway.log` under `logDirectory`. If `logDirectory` is omitted, the gateway writes logs to the current working directory.
|
|
144
|
+
|
|
143
145
|
A service is loaded only when `enable` is missing or set to `true`. If `enable` is set to `false`, the gateway skips that service entirely. During hot reload, disabling or removing a service also stops its existing downstream process if one is running.
|
|
144
146
|
|
|
147
|
+
- `logDirectory` is optional. When omitted, the gateway writes `mcp-gateway.log` to the current working directory.
|
|
145
148
|
- `enable` is optional. When omitted, the gateway treats the service as enabled.
|
|
146
149
|
- `cwd` is optional. When omitted, the gateway uses its current working directory.
|
|
147
150
|
- `env` is optional.
|
|
@@ -151,6 +154,7 @@ A service is loaded only when `enable` is missing or set to `true`. If `enable`
|
|
|
151
154
|
|
|
152
155
|
```json
|
|
153
156
|
{
|
|
157
|
+
"logDirectory": "./logs",
|
|
154
158
|
"services": [
|
|
155
159
|
{
|
|
156
160
|
"serviceId": "demo-echo",
|
|
@@ -271,4 +275,4 @@ args = ["--config", "./config.json"]
|
|
|
271
275
|
|
|
272
276
|
## License
|
|
273
277
|
|
|
274
|
-
Released under the [MIT License](./LICENSE).
|
|
278
|
+
Released under the [MIT License](./LICENSE).
|
package/README_zh.md
CHANGED
|
@@ -146,8 +146,11 @@ mcp-gateway-service -v
|
|
|
146
146
|
|
|
147
147
|
当前只支持 `stdio` 类型的下游传输。
|
|
148
148
|
|
|
149
|
+
网关会把日志写到 `logDirectory` 目录下的 `mcp-gateway.log`。如果没有配置 `logDirectory`,默认写到当前工作目录。
|
|
150
|
+
|
|
149
151
|
只有当 `enable` 未填写或显式为 `true` 时,服务才会被加载;如果 `enable` 为 `false`,网关会跳过该服务。热刷新时,如果某个服务被禁用或从配置中删除,网关也会停掉它当前已启动的下游进程。
|
|
150
152
|
|
|
153
|
+
- `logDirectory` 可选,不填时会把 `mcp-gateway.log` 写到当前工作目录
|
|
151
154
|
- `enable` 可选,不填时默认按启用处理
|
|
152
155
|
- `cwd` 可选,不填时使用当前工作目录
|
|
153
156
|
- `env` 可选
|
|
@@ -155,6 +158,7 @@ mcp-gateway-service -v
|
|
|
155
158
|
|
|
156
159
|
```json
|
|
157
160
|
{
|
|
161
|
+
"logDirectory": "./logs",
|
|
158
162
|
"services": [
|
|
159
163
|
{
|
|
160
164
|
"serviceId": "demo-echo",
|
|
@@ -273,4 +277,4 @@ args = ["--config", "./config.json"]
|
|
|
273
277
|
|
|
274
278
|
## License
|
|
275
279
|
|
|
276
|
-
项目采用 [MIT License](./LICENSE) 开源。
|
|
280
|
+
项目采用 [MIT License](./LICENSE) 开源。
|
package/config.example.json
CHANGED
package/dist/config.js
CHANGED
|
@@ -21,6 +21,7 @@ export function validateGatewayConfig(input) {
|
|
|
21
21
|
if (!isRecord(input)) {
|
|
22
22
|
throw new Error("The gateway config must be a JSON object.");
|
|
23
23
|
}
|
|
24
|
+
const logDirectory = optionalString(input.logDirectory, "logDirectory");
|
|
24
25
|
if (!Array.isArray(input.services)) {
|
|
25
26
|
throw new Error("The 'services' field must be an array.");
|
|
26
27
|
}
|
|
@@ -33,6 +34,7 @@ export function validateGatewayConfig(input) {
|
|
|
33
34
|
seenServiceIds.add(service.serviceId);
|
|
34
35
|
}
|
|
35
36
|
return {
|
|
37
|
+
logDirectory,
|
|
36
38
|
services: services.filter((service) => service.enable)
|
|
37
39
|
};
|
|
38
40
|
}
|
package/dist/gateway-server.js
CHANGED
|
@@ -19,6 +19,10 @@ export class GatewayServer {
|
|
|
19
19
|
* Stores the stdio message writer bound to the current process.
|
|
20
20
|
*/
|
|
21
21
|
writer = createMessageWriter(process.stdout);
|
|
22
|
+
/**
|
|
23
|
+
* Stores the startup barrier that must resolve before gateway tools can use the registry.
|
|
24
|
+
*/
|
|
25
|
+
startupBarrier = Promise.resolve();
|
|
22
26
|
/**
|
|
23
27
|
* Creates the gateway server.
|
|
24
28
|
*/
|
|
@@ -26,6 +30,12 @@ export class GatewayServer {
|
|
|
26
30
|
this.registry = registry;
|
|
27
31
|
this.logger = logger;
|
|
28
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Sets the startup barrier used to delay tool handling until the registry is ready.
|
|
35
|
+
*/
|
|
36
|
+
setStartupBarrier(barrier) {
|
|
37
|
+
this.startupBarrier = barrier;
|
|
38
|
+
}
|
|
29
39
|
/**
|
|
30
40
|
* Starts consuming inbound MCP messages from stdin.
|
|
31
41
|
*/
|
|
@@ -64,6 +74,7 @@ export class GatewayServer {
|
|
|
64
74
|
return;
|
|
65
75
|
}
|
|
66
76
|
if (request.method === "tools/call") {
|
|
77
|
+
await this.startupBarrier;
|
|
67
78
|
const result = await this.handleToolCall(request);
|
|
68
79
|
this.writer.write(jsonRpcResult(request.id, result));
|
|
69
80
|
return;
|
|
@@ -176,7 +187,7 @@ export class GatewayServer {
|
|
|
176
187
|
},
|
|
177
188
|
serverInfo: {
|
|
178
189
|
name: "mcp-gateway",
|
|
179
|
-
version: "0.1.
|
|
190
|
+
version: "0.1.2"
|
|
180
191
|
}
|
|
181
192
|
};
|
|
182
193
|
}
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,10 @@ class Application {
|
|
|
15
15
|
* Stores a shared logger instance.
|
|
16
16
|
*/
|
|
17
17
|
logger = new Logger();
|
|
18
|
+
/**
|
|
19
|
+
* Stores the shared config loader instance.
|
|
20
|
+
*/
|
|
21
|
+
configLoader = new ConfigLoader();
|
|
18
22
|
/**
|
|
19
23
|
* Stores the config file path in use for the current process.
|
|
20
24
|
*/
|
|
@@ -22,7 +26,7 @@ class Application {
|
|
|
22
26
|
/**
|
|
23
27
|
* Stores the runtime service registry.
|
|
24
28
|
*/
|
|
25
|
-
registry = new ServiceRegistry(this.configPath,
|
|
29
|
+
registry = new ServiceRegistry(this.configPath, this.configLoader, this.logger);
|
|
26
30
|
/**
|
|
27
31
|
* Stores the gateway server bound to stdin/stdout.
|
|
28
32
|
*/
|
|
@@ -31,6 +35,8 @@ class Application {
|
|
|
31
35
|
* Stores the file watcher used for config hot reload.
|
|
32
36
|
*/
|
|
33
37
|
watcher = new ConfigFileWatcher(this.configPath, this.logger, async () => {
|
|
38
|
+
const config = await this.configLoader.load(this.configPath);
|
|
39
|
+
this.logger.setLogDirectory(config.logDirectory ?? process.cwd());
|
|
34
40
|
await this.registry.reload();
|
|
35
41
|
});
|
|
36
42
|
/**
|
|
@@ -40,10 +46,14 @@ class Application {
|
|
|
40
46
|
if (!existsSync(this.configPath)) {
|
|
41
47
|
throw new Error(`Config file was not found: ${this.configPath}`);
|
|
42
48
|
}
|
|
43
|
-
await this.
|
|
49
|
+
const initialConfig = await this.configLoader.load(this.configPath);
|
|
50
|
+
this.logger.setLogDirectory(initialConfig.logDirectory ?? process.cwd());
|
|
51
|
+
const startup = this.registry.initialize();
|
|
52
|
+
this.server.setStartupBarrier(startup);
|
|
44
53
|
this.server.start();
|
|
45
|
-
this.watcher.start();
|
|
46
54
|
this.registerSignals();
|
|
55
|
+
await startup;
|
|
56
|
+
this.watcher.start();
|
|
47
57
|
this.logger.info("gateway.started", {
|
|
48
58
|
configPath: this.configPath
|
|
49
59
|
});
|
package/dist/logger.js
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
1
3
|
/**
|
|
2
4
|
* Provides a minimal structured logger that writes newline-delimited JSON to stderr.
|
|
3
5
|
*/
|
|
4
6
|
export class Logger {
|
|
7
|
+
/**
|
|
8
|
+
* Stores the optional log file path used in addition to stderr.
|
|
9
|
+
*/
|
|
10
|
+
logFilePath = null;
|
|
11
|
+
/**
|
|
12
|
+
* Tracks whether file logging already failed to avoid repeated fallback noise.
|
|
13
|
+
*/
|
|
14
|
+
fileLoggingErrorReported = false;
|
|
15
|
+
/**
|
|
16
|
+
* Updates the directory used for the gateway log file.
|
|
17
|
+
*/
|
|
18
|
+
setLogDirectory(directory) {
|
|
19
|
+
const absoluteDirectory = resolve(directory);
|
|
20
|
+
mkdirSync(absoluteDirectory, { recursive: true });
|
|
21
|
+
this.logFilePath = join(absoluteDirectory, "mcp-gateway.log");
|
|
22
|
+
this.fileLoggingErrorReported = false;
|
|
23
|
+
}
|
|
5
24
|
/**
|
|
6
25
|
* Logs one structured event.
|
|
7
26
|
*/
|
|
@@ -30,6 +49,33 @@ export class Logger {
|
|
|
30
49
|
event,
|
|
31
50
|
...details
|
|
32
51
|
};
|
|
33
|
-
|
|
52
|
+
const line = `${JSON.stringify(payload)}\n`;
|
|
53
|
+
process.stderr.write(line);
|
|
54
|
+
this.writeToFile(line);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Appends one log line to the configured log file when file logging is enabled.
|
|
58
|
+
*/
|
|
59
|
+
writeToFile(line) {
|
|
60
|
+
if (!this.logFilePath) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
appendFileSync(this.logFilePath, line, "utf8");
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (this.fileLoggingErrorReported) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.fileLoggingErrorReported = true;
|
|
71
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
72
|
+
process.stderr.write(`${JSON.stringify({
|
|
73
|
+
timestamp: new Date().toISOString(),
|
|
74
|
+
level: "error",
|
|
75
|
+
event: "logger.file_write_failed",
|
|
76
|
+
message,
|
|
77
|
+
logFilePath: this.logFilePath
|
|
78
|
+
})}\n`);
|
|
79
|
+
}
|
|
34
80
|
}
|
|
35
81
|
}
|
package/dist/mcp/client.js
CHANGED
package/package.json
CHANGED