orbitx 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +65 -9
- package/dist/core/anthropic-provider.d.ts +15 -0
- package/dist/core/anthropic-provider.d.ts.map +1 -0
- package/dist/core/anthropic-provider.js +208 -0
- package/dist/core/anthropic-provider.js.map +1 -0
- package/dist/core/deepseek-provider.d.ts +1 -4
- package/dist/core/deepseek-provider.d.ts.map +1 -1
- package/dist/core/deepseek-provider.js +15 -15
- package/dist/core/deepseek-provider.js.map +1 -1
- package/dist/core/mcp-client.d.ts +0 -8
- package/dist/core/mcp-client.d.ts.map +1 -1
- package/dist/core/mcp-client.js +14 -20
- package/dist/core/mcp-client.js.map +1 -1
- package/dist/core/mcp-computer.d.ts +3 -5
- package/dist/core/mcp-computer.d.ts.map +1 -1
- package/dist/core/mcp-computer.js +71 -28
- package/dist/core/mcp-computer.js.map +1 -1
- package/dist/core/mcp-connection.d.ts +17 -0
- package/dist/core/mcp-connection.d.ts.map +1 -1
- package/dist/core/mcp-connection.js +24 -2
- package/dist/core/mcp-connection.js.map +1 -1
- package/dist/core/mcp-ipc-connection.d.ts +13 -14
- package/dist/core/mcp-ipc-connection.d.ts.map +1 -1
- package/dist/core/mcp-ipc-connection.js +75 -64
- package/dist/core/mcp-ipc-connection.js.map +1 -1
- package/dist/core/mcp-server.js +4 -4
- package/dist/core/mcp-server.js.map +1 -1
- package/dist/core/mcp-ws-connection.d.ts +45 -0
- package/dist/core/mcp-ws-connection.d.ts.map +1 -0
- package/dist/core/mcp-ws-connection.js +165 -0
- package/dist/core/mcp-ws-connection.js.map +1 -0
- package/dist/core/ollama-provider.d.ts +1 -4
- package/dist/core/ollama-provider.d.ts.map +1 -1
- package/dist/core/ollama-provider.js +16 -16
- package/dist/core/ollama-provider.js.map +1 -1
- package/dist/core/openai-provider.d.ts +15 -0
- package/dist/core/openai-provider.d.ts.map +1 -0
- package/dist/core/openai-provider.js +180 -0
- package/dist/core/openai-provider.js.map +1 -0
- package/dist/core/tool-schema-translator.d.ts +12 -2
- package/dist/core/tool-schema-translator.d.ts.map +1 -1
- package/dist/core/tool-schema-translator.js +29 -2
- package/dist/core/tool-schema-translator.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/sandbox.js +19 -3
- package/dist/sandbox.js.map +1 -1
- package/package.json +5 -2
|
@@ -2,54 +2,61 @@ import os from "os";
|
|
|
2
2
|
import { randomUUID } from "crypto";
|
|
3
3
|
import { spawn, execFileSync } from "child_process";
|
|
4
4
|
import fs from "fs";
|
|
5
|
+
import net from "net";
|
|
5
6
|
import path from "path";
|
|
7
|
+
import MCPIPCConnection from "./mcp-ipc-connection.js";
|
|
8
|
+
import MCPWSConnection from "./mcp-ws-connection.js";
|
|
6
9
|
export class MCPComputer {
|
|
7
10
|
#mountPath;
|
|
8
11
|
#ports;
|
|
9
12
|
#hostNetwork;
|
|
10
13
|
#image;
|
|
11
|
-
#ipcPath;
|
|
12
14
|
#containerName;
|
|
13
15
|
#child = null;
|
|
14
16
|
#exitHandler = null;
|
|
17
|
+
#connectionMode;
|
|
18
|
+
#ipcPath;
|
|
19
|
+
#wsPort;
|
|
20
|
+
#wsToken;
|
|
15
21
|
constructor(mountPath, ports, image = "orbitx-sandbox:0.1") {
|
|
16
|
-
|
|
17
|
-
throw new Error("This feature is not currently supported natively on Windows. " +
|
|
18
|
-
"Please run via WSL, or use a Unix-like filesystem (this feature needs one).");
|
|
19
|
-
}
|
|
22
|
+
this.#connectionMode = os.platform() === "win32" ? "ws" : "ipc";
|
|
20
23
|
this.#hostNetwork = ports === "*";
|
|
21
|
-
if (this.#hostNetwork && os.platform() !== "linux") {
|
|
22
|
-
throw new Error("Host network mode (ports: \"*\") requires Docker's --network host, " +
|
|
23
|
-
"which is only supported on Linux. Pass an explicit array of ports instead.");
|
|
24
|
-
}
|
|
25
24
|
this.#mountPath = path.resolve(mountPath);
|
|
26
25
|
this.#ports = ports;
|
|
27
26
|
this.#image = image;
|
|
28
|
-
this.#ipcPath = `/tmp/mcp-server-${randomUUID()}`;
|
|
29
27
|
this.#containerName = `mcp-sandbox-${randomUUID()}`;
|
|
28
|
+
this.#ipcPath = `/tmp/mcp-server-${randomUUID()}`;
|
|
29
|
+
this.#wsPort = 0;
|
|
30
|
+
this.#wsToken = randomUUID();
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
getConnection(wsHost = "localhost") {
|
|
33
|
+
if (this.#connectionMode === "ipc") {
|
|
34
|
+
return new MCPIPCConnection({
|
|
35
|
+
socketPath: `${this.#ipcPath}/socket.sock`,
|
|
36
|
+
mode: "client",
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (!this.#wsPort) {
|
|
40
|
+
throw new Error("getConnection() is only available in 'ws' mode after start() has resolved a port.");
|
|
41
|
+
}
|
|
42
|
+
return new MCPWSConnection({
|
|
43
|
+
mode: "client",
|
|
44
|
+
url: `ws://${wsHost}:${this.#wsPort}`,
|
|
45
|
+
token: this.#wsToken,
|
|
46
|
+
});
|
|
33
47
|
}
|
|
34
48
|
getPresentsHostPath() {
|
|
35
49
|
return path.join(this.#mountPath, "presents");
|
|
36
50
|
}
|
|
37
|
-
isHostNetwork() {
|
|
38
|
-
return this.#hostNetwork;
|
|
39
|
-
}
|
|
40
|
-
getPortUrl(port, host = "localhost") {
|
|
41
|
-
if (!this.#hostNetwork && !this.#ports.includes(port)) {
|
|
42
|
-
throw new Error(`Port ${port} was not exposed for this sandbox. ` +
|
|
43
|
-
`Available ports: ${this.#ports.join(", ") || "none"}. ` +
|
|
44
|
-
`Pass ports: "*" at construction time to allow any port.`);
|
|
45
|
-
}
|
|
46
|
-
return `http://${host}:${port}`;
|
|
47
|
-
}
|
|
48
51
|
#prepareHostPaths(containerUid = 1000, containerGid = 1000) {
|
|
49
52
|
const workspaceHost = path.join(this.#mountPath, "workspace");
|
|
50
53
|
const storageHost = path.join(this.#mountPath, "mcp-server-storage");
|
|
51
54
|
const presentsHost = path.join(this.#mountPath, "presents");
|
|
52
|
-
const
|
|
55
|
+
const userInputsHost = path.join(this.#mountPath, "user-inputs");
|
|
56
|
+
const dirs = [workspaceHost, storageHost, presentsHost, userInputsHost];
|
|
57
|
+
if (this.#connectionMode === "ipc") {
|
|
58
|
+
dirs.push(this.#ipcPath);
|
|
59
|
+
}
|
|
53
60
|
for (const dir of dirs) {
|
|
54
61
|
fs.mkdirSync(dir, { recursive: true });
|
|
55
62
|
}
|
|
@@ -72,7 +79,25 @@ export class MCPComputer {
|
|
|
72
79
|
}
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
|
-
|
|
82
|
+
/** Finds a free TCP port on the host by briefly binding to port 0. */
|
|
83
|
+
#pickFreePort() {
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
const srv = net.createServer();
|
|
86
|
+
srv.unref();
|
|
87
|
+
srv.on("error", reject);
|
|
88
|
+
srv.listen(0, () => {
|
|
89
|
+
const address = srv.address();
|
|
90
|
+
if (address && typeof address === "object") {
|
|
91
|
+
const port = address.port;
|
|
92
|
+
srv.close(() => resolve(port));
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
srv.close(() => reject(new Error("Could not determine a free port.")));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
#buildDockerArgs() {
|
|
76
101
|
const workspaceHost = path.join(this.#mountPath, "workspace");
|
|
77
102
|
const storageHost = path.join(this.#mountPath, "mcp-server-storage");
|
|
78
103
|
const presentsHost = path.join(this.#mountPath, "presents");
|
|
@@ -84,9 +109,19 @@ export class MCPComputer {
|
|
|
84
109
|
"-v", `${storageHost}:/home/ubuntu/mcp-data`,
|
|
85
110
|
"-v", `${presentsHost}:/home/ubuntu/presents`,
|
|
86
111
|
"-v", `${userInputsHost}:/home/ubuntu/user-inputs`,
|
|
87
|
-
"-v", `${this.#ipcPath}:/tmp/mcp-server`,
|
|
88
112
|
"-e", "PRESENT_PATH=/home/ubuntu/presents",
|
|
89
113
|
];
|
|
114
|
+
if (this.#connectionMode === "ipc") {
|
|
115
|
+
args.push("-v", `${this.#ipcPath}:/tmp/mcp-server`);
|
|
116
|
+
args.push("-e", "CONNECTION_MODE=IPC");
|
|
117
|
+
args.push("-e", "CONNECTION_PATH=/tmp/mcp-server/socket.sock");
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
args.push("-e", "CONNECTION_MODE=WS");
|
|
121
|
+
args.push("-e", "CONNECTION_HOST=0.0.0.0");
|
|
122
|
+
args.push("-e", `CONNECTION_PORT=${this.#wsPort}`);
|
|
123
|
+
args.push("-e", `CONNECTION_TOKEN=${this.#wsToken}`);
|
|
124
|
+
}
|
|
90
125
|
if (this.#hostNetwork) {
|
|
91
126
|
args.push("--network", "host");
|
|
92
127
|
}
|
|
@@ -94,13 +129,21 @@ export class MCPComputer {
|
|
|
94
129
|
for (const port of this.#ports) {
|
|
95
130
|
args.push("-p", `${port}:${port}`);
|
|
96
131
|
}
|
|
132
|
+
// The WS control channel needs its own published port too, since
|
|
133
|
+
// it isn't part of the user-facing #ports list.
|
|
134
|
+
if (this.#connectionMode === "ws") {
|
|
135
|
+
args.push("-p", `${this.#wsPort}:${this.#wsPort}`);
|
|
136
|
+
}
|
|
97
137
|
}
|
|
98
138
|
args.push(this.#image);
|
|
99
139
|
return args;
|
|
100
140
|
}
|
|
101
|
-
start() {
|
|
141
|
+
async start() {
|
|
142
|
+
if (this.#connectionMode === "ws") {
|
|
143
|
+
this.#wsPort = await this.#pickFreePort();
|
|
144
|
+
}
|
|
102
145
|
this.#prepareHostPaths();
|
|
103
|
-
const args = this
|
|
146
|
+
const args = this.#buildDockerArgs();
|
|
104
147
|
const child = spawn("docker", args, { stdio: "ignore" });
|
|
105
148
|
this.#child = child;
|
|
106
149
|
this.#exitHandler = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-computer.js","sourceRoot":"","sources":["../../src/core/mcp-computer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,OAAO,WAAW;IACpB,UAAU,CAAS;IACnB,MAAM,CAAiB;IACvB,YAAY,CAAU;IACtB,MAAM,CAAS;IACf,
|
|
1
|
+
{"version":3,"file":"mcp-computer.js","sourceRoot":"","sources":["../../src/core/mcp-computer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,gBAAgB,MAAM,yBAAyB,CAAC;AACvD,OAAO,eAAe,MAAM,wBAAwB,CAAC;AAIrD,MAAM,OAAO,WAAW;IACpB,UAAU,CAAS;IACnB,MAAM,CAAiB;IACvB,YAAY,CAAU;IACtB,MAAM,CAAS;IACf,cAAc,CAAS;IACvB,MAAM,GAAoC,IAAI,CAAC;IAC/C,YAAY,GAAwB,IAAI,CAAC;IAEzC,eAAe,CAAiB;IAChC,QAAQ,CAAS;IACjB,OAAO,CAAS;IAChB,QAAQ,CAAS;IAEjB,YAAY,SAAiB,EAAE,KAAqB,EAAE,QAAgB,oBAAoB;QACtF,IAAI,CAAC,eAAe,GAAG,EAAE,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,I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GAAG,wBAAwB,CAAC;QAC/C,MAAM,YAAY,GAAG,uBAAuB,CAAC;QAC7C,MAAM,cAAc,GAAG,0BAA0B,CAAC;QAClD,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY;YAC/B,CAAC,CAAC,8DAA8D;YAChE,CAAC,CAAE,IAAI,CAAC,MAAmB,CAAC,MAAM,GAAG,CAAC;gBAClC,CAAC,CAAE,IAAI,CAAC,MAAmB,CAAC,IAAI,CAAC,IAAI,CAAC;gBACtC,CAAC,CAAC,MAAM,CAAC;QAEjB,OAAO;YACH,cAAc;YACd,mBAAmB,aAAa,EAAE;YAClC,0BAA0B,cAAc,EAAE;YAC1C,kBAAkB,YAAY,0GAA0G;YACxI,eAAe,SAAS,EAAE;YAC1B,qBAAqB;SACxB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;CACJ;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for all MCP connections.
|
|
4
|
+
*
|
|
5
|
+
* Two logical, one-directional channels are defined:
|
|
6
|
+
* - "write_to_server" -> received by the server side as "server_read"
|
|
7
|
+
* - "write_to_client" -> received by the client side as "client_read"
|
|
8
|
+
*
|
|
9
|
+
* This keeps client writes from looping back to the client (and vice versa)
|
|
10
|
+
* once a connection is shared/subscribed by both a client and a server on
|
|
11
|
+
* the same object, which was the bug with the old single "write"/"read" pair.
|
|
12
|
+
*
|
|
13
|
+
* Subclasses that talk to a real transport (IPC socket, WebSocket, etc.)
|
|
14
|
+
* should pass `overrided = true` and implement their own wiring: forward
|
|
15
|
+
* "write_to_server"/"write_to_client" over the wire, and emit
|
|
16
|
+
* "server_read"/"client_read" locally depending on which role the transport
|
|
17
|
+
* is currently playing.
|
|
18
|
+
*/
|
|
2
19
|
export declare class MCPConnection extends EventEmitter {
|
|
3
20
|
constructor(overrided?: boolean);
|
|
4
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-connection.d.ts","sourceRoot":"","sources":["../../src/core/mcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAElC,qBAAa,aAAc,SAAQ,YAAY;gBAC/B,SAAS,GAAE,OAAe;
|
|
1
|
+
{"version":3,"file":"mcp-connection.d.ts","sourceRoot":"","sources":["../../src/core/mcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAElC;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAc,SAAQ,YAAY;gBAC/B,SAAS,GAAE,OAAe;CAWzC;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for all MCP connections.
|
|
4
|
+
*
|
|
5
|
+
* Two logical, one-directional channels are defined:
|
|
6
|
+
* - "write_to_server" -> received by the server side as "server_read"
|
|
7
|
+
* - "write_to_client" -> received by the client side as "client_read"
|
|
8
|
+
*
|
|
9
|
+
* This keeps client writes from looping back to the client (and vice versa)
|
|
10
|
+
* once a connection is shared/subscribed by both a client and a server on
|
|
11
|
+
* the same object, which was the bug with the old single "write"/"read" pair.
|
|
12
|
+
*
|
|
13
|
+
* Subclasses that talk to a real transport (IPC socket, WebSocket, etc.)
|
|
14
|
+
* should pass `overrided = true` and implement their own wiring: forward
|
|
15
|
+
* "write_to_server"/"write_to_client" over the wire, and emit
|
|
16
|
+
* "server_read"/"client_read" locally depending on which role the transport
|
|
17
|
+
* is currently playing.
|
|
18
|
+
*/
|
|
2
19
|
export class MCPConnection extends EventEmitter {
|
|
3
20
|
constructor(overrided = false) {
|
|
4
21
|
super();
|
|
5
|
-
if (!overrided)
|
|
6
|
-
|
|
22
|
+
if (!overrided) {
|
|
23
|
+
// Loopback mode (e.g. same-process client+server for testing):
|
|
24
|
+
// whatever is written to the server is what the server reads,
|
|
25
|
+
// whatever is written to the client is what the client reads.
|
|
26
|
+
this.on("write_to_server", (data) => this.emit("server_read", data));
|
|
27
|
+
this.on("write_to_client", (data) => this.emit("client_read", data));
|
|
28
|
+
}
|
|
7
29
|
}
|
|
8
30
|
}
|
|
9
31
|
export default MCPConnection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-connection.js","sourceRoot":"","sources":["../../src/core/mcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAElC,MAAM,OAAO,aAAc,SAAQ,YAAY;IAC3C,YAAY,YAAqB,KAAK;QAClC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,SAAS;
|
|
1
|
+
{"version":3,"file":"mcp-connection.js","sourceRoot":"","sources":["../../src/core/mcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAElC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,aAAc,SAAQ,YAAY;IAC3C,YAAY,YAAqB,KAAK;QAClC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,SAAS,EAAE,CAAC;YACb,+DAA+D;YAC/D,8DAA8D;YAC9D,8DAA8D;YAC9D,IAAI,CAAC,EAAE,CAAC,iBAAiB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC,CAAC;YACrE,IAAI,CAAC,EAAE,CAAC,iBAAiB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC,CAAC;QACzE,CAAC;IACL,CAAC;CACJ;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import MCPConnection from "./mcp-connection.js";
|
|
2
|
+
export interface MCPIPCConnectionOptions {
|
|
3
|
+
/** Filesystem path of the IPC socket. */
|
|
4
|
+
socketPath: string;
|
|
5
|
+
/**
|
|
6
|
+
* "server": bind the socket and listen for a peer to connect.
|
|
7
|
+
* "client": connect out to an existing socket.
|
|
8
|
+
*/
|
|
9
|
+
mode: "server" | "client";
|
|
10
|
+
/** Reconnect delay (ms) for client mode after a disconnect. Default 500. */
|
|
11
|
+
reconnectDelay?: number;
|
|
12
|
+
}
|
|
2
13
|
export declare class MCPIPCConnection extends MCPConnection {
|
|
3
|
-
private
|
|
4
|
-
|
|
5
|
-
private server;
|
|
6
|
-
private role;
|
|
7
|
-
private writeBuffer;
|
|
8
|
-
private readBuffer;
|
|
9
|
-
constructor(socketPath: string);
|
|
14
|
+
#private;
|
|
15
|
+
constructor(options: MCPIPCConnectionOptions);
|
|
10
16
|
close(): void;
|
|
11
|
-
private _tryConnect;
|
|
12
|
-
private _becomeServer;
|
|
13
|
-
private _attachSocket;
|
|
14
|
-
private _sendJSON;
|
|
15
|
-
private _handleIncoming;
|
|
16
|
-
private _cleanSocketFile;
|
|
17
|
-
private _maybeReconnect;
|
|
18
17
|
}
|
|
19
18
|
export default MCPIPCConnection;
|
|
20
19
|
//# sourceMappingURL=mcp-ipc-connection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-ipc-connection.d.ts","sourceRoot":"","sources":["../../src/core/mcp-ipc-connection.ts"],"names":[],"mappings":"AAEA,OAAO,aAAa,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"mcp-ipc-connection.d.ts","sourceRoot":"","sources":["../../src/core/mcp-ipc-connection.ts"],"names":[],"mappings":"AAEA,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAEhD,MAAM,WAAW,uBAAuB;IACpC,yCAAyC;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,IAAI,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAC1B,4EAA4E;IAC5E,cAAc,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,qBAAa,gBAAiB,SAAQ,aAAa;;gBAQnC,OAAO,EAAE,uBAAuB;IAkB5C,KAAK,IAAI,IAAI;CA4HhB;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -2,78 +2,83 @@ import * as net from "net";
|
|
|
2
2
|
import * as fs from "fs";
|
|
3
3
|
import MCPConnection from "./mcp-connection.js";
|
|
4
4
|
export class MCPIPCConnection extends MCPConnection {
|
|
5
|
-
|
|
6
|
-
socket = null;
|
|
7
|
-
server = null;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
5
|
+
#options;
|
|
6
|
+
#socket = null;
|
|
7
|
+
#server = null;
|
|
8
|
+
#writeBuffer = [];
|
|
9
|
+
#readBuffer = "";
|
|
10
|
+
#closed = false;
|
|
11
|
+
constructor(options) {
|
|
12
12
|
super(true);
|
|
13
|
-
this
|
|
14
|
-
this.on("
|
|
15
|
-
this.
|
|
13
|
+
this.#options = options;
|
|
14
|
+
this.on("write_to_server", (data) => {
|
|
15
|
+
if (this.#options.mode === "client")
|
|
16
|
+
this.#sendJSON(data);
|
|
16
17
|
});
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
|
|
18
|
+
this.on("write_to_client", (data) => {
|
|
19
|
+
if (this.#options.mode === "server")
|
|
20
|
+
this.#sendJSON(data);
|
|
21
|
+
});
|
|
22
|
+
if (this.#options.mode === "server") {
|
|
23
|
+
this.#startServer();
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this.#connectClient();
|
|
27
|
+
}
|
|
20
28
|
}
|
|
21
29
|
close() {
|
|
22
|
-
this
|
|
23
|
-
this
|
|
24
|
-
this
|
|
30
|
+
this.#closed = true;
|
|
31
|
+
this.#socket?.destroy();
|
|
32
|
+
this.#server?.close();
|
|
33
|
+
if (this.#options.mode === "server") {
|
|
34
|
+
this.#cleanSocketFile();
|
|
35
|
+
}
|
|
25
36
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this
|
|
31
|
-
this._attachSocket(sock);
|
|
37
|
+
#startServer() {
|
|
38
|
+
this.emit("role", "server");
|
|
39
|
+
this.#cleanSocketFile();
|
|
40
|
+
const srv = net.createServer((peer) => {
|
|
41
|
+
this.#attachSocket(peer);
|
|
32
42
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.emit("error", err);
|
|
40
|
-
}
|
|
43
|
+
srv.on("error", (err) => this.emit("error", err));
|
|
44
|
+
srv.listen({ path: this.#options.socketPath }, () => {
|
|
45
|
+
this.#server = srv;
|
|
46
|
+
this.emit("listening", this.#options.socketPath);
|
|
41
47
|
});
|
|
42
48
|
}
|
|
43
|
-
|
|
44
|
-
this.
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
this
|
|
48
|
-
srv.close();
|
|
49
|
-
this._attachSocket(peer);
|
|
49
|
+
#connectClient() {
|
|
50
|
+
this.emit("role", "client");
|
|
51
|
+
const sock = net.createConnection({ path: this.#options.socketPath });
|
|
52
|
+
sock.once("connect", () => {
|
|
53
|
+
this.#attachSocket(sock);
|
|
50
54
|
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this
|
|
54
|
-
this.emit("listening", this.socketPath);
|
|
55
|
+
sock.once("error", (err) => {
|
|
56
|
+
this.emit("error", err);
|
|
57
|
+
this.#maybeReconnect();
|
|
55
58
|
});
|
|
56
59
|
}
|
|
57
|
-
|
|
58
|
-
this
|
|
59
|
-
for (const chunk of this
|
|
60
|
+
#attachSocket(sock) {
|
|
61
|
+
this.#socket = sock;
|
|
62
|
+
for (const chunk of this.#writeBuffer) {
|
|
60
63
|
sock.write(chunk);
|
|
61
64
|
}
|
|
62
|
-
this
|
|
65
|
+
this.#writeBuffer = [];
|
|
63
66
|
sock.setEncoding("utf8");
|
|
64
67
|
sock.on("data", (chunk) => {
|
|
65
|
-
this
|
|
68
|
+
this.#handleIncoming(chunk);
|
|
66
69
|
});
|
|
67
70
|
sock.on("end", () => {
|
|
68
71
|
this.emit("disconnected");
|
|
69
|
-
this
|
|
72
|
+
if (this.#socket === sock)
|
|
73
|
+
this.#socket = null;
|
|
74
|
+
this.#maybeReconnect();
|
|
70
75
|
});
|
|
71
76
|
sock.on("error", (err) => {
|
|
72
77
|
this.emit("error", err);
|
|
73
78
|
});
|
|
74
|
-
this.emit("connected", this.
|
|
79
|
+
this.emit("connected", this.#options.mode);
|
|
75
80
|
}
|
|
76
|
-
|
|
81
|
+
#sendJSON(data) {
|
|
77
82
|
let line;
|
|
78
83
|
try {
|
|
79
84
|
line = JSON.stringify(data) + "\n";
|
|
@@ -83,42 +88,48 @@ export class MCPIPCConnection extends MCPConnection {
|
|
|
83
88
|
return;
|
|
84
89
|
}
|
|
85
90
|
const chunk = Buffer.from(line, "utf8");
|
|
86
|
-
if (this
|
|
87
|
-
this
|
|
91
|
+
if (this.#socket && !this.#socket.destroyed) {
|
|
92
|
+
this.#socket.write(chunk);
|
|
88
93
|
}
|
|
89
94
|
else {
|
|
90
|
-
this
|
|
95
|
+
this.#writeBuffer.push(chunk);
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
|
-
|
|
94
|
-
this
|
|
95
|
-
const lines = this
|
|
96
|
-
this
|
|
98
|
+
#handleIncoming(chunk) {
|
|
99
|
+
this.#readBuffer += chunk;
|
|
100
|
+
const lines = this.#readBuffer.split("\n");
|
|
101
|
+
this.#readBuffer = lines.pop() ?? "";
|
|
97
102
|
for (const line of lines) {
|
|
98
103
|
const trimmed = line.trim();
|
|
99
104
|
if (!trimmed)
|
|
100
105
|
continue;
|
|
101
106
|
try {
|
|
102
107
|
const parsed = JSON.parse(trimmed);
|
|
103
|
-
this.
|
|
108
|
+
if (this.#options.mode === "server") {
|
|
109
|
+
this.emit("server_read", parsed);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
this.emit("client_read", parsed);
|
|
113
|
+
}
|
|
104
114
|
}
|
|
105
115
|
catch {
|
|
106
116
|
this.emit("error", new Error(`MCPIPCConnection: malformed JSON – ${trimmed}`));
|
|
107
117
|
}
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
|
-
|
|
120
|
+
#cleanSocketFile() {
|
|
111
121
|
try {
|
|
112
|
-
fs.unlinkSync(this.socketPath);
|
|
122
|
+
fs.unlinkSync(this.#options.socketPath);
|
|
113
123
|
}
|
|
114
124
|
catch { }
|
|
115
125
|
}
|
|
116
|
-
|
|
117
|
-
if (this
|
|
126
|
+
#maybeReconnect() {
|
|
127
|
+
if (this.#closed)
|
|
128
|
+
return;
|
|
129
|
+
if (this.#options.mode === "client") {
|
|
118
130
|
setTimeout(() => {
|
|
119
|
-
this
|
|
120
|
-
|
|
121
|
-
}, 500);
|
|
131
|
+
this.#connectClient();
|
|
132
|
+
}, this.#options.reconnectDelay ?? 500);
|
|
122
133
|
}
|
|
123
134
|
}
|
|
124
135
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-ipc-connection.js","sourceRoot":"","sources":["../../src/core/mcp-ipc-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,KAAK,CAAC;AAC3B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,aAAa,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"mcp-ipc-connection.js","sourceRoot":"","sources":["../../src/core/mcp-ipc-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,KAAK,CAAC;AAC3B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAchD,MAAM,OAAO,gBAAiB,SAAQ,aAAa;IAC/C,QAAQ,CAA0B;IAClC,OAAO,GAAsB,IAAI,CAAC;IAClC,OAAO,GAAsB,IAAI,CAAC;IAClC,YAAY,GAAa,EAAE,CAAC;IAC5B,WAAW,GAAG,EAAE,CAAC;IACjB,OAAO,GAAG,KAA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,cAAc,IAAI,GAAG,CAAC,CAAC;QAC5C,CAAC;IACL,CAAC;CACJ;AAED,eAAe,gBAAgB,CAAC"}
|
package/dist/core/mcp-server.js
CHANGED
|
@@ -13,11 +13,11 @@ export class MCPServer extends MCP {
|
|
|
13
13
|
if (!rng)
|
|
14
14
|
rng = new MCPRNG(storage);
|
|
15
15
|
this.#rng = rng;
|
|
16
|
-
this.#connection.on("
|
|
16
|
+
this.#connection.on("server_read", (data) => {
|
|
17
17
|
const topic = data.topic;
|
|
18
18
|
const pid = data.pid;
|
|
19
19
|
if (topic == "getTools") {
|
|
20
|
-
this.#connection.emit("
|
|
20
|
+
this.#connection.emit("write_to_client", {
|
|
21
21
|
pid,
|
|
22
22
|
topic: "getToolsCallback",
|
|
23
23
|
tools: this.#tools.map(tool => ({
|
|
@@ -31,13 +31,13 @@ export class MCPServer extends MCP {
|
|
|
31
31
|
const tool = this.#tools.find(t => t.getOptions().name == data.tool);
|
|
32
32
|
if (tool) {
|
|
33
33
|
tool.getOptions().execute(data.envID, data.inputs, tool.getMCP(), tool.getOptions().customClass).then((response) => {
|
|
34
|
-
this.#connection.emit("
|
|
34
|
+
this.#connection.emit("write_to_client", {
|
|
35
35
|
pid,
|
|
36
36
|
topic: "toolCallCallback",
|
|
37
37
|
output: response
|
|
38
38
|
});
|
|
39
39
|
}).catch(error => {
|
|
40
|
-
this.#connection.emit("
|
|
40
|
+
this.#connection.emit("write_to_client", {
|
|
41
41
|
pid,
|
|
42
42
|
topic: "toolCallCallback",
|
|
43
43
|
output: { error: error.message }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-server.js","sourceRoot":"","sources":["../../src/core/mcp-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAgB,MAAM,UAAU,CAAC;AAG7C,OAAO,YAAY,MAAM,qBAAqB,CAAC;AAC/C,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC,MAAM,OAAO,SAAU,SAAQ,GAAG;IAC9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nC,CAAC,CAAC;oBACP,CAAC,CAAC,CAAC;gBACP,CAAC;YACL,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IAED,UAAU;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IAED,MAAM;QACF,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,YAAY,CAAC,IAAkB;QAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;CACJ;AAED,eAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import MCPConnection from "./mcp-connection.js";
|
|
2
|
+
export interface MCPWSTLSOptions {
|
|
3
|
+
/** Private key (PEM), e.g. fs.readFileSync("key.pem") or its contents. */
|
|
4
|
+
key: string | Buffer;
|
|
5
|
+
/** Certificate (PEM), e.g. fs.readFileSync("cert.pem") or its contents. */
|
|
6
|
+
cert: string | Buffer;
|
|
7
|
+
/** Optional CA certificate(s), useful for verifying client certs or self-signed chains. */
|
|
8
|
+
ca?: string | Buffer | (string | Buffer)[];
|
|
9
|
+
}
|
|
10
|
+
export interface MCPWSConnectionOptions {
|
|
11
|
+
/**
|
|
12
|
+
* "server": bind a WebSocketServer and wait for a peer to connect.
|
|
13
|
+
* "client": connect out to `url`.
|
|
14
|
+
*/
|
|
15
|
+
mode: "server" | "client";
|
|
16
|
+
/** Required when mode === "client". e.g. "ws://localhost:8080" or "wss://host:port" */
|
|
17
|
+
url?: string;
|
|
18
|
+
/** Required when mode === "server". Port to listen on. */
|
|
19
|
+
port?: number;
|
|
20
|
+
/** Optional host to bind to when mode === "server". */
|
|
21
|
+
host?: string;
|
|
22
|
+
/** Reconnect delay (ms) for client mode after a disconnect. Default 500. */
|
|
23
|
+
reconnectDelay?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Shared token both sides must agree on. The server only accepts
|
|
26
|
+
* connections from clients that present this exact token; the client
|
|
27
|
+
* sends it automatically. If omitted, no auth check is performed.
|
|
28
|
+
*/
|
|
29
|
+
token?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Optional TLS key/cert (and CA). When provided:
|
|
32
|
+
* - server mode binds an HTTPS server and serves WSS.
|
|
33
|
+
* - client mode is expected to connect via a wss:// url; the ca
|
|
34
|
+
* (if given) is used to validate the server's certificate.
|
|
35
|
+
* When omitted, server mode uses plain HTTP/WS.
|
|
36
|
+
*/
|
|
37
|
+
tls?: MCPWSTLSOptions;
|
|
38
|
+
}
|
|
39
|
+
export declare class MCPWSConnection extends MCPConnection {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(options: MCPWSConnectionOptions);
|
|
42
|
+
close(): void;
|
|
43
|
+
}
|
|
44
|
+
export default MCPWSConnection;
|
|
45
|
+
//# sourceMappingURL=mcp-ws-connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-ws-connection.d.ts","sourceRoot":"","sources":["../../src/core/mcp-ws-connection.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAEhD,MAAM,WAAW,eAAe;IAC5B,0EAA0E;IAC1E,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IACrB,2EAA2E;IAC3E,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,2FAA2F;IAC3F,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;CAC9C;AAED,MAAM,WAAW,sBAAsB;IACnC;;;OAGG;IACH,IAAI,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAC1B,uFAAuF;IACvF,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4EAA4E;IAC5E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,eAAe,CAAC;CACzB;AAID,qBAAa,eAAgB,SAAQ,aAAa;;gBAQlC,OAAO,EAAE,sBAAsB;IAyB3C,KAAK,IAAI,IAAI;CAmJhB;AAED,eAAe,eAAe,CAAC"}
|