@jadchene/mcp-gateway-service 0.4.4 → 0.6.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.
@@ -1,193 +0,0 @@
1
- import { EventEmitter } from "node:events";
2
- /**
3
- * Provides a framed MCP stdio message reader compatible with both line-delimited and Content-Length transports.
4
- */
5
- export class McpMessageReader extends EventEmitter {
6
- /**
7
- * Stores raw bytes that have not yet formed a complete frame.
8
- */
9
- buffer = Buffer.alloc(0);
10
- /**
11
- * Stores the configured framing preference.
12
- */
13
- preferredMode;
14
- /**
15
- * Stores the framing mode detected from the incoming stream.
16
- */
17
- detectedMode = null;
18
- /**
19
- * Starts reading from the provided stream immediately.
20
- */
21
- constructor(stream, preferredMode = "auto") {
22
- super();
23
- this.preferredMode = preferredMode;
24
- stream.on("data", (chunk) => {
25
- const next = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
26
- this.buffer = Buffer.concat([this.buffer, next]);
27
- this.consumeFrames();
28
- });
29
- stream.on("error", (error) => this.emit("error", error));
30
- stream.on("close", () => this.emit("close"));
31
- }
32
- /**
33
- * Returns the currently selected framing mode when known.
34
- */
35
- get framingMode() {
36
- return this.preferredMode === "auto" ? this.detectedMode : this.preferredMode;
37
- }
38
- /**
39
- * Attempts to parse as many complete frames as possible from the current buffer.
40
- */
41
- consumeFrames() {
42
- while (true) {
43
- const framingMode = this.resolveFramingMode();
44
- if (!framingMode) {
45
- return;
46
- }
47
- try {
48
- const payload = framingMode === "content-length"
49
- ? this.readContentLengthPayload()
50
- : this.readLinePayload();
51
- if (payload === null) {
52
- return;
53
- }
54
- const message = JSON.parse(payload);
55
- this.emit("message", message);
56
- }
57
- catch (error) {
58
- this.emit("error", error);
59
- }
60
- }
61
- }
62
- /**
63
- * Resolves the framing mode using the configured preference or buffered bytes.
64
- */
65
- resolveFramingMode() {
66
- if (this.preferredMode !== "auto") {
67
- return this.preferredMode;
68
- }
69
- if (this.detectedMode) {
70
- return this.detectedMode;
71
- }
72
- const sample = this.buffer.toString("utf8");
73
- const trimmed = sample.trimStart();
74
- if (trimmed.startsWith("Content-Length:")) {
75
- this.detectedMode = "content-length";
76
- return this.detectedMode;
77
- }
78
- if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
79
- this.detectedMode = "line";
80
- return this.detectedMode;
81
- }
82
- return null;
83
- }
84
- /**
85
- * Reads one Content-Length framed payload when available.
86
- */
87
- readContentLengthPayload() {
88
- const headerEnd = this.buffer.indexOf("\r\n\r\n");
89
- if (headerEnd < 0) {
90
- return null;
91
- }
92
- const headerText = this.buffer.subarray(0, headerEnd).toString("utf8");
93
- const lengthMatch = /Content-Length:\s*(\d+)/i.exec(headerText);
94
- if (!lengthMatch) {
95
- throw new Error("Missing Content-Length header.");
96
- }
97
- const contentLength = Number(lengthMatch[1]);
98
- const frameStart = headerEnd + 4;
99
- const frameEnd = frameStart + contentLength;
100
- if (this.buffer.length < frameEnd) {
101
- return null;
102
- }
103
- const payload = this.buffer.subarray(frameStart, frameEnd).toString("utf8");
104
- this.buffer = this.buffer.subarray(frameEnd);
105
- return payload;
106
- }
107
- /**
108
- * Reads one newline-delimited JSON payload when available.
109
- */
110
- readLinePayload() {
111
- const lineEnd = this.buffer.indexOf("\n");
112
- if (lineEnd < 0) {
113
- return null;
114
- }
115
- const payload = this.buffer.toString("utf8", 0, lineEnd).replace(/\r$/, "");
116
- this.buffer = this.buffer.subarray(lineEnd + 1);
117
- return payload;
118
- }
119
- }
120
- /**
121
- * Provides a framed MCP stdio message writer compatible with line-delimited and Content-Length transports.
122
- */
123
- export class McpMessageWriter {
124
- /**
125
- * Stores the target writable stream.
126
- */
127
- stream;
128
- /**
129
- * Stores the active framing mode used for outbound messages.
130
- */
131
- mode;
132
- /**
133
- * Creates a writer bound to one writable stream.
134
- */
135
- constructor(stream, mode = "line") {
136
- this.stream = stream;
137
- this.mode = mode;
138
- }
139
- /**
140
- * Updates the framing mode used for future outbound messages.
141
- */
142
- setFramingMode(mode) {
143
- this.mode = mode;
144
- }
145
- /**
146
- * Writes one framed JSON-RPC message to the target stream.
147
- */
148
- write(message) {
149
- const payload = Buffer.from(JSON.stringify(message), "utf8");
150
- if (this.mode === "content-length") {
151
- const header = Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, "utf8");
152
- this.stream.write(Buffer.concat([header, payload]));
153
- return;
154
- }
155
- this.stream.write(Buffer.concat([payload, Buffer.from("\n", "utf8")]));
156
- }
157
- }
158
- /**
159
- * Creates a typed message reader.
160
- */
161
- export function createMessageReader(stream, preferredMode = "auto") {
162
- return new McpMessageReader(stream, preferredMode);
163
- }
164
- /**
165
- * Creates a typed message writer.
166
- */
167
- export function createMessageWriter(stream, mode = "line") {
168
- return new McpMessageWriter(stream, mode);
169
- }
170
- /**
171
- * Builds one JSON-RPC success response.
172
- */
173
- export function jsonRpcResult(id, result) {
174
- return {
175
- jsonrpc: "2.0",
176
- id,
177
- result
178
- };
179
- }
180
- /**
181
- * Builds one JSON-RPC error response.
182
- */
183
- export function jsonRpcError(id, code, message, data) {
184
- return {
185
- jsonrpc: "2.0",
186
- id,
187
- error: {
188
- code,
189
- message,
190
- data
191
- }
192
- };
193
- }