@jiangxiaoxu/lm-tools-bridge-proxy 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/index.js +210 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# LM Tools Bridge Proxy
|
|
2
|
+
|
|
3
|
+
Stdio MCP proxy that resolves the active VS Code instance via the LM Tools Bridge Manager (Windows Named Pipe).
|
|
4
|
+
|
|
5
|
+
## Usage (Codex)
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
command = "npx"
|
|
9
|
+
args = ["-y", "@jiangxiaoxu/lm-tools-bridge-proxy"]
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The proxy uses the current working directory (`cwd`) to resolve the VS Code instance.
|
|
13
|
+
|
|
14
|
+
## Development
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install
|
|
18
|
+
npm run build
|
|
19
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var import_node_http = __toESM(require("node:http"));
|
|
27
|
+
var import_node_process = __toESM(require("node:process"));
|
|
28
|
+
var import_node_crypto = __toESM(require("node:crypto"));
|
|
29
|
+
var import_node_os = __toESM(require("node:os"));
|
|
30
|
+
var MANAGER_TIMEOUT_MS = 1500;
|
|
31
|
+
var RESOLVE_RETRIES = 5;
|
|
32
|
+
var RESOLVE_RETRY_DELAY_MS = 1e3;
|
|
33
|
+
var TARGET_HOST_HEADER = "x-mcp-target";
|
|
34
|
+
function getUserSeed() {
|
|
35
|
+
return import_node_process.default.env.USERNAME ?? import_node_process.default.env.USERPROFILE ?? import_node_os.default.userInfo().username ?? "default-user";
|
|
36
|
+
}
|
|
37
|
+
function getManagerPipeName() {
|
|
38
|
+
const hash = import_node_crypto.default.createHash("sha1").update(getUserSeed()).digest("hex").slice(0, 12);
|
|
39
|
+
return `\\\\.\\pipe\\lm-tools-bridge-manager-${hash}`;
|
|
40
|
+
}
|
|
41
|
+
async function delay(ms) {
|
|
42
|
+
return new Promise((resolve) => {
|
|
43
|
+
setTimeout(resolve, ms);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async function resolveTarget(cwd) {
|
|
47
|
+
for (let attempt = 0; attempt < RESOLVE_RETRIES; attempt += 1) {
|
|
48
|
+
const result = await managerRequest("POST", "/resolve", { cwd });
|
|
49
|
+
if (result.ok && result.data && result.data.match) {
|
|
50
|
+
return result.data.match;
|
|
51
|
+
}
|
|
52
|
+
await delay(RESOLVE_RETRY_DELAY_MS);
|
|
53
|
+
}
|
|
54
|
+
return void 0;
|
|
55
|
+
}
|
|
56
|
+
async function managerRequest(method, requestPath, body) {
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
const payload = body ? JSON.stringify(body) : void 0;
|
|
59
|
+
const request = import_node_http.default.request(
|
|
60
|
+
{
|
|
61
|
+
socketPath: getManagerPipeName(),
|
|
62
|
+
path: requestPath,
|
|
63
|
+
method,
|
|
64
|
+
headers: payload ? {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
67
|
+
} : void 0
|
|
68
|
+
},
|
|
69
|
+
(response) => {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
response.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
|
|
72
|
+
response.on("end", () => {
|
|
73
|
+
const status = response.statusCode ?? 500;
|
|
74
|
+
if (chunks.length === 0) {
|
|
75
|
+
resolve({ ok: status >= 200 && status < 300, status });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
80
|
+
const parsed = JSON.parse(text);
|
|
81
|
+
resolve({ ok: status >= 200 && status < 300, status, data: parsed });
|
|
82
|
+
} catch {
|
|
83
|
+
resolve({ ok: false, status });
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
const timeout = setTimeout(() => {
|
|
89
|
+
request.destroy(new Error("Timeout"));
|
|
90
|
+
}, MANAGER_TIMEOUT_MS);
|
|
91
|
+
request.on("error", () => {
|
|
92
|
+
clearTimeout(timeout);
|
|
93
|
+
resolve({ ok: false });
|
|
94
|
+
});
|
|
95
|
+
request.on("close", () => {
|
|
96
|
+
clearTimeout(timeout);
|
|
97
|
+
});
|
|
98
|
+
if (payload) {
|
|
99
|
+
request.write(payload);
|
|
100
|
+
}
|
|
101
|
+
request.end();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function createStdioMessageHandler(target) {
|
|
105
|
+
const targetUrl = new URL(`http://${target.host}:${target.port}/mcp`);
|
|
106
|
+
return (message) => {
|
|
107
|
+
const payload = JSON.stringify(message);
|
|
108
|
+
const request = import_node_http.default.request(
|
|
109
|
+
{
|
|
110
|
+
hostname: targetUrl.hostname,
|
|
111
|
+
port: Number(targetUrl.port),
|
|
112
|
+
path: targetUrl.pathname,
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"Content-Length": Buffer.byteLength(payload),
|
|
117
|
+
[TARGET_HOST_HEADER]: `${target.host}:${target.port}`
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
(response) => {
|
|
121
|
+
const chunks = [];
|
|
122
|
+
response.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
|
|
123
|
+
response.on("end", () => {
|
|
124
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
125
|
+
if (text.length > 0) {
|
|
126
|
+
import_node_process.default.stdout.write(`${text}
|
|
127
|
+
`);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
request.on("error", (error) => {
|
|
133
|
+
const errorPayload = {
|
|
134
|
+
jsonrpc: "2.0",
|
|
135
|
+
id: message.id ?? null,
|
|
136
|
+
error: {
|
|
137
|
+
code: -32e3,
|
|
138
|
+
message: `MCP proxy request failed: ${String(error)}`
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
import_node_process.default.stdout.write(`${JSON.stringify(errorPayload)}
|
|
142
|
+
`);
|
|
143
|
+
});
|
|
144
|
+
request.write(payload);
|
|
145
|
+
request.end();
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function main() {
|
|
149
|
+
const cwd = import_node_process.default.cwd();
|
|
150
|
+
const target = await resolveTarget(cwd);
|
|
151
|
+
if (!target) {
|
|
152
|
+
const errorPayload = {
|
|
153
|
+
jsonrpc: "2.0",
|
|
154
|
+
id: null,
|
|
155
|
+
error: {
|
|
156
|
+
code: -32001,
|
|
157
|
+
message: `No VS Code instance registered for cwd: ${cwd}`
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
import_node_process.default.stdout.write(`${JSON.stringify(errorPayload)}
|
|
161
|
+
`);
|
|
162
|
+
import_node_process.default.exit(1);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const handler = createStdioMessageHandler(target);
|
|
166
|
+
let buffer = "";
|
|
167
|
+
import_node_process.default.stdin.setEncoding("utf8");
|
|
168
|
+
import_node_process.default.stdin.on("data", (chunk) => {
|
|
169
|
+
buffer += chunk;
|
|
170
|
+
let index = buffer.indexOf("\n");
|
|
171
|
+
while (index !== -1) {
|
|
172
|
+
const line = buffer.slice(0, index).trim();
|
|
173
|
+
buffer = buffer.slice(index + 1);
|
|
174
|
+
if (line.length > 0) {
|
|
175
|
+
try {
|
|
176
|
+
const message = JSON.parse(line);
|
|
177
|
+
handler(message);
|
|
178
|
+
} catch {
|
|
179
|
+
const errorPayload = {
|
|
180
|
+
jsonrpc: "2.0",
|
|
181
|
+
id: null,
|
|
182
|
+
error: {
|
|
183
|
+
code: -32700,
|
|
184
|
+
message: "Invalid JSON received by MCP proxy."
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
import_node_process.default.stdout.write(`${JSON.stringify(errorPayload)}
|
|
188
|
+
`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
index = buffer.indexOf("\n");
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
import_node_process.default.stdin.on("end", () => {
|
|
195
|
+
import_node_process.default.exit(0);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
main().catch((error) => {
|
|
199
|
+
const errorPayload = {
|
|
200
|
+
jsonrpc: "2.0",
|
|
201
|
+
id: null,
|
|
202
|
+
error: {
|
|
203
|
+
code: -32002,
|
|
204
|
+
message: `MCP proxy startup failed: ${String(error)}`
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
import_node_process.default.stdout.write(`${JSON.stringify(errorPayload)}
|
|
208
|
+
`);
|
|
209
|
+
import_node_process.default.exit(1);
|
|
210
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jiangxiaoxu/lm-tools-bridge-proxy",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Stdio MCP proxy for LM Tools Bridge (Windows Named Pipe resolve).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"bin": {
|
|
7
|
+
"lm-tools-bridge-proxy": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "commonjs",
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "node ./scripts/build.mjs",
|
|
16
|
+
"prepare": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"esbuild": "^0.27.2",
|
|
24
|
+
"typescript": "^5.4.5"
|
|
25
|
+
}
|
|
26
|
+
}
|