@jadchene/mcp-gateway-service 0.1.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/LICENSE +21 -0
- package/README.md +274 -0
- package/README_zh.md +276 -0
- package/config.example.json +18 -0
- package/dist/config.js +163 -0
- package/dist/file-watcher.js +82 -0
- package/dist/gateway-server.js +298 -0
- package/dist/index.js +82 -0
- package/dist/logger.js +35 -0
- package/dist/mcp/client.js +571 -0
- package/dist/mcp/protocol.js +193 -0
- package/dist/service-registry.js +250 -0
- package/dist/types.js +1 -0
- package/dist/version.js +8 -0
- package/package.json +44 -0
- package/scripts/install-global.ps1 +111 -0
- package/scripts/publish-npm.ps1 +108 -0
- package/skills/mcp-gateway/SKILL.md +64 -0
package/dist/config.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Provides config loading and validation for the gateway service pool definition.
|
|
5
|
+
*/
|
|
6
|
+
export class ConfigLoader {
|
|
7
|
+
/**
|
|
8
|
+
* Loads, parses, and validates the config file from disk.
|
|
9
|
+
*/
|
|
10
|
+
async load(configPath) {
|
|
11
|
+
const absolutePath = resolve(configPath);
|
|
12
|
+
const rawText = await readFile(absolutePath, "utf8");
|
|
13
|
+
const parsed = JSON.parse(rawText);
|
|
14
|
+
return validateGatewayConfig(parsed);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates the top-level config object and normalizes optional fields.
|
|
19
|
+
*/
|
|
20
|
+
export function validateGatewayConfig(input) {
|
|
21
|
+
if (!isRecord(input)) {
|
|
22
|
+
throw new Error("The gateway config must be a JSON object.");
|
|
23
|
+
}
|
|
24
|
+
if (!Array.isArray(input.services)) {
|
|
25
|
+
throw new Error("The 'services' field must be an array.");
|
|
26
|
+
}
|
|
27
|
+
const services = input.services.map(validateServiceConfig);
|
|
28
|
+
const seenServiceIds = new Set();
|
|
29
|
+
for (const service of services) {
|
|
30
|
+
if (seenServiceIds.has(service.serviceId)) {
|
|
31
|
+
throw new Error(`Duplicate serviceId '${service.serviceId}' was found in the config.`);
|
|
32
|
+
}
|
|
33
|
+
seenServiceIds.add(service.serviceId);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
services: services.filter((service) => service.enable)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validates one logical service definition.
|
|
41
|
+
*/
|
|
42
|
+
function validateServiceConfig(input) {
|
|
43
|
+
if (!isRecord(input)) {
|
|
44
|
+
throw new Error("Each service must be a JSON object.");
|
|
45
|
+
}
|
|
46
|
+
const serviceId = requireNonEmptyString(input.serviceId, "service.serviceId");
|
|
47
|
+
const enable = optionalBoolean(input.enable, `service '${serviceId}' enable`) ?? true;
|
|
48
|
+
const name = requireNonEmptyString(input.name, `service '${serviceId}' name`);
|
|
49
|
+
const description = optionalString(input.description, `service '${serviceId}' description`);
|
|
50
|
+
return {
|
|
51
|
+
serviceId,
|
|
52
|
+
enable,
|
|
53
|
+
name,
|
|
54
|
+
description,
|
|
55
|
+
transport: validateTransportConfig(serviceId, input.transport)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validates the supported transport definition.
|
|
60
|
+
*/
|
|
61
|
+
function validateTransportConfig(serviceId, input) {
|
|
62
|
+
if (!isRecord(input)) {
|
|
63
|
+
throw new Error(`Service '${serviceId}' transport must be a JSON object.`);
|
|
64
|
+
}
|
|
65
|
+
const type = requireNonEmptyString(input.type, `service '${serviceId}' transport.type`);
|
|
66
|
+
if (type !== "stdio") {
|
|
67
|
+
throw new Error(`Service '${serviceId}' uses unsupported transport type '${type}'.`);
|
|
68
|
+
}
|
|
69
|
+
const command = requireNonEmptyString(input.command, `service '${serviceId}' transport.command`);
|
|
70
|
+
const args = optionalStringArray(input.args, `service '${serviceId}' transport.args`);
|
|
71
|
+
const cwd = optionalString(input.cwd, `service '${serviceId}' transport.cwd`);
|
|
72
|
+
const env = optionalStringRecord(input.env, `service '${serviceId}' transport.env`);
|
|
73
|
+
const framing = optionalFraming(input.framing, `service '${serviceId}' transport.framing`);
|
|
74
|
+
return {
|
|
75
|
+
type: "stdio",
|
|
76
|
+
command,
|
|
77
|
+
args,
|
|
78
|
+
cwd,
|
|
79
|
+
env,
|
|
80
|
+
framing
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks whether a value is a record-like object.
|
|
85
|
+
*/
|
|
86
|
+
function isRecord(input) {
|
|
87
|
+
return typeof input === "object" && input !== null && !Array.isArray(input);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Reads a required non-empty string field.
|
|
91
|
+
*/
|
|
92
|
+
function requireNonEmptyString(input, label) {
|
|
93
|
+
if (typeof input !== "string" || input.trim() === "") {
|
|
94
|
+
throw new Error(`The '${label}' field must be a non-empty string.`);
|
|
95
|
+
}
|
|
96
|
+
return input;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Reads an optional string field.
|
|
100
|
+
*/
|
|
101
|
+
function optionalString(input, label) {
|
|
102
|
+
if (input === undefined) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
if (typeof input !== "string") {
|
|
106
|
+
throw new Error(`The '${label}' field must be a string when present.`);
|
|
107
|
+
}
|
|
108
|
+
return input;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reads an optional boolean field.
|
|
112
|
+
*/
|
|
113
|
+
function optionalBoolean(input, label) {
|
|
114
|
+
if (input === undefined) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
if (typeof input !== "boolean") {
|
|
118
|
+
throw new Error(`The '${label}' field must be a boolean when present.`);
|
|
119
|
+
}
|
|
120
|
+
return input;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Reads an optional string array field.
|
|
124
|
+
*/
|
|
125
|
+
function optionalStringArray(input, label) {
|
|
126
|
+
if (input === undefined) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
if (!Array.isArray(input) || input.some((item) => typeof item !== "string")) {
|
|
130
|
+
throw new Error(`The '${label}' field must be an array of strings when present.`);
|
|
131
|
+
}
|
|
132
|
+
return input;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Reads an optional string-to-string object field.
|
|
136
|
+
*/
|
|
137
|
+
function optionalStringRecord(input, label) {
|
|
138
|
+
if (input === undefined) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
if (!isRecord(input)) {
|
|
142
|
+
throw new Error(`The '${label}' field must be an object when present.`);
|
|
143
|
+
}
|
|
144
|
+
const entries = Object.entries(input);
|
|
145
|
+
for (const [key, value] of entries) {
|
|
146
|
+
if (typeof value !== "string") {
|
|
147
|
+
throw new Error(`The '${label}.${key}' field must be a string.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return Object.fromEntries(entries);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reads an optional stdio framing field.
|
|
154
|
+
*/
|
|
155
|
+
function optionalFraming(input, label) {
|
|
156
|
+
if (input === undefined) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
if (input !== "line" && input !== "content-length") {
|
|
160
|
+
throw new Error(`The '${label}' field must be 'line' or 'content-length' when present.`);
|
|
161
|
+
}
|
|
162
|
+
return input;
|
|
163
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { watch } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Watches one config file and emits debounced reload callbacks.
|
|
5
|
+
*/
|
|
6
|
+
export class ConfigFileWatcher {
|
|
7
|
+
/**
|
|
8
|
+
* Stores the config file path being watched.
|
|
9
|
+
*/
|
|
10
|
+
configPath;
|
|
11
|
+
/**
|
|
12
|
+
* Stores the shared logger instance.
|
|
13
|
+
*/
|
|
14
|
+
logger;
|
|
15
|
+
/**
|
|
16
|
+
* Stores the async callback used after debounced file changes.
|
|
17
|
+
*/
|
|
18
|
+
onChange;
|
|
19
|
+
/**
|
|
20
|
+
* Stores the debounce duration in milliseconds.
|
|
21
|
+
*/
|
|
22
|
+
debounceMs;
|
|
23
|
+
/**
|
|
24
|
+
* Stores the active file watcher instance.
|
|
25
|
+
*/
|
|
26
|
+
watcher = null;
|
|
27
|
+
/**
|
|
28
|
+
* Stores the debouncing timer used to collapse rapid save bursts.
|
|
29
|
+
*/
|
|
30
|
+
timer = null;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a watcher bound to one config file path.
|
|
33
|
+
*/
|
|
34
|
+
constructor(configPath, logger, onChange, debounceMs = 250) {
|
|
35
|
+
this.configPath = configPath;
|
|
36
|
+
this.logger = logger;
|
|
37
|
+
this.onChange = onChange;
|
|
38
|
+
this.debounceMs = debounceMs;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Starts watching the config file.
|
|
42
|
+
*/
|
|
43
|
+
start() {
|
|
44
|
+
if (this.watcher) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const absolutePath = resolve(this.configPath);
|
|
48
|
+
this.watcher = watch(absolutePath, () => {
|
|
49
|
+
this.logger.info("config.file.changed", { configPath: absolutePath });
|
|
50
|
+
this.scheduleReload();
|
|
51
|
+
});
|
|
52
|
+
this.watcher.on("error", (error) => {
|
|
53
|
+
this.logger.error("config.file.watch_error", {
|
|
54
|
+
configPath: absolutePath,
|
|
55
|
+
message: error.message
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Stops watching the config file.
|
|
61
|
+
*/
|
|
62
|
+
stop() {
|
|
63
|
+
if (this.timer) {
|
|
64
|
+
clearTimeout(this.timer);
|
|
65
|
+
this.timer = null;
|
|
66
|
+
}
|
|
67
|
+
this.watcher?.close();
|
|
68
|
+
this.watcher = null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Schedules one debounced reload operation.
|
|
72
|
+
*/
|
|
73
|
+
scheduleReload() {
|
|
74
|
+
if (this.timer) {
|
|
75
|
+
clearTimeout(this.timer);
|
|
76
|
+
}
|
|
77
|
+
this.timer = setTimeout(() => {
|
|
78
|
+
this.timer = null;
|
|
79
|
+
void this.onChange();
|
|
80
|
+
}, this.debounceMs);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { createMessageReader, createMessageWriter, jsonRpcError, jsonRpcResult } from "./mcp/protocol.js";
|
|
2
|
+
/**
|
|
3
|
+
* Exposes the MCP gateway tools over stdio.
|
|
4
|
+
*/
|
|
5
|
+
export class GatewayServer {
|
|
6
|
+
/**
|
|
7
|
+
* Stores the registry used for metadata lookup and routing.
|
|
8
|
+
*/
|
|
9
|
+
registry;
|
|
10
|
+
/**
|
|
11
|
+
* Stores the shared logger instance.
|
|
12
|
+
*/
|
|
13
|
+
logger;
|
|
14
|
+
/**
|
|
15
|
+
* Stores the stdio message reader bound to the current process.
|
|
16
|
+
*/
|
|
17
|
+
reader = createMessageReader(process.stdin);
|
|
18
|
+
/**
|
|
19
|
+
* Stores the stdio message writer bound to the current process.
|
|
20
|
+
*/
|
|
21
|
+
writer = createMessageWriter(process.stdout);
|
|
22
|
+
/**
|
|
23
|
+
* Creates the gateway server.
|
|
24
|
+
*/
|
|
25
|
+
constructor(registry, logger) {
|
|
26
|
+
this.registry = registry;
|
|
27
|
+
this.logger = logger;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Starts consuming inbound MCP messages from stdin.
|
|
31
|
+
*/
|
|
32
|
+
start() {
|
|
33
|
+
this.reader.on("message", (message) => {
|
|
34
|
+
const framingMode = this.reader.framingMode;
|
|
35
|
+
if (framingMode) {
|
|
36
|
+
this.writer.setFramingMode(framingMode);
|
|
37
|
+
}
|
|
38
|
+
if (!isJsonRpcRequest(message)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
void this.handleRequest(message);
|
|
42
|
+
});
|
|
43
|
+
this.reader.on("error", (error) => {
|
|
44
|
+
this.logger.error("gateway.protocol_error", {
|
|
45
|
+
message: error instanceof Error ? error.message : String(error)
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Handles one inbound JSON-RPC request.
|
|
51
|
+
*/
|
|
52
|
+
async handleRequest(request) {
|
|
53
|
+
try {
|
|
54
|
+
if (request.method === "initialize") {
|
|
55
|
+
this.writer.write(jsonRpcResult(request.id, this.buildInitializeResult()));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (request.method === "ping") {
|
|
59
|
+
this.writer.write(jsonRpcResult(request.id, {}));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (request.method === "tools/list") {
|
|
63
|
+
this.writer.write(jsonRpcResult(request.id, { tools: buildGatewayTools() }));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (request.method === "tools/call") {
|
|
67
|
+
const result = await this.handleToolCall(request);
|
|
68
|
+
this.writer.write(jsonRpcResult(request.id, result));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (request.method === "notifications/initialized") {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.writer.write(jsonRpcError(request.id, -32601, `Unsupported method '${request.method}'.`));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
78
|
+
this.logger.error("gateway.request_failed", {
|
|
79
|
+
method: request.method,
|
|
80
|
+
message
|
|
81
|
+
});
|
|
82
|
+
this.writer.write(jsonRpcError(request.id, -32000, message));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Handles one gateway tool invocation.
|
|
87
|
+
*/
|
|
88
|
+
async handleToolCall(request) {
|
|
89
|
+
const params = toObject(request.params, "The tools/call params must be an object.");
|
|
90
|
+
const toolName = requireString(params.name, "The tool name must be a string.");
|
|
91
|
+
const args = toObject(params.arguments ?? {}, "The tool arguments must be an object.");
|
|
92
|
+
switch (toolName) {
|
|
93
|
+
case "gateway.listServices":
|
|
94
|
+
return successContent({
|
|
95
|
+
services: this.registry.listServices().map(formatServiceSummary)
|
|
96
|
+
});
|
|
97
|
+
case "gateway.getService":
|
|
98
|
+
return this.getService(args);
|
|
99
|
+
case "gateway.listTools":
|
|
100
|
+
return this.listTools(args);
|
|
101
|
+
case "gateway.getToolSchema":
|
|
102
|
+
return this.getToolSchema(args);
|
|
103
|
+
case "gateway.callTool":
|
|
104
|
+
return this.callDownstreamTool(args);
|
|
105
|
+
default:
|
|
106
|
+
throw new Error(`Unknown gateway tool '${toolName}'.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns detailed metadata for one logical service.
|
|
111
|
+
*/
|
|
112
|
+
getService(args) {
|
|
113
|
+
const serviceId = requireString(args.serviceId, "The 'serviceId' argument must be a string.");
|
|
114
|
+
const snapshot = this.registry.getService(serviceId);
|
|
115
|
+
if (!snapshot) {
|
|
116
|
+
throw new Error(`Unknown service '${serviceId}'.`);
|
|
117
|
+
}
|
|
118
|
+
return successContent({
|
|
119
|
+
serviceId: snapshot.config.serviceId,
|
|
120
|
+
name: snapshot.config.name,
|
|
121
|
+
description: snapshot.config.description ?? null,
|
|
122
|
+
available: snapshot.runtime.available,
|
|
123
|
+
lastError: snapshot.runtime.lastError,
|
|
124
|
+
lastConnectedAt: snapshot.runtime.lastConnectedAt,
|
|
125
|
+
protocolVersion: snapshot.metadata.protocolVersion,
|
|
126
|
+
serverInfo: snapshot.metadata.serverInfo
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns tool summaries for one logical service.
|
|
131
|
+
*/
|
|
132
|
+
listTools(args) {
|
|
133
|
+
const serviceId = requireString(args.serviceId, "The 'serviceId' argument must be a string.");
|
|
134
|
+
return successContent({
|
|
135
|
+
tools: this.registry.listTools(serviceId).map((tool) => ({
|
|
136
|
+
name: tool.name,
|
|
137
|
+
description: tool.description ?? null
|
|
138
|
+
}))
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns input and output schemas for one downstream tool.
|
|
143
|
+
*/
|
|
144
|
+
getToolSchema(args) {
|
|
145
|
+
const serviceId = requireString(args.serviceId, "The 'serviceId' argument must be a string.");
|
|
146
|
+
const toolName = requireString(args.toolName, "The 'toolName' argument must be a string.");
|
|
147
|
+
const tool = this.registry.getTool(serviceId, toolName);
|
|
148
|
+
if (!tool) {
|
|
149
|
+
throw new Error(`Unknown tool '${toolName}' in service '${serviceId}'.`);
|
|
150
|
+
}
|
|
151
|
+
return successContent({
|
|
152
|
+
inputSchema: tool.inputSchema ?? null,
|
|
153
|
+
outputSchema: tool.outputSchema ?? null
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Routes one downstream tool call through the service registry.
|
|
158
|
+
*/
|
|
159
|
+
async callDownstreamTool(args) {
|
|
160
|
+
const serviceId = requireString(args.serviceId, "The 'serviceId' argument must be a string.");
|
|
161
|
+
const toolName = requireString(args.toolName, "The 'toolName' argument must be a string.");
|
|
162
|
+
const toolArgs = toObject(args.arguments ?? {}, "The 'arguments' field must be an object.");
|
|
163
|
+
const call = await this.registry.callTool(serviceId, toolName, toolArgs);
|
|
164
|
+
return call.result;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Builds the MCP initialize result advertised by the gateway.
|
|
168
|
+
*/
|
|
169
|
+
buildInitializeResult() {
|
|
170
|
+
return {
|
|
171
|
+
protocolVersion: "2025-06-18",
|
|
172
|
+
capabilities: {
|
|
173
|
+
tools: {
|
|
174
|
+
listChanged: false
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
serverInfo: {
|
|
178
|
+
name: "mcp-gateway",
|
|
179
|
+
version: "0.1.0"
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Builds the fixed gateway tool definitions exposed to all MCP clients.
|
|
186
|
+
*/
|
|
187
|
+
function buildGatewayTools() {
|
|
188
|
+
return [
|
|
189
|
+
{
|
|
190
|
+
name: "gateway.listServices",
|
|
191
|
+
description: "Lists all MCP services currently managed by the gateway.",
|
|
192
|
+
inputSchema: {
|
|
193
|
+
type: "object",
|
|
194
|
+
properties: {},
|
|
195
|
+
additionalProperties: false
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "gateway.getService",
|
|
200
|
+
description: "Returns one service summary, runtime state, and metadata cache details.",
|
|
201
|
+
inputSchema: objectSchema(["serviceId"], {
|
|
202
|
+
serviceId: stringSchema("Logical service identifier.")
|
|
203
|
+
})
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
name: "gateway.listTools",
|
|
207
|
+
description: "Lists all tools exposed by one downstream service.",
|
|
208
|
+
inputSchema: objectSchema(["serviceId"], {
|
|
209
|
+
serviceId: stringSchema("Logical service identifier.")
|
|
210
|
+
})
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "gateway.getToolSchema",
|
|
214
|
+
description: "Returns the input and output schema for one downstream tool.",
|
|
215
|
+
inputSchema: objectSchema(["serviceId", "toolName"], {
|
|
216
|
+
serviceId: stringSchema("Logical service identifier."),
|
|
217
|
+
toolName: stringSchema("Downstream tool name.")
|
|
218
|
+
})
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
name: "gateway.callTool",
|
|
222
|
+
description: "Calls one downstream tool through the gateway service pool.",
|
|
223
|
+
inputSchema: objectSchema(["serviceId", "toolName", "arguments"], {
|
|
224
|
+
serviceId: stringSchema("Logical service identifier."),
|
|
225
|
+
toolName: stringSchema("Downstream tool name."),
|
|
226
|
+
arguments: {
|
|
227
|
+
type: "object",
|
|
228
|
+
description: "Arguments passed to the downstream tool."
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Formats one service into the compact listServices result shape.
|
|
236
|
+
*/
|
|
237
|
+
function formatServiceSummary(snapshot) {
|
|
238
|
+
return {
|
|
239
|
+
serviceId: snapshot.config.serviceId,
|
|
240
|
+
description: snapshot.config.description ?? null,
|
|
241
|
+
available: snapshot.runtime.available
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Builds a standard MCP tool success payload with text and structured content.
|
|
246
|
+
*/
|
|
247
|
+
function successContent(data) {
|
|
248
|
+
return {
|
|
249
|
+
content: [
|
|
250
|
+
{
|
|
251
|
+
type: "text",
|
|
252
|
+
text: JSON.stringify(data, null, 2)
|
|
253
|
+
}
|
|
254
|
+
],
|
|
255
|
+
structuredContent: data
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Builds a simple JSON schema object descriptor.
|
|
260
|
+
*/
|
|
261
|
+
function objectSchema(required, properties) {
|
|
262
|
+
return {
|
|
263
|
+
type: "object",
|
|
264
|
+
properties,
|
|
265
|
+
required,
|
|
266
|
+
additionalProperties: false
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Builds a simple JSON schema string descriptor.
|
|
271
|
+
*/
|
|
272
|
+
function stringSchema(description) {
|
|
273
|
+
return {
|
|
274
|
+
type: "string",
|
|
275
|
+
description
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Ensures the input is a plain object.
|
|
280
|
+
*/
|
|
281
|
+
function toObject(input, message) {
|
|
282
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
283
|
+
throw new Error(message);
|
|
284
|
+
}
|
|
285
|
+
return input;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Ensures the input is a string.
|
|
289
|
+
*/
|
|
290
|
+
function requireString(input, message) {
|
|
291
|
+
if (typeof input !== "string" || input.trim() === "") {
|
|
292
|
+
throw new Error(message);
|
|
293
|
+
}
|
|
294
|
+
return input;
|
|
295
|
+
}
|
|
296
|
+
function isJsonRpcRequest(message) {
|
|
297
|
+
return "id" in message && "method" in message && typeof message.method === "string";
|
|
298
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { ConfigLoader } from "./config.js";
|
|
5
|
+
import { ConfigFileWatcher } from "./file-watcher.js";
|
|
6
|
+
import { GatewayServer } from "./gateway-server.js";
|
|
7
|
+
import { Logger } from "./logger.js";
|
|
8
|
+
import { ServiceRegistry } from "./service-registry.js";
|
|
9
|
+
import { VERSION } from "./version.js";
|
|
10
|
+
/**
|
|
11
|
+
* Boots the MCP gateway process and wires config reload handling.
|
|
12
|
+
*/
|
|
13
|
+
class Application {
|
|
14
|
+
/**
|
|
15
|
+
* Stores a shared logger instance.
|
|
16
|
+
*/
|
|
17
|
+
logger = new Logger();
|
|
18
|
+
/**
|
|
19
|
+
* Stores the config file path in use for the current process.
|
|
20
|
+
*/
|
|
21
|
+
configPath = resolve(getConfigPath());
|
|
22
|
+
/**
|
|
23
|
+
* Stores the runtime service registry.
|
|
24
|
+
*/
|
|
25
|
+
registry = new ServiceRegistry(this.configPath, new ConfigLoader(), this.logger);
|
|
26
|
+
/**
|
|
27
|
+
* Stores the gateway server bound to stdin/stdout.
|
|
28
|
+
*/
|
|
29
|
+
server = new GatewayServer(this.registry, this.logger);
|
|
30
|
+
/**
|
|
31
|
+
* Stores the file watcher used for config hot reload.
|
|
32
|
+
*/
|
|
33
|
+
watcher = new ConfigFileWatcher(this.configPath, this.logger, async () => {
|
|
34
|
+
await this.registry.reload();
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Starts the application and registers shutdown hooks.
|
|
38
|
+
*/
|
|
39
|
+
async start() {
|
|
40
|
+
if (!existsSync(this.configPath)) {
|
|
41
|
+
throw new Error(`Config file was not found: ${this.configPath}`);
|
|
42
|
+
}
|
|
43
|
+
await this.registry.initialize();
|
|
44
|
+
this.server.start();
|
|
45
|
+
this.watcher.start();
|
|
46
|
+
this.registerSignals();
|
|
47
|
+
this.logger.info("gateway.started", {
|
|
48
|
+
configPath: this.configPath
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Registers signal handlers for graceful shutdown.
|
|
53
|
+
*/
|
|
54
|
+
registerSignals() {
|
|
55
|
+
const shutdown = async (signal) => {
|
|
56
|
+
this.logger.info("gateway.stopping", { signal });
|
|
57
|
+
this.watcher.stop();
|
|
58
|
+
await this.registry.dispose();
|
|
59
|
+
process.exit(0);
|
|
60
|
+
};
|
|
61
|
+
process.on("SIGINT", () => void shutdown("SIGINT"));
|
|
62
|
+
process.on("SIGTERM", () => void shutdown("SIGTERM"));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const application = new Application();
|
|
66
|
+
if (process.argv.includes("-v") || process.argv.includes("--version")) {
|
|
67
|
+
process.stdout.write(`${VERSION}\n`);
|
|
68
|
+
process.exit(0);
|
|
69
|
+
}
|
|
70
|
+
void application.start().catch((error) => {
|
|
71
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
72
|
+
process.stderr.write(`${message}\n`);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
});
|
|
75
|
+
function getConfigPath() {
|
|
76
|
+
const args = process.argv.slice(2);
|
|
77
|
+
const configIndex = args.indexOf("--config");
|
|
78
|
+
if (configIndex !== -1 && args[configIndex + 1]) {
|
|
79
|
+
return args[configIndex + 1];
|
|
80
|
+
}
|
|
81
|
+
return process.env.MCP_GATEWAY_CONFIG ?? "config.json";
|
|
82
|
+
}
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a minimal structured logger that writes newline-delimited JSON to stderr.
|
|
3
|
+
*/
|
|
4
|
+
export class Logger {
|
|
5
|
+
/**
|
|
6
|
+
* Logs one structured event.
|
|
7
|
+
*/
|
|
8
|
+
info(event, details = {}) {
|
|
9
|
+
this.write("info", event, details);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Logs one warning event.
|
|
13
|
+
*/
|
|
14
|
+
warn(event, details = {}) {
|
|
15
|
+
this.write("warn", event, details);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Logs one error event.
|
|
19
|
+
*/
|
|
20
|
+
error(event, details = {}) {
|
|
21
|
+
this.write("error", event, details);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Serializes a log entry to stderr without polluting stdout-based MCP traffic.
|
|
25
|
+
*/
|
|
26
|
+
write(level, event, details) {
|
|
27
|
+
const payload = {
|
|
28
|
+
timestamp: new Date().toISOString(),
|
|
29
|
+
level,
|
|
30
|
+
event,
|
|
31
|
+
...details
|
|
32
|
+
};
|
|
33
|
+
process.stderr.write(`${JSON.stringify(payload)}\n`);
|
|
34
|
+
}
|
|
35
|
+
}
|