@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.
- package/LICENSE +7 -0
- package/README.md +110 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/client/index.d.ts +762 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +110 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.test.d.ts +2 -0
- package/dist/client/index.test.d.ts.map +1 -0
- package/dist/client/index.test.js +59 -0
- package/dist/client/index.test.js.map +1 -0
- package/dist/client/sse.d.ts +22 -0
- package/dist/client/sse.d.ts.map +1 -0
- package/dist/client/sse.js +91 -0
- package/dist/client/sse.js.map +1 -0
- package/dist/client/stdio.d.ts +49 -0
- package/dist/client/stdio.d.ts.map +1 -0
- package/dist/client/stdio.js +134 -0
- package/dist/client/stdio.js.map +1 -0
- package/dist/client/stdio.test.d.ts +2 -0
- package/dist/client/stdio.test.d.ts.map +1 -0
- package/dist/client/stdio.test.js +51 -0
- package/dist/client/stdio.test.js.map +1 -0
- package/dist/client/websocket.d.ts +17 -0
- package/dist/client/websocket.d.ts.map +1 -0
- package/dist/client/websocket.js +61 -0
- package/dist/client/websocket.js.map +1 -0
- package/dist/server/index.d.ts +102 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +106 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.test.d.ts +2 -0
- package/dist/server/index.test.d.ts.map +1 -0
- package/dist/server/index.test.js +53 -0
- package/dist/server/index.test.js.map +1 -0
- package/dist/server/sse.d.ts +46 -0
- package/dist/server/sse.d.ts.map +1 -0
- package/dist/server/sse.js +116 -0
- package/dist/server/sse.js.map +1 -0
- package/dist/server/stdio.d.ts +28 -0
- package/dist/server/stdio.d.ts.map +1 -0
- package/dist/server/stdio.js +69 -0
- package/dist/server/stdio.js.map +1 -0
- package/dist/server/stdio.test.d.ts +2 -0
- package/dist/server/stdio.test.d.ts.map +1 -0
- package/dist/server/stdio.test.js +87 -0
- package/dist/server/stdio.test.js.map +1 -0
- package/dist/shared/protocol.d.ts +92 -0
- package/dist/shared/protocol.d.ts.map +1 -0
- package/dist/shared/protocol.js +225 -0
- package/dist/shared/protocol.js.map +1 -0
- package/dist/shared/stdio.d.ts +13 -0
- package/dist/shared/stdio.d.ts.map +1 -0
- package/dist/shared/stdio.js +31 -0
- package/dist/shared/stdio.js.map +1 -0
- package/dist/shared/stdio.test.d.ts +2 -0
- package/dist/shared/stdio.test.d.ts.map +1 -0
- package/dist/shared/stdio.test.js +27 -0
- package/dist/shared/stdio.test.js.map +1 -0
- package/dist/shared/transport.d.ts +39 -0
- package/dist/shared/transport.d.ts.map +1 -0
- package/dist/shared/transport.js +2 -0
- package/dist/shared/transport.js.map +1 -0
- package/dist/types.d.ts +26033 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +967 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { JSONRPCMessageSchema } from "../types.js";
|
|
2
|
+
const SUBPROTOCOL = "mcp";
|
|
3
|
+
/**
|
|
4
|
+
* Client transport for WebSocket: this will connect to a server over the WebSocket protocol.
|
|
5
|
+
*/
|
|
6
|
+
export class WebSocketClientTransport {
|
|
7
|
+
constructor(url) {
|
|
8
|
+
this._url = url;
|
|
9
|
+
}
|
|
10
|
+
start() {
|
|
11
|
+
if (this._socket) {
|
|
12
|
+
throw new Error("WebSocketClientTransport already started! If using Client class, note that connect() calls start() automatically.");
|
|
13
|
+
}
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
this._socket = new WebSocket(this._url, SUBPROTOCOL);
|
|
16
|
+
this._socket.onerror = (event) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const error = "error" in event
|
|
19
|
+
? event.error
|
|
20
|
+
: new Error(`WebSocket error: ${JSON.stringify(event)}`);
|
|
21
|
+
reject(error);
|
|
22
|
+
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
23
|
+
};
|
|
24
|
+
this._socket.onopen = () => {
|
|
25
|
+
resolve();
|
|
26
|
+
};
|
|
27
|
+
this._socket.onclose = () => {
|
|
28
|
+
var _a;
|
|
29
|
+
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
30
|
+
};
|
|
31
|
+
this._socket.onmessage = (event) => {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
let message;
|
|
34
|
+
try {
|
|
35
|
+
message = JSONRPCMessageSchema.parse(JSON.parse(event.data));
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
(_b = this.onmessage) === null || _b === void 0 ? void 0 : _b.call(this, message);
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async close() {
|
|
46
|
+
var _a;
|
|
47
|
+
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
48
|
+
}
|
|
49
|
+
send(message) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
var _a;
|
|
52
|
+
if (!this._socket) {
|
|
53
|
+
reject(new Error("Not connected"));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
(_a = this._socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(message));
|
|
57
|
+
resolve();
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=websocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../../src/client/websocket.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnE,MAAM,WAAW,GAAG,K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AC;oBACf,MAAA,IAAI,CAAC,OAAO,qDAAG,KAAc,CAAC,CAAC;oBAC/B,OAAO;gBACT,CAAC;gBAED,MAAA,IAAI,CAAC,SAAS,qDAAG,OAAO,CAAC,CAAC;YAC5B,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK;;QACT,MAAA,IAAI,CAAC,OAAO,0CAAE,KAAK,EAAE,CAAC;IACxB,CAAC;IAED,IAAI,CAAC,OAAuB;QAC1B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;;YACrC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBAClB,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;gBACnC,OAAO;YACT,CAAC;YAED,MAAA,IAAI,CAAC,OAAO,0CAAE,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;YAC5C,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ProgressCallback, Protocol } from "../shared/protocol.js";
|
|
2
|
+
import { ClientCapabilities, CreateMessageRequest, Implementation, ListRootsRequest, LoggingMessageNotification, Notification, Request, ResourceUpdatedNotification, Result, ServerNotification, ServerRequest, ServerResult } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* An MCP server on top of a pluggable transport.
|
|
5
|
+
*
|
|
6
|
+
* This server will automatically respond to the initialization flow as initiated from the client.
|
|
7
|
+
*
|
|
8
|
+
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Custom schemas
|
|
12
|
+
* const CustomRequestSchema = RequestSchema.extend({...})
|
|
13
|
+
* const CustomNotificationSchema = NotificationSchema.extend({...})
|
|
14
|
+
* const CustomResultSchema = ResultSchema.extend({...})
|
|
15
|
+
*
|
|
16
|
+
* // Type aliases
|
|
17
|
+
* type CustomRequest = z.infer<typeof CustomRequestSchema>
|
|
18
|
+
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
|
|
19
|
+
* type CustomResult = z.infer<typeof CustomResultSchema>
|
|
20
|
+
*
|
|
21
|
+
* // Create typed server
|
|
22
|
+
* const server = new Server<CustomRequest, CustomNotification, CustomResult>({
|
|
23
|
+
* name: "CustomServer",
|
|
24
|
+
* version: "1.0.0"
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class Server<RequestT extends Request = Request, NotificationT extends Notification = Notification, ResultT extends Result = Result> extends Protocol<ServerRequest | RequestT, ServerNotification | NotificationT, ServerResult | ResultT> {
|
|
29
|
+
private _serverInfo;
|
|
30
|
+
private _clientCapabilities?;
|
|
31
|
+
private _clientVersion?;
|
|
32
|
+
/**
|
|
33
|
+
* Callback for when initialization has fully completed (i.e., the client has sent an `initialized` notification).
|
|
34
|
+
*/
|
|
35
|
+
oninitialized?: () => void;
|
|
36
|
+
/**
|
|
37
|
+
* Initializes this server with the given name and version information.
|
|
38
|
+
*/
|
|
39
|
+
constructor(_serverInfo: Implementation);
|
|
40
|
+
private _oninitialize;
|
|
41
|
+
/**
|
|
42
|
+
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
43
|
+
*/
|
|
44
|
+
getClientCapabilities(): ClientCapabilities | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
47
|
+
*/
|
|
48
|
+
getClientVersion(): Implementation | undefined;
|
|
49
|
+
private getCapabilities;
|
|
50
|
+
ping(): Promise<{
|
|
51
|
+
_meta?: import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough"> | undefined;
|
|
52
|
+
}>;
|
|
53
|
+
createMessage(params: CreateMessageRequest["params"], onprogress?: ProgressCallback): Promise<import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
|
|
54
|
+
_meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
55
|
+
}, {
|
|
56
|
+
model: import("zod").ZodString;
|
|
57
|
+
stopReason: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodEnum<["endTurn", "stopSequence", "maxTokens"]>, import("zod").ZodString]>>;
|
|
58
|
+
role: import("zod").ZodEnum<["user", "assistant"]>;
|
|
59
|
+
content: import("zod").ZodDiscriminatedUnion<"type", [import("zod").ZodObject<{
|
|
60
|
+
type: import("zod").ZodLiteral<"text">;
|
|
61
|
+
text: import("zod").ZodString;
|
|
62
|
+
}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
|
|
63
|
+
type: import("zod").ZodLiteral<"text">;
|
|
64
|
+
text: import("zod").ZodString;
|
|
65
|
+
}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
|
|
66
|
+
type: import("zod").ZodLiteral<"text">;
|
|
67
|
+
text: import("zod").ZodString;
|
|
68
|
+
}, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<{
|
|
69
|
+
type: import("zod").ZodLiteral<"image">;
|
|
70
|
+
data: import("zod").ZodString;
|
|
71
|
+
mimeType: import("zod").ZodString;
|
|
72
|
+
}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
|
|
73
|
+
type: import("zod").ZodLiteral<"image">;
|
|
74
|
+
data: import("zod").ZodString;
|
|
75
|
+
mimeType: import("zod").ZodString;
|
|
76
|
+
}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
|
|
77
|
+
type: import("zod").ZodLiteral<"image">;
|
|
78
|
+
data: import("zod").ZodString;
|
|
79
|
+
mimeType: import("zod").ZodString;
|
|
80
|
+
}, import("zod").ZodTypeAny, "passthrough">>]>;
|
|
81
|
+
}>, import("zod").ZodTypeAny, "passthrough">>;
|
|
82
|
+
listRoots(params?: ListRootsRequest["params"], onprogress?: ProgressCallback): Promise<import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
|
|
83
|
+
_meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
|
|
84
|
+
}, {
|
|
85
|
+
roots: import("zod").ZodArray<import("zod").ZodObject<{
|
|
86
|
+
uri: import("zod").ZodString;
|
|
87
|
+
name: import("zod").ZodOptional<import("zod").ZodString>;
|
|
88
|
+
}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
|
|
89
|
+
uri: import("zod").ZodString;
|
|
90
|
+
name: import("zod").ZodOptional<import("zod").ZodString>;
|
|
91
|
+
}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
|
|
92
|
+
uri: import("zod").ZodString;
|
|
93
|
+
name: import("zod").ZodOptional<import("zod").ZodString>;
|
|
94
|
+
}, import("zod").ZodTypeAny, "passthrough">>, "many">;
|
|
95
|
+
}>, import("zod").ZodTypeAny, "passthrough">>;
|
|
96
|
+
sendLoggingMessage(params: LoggingMessageNotification["params"]): Promise<void>;
|
|
97
|
+
sendResourceUpdated(params: ResourceUpdatedNotification["params"]): Promise<void>;
|
|
98
|
+
sendResourceListChanged(): Promise<void>;
|
|
99
|
+
sendToolListChanged(): Promise<void>;
|
|
100
|
+
sendPromptListChanged(): Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EAGpB,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Protocol } from "../shared/protocol.js";
|
|
2
|
+
import { CreateMessageResultSchema, EmptyResultSchema, InitializedNotificationSchema, InitializeRequestSchema, LATEST_PROTOCOL_VERSION, ListPromptsRequestSchema, ListResourcesRequestSchema, ListRootsResultSchema, ListToolsRequestSchema, SetLevelRequestSchema, SUPPORTED_PROTOCOL_VERSIONS } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* An MCP server on top of a pluggable transport.
|
|
5
|
+
*
|
|
6
|
+
* This server will automatically respond to the initialization flow as initiated from the client.
|
|
7
|
+
*
|
|
8
|
+
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Custom schemas
|
|
12
|
+
* const CustomRequestSchema = RequestSchema.extend({...})
|
|
13
|
+
* const CustomNotificationSchema = NotificationSchema.extend({...})
|
|
14
|
+
* const CustomResultSchema = ResultSchema.extend({...})
|
|
15
|
+
*
|
|
16
|
+
* // Type aliases
|
|
17
|
+
* type CustomRequest = z.infer<typeof CustomRequestSchema>
|
|
18
|
+
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
|
|
19
|
+
* type CustomResult = z.infer<typeof CustomResultSchema>
|
|
20
|
+
*
|
|
21
|
+
* // Create typed server
|
|
22
|
+
* const server = new Server<CustomRequest, CustomNotification, CustomResult>({
|
|
23
|
+
* name: "CustomServer",
|
|
24
|
+
* version: "1.0.0"
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export class Server extends Protocol {
|
|
29
|
+
/**
|
|
30
|
+
* Initializes this server with the given name and version information.
|
|
31
|
+
*/
|
|
32
|
+
constructor(_serverInfo) {
|
|
33
|
+
super();
|
|
34
|
+
this._serverInfo = _serverInfo;
|
|
35
|
+
this.setRequestHandler(InitializeRequestSchema, (request) => this._oninitialize(request));
|
|
36
|
+
this.setNotificationHandler(InitializedNotificationSchema, () => { var _a; return (_a = this.oninitialized) === null || _a === void 0 ? void 0 : _a.call(this); });
|
|
37
|
+
}
|
|
38
|
+
async _oninitialize(request) {
|
|
39
|
+
const requestedVersion = request.params.protocolVersion;
|
|
40
|
+
this._clientCapabilities = request.params.capabilities;
|
|
41
|
+
this._clientVersion = request.params.clientInfo;
|
|
42
|
+
return {
|
|
43
|
+
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : LATEST_PROTOCOL_VERSION,
|
|
44
|
+
capabilities: this.getCapabilities(),
|
|
45
|
+
serverInfo: this._serverInfo,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
50
|
+
*/
|
|
51
|
+
getClientCapabilities() {
|
|
52
|
+
return this._clientCapabilities;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
56
|
+
*/
|
|
57
|
+
getClientVersion() {
|
|
58
|
+
return this._clientVersion;
|
|
59
|
+
}
|
|
60
|
+
getCapabilities() {
|
|
61
|
+
return {
|
|
62
|
+
prompts: this._requestHandlers.has(ListPromptsRequestSchema.shape.method.value)
|
|
63
|
+
? {}
|
|
64
|
+
: undefined,
|
|
65
|
+
resources: this._requestHandlers.has(ListResourcesRequestSchema.shape.method.value)
|
|
66
|
+
? {}
|
|
67
|
+
: undefined,
|
|
68
|
+
tools: this._requestHandlers.has(ListToolsRequestSchema.shape.method.value)
|
|
69
|
+
? {}
|
|
70
|
+
: undefined,
|
|
71
|
+
logging: this._requestHandlers.has(SetLevelRequestSchema.shape.method.value)
|
|
72
|
+
? {}
|
|
73
|
+
: undefined,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async ping() {
|
|
77
|
+
return this.request({ method: "ping" }, EmptyResultSchema);
|
|
78
|
+
}
|
|
79
|
+
async createMessage(params, onprogress) {
|
|
80
|
+
return this.request({ method: "sampling/createMessage", params }, CreateMessageResultSchema, onprogress);
|
|
81
|
+
}
|
|
82
|
+
async listRoots(params, onprogress) {
|
|
83
|
+
return this.request({ method: "roots/list", params }, ListRootsResultSchema, onprogress);
|
|
84
|
+
}
|
|
85
|
+
async sendLoggingMessage(params) {
|
|
86
|
+
return this.notification({ method: "notifications/message", params });
|
|
87
|
+
}
|
|
88
|
+
async sendResourceUpdated(params) {
|
|
89
|
+
return this.notification({
|
|
90
|
+
method: "notifications/resources/updated",
|
|
91
|
+
params,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
async sendResourceListChanged() {
|
|
95
|
+
return this.notification({
|
|
96
|
+
method: "notifications/resources/list_changed",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async sendToolListChanged() {
|
|
100
|
+
return this.notification({ method: "notifications/tools/list_changed" });
|
|
101
|
+
}
|
|
102
|
+
async sendPromptListChanged() {
|
|
103
|
+
return this.notification({ method: "notifications/prompts/list_changed" });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAGL,yBAAyB,EACzB,iBAAiB,EAEjB,6BAA6B,EAE7B,uBAAuB,EAEvB,uBAAuB,EACvB,wBAAwB,EACxB,0BAA0B,EAE1B,qBAAqB,EACrB,sBAAsB,EAUtB,qBAAqB,EACrB,2BAA2B,EAC5B,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,MAIX,SAAQ,QAIT;IASC;;OAEG;IACH,YAAoB,WAA2B;QAC7C,KAAK,EAAE,CAAC;QADU,gBAAW,GAAX,WAAW,CAAgB;QAG7C,IAAI,CAAC,iBAAiB,CAAC,uBAAuB,EAAE,CAAC,OAAO,EAAE,EAAE,CAC1D,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;IAC3E,CAAC;IAED,KAAK,CAAC,qBAAqB;QACzB,OAAO,IAAI,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,oCAAoC,EAAE,CAAC,CAAC;IAC7E,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/server/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
/* eslint-disable no-constant-binary-expression */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
|
4
|
+
import { Server } from "./index.js";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
|
|
7
|
+
/*
|
|
8
|
+
Test that custom request/notification/result schemas can be used with the Server class.
|
|
9
|
+
*/
|
|
10
|
+
test("should typecheck", () => {
|
|
11
|
+
const GetWeatherRequestSchema = RequestSchema.extend({
|
|
12
|
+
method: z.literal("weather/get"),
|
|
13
|
+
params: z.object({
|
|
14
|
+
city: z.string(),
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
const GetForecastRequestSchema = RequestSchema.extend({
|
|
18
|
+
method: z.literal("weather/forecast"),
|
|
19
|
+
params: z.object({
|
|
20
|
+
city: z.string(),
|
|
21
|
+
days: z.number(),
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
const WeatherForecastNotificationSchema = NotificationSchema.extend({
|
|
25
|
+
method: z.literal("weather/alert"),
|
|
26
|
+
params: z.object({
|
|
27
|
+
severity: z.enum(["warning", "watch"]),
|
|
28
|
+
message: z.string(),
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
const WeatherRequestSchema = GetWeatherRequestSchema.or(GetForecastRequestSchema);
|
|
32
|
+
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
|
|
33
|
+
const WeatherResultSchema = ResultSchema.extend({
|
|
34
|
+
temperature: z.number(),
|
|
35
|
+
conditions: z.string(),
|
|
36
|
+
});
|
|
37
|
+
// Create a typed Server for weather data
|
|
38
|
+
const weatherServer = new Server({
|
|
39
|
+
name: "WeatherServer",
|
|
40
|
+
version: "1.0.0",
|
|
41
|
+
});
|
|
42
|
+
// Typecheck that only valid weather requests/notifications/results are allowed
|
|
43
|
+
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {
|
|
44
|
+
return {
|
|
45
|
+
temperature: 72,
|
|
46
|
+
conditions: "sunny",
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
weatherServer.setNotificationHandler(WeatherForecastNotificationSchema, (notification) => {
|
|
50
|
+
console.log(`Weather alert: ${notification.params.message}`);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/server/index.test.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,kDAAkD;AAClD,6DAA6D;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE9E;;EAEE;AACF,IAAI,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAC5B,MAAM,uBAAuB,GAAG,aAAa,CAAC,MAAM,CAAC;QACnD,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC;QAChC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC;YACf,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;SACjB,CAAC;KACH,CAAC,CAAC;IAEH,MAAM,wBAAwB,GAAG,aAAa,CAAC,MAAM,CAAC;QACpD,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,kBAAkB,CAAC;QACr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@ -0,0 +1,46 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { Transport } from "../shared/transport.js";
|
|
3
|
+
import { JSONRPCMessage } from "../types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Server transport for SSE: this will send messages over an SSE connection and receive messages from HTTP POST requests.
|
|
6
|
+
*
|
|
7
|
+
* This transport is only available in Node.js environments.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SSEServerTransport implements Transport {
|
|
10
|
+
private _endpoint;
|
|
11
|
+
private res;
|
|
12
|
+
private _sseResponse?;
|
|
13
|
+
private _sessionId;
|
|
14
|
+
onclose?: () => void;
|
|
15
|
+
onerror?: (error: Error) => void;
|
|
16
|
+
onmessage?: (message: JSONRPCMessage) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
|
|
19
|
+
*/
|
|
20
|
+
constructor(_endpoint: string, res: ServerResponse);
|
|
21
|
+
/**
|
|
22
|
+
* Handles the initial SSE connection request.
|
|
23
|
+
*
|
|
24
|
+
* This should be called when a GET request is made to establish the SSE stream.
|
|
25
|
+
*/
|
|
26
|
+
start(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Handles incoming POST messages.
|
|
29
|
+
*
|
|
30
|
+
* This should be called when a POST request is made to send a message to the server.
|
|
31
|
+
*/
|
|
32
|
+
handlePostMessage(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Handle a client message, regardless of how it arrived. This can be used to inform the server of messages that arrive via a means different than HTTP POST.
|
|
35
|
+
*/
|
|
36
|
+
handleMessage(message: unknown): Promise<void>;
|
|
37
|
+
close(): Promise<void>;
|
|
38
|
+
send(message: JSONRPCMessage): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the session ID for this transport.
|
|
41
|
+
*
|
|
42
|
+
* This can be used to route incoming POST requests.
|
|
43
|
+
*/
|
|
44
|
+
get sessionId(): string;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/server/sse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAwB,MAAM,aAAa,CAAC;AAMnE;;;;GAIG;AACH,qBAAa,kBAAmB,YAAW,SAAS;IAYhD,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,GAAG;IAZb,OAAO,CAAC,YAAY,CAAC,CAAiB;IACtC,OAAO,CAAC,UAAU,CAAS;IAE3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IAE9C;;OAEG;gBAEO,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,cAAc;IAK7B;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAyB5B;;;;OAIG;IACG,iBAAiB,CACrB,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,GAClB,OAAO,CAAC,IAAI,CAAC;IAkChB;;OAEG;IACG,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAY9C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAUlD;;;;OAIG;IACH,IAAI,SAAS,IAAI,MAAM,CAEtB;CACF"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { JSONRPCMessageSchema } from "../types.js";
|
|
3
|
+
import getRawBody from "raw-body";
|
|
4
|
+
import contentType from "content-type";
|
|
5
|
+
const MAXIMUM_MESSAGE_SIZE = "4mb";
|
|
6
|
+
/**
|
|
7
|
+
* Server transport for SSE: this will send messages over an SSE connection and receive messages from HTTP POST requests.
|
|
8
|
+
*
|
|
9
|
+
* This transport is only available in Node.js environments.
|
|
10
|
+
*/
|
|
11
|
+
export class SSEServerTransport {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
|
|
14
|
+
*/
|
|
15
|
+
constructor(_endpoint, res) {
|
|
16
|
+
this._endpoint = _endpoint;
|
|
17
|
+
this.res = res;
|
|
18
|
+
this._sessionId = randomUUID();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Handles the initial SSE connection request.
|
|
22
|
+
*
|
|
23
|
+
* This should be called when a GET request is made to establish the SSE stream.
|
|
24
|
+
*/
|
|
25
|
+
async start() {
|
|
26
|
+
if (this._sseResponse) {
|
|
27
|
+
throw new Error("SSEServerTransport already started! If using Server class, note that connect() calls start() automatically.");
|
|
28
|
+
}
|
|
29
|
+
this.res.writeHead(200, {
|
|
30
|
+
"Content-Type": "text/event-stream",
|
|
31
|
+
"Cache-Control": "no-cache",
|
|
32
|
+
Connection: "keep-alive",
|
|
33
|
+
});
|
|
34
|
+
// Send the endpoint event
|
|
35
|
+
this.res.write(`event: endpoint\ndata: ${encodeURI(this._endpoint)}?sessionId=${this._sessionId}\n\n`);
|
|
36
|
+
this._sseResponse = this.res;
|
|
37
|
+
this.res.on("close", () => {
|
|
38
|
+
var _a;
|
|
39
|
+
this._sseResponse = undefined;
|
|
40
|
+
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Handles incoming POST messages.
|
|
45
|
+
*
|
|
46
|
+
* This should be called when a POST request is made to send a message to the server.
|
|
47
|
+
*/
|
|
48
|
+
async handlePostMessage(req, res) {
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
if (!this._sseResponse) {
|
|
51
|
+
const message = "SSE connection not established";
|
|
52
|
+
res.writeHead(500).end(message);
|
|
53
|
+
throw new Error(message);
|
|
54
|
+
}
|
|
55
|
+
let body;
|
|
56
|
+
try {
|
|
57
|
+
const ct = contentType.parse((_a = req.headers["content-type"]) !== null && _a !== void 0 ? _a : "");
|
|
58
|
+
if (ct.type !== "application/json") {
|
|
59
|
+
throw new Error(`Unsupported content-type: ${ct}`);
|
|
60
|
+
}
|
|
61
|
+
body = await getRawBody(req, {
|
|
62
|
+
limit: MAXIMUM_MESSAGE_SIZE,
|
|
63
|
+
encoding: (_b = ct.parameters.charset) !== null && _b !== void 0 ? _b : "utf-8",
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
res.writeHead(400).end(String(error));
|
|
68
|
+
(_c = this.onerror) === null || _c === void 0 ? void 0 : _c.call(this, error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
await this.handleMessage(JSON.parse(body));
|
|
73
|
+
}
|
|
74
|
+
catch (_d) {
|
|
75
|
+
res.writeHead(400).end(`Invalid message: ${body}`);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
res.writeHead(202).end("Accepted");
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Handle a client message, regardless of how it arrived. This can be used to inform the server of messages that arrive via a means different than HTTP POST.
|
|
82
|
+
*/
|
|
83
|
+
async handleMessage(message) {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
let parsedMessage;
|
|
86
|
+
try {
|
|
87
|
+
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
(_b = this.onmessage) === null || _b === void 0 ? void 0 : _b.call(this, parsedMessage);
|
|
94
|
+
}
|
|
95
|
+
async close() {
|
|
96
|
+
var _a, _b;
|
|
97
|
+
(_a = this._sseResponse) === null || _a === void 0 ? void 0 : _a.end();
|
|
98
|
+
this._sseResponse = undefined;
|
|
99
|
+
(_b = this.onclose) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
100
|
+
}
|
|
101
|
+
async send(message) {
|
|
102
|
+
if (!this._sseResponse) {
|
|
103
|
+
throw new Error("Not connected");
|
|
104
|
+
}
|
|
105
|
+
this._sseResponse.write(`event: message\ndata: ${JSON.stringify(message)}\n\n`);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns the session ID for this transport.
|
|
109
|
+
*
|
|
110
|
+
* This can be used to route incoming POST requests.
|
|
111
|
+
*/
|
|
112
|
+
get sessionId() {
|
|
113
|
+
return this._sessionId;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/server/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAkB,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,UAAU,MAAM,UAAU,CAAC;AAClC,OAAO,WAAW,MAAM,cAAc,CAAC;AAEvC,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IAQ7B;;OAEG;IACH,YACU,SAAiB,EACjB,GAAmB;QADnB,cAAS,GAAT,SAAS,CAAQ;QACjB,QAAG,GAAH,GAAG,CAAgB;QAE3B,IAAI,CAAC,UAAU,GAAG,UAAU,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK;QACT,IAAI,IA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CAAC,IAAI,CAAC,OAAuB;QAChC,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACnC,CAAC;QAED,IAAI,CAAC,YAAY,CAAC,KAAK,CACrB,yBAAyB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CACvD,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Readable, Writable } from "node:stream";
|
|
2
|
+
import { JSONRPCMessage } from "../types.js";
|
|
3
|
+
import { Transport } from "../shared/transport.js";
|
|
4
|
+
/**
|
|
5
|
+
* Server transport for stdio: this communicates with a MCP client by reading from the current process' stdin and writing to stdout.
|
|
6
|
+
*
|
|
7
|
+
* This transport is only available in Node.js environments.
|
|
8
|
+
*/
|
|
9
|
+
export declare class StdioServerTransport implements Transport {
|
|
10
|
+
private _stdin;
|
|
11
|
+
private _stdout;
|
|
12
|
+
private _readBuffer;
|
|
13
|
+
private _started;
|
|
14
|
+
constructor(_stdin?: Readable, _stdout?: Writable);
|
|
15
|
+
onclose?: () => void;
|
|
16
|
+
onerror?: (error: Error) => void;
|
|
17
|
+
onmessage?: (message: JSONRPCMessage) => void;
|
|
18
|
+
_ondata: (chunk: Buffer) => void;
|
|
19
|
+
_onerror: (error: Error) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Starts listening for messages on stdin.
|
|
22
|
+
*/
|
|
23
|
+
start(): Promise<void>;
|
|
24
|
+
private processReadBuffer;
|
|
25
|
+
close(): Promise<void>;
|
|
26
|
+
send(message: JSONRPCMessage): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=stdio.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["../../src/server/stdio.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;;;GAIG;AACH,qBAAa,oBAAqB,YAAW,SAAS;IAKlD,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,OAAO;IALjB,OAAO,CAAC,WAAW,CAAgC;IACnD,OAAO,CAAC,QAAQ,CAAS;gBAGf,MAAM,GAAE,QAAwB,EAChC,OAAO,GAAE,QAAyB;IAG5C,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IAG9C,OAAO,UAAW,MAAM,UAGtB;IACF,QAAQ,UAAW,KAAK,UAEtB;IAEF;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAY5B,OAAO,CAAC,iBAAiB;IAenB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAO5B,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAU7C"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import { ReadBuffer, serializeMessage } from "../shared/stdio.js";
|
|
3
|
+
/**
|
|
4
|
+
* Server transport for stdio: this communicates with a MCP client by reading from the current process' stdin and writing to stdout.
|
|
5
|
+
*
|
|
6
|
+
* This transport is only available in Node.js environments.
|
|
7
|
+
*/
|
|
8
|
+
export class StdioServerTransport {
|
|
9
|
+
constructor(_stdin = process.stdin, _stdout = process.stdout) {
|
|
10
|
+
this._stdin = _stdin;
|
|
11
|
+
this._stdout = _stdout;
|
|
12
|
+
this._readBuffer = new ReadBuffer();
|
|
13
|
+
this._started = false;
|
|
14
|
+
// Arrow functions to bind `this` properly, while maintaining function identity.
|
|
15
|
+
this._ondata = (chunk) => {
|
|
16
|
+
this._readBuffer.append(chunk);
|
|
17
|
+
this.processReadBuffer();
|
|
18
|
+
};
|
|
19
|
+
this._onerror = (error) => {
|
|
20
|
+
var _a;
|
|
21
|
+
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Starts listening for messages on stdin.
|
|
26
|
+
*/
|
|
27
|
+
async start() {
|
|
28
|
+
if (this._started) {
|
|
29
|
+
throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");
|
|
30
|
+
}
|
|
31
|
+
this._started = true;
|
|
32
|
+
this._stdin.on("data", this._ondata);
|
|
33
|
+
this._stdin.on("error", this._onerror);
|
|
34
|
+
}
|
|
35
|
+
processReadBuffer() {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
while (true) {
|
|
38
|
+
try {
|
|
39
|
+
const message = this._readBuffer.readMessage();
|
|
40
|
+
if (message === null) {
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
(_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, message);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
(_b = this.onerror) === null || _b === void 0 ? void 0 : _b.call(this, error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async close() {
|
|
51
|
+
var _a;
|
|
52
|
+
this._stdin.off("data", this._ondata);
|
|
53
|
+
this._stdin.off("error", this._onerror);
|
|
54
|
+
this._readBuffer.clear();
|
|
55
|
+
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
56
|
+
}
|
|
57
|
+
send(message) {
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
const json = serializeMessage(message);
|
|
60
|
+
if (this._stdout.write(json)) {
|
|
61
|
+
resolve();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this._stdout.once("drain", resolve);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=stdio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["../../src/server/stdio.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAIlE;;;;GAIG;AACH,MAAM,OAAO,oBAAoB;IAI/B,YACU,SAAmB,OAAO,CAAC,KAAK,EAChC,UAAoB,OAAO,CAAC,MAAM;QADlC,WAAM,GAAN,MAAM,CAA0B;QAChC,YAAO,GAAP,OAAO,CAA2B;QALpC,gBAAW,GAAe,IAAI,UAAU,EAAE,CAAC;QAC3C,aAAQ,GAAG,KAAK,CAAC;QAWzB,gFAAgF;QAChF,YAAO,GAAG,CAAC,KAAa,EAAE,EAAE;YAC1B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC/B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC3B,CAAC,CAAC;QACF,aAAQ,GAAG,CAAC,KAAY,EAAE,EAAE;;YAC1B,MAAA,IAAI,CAAC,OAAO,qDAAG,KAAK,CAAC,CAAC;QACxB,CAAC,CAAC;IAbC,CAAC;IAeJ;;OAEG;IACH,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CACb,+GAA+G,CAChH,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAA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