@modelcontextprotocol/sdk 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.
Files changed (71) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +110 -0
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +116 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/client/index.d.ts +762 -0
  8. package/dist/client/index.d.ts.map +1 -0
  9. package/dist/client/index.js +110 -0
  10. package/dist/client/index.js.map +1 -0
  11. package/dist/client/index.test.d.ts +2 -0
  12. package/dist/client/index.test.d.ts.map +1 -0
  13. package/dist/client/index.test.js +59 -0
  14. package/dist/client/index.test.js.map +1 -0
  15. package/dist/client/sse.d.ts +22 -0
  16. package/dist/client/sse.d.ts.map +1 -0
  17. package/dist/client/sse.js +91 -0
  18. package/dist/client/sse.js.map +1 -0
  19. package/dist/client/stdio.d.ts +49 -0
  20. package/dist/client/stdio.d.ts.map +1 -0
  21. package/dist/client/stdio.js +134 -0
  22. package/dist/client/stdio.js.map +1 -0
  23. package/dist/client/stdio.test.d.ts +2 -0
  24. package/dist/client/stdio.test.d.ts.map +1 -0
  25. package/dist/client/stdio.test.js +51 -0
  26. package/dist/client/stdio.test.js.map +1 -0
  27. package/dist/client/websocket.d.ts +17 -0
  28. package/dist/client/websocket.d.ts.map +1 -0
  29. package/dist/client/websocket.js +61 -0
  30. package/dist/client/websocket.js.map +1 -0
  31. package/dist/server/index.d.ts +102 -0
  32. package/dist/server/index.d.ts.map +1 -0
  33. package/dist/server/index.js +106 -0
  34. package/dist/server/index.js.map +1 -0
  35. package/dist/server/index.test.d.ts +2 -0
  36. package/dist/server/index.test.d.ts.map +1 -0
  37. package/dist/server/index.test.js +53 -0
  38. package/dist/server/index.test.js.map +1 -0
  39. package/dist/server/sse.d.ts +46 -0
  40. package/dist/server/sse.d.ts.map +1 -0
  41. package/dist/server/sse.js +116 -0
  42. package/dist/server/sse.js.map +1 -0
  43. package/dist/server/stdio.d.ts +28 -0
  44. package/dist/server/stdio.d.ts.map +1 -0
  45. package/dist/server/stdio.js +69 -0
  46. package/dist/server/stdio.js.map +1 -0
  47. package/dist/server/stdio.test.d.ts +2 -0
  48. package/dist/server/stdio.test.d.ts.map +1 -0
  49. package/dist/server/stdio.test.js +87 -0
  50. package/dist/server/stdio.test.js.map +1 -0
  51. package/dist/shared/protocol.d.ts +92 -0
  52. package/dist/shared/protocol.d.ts.map +1 -0
  53. package/dist/shared/protocol.js +225 -0
  54. package/dist/shared/protocol.js.map +1 -0
  55. package/dist/shared/stdio.d.ts +13 -0
  56. package/dist/shared/stdio.d.ts.map +1 -0
  57. package/dist/shared/stdio.js +31 -0
  58. package/dist/shared/stdio.js.map +1 -0
  59. package/dist/shared/stdio.test.d.ts +2 -0
  60. package/dist/shared/stdio.test.d.ts.map +1 -0
  61. package/dist/shared/stdio.test.js +27 -0
  62. package/dist/shared/stdio.test.js.map +1 -0
  63. package/dist/shared/transport.d.ts +39 -0
  64. package/dist/shared/transport.d.ts.map +1 -0
  65. package/dist/shared/transport.js +2 -0
  66. package/dist/shared/transport.js.map +1 -0
  67. package/dist/types.d.ts +26033 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +967 -0
  70. package/dist/types.js.map +1 -0
  71. package/package.json +61 -0
@@ -0,0 +1,87 @@
1
+ import { Readable, Writable } from "node:stream";
2
+ import { ReadBuffer, serializeMessage } from "../shared/stdio.js";
3
+ import { StdioServerTransport } from "./stdio.js";
4
+ let input;
5
+ let outputBuffer;
6
+ let output;
7
+ beforeEach(() => {
8
+ input = new Readable({
9
+ // We'll use input.push() instead.
10
+ read: () => { },
11
+ });
12
+ outputBuffer = new ReadBuffer();
13
+ output = new Writable({
14
+ write(chunk, encoding, callback) {
15
+ outputBuffer.append(chunk);
16
+ callback();
17
+ },
18
+ });
19
+ });
20
+ test("should start then close cleanly", async () => {
21
+ const server = new StdioServerTransport(input, output);
22
+ server.onerror = (error) => {
23
+ throw error;
24
+ };
25
+ let didClose = false;
26
+ server.onclose = () => {
27
+ didClose = true;
28
+ };
29
+ await server.start();
30
+ expect(didClose).toBeFalsy();
31
+ await server.close();
32
+ expect(didClose).toBeTruthy();
33
+ });
34
+ test("should not read until started", async () => {
35
+ const server = new StdioServerTransport(input, output);
36
+ server.onerror = (error) => {
37
+ throw error;
38
+ };
39
+ let didRead = false;
40
+ const readMessage = new Promise((resolve) => {
41
+ server.onmessage = (message) => {
42
+ didRead = true;
43
+ resolve(message);
44
+ };
45
+ });
46
+ const message = {
47
+ jsonrpc: "2.0",
48
+ id: 1,
49
+ method: "ping",
50
+ };
51
+ input.push(serializeMessage(message));
52
+ expect(didRead).toBeFalsy();
53
+ await server.start();
54
+ expect(await readMessage).toEqual(message);
55
+ });
56
+ test("should read multiple messages", async () => {
57
+ const server = new StdioServerTransport(input, output);
58
+ server.onerror = (error) => {
59
+ throw error;
60
+ };
61
+ const messages = [
62
+ {
63
+ jsonrpc: "2.0",
64
+ id: 1,
65
+ method: "ping",
66
+ },
67
+ {
68
+ jsonrpc: "2.0",
69
+ method: "notifications/initialized",
70
+ },
71
+ ];
72
+ const readMessages = [];
73
+ const finished = new Promise((resolve) => {
74
+ server.onmessage = (message) => {
75
+ readMessages.push(message);
76
+ if (JSON.stringify(message) === JSON.stringify(messages[1])) {
77
+ resolve();
78
+ }
79
+ };
80
+ });
81
+ input.push(serializeMessage(messages[0]));
82
+ input.push(serializeMessage(messages[1]));
83
+ await server.start();
84
+ await finished;
85
+ expect(readMessages).toEqual(messages);
86
+ });
87
+ //# sourceMappingURL=stdio.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.test.js","sourceRoot":"","sources":["../../src/server/stdio.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,oBAA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C7C,MAAM,CAAC,SAAS,GAAG,CAAC,OAAO,EAAE,EAAE;YAC7B,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC3B,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5D,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1C,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IACrB,MAAM,QAAQ,CAAC;IACf,MAAM,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACzC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,92 @@
1
+ import { ZodLiteral, ZodObject, ZodType, z } from "zod";
2
+ import { JSONRPCRequest, Notification, Progress, Request, Result } from "../types.js";
3
+ import { Transport } from "./transport.js";
4
+ /**
5
+ * Callback for progress notifications.
6
+ */
7
+ export type ProgressCallback = (progress: Progress) => void;
8
+ /**
9
+ * Implements MCP protocol framing on top of a pluggable transport, including
10
+ * features like request/response linking, notifications, and progress.
11
+ */
12
+ export declare class Protocol<SendRequestT extends Request, SendNotificationT extends Notification, SendResultT extends Result> {
13
+ private _transport?;
14
+ private _requestMessageId;
15
+ protected _requestHandlers: Map<string, (request: JSONRPCRequest) => Promise<SendResultT>>;
16
+ private _notificationHandlers;
17
+ private _responseHandlers;
18
+ private _progressHandlers;
19
+ /**
20
+ * Callback for when the connection is closed for any reason.
21
+ *
22
+ * This is invoked when close() is called as well.
23
+ */
24
+ onclose?: () => void;
25
+ /**
26
+ * Callback for when an error occurs.
27
+ *
28
+ * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
29
+ */
30
+ onerror?: (error: Error) => void;
31
+ /**
32
+ * A handler to invoke for any request types that do not have their own handler installed.
33
+ */
34
+ fallbackRequestHandler?: (request: Request) => Promise<SendResultT>;
35
+ /**
36
+ * A handler to invoke for any notification types that do not have their own handler installed.
37
+ */
38
+ fallbackNotificationHandler?: (notification: Notification) => Promise<void>;
39
+ constructor();
40
+ /**
41
+ * Attaches to the given transport, starts it, and starts listening for messages.
42
+ *
43
+ * The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
44
+ */
45
+ connect(transport: Transport): Promise<void>;
46
+ private _onclose;
47
+ private _onerror;
48
+ private _onnotification;
49
+ private _onrequest;
50
+ private _onprogress;
51
+ private _onresponse;
52
+ get transport(): Transport | undefined;
53
+ /**
54
+ * Closes the connection.
55
+ */
56
+ close(): Promise<void>;
57
+ /**
58
+ * Sends a request and wait for a response, with optional progress notifications in the meantime (if supported by the server).
59
+ *
60
+ * Do not use this method to emit notifications! Use notification() instead.
61
+ */
62
+ request<T extends ZodType<object>>(request: SendRequestT, resultSchema: T, onprogress?: ProgressCallback): Promise<z.infer<T>>;
63
+ /**
64
+ * Emits a notification, which is a one-way message that does not expect a response.
65
+ */
66
+ notification(notification: SendNotificationT): Promise<void>;
67
+ /**
68
+ * Registers a handler to invoke when this protocol object receives a request with the given method.
69
+ *
70
+ * Note that this will replace any previous request handler for the same method.
71
+ */
72
+ setRequestHandler<T extends ZodObject<{
73
+ method: ZodLiteral<string>;
74
+ }>>(requestSchema: T, handler: (request: z.infer<T>) => SendResultT | Promise<SendResultT>): void;
75
+ /**
76
+ * Removes the request handler for the given method.
77
+ */
78
+ removeRequestHandler(method: string): void;
79
+ /**
80
+ * Registers a handler to invoke when this protocol object receives a notification with the given method.
81
+ *
82
+ * Note that this will replace any previous notification handler for the same method.
83
+ */
84
+ setNotificationHandler<T extends ZodObject<{
85
+ method: ZodLiteral<string>;
86
+ }>>(notificationSchema: T, handler: (notification: z.infer<T>) => void | Promise<void>): void;
87
+ /**
88
+ * Removes the notification handler for the given method.
89
+ */
90
+ removeNotificationHandler(method: string): void;
91
+ }
92
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/shared/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxD,OAAO,EAIL,cAAc,EAGd,YAAY,EAEZ,QAAQ,EAGR,OAAO,EACP,MAAM,EACP,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;AAE5D;;;GAGG;AACH,qBAAa,QAAQ,CACnB,YAAY,SAAS,OAAO,EAC5B,iBAAiB,SAAS,YAAY,EACtC,WAAW,SAAS,MAAM;IAE1B,OAAO,CAAC,UAAU,CAAC,CAAY;IAC/B,OAAO,CAAC,iBAAiB,CAAK;IAC9B,SAAS,CAAC,gBAAgB,EAAE,GAAG,CAC7B,MAAM,EACN,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,WAAW,CAAC,CAClD,CAAa;IACd,OAAO,CAAC,qBAAqB,CAGf;IACd,OAAO,CAAC,iBAAiB,CAGX;IACd,OAAO,CAAC,iBAAiB,CAA4C;IAErE;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEjC;;OAEG;IACH,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpE;;O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AAY,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAC1D,IAAI;IAQP;;OAEG;IACH,yBAAyB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAGhD"}
@@ -0,0 +1,225 @@
1
+ import { ErrorCode, McpError, PingRequestSchema, ProgressNotificationSchema, } from "../types.js";
2
+ /**
3
+ * Implements MCP protocol framing on top of a pluggable transport, including
4
+ * features like request/response linking, notifications, and progress.
5
+ */
6
+ export class Protocol {
7
+ constructor() {
8
+ this._requestMessageId = 0;
9
+ this._requestHandlers = new Map();
10
+ this._notificationHandlers = new Map();
11
+ this._responseHandlers = new Map();
12
+ this._progressHandlers = new Map();
13
+ this.setNotificationHandler(ProgressNotificationSchema, (notification) => {
14
+ this._onprogress(notification);
15
+ });
16
+ this.setRequestHandler(PingRequestSchema,
17
+ // Automatic pong by default.
18
+ (_request) => ({}));
19
+ }
20
+ /**
21
+ * Attaches to the given transport, starts it, and starts listening for messages.
22
+ *
23
+ * The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
24
+ */
25
+ async connect(transport) {
26
+ this._transport = transport;
27
+ this._transport.onclose = () => {
28
+ this._onclose();
29
+ };
30
+ this._transport.onerror = (error) => {
31
+ this._onerror(error);
32
+ };
33
+ this._transport.onmessage = (message) => {
34
+ if (!("method" in message)) {
35
+ this._onresponse(message);
36
+ }
37
+ else if ("id" in message) {
38
+ this._onrequest(message);
39
+ }
40
+ else {
41
+ this._onnotification(message);
42
+ }
43
+ };
44
+ await this._transport.start();
45
+ }
46
+ _onclose() {
47
+ var _a;
48
+ const responseHandlers = this._responseHandlers;
49
+ this._responseHandlers = new Map();
50
+ this._progressHandlers.clear();
51
+ this._transport = undefined;
52
+ (_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
53
+ const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
54
+ for (const handler of responseHandlers.values()) {
55
+ handler(error);
56
+ }
57
+ }
58
+ _onerror(error) {
59
+ var _a;
60
+ (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
61
+ }
62
+ _onnotification(notification) {
63
+ var _a;
64
+ const handler = (_a = this._notificationHandlers.get(notification.method)) !== null && _a !== void 0 ? _a : this.fallbackNotificationHandler;
65
+ // Ignore notifications not being subscribed to.
66
+ if (handler === undefined) {
67
+ return;
68
+ }
69
+ handler(notification).catch((error) => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
70
+ }
71
+ _onrequest(request) {
72
+ var _a, _b;
73
+ const handler = (_a = this._requestHandlers.get(request.method)) !== null && _a !== void 0 ? _a : this.fallbackRequestHandler;
74
+ if (handler === undefined) {
75
+ (_b = this._transport) === null || _b === void 0 ? void 0 : _b.send({
76
+ jsonrpc: "2.0",
77
+ id: request.id,
78
+ error: {
79
+ code: ErrorCode.MethodNotFound,
80
+ message: "Method not found",
81
+ },
82
+ }).catch((error) => this._onerror(new Error(`Failed to send an error response: ${error}`)));
83
+ return;
84
+ }
85
+ handler(request)
86
+ .then((result) => {
87
+ var _a;
88
+ (_a = this._transport) === null || _a === void 0 ? void 0 : _a.send({
89
+ result,
90
+ jsonrpc: "2.0",
91
+ id: request.id,
92
+ });
93
+ }, (error) => {
94
+ var _a, _b;
95
+ return (_a = this._transport) === null || _a === void 0 ? void 0 : _a.send({
96
+ jsonrpc: "2.0",
97
+ id: request.id,
98
+ error: {
99
+ code: Number.isSafeInteger(error["code"])
100
+ ? error["code"]
101
+ : ErrorCode.InternalError,
102
+ message: (_b = error.message) !== null && _b !== void 0 ? _b : "Internal error",
103
+ },
104
+ });
105
+ })
106
+ .catch((error) => this._onerror(new Error(`Failed to send response: ${error}`)));
107
+ }
108
+ _onprogress(notification) {
109
+ const { progress, total, progressToken } = notification.params;
110
+ const handler = this._progressHandlers.get(Number(progressToken));
111
+ if (handler === undefined) {
112
+ this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
113
+ return;
114
+ }
115
+ handler({ progress, total });
116
+ }
117
+ _onresponse(response) {
118
+ const messageId = response.id;
119
+ const handler = this._responseHandlers.get(Number(messageId));
120
+ if (handler === undefined) {
121
+ this._onerror(new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
122
+ return;
123
+ }
124
+ this._responseHandlers.delete(Number(messageId));
125
+ this._progressHandlers.delete(Number(messageId));
126
+ if ("result" in response) {
127
+ handler(response);
128
+ }
129
+ else {
130
+ const error = new McpError(response.error.code, response.error.message, response.error.data);
131
+ handler(error);
132
+ }
133
+ }
134
+ get transport() {
135
+ return this._transport;
136
+ }
137
+ /**
138
+ * Closes the connection.
139
+ */
140
+ async close() {
141
+ var _a;
142
+ await ((_a = this._transport) === null || _a === void 0 ? void 0 : _a.close());
143
+ }
144
+ /**
145
+ * Sends a request and wait for a response, with optional progress notifications in the meantime (if supported by the server).
146
+ *
147
+ * Do not use this method to emit notifications! Use notification() instead.
148
+ */
149
+ request(request, resultSchema, onprogress) {
150
+ return new Promise((resolve, reject) => {
151
+ if (!this._transport) {
152
+ reject(new Error("Not connected"));
153
+ return;
154
+ }
155
+ const messageId = this._requestMessageId++;
156
+ const jsonrpcRequest = {
157
+ ...request,
158
+ jsonrpc: "2.0",
159
+ id: messageId,
160
+ };
161
+ if (onprogress) {
162
+ this._progressHandlers.set(messageId, onprogress);
163
+ jsonrpcRequest.params = {
164
+ ...request.params,
165
+ _meta: { progressToken: messageId },
166
+ };
167
+ }
168
+ this._responseHandlers.set(messageId, (response) => {
169
+ if (response instanceof Error) {
170
+ return reject(response);
171
+ }
172
+ try {
173
+ const result = resultSchema.parse(response.result);
174
+ resolve(result);
175
+ }
176
+ catch (error) {
177
+ reject(error);
178
+ }
179
+ });
180
+ this._transport.send(jsonrpcRequest).catch(reject);
181
+ });
182
+ }
183
+ /**
184
+ * Emits a notification, which is a one-way message that does not expect a response.
185
+ */
186
+ async notification(notification) {
187
+ if (!this._transport) {
188
+ throw new Error("Not connected");
189
+ }
190
+ const jsonrpcNotification = {
191
+ ...notification,
192
+ jsonrpc: "2.0",
193
+ };
194
+ await this._transport.send(jsonrpcNotification);
195
+ }
196
+ /**
197
+ * Registers a handler to invoke when this protocol object receives a request with the given method.
198
+ *
199
+ * Note that this will replace any previous request handler for the same method.
200
+ */
201
+ setRequestHandler(requestSchema, handler) {
202
+ this._requestHandlers.set(requestSchema.shape.method.value, (request) => Promise.resolve(handler(requestSchema.parse(request))));
203
+ }
204
+ /**
205
+ * Removes the request handler for the given method.
206
+ */
207
+ removeRequestHandler(method) {
208
+ this._requestHandlers.delete(method);
209
+ }
210
+ /**
211
+ * Registers a handler to invoke when this protocol object receives a notification with the given method.
212
+ *
213
+ * Note that this will replace any previous notification handler for the same method.
214
+ */
215
+ setNotificationHandler(notificationSchema, handler) {
216
+ this._notificationHandlers.set(notificationSchema.shape.method.value, (notification) => Promise.resolve(handler(notificationSchema.parse(notification))));
217
+ }
218
+ /**
219
+ * Removes the notification handler for the given method.
220
+ */
221
+ removeNotificationHandler(method) {
222
+ this._notificationHandlers.delete(method);
223
+ }
224
+ }
225
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/shared/protocol.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EAKT,QAAQ,EAER,iBAAiB,EAGjB,0BAA0B,GAG3B,MAAM,aAAa,CAAC;AAQrB;;;GAGG;AACH,MAAM,OAAO,QAAQ;IA6CnB;QAvCQ,sBAAiB,GAAG,CAAC,CAAC;QACpB,qBAAgB,GAGtB,IAAI,GAAG,EAAE,CAAC;QACN,0BAAqB,GAGzB,IAAI,GAAG,EAAE,CAAC;QACN,sBAAiB,GAGrB,IAAI,GAAG,EAAE,CAAC;QACN,sBAAiB,GAAkC,IAAI,GAAG,EAAE,CAAC;QA2BnE,IAAI,CAAC,sBAAsB,CAAC,0BAA0B,EAAE,CAAC,YAAY,EAAE,E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KAAK,CAAC,eAAe,CAAC,CAAC;QACnC,CAAC;QAED,MAAM,mBAAmB,GAAwB;YAC/C,GAAG,YAAY;YACf,OAAO,EAAE,KAAK;SACf,CAAC;QAEF,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,iBAAiB,CAKf,aAAgB,EAChB,OAAoE;QAEpE,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE,CACtE,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CACvD,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,oBAAoB,CAAC,MAAc;QACjC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;;;OAIG;IACH,sBAAsB,CAKpB,kBAAqB,EACrB,OAA2D;QAE3D,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAC5B,kBAAkB,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EACrC,CAAC,YAAY,EAAE,EAAE,CACf,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CACnE,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,yBAAyB,CAAC,MAAc;QACtC,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;CACF"}
@@ -0,0 +1,13 @@
1
+ import { JSONRPCMessage } from "../types.js";
2
+ /**
3
+ * Buffers a continuous stdio stream into discrete JSON-RPC messages.
4
+ */
5
+ export declare class ReadBuffer {
6
+ private _buffer?;
7
+ append(chunk: Buffer): void;
8
+ readMessage(): JSONRPCMessage | null;
9
+ clear(): void;
10
+ }
11
+ export declare function deserializeMessage(line: string): JSONRPCMessage;
12
+ export declare function serializeMessage(message: JSONRPCMessage): string;
13
+ //# sourceMappingURL=stdio.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["../../src/shared/stdio.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAwB,MAAM,aAAa,CAAC;AAEnE;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAC,CAAS;IAEzB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAI3B,WAAW,IAAI,cAAc,GAAG,IAAI;IAepC,KAAK,IAAI,IAAI;CAGd;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAE/D;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,cAAc,GAAG,MAAM,CAEhE"}
@@ -0,0 +1,31 @@
1
+ import { JSONRPCMessageSchema } from "../types.js";
2
+ /**
3
+ * Buffers a continuous stdio stream into discrete JSON-RPC messages.
4
+ */
5
+ export class ReadBuffer {
6
+ append(chunk) {
7
+ this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
8
+ }
9
+ readMessage() {
10
+ if (!this._buffer) {
11
+ return null;
12
+ }
13
+ const index = this._buffer.indexOf("\n");
14
+ if (index === -1) {
15
+ return null;
16
+ }
17
+ const line = this._buffer.toString("utf8", 0, index);
18
+ this._buffer = this._buffer.subarray(index + 1);
19
+ return deserializeMessage(line);
20
+ }
21
+ clear() {
22
+ this._buffer = undefined;
23
+ }
24
+ }
25
+ export function deserializeMessage(line) {
26
+ return JSONRPCMessageSchema.parse(JSON.parse(line));
27
+ }
28
+ export function serializeMessage(message) {
29
+ return JSON.stringify(message) + "\n";
30
+ }
31
+ //# sourceMappingURL=stdio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.js","sourceRoot":"","sources":["../../src/shared/stdio.ts"],"names":[],"mappings":"AAAA,O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","sourceRoot":"","sources":["../../src/shared/stdio.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,27 @@
1
+ import { ReadBuffer } from "./stdio.js";
2
+ const testMessage = {
3
+ jsonrpc: "2.0",
4
+ method: "foobar",
5
+ };
6
+ test("should have no messages after initialization", () => {
7
+ const readBuffer = new ReadBuffer();
8
+ expect(readBuffer.readMessage()).toBeNull();
9
+ });
10
+ test("should only yield a message after a newline", () => {
11
+ const readBuffer = new ReadBuffer();
12
+ readBuffer.append(Buffer.from(JSON.stringify(testMessage)));
13
+ expect(readBuffer.readMessage()).toBeNull();
14
+ readBuffer.append(Buffer.from("\n"));
15
+ expect(readBuffer.readMessage()).toEqual(testMessage);
16
+ expect(readBuffer.readMessage()).toBeNull();
17
+ });
18
+ test("should be reusable after clearing", () => {
19
+ const readBuffer = new ReadBuffer();
20
+ readBuffer.append(Buffer.from("foobar"));
21
+ readBuffer.clear();
22
+ expect(readBuffer.readMessage()).toBeNull();
23
+ readBuffer.append(Buffer.from(JSON.stringify(testMessage)));
24
+ readBuffer.append(Buffer.from("\n"));
25
+ expect(readBuffer.readMessage()).toEqual(testMessage);
26
+ });
27
+ //# sourceMappingURL=stdio.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stdio.test.js","sourceRoot":"","sources":["../../src/shared/stdio.test.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,WAAW,GAAmB;IAClC,OAAO,EAAE,KAAK;IACd,MAAM,EAAE,QAAQ;CACjB,CAAC;AAEF,IAAI,CAAC,8CAA8C,EAAE,GAAG,EAAE;IACxD,MAAM,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IACpC,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,6CAA6C,EAAE,GAAG,EAAE;IACvD,MAAM,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IAEpC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAC5D,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;IAE5C,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACrC,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACtD,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC7C,MAAM,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IAEpC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IACzC,UAAU,CAAC,KAAK,EAAE,CAAC;IACnB,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;IAE5C,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAC5D,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACrC,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;AACxD,CAAC,CAAC,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { JSONRPCMessage } from "../types.js";
2
+ /**
3
+ * Describes the minimal contract for a MCP transport that a client or server can communicate over.
4
+ */
5
+ export interface Transport {
6
+ /**
7
+ * Starts processing messages on the transport, including any connection steps that might need to be taken.
8
+ *
9
+ * This method should only be called after callbacks are installed, or else messages may be lost.
10
+ *
11
+ * NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes, as they will implicitly call start().
12
+ */
13
+ start(): Promise<void>;
14
+ /**
15
+ * Sends a JSON-RPC message (request or response).
16
+ */
17
+ send(message: JSONRPCMessage): Promise<void>;
18
+ /**
19
+ * Closes the connection.
20
+ */
21
+ close(): Promise<void>;
22
+ /**
23
+ * Callback for when the connection is closed for any reason.
24
+ *
25
+ * This should be invoked when close() is called as well.
26
+ */
27
+ onclose?: () => void;
28
+ /**
29
+ * Callback for when an error occurs.
30
+ *
31
+ * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
32
+ */
33
+ onerror?: (error: Error) => void;
34
+ /**
35
+ * Callback for when a message (request or response) is received over the connection.
36
+ */
37
+ onmessage?: (message: JSONRPCMessage) => void;
38
+ }
39
+ //# sourceMappingURL=transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../src/shared/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;OAMG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7C;;OAEG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEjC;;OAEG;IACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;CAC/C"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/shared/transport.ts"],"names":[],"mappings":""}