lceda-connect 0.3.0 → 0.4.1
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/CHANGELOG.md +22 -0
- package/README.md +92 -30
- package/dist/{cli-CjcJVM6z.mjs → cli-1_urvve3.mjs} +4 -4
- package/dist/{cli-C4ZkWlV2.mjs → cli-CXk5pvVm.mjs} +1 -1
- package/dist/{cli-C6saKrQp.mjs → cli-DFqBHofJ.mjs} +18 -3
- package/dist/cli-bridge-server.mjs +1 -1
- package/dist/cli-bridge.mjs +1 -1
- package/dist/cli-cdp.mjs +1 -1
- package/dist/cli-mcp.mjs +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/config-BKbwyvsG.mjs +516 -0
- package/dist/{discover-D5mRrsxg.mjs → discover-DmRYlAlR.mjs} +13 -64
- package/dist/environment-BegP0n2N.mjs +439 -0
- package/dist/environment-Clx1-nDW.mjs +146 -0
- package/dist/http-Cdp6S2rm.mjs +366 -0
- package/dist/http-D7sYy2y4.mjs +1 -0
- package/dist/index.d.mts +226 -6
- package/dist/index.mjs +7 -5
- package/dist/{main-CcKvXSJG.mjs → main-B3aaKvqY.mjs} +688 -89
- package/dist/mcpHost-CRoN6MnG.mjs +331 -0
- package/dist/{registry-Dmti2u6s.mjs → registry-3WTFCZsR.mjs} +84 -24
- package/dist/registry-DdJ8LEdd.mjs +1 -0
- package/dist/{server-BduSjwzu.mjs → server-zw6t1Pmk.mjs} +9 -14
- package/dist/{session-DvIXO5Hf.mjs → session-AQbRX6Ys.mjs} +2 -2
- package/dist/{sideEffects-DF_tIVsf.mjs → sideEffects-Ds_T-_B4.mjs} +3 -3
- package/dist/stdio-BZPlcfDR.mjs +27 -0
- package/dist/stdio-Du1oLqfJ.mjs +1 -0
- package/dist/{targets-D5nmbsyz.mjs → targets-8LBEgvbq.mjs} +1 -1
- package/dist/{verbs-5M7rj1-E.mjs → verbs-DjQQTL2k.mjs} +191 -54
- package/dist/windowsHost-CTZXMA8D.mjs +1 -0
- package/dist/windowsHost-CtRidgJp.mjs +309 -0
- package/docs/architecture.md +81 -0
- package/docs/environments.md +175 -0
- package/docs/quickstart.md +59 -4
- package/docs/transports.md +189 -0
- package/package.json +5 -2
- package/dist/registry-zIKWeXV6.mjs +0 -1
- package/dist/server-Chka6eAQ.mjs +0 -1
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { i as loadConfig } from "./config-BKbwyvsG.mjs";
|
|
2
|
+
import { o as isLoopbackAddress } from "./environment-BegP0n2N.mjs";
|
|
3
|
+
import { t as packageVersion } from "./version-B39-elun.mjs";
|
|
4
|
+
import { r as createServerFactory, t as SERVER_NAME } from "./server-zw6t1Pmk.mjs";
|
|
5
|
+
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
6
|
+
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
9
|
+
// src/mcp/transports/http.ts
|
|
10
|
+
const DEFAULT_HTTP_HOST = "127.0.0.1";
|
|
11
|
+
const DEFAULT_HTTP_PORT = 3939;
|
|
12
|
+
const DEFAULT_HTTP_PATH = "/mcp";
|
|
13
|
+
const MAX_BODY_BYTES = 4194304;
|
|
14
|
+
/** 空闲会话回收时间;SDK 的 close 不会自动 DELETE,常驻服务必须自己收。 */
|
|
15
|
+
const SESSION_IDLE_TTL_MS = 18e5;
|
|
16
|
+
const MAX_SESSIONS = 64;
|
|
17
|
+
const SESSION_SWEEP_MS = 6e4;
|
|
18
|
+
const TRUSTED_LOOPBACK = /* @__PURE__ */ new Set([
|
|
19
|
+
"localhost",
|
|
20
|
+
"127.0.0.1",
|
|
21
|
+
"[::1]",
|
|
22
|
+
"::1"
|
|
23
|
+
]);
|
|
24
|
+
function normalizePath(value) {
|
|
25
|
+
const raw = (value ?? "/mcp").trim() || "/mcp";
|
|
26
|
+
const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
|
27
|
+
return withSlash.length > 1 ? withSlash.replace(/\/+$/, "") : withSlash;
|
|
28
|
+
}
|
|
29
|
+
function hostnameOf(hostHeader) {
|
|
30
|
+
const value = (hostHeader ?? "").trim().toLowerCase();
|
|
31
|
+
if (!value) return "";
|
|
32
|
+
if (value.startsWith("[")) {
|
|
33
|
+
const end = value.indexOf("]");
|
|
34
|
+
return end >= 0 ? value.slice(0, end + 1) : value;
|
|
35
|
+
}
|
|
36
|
+
return value.split(":")[0] ?? "";
|
|
37
|
+
}
|
|
38
|
+
function ipv4Literal(name) {
|
|
39
|
+
return /^\d{1,3}(\.\d{1,3}){3}$/.test(name);
|
|
40
|
+
}
|
|
41
|
+
function ipv6Literal(name) {
|
|
42
|
+
const inner = name.replace(/^\[|\]$/g, "");
|
|
43
|
+
return inner.includes(":") && /^[0-9a-f:.%]+$/i.test(inner);
|
|
44
|
+
}
|
|
45
|
+
/** Host 头校验:只接受回环与显式绑定地址,降低 DNS rebinding 风险。 */
|
|
46
|
+
function isAllowedHostHeader(hostHeader, bindHost) {
|
|
47
|
+
const name = hostnameOf(hostHeader);
|
|
48
|
+
if (!name) return false;
|
|
49
|
+
const allowed = new Set(TRUSTED_LOOPBACK);
|
|
50
|
+
const bind = bindHost.trim().toLowerCase();
|
|
51
|
+
if (bind && bind !== "0.0.0.0" && bind !== "::") {
|
|
52
|
+
allowed.add(bind.replace(/^\[|\]$/g, ""));
|
|
53
|
+
return allowed.has(name);
|
|
54
|
+
}
|
|
55
|
+
if (allowed.has(name)) return true;
|
|
56
|
+
return ipv4Literal(name) || ipv6Literal(name);
|
|
57
|
+
}
|
|
58
|
+
/** Origin 校验(规范 Security Warning 要求 MUST)。没有 Origin 的本地客户端放行。 */
|
|
59
|
+
function isAllowedOrigin(origin, bindHost) {
|
|
60
|
+
if (origin === void 0 || origin.trim() === "") return true;
|
|
61
|
+
if (origin.trim().toLowerCase() === "null") return false;
|
|
62
|
+
try {
|
|
63
|
+
return isAllowedHostHeader(new URL(origin).host, bindHost);
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function jsonResponse(res, status, payload, headers = {}) {
|
|
69
|
+
const body = JSON.stringify(payload);
|
|
70
|
+
res.writeHead(status, {
|
|
71
|
+
"content-type": "application/json; charset=utf-8",
|
|
72
|
+
"content-length": Buffer.byteLength(body),
|
|
73
|
+
...headers
|
|
74
|
+
});
|
|
75
|
+
res.end(body);
|
|
76
|
+
}
|
|
77
|
+
function bearerOf(header) {
|
|
78
|
+
return /^bearer\s+(.+)$/i.exec((header ?? "").trim())?.[1] ?? null;
|
|
79
|
+
}
|
|
80
|
+
function constantTimeEqual(left, right) {
|
|
81
|
+
const a = createHash("sha256").update(left).digest();
|
|
82
|
+
const b = createHash("sha256").update(right).digest();
|
|
83
|
+
return timingSafeEqual(a, b);
|
|
84
|
+
}
|
|
85
|
+
function authOk(req, token) {
|
|
86
|
+
if (!token) return true;
|
|
87
|
+
const presented = bearerOf(req.headers.authorization);
|
|
88
|
+
return presented !== null && constantTimeEqual(presented, token);
|
|
89
|
+
}
|
|
90
|
+
function readBody(req) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const chunks = [];
|
|
93
|
+
let size = 0;
|
|
94
|
+
let tooLarge = false;
|
|
95
|
+
req.on("data", (chunk) => {
|
|
96
|
+
if (tooLarge) return;
|
|
97
|
+
size += chunk.length;
|
|
98
|
+
if (size > MAX_BODY_BYTES) {
|
|
99
|
+
tooLarge = true;
|
|
100
|
+
chunks.length = 0;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
chunks.push(chunk);
|
|
104
|
+
});
|
|
105
|
+
req.on("end", () => {
|
|
106
|
+
if (tooLarge) return reject(/* @__PURE__ */ new Error("BODY_TOO_LARGE"));
|
|
107
|
+
const text = Buffer.concat(chunks).toString("utf8").trim();
|
|
108
|
+
if (!text) return resolve(void 0);
|
|
109
|
+
try {
|
|
110
|
+
resolve(JSON.parse(text));
|
|
111
|
+
} catch {
|
|
112
|
+
reject(/* @__PURE__ */ new Error("BAD_JSON"));
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
req.on("error", reject);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async function startHttpTransport(options) {
|
|
119
|
+
const env = options.env;
|
|
120
|
+
const host = (options.host ?? DEFAULT_HTTP_HOST).trim() || DEFAULT_HTTP_HOST;
|
|
121
|
+
const port = options.port ?? 3939;
|
|
122
|
+
const mcpPath = normalizePath(options.path);
|
|
123
|
+
const log = options.log ?? ((line) => process.stderr.write(`${line}\n`));
|
|
124
|
+
const serverFactory = options.serverFactory ?? createServerFactory(options);
|
|
125
|
+
const sessions = /* @__PURE__ */ new Map();
|
|
126
|
+
const stateless = options.stateless === true;
|
|
127
|
+
let actualPort = port;
|
|
128
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error(`HTTP 端口不合法:${port}`);
|
|
129
|
+
if (!isLoopbackAddress(host) && !options.token) throw new Error("绑定非回环地址必须提供 Bearer token(--token / LCEDA_HTTP_TOKEN / 配置 mcp.http.token)");
|
|
130
|
+
if (!isLoopbackAddress(host) && host !== "0.0.0.0" && host !== "::") log(`[${SERVER_NAME}] 警告:HTTP MCP 绑定在非回环地址 ${host};请仅绑定 WSL 虚拟网卡地址,并考虑加 --token`);
|
|
131
|
+
const closeEntry = async (entry) => {
|
|
132
|
+
try {
|
|
133
|
+
await entry.transport.close();
|
|
134
|
+
} catch {}
|
|
135
|
+
try {
|
|
136
|
+
await entry.server.close();
|
|
137
|
+
} catch {}
|
|
138
|
+
};
|
|
139
|
+
const evictOldest = async () => {
|
|
140
|
+
const first = sessions.entries().next();
|
|
141
|
+
if (first.done) return;
|
|
142
|
+
const [sessionId, entry] = first.value;
|
|
143
|
+
sessions.delete(sessionId);
|
|
144
|
+
await closeEntry(entry);
|
|
145
|
+
log(`[${SERVER_NAME}] 会话数达到上限 ${MAX_SESSIONS},已回收最早会话 ${sessionId}`);
|
|
146
|
+
};
|
|
147
|
+
const buildSession = async () => {
|
|
148
|
+
if (!stateless && sessions.size >= MAX_SESSIONS) await evictOldest();
|
|
149
|
+
const server = serverFactory();
|
|
150
|
+
let entry = null;
|
|
151
|
+
const transport = new StreamableHTTPServerTransport({
|
|
152
|
+
sessionIdGenerator: stateless ? void 0 : () => randomUUID(),
|
|
153
|
+
enableJsonResponse: true,
|
|
154
|
+
onsessioninitialized: (sessionId) => {
|
|
155
|
+
if (entry) {
|
|
156
|
+
entry.lastSeen = Date.now();
|
|
157
|
+
sessions.set(sessionId, entry);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
onsessionclosed: (sessionId) => {
|
|
161
|
+
sessions.delete(sessionId);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
await server.connect(transport);
|
|
165
|
+
entry = {
|
|
166
|
+
transport,
|
|
167
|
+
server,
|
|
168
|
+
lastSeen: Date.now()
|
|
169
|
+
};
|
|
170
|
+
transport.onclose = () => {
|
|
171
|
+
const sessionId = transport.sessionId;
|
|
172
|
+
if (sessionId) sessions.delete(sessionId);
|
|
173
|
+
};
|
|
174
|
+
return entry;
|
|
175
|
+
};
|
|
176
|
+
/** 常驻服务的会话回收:客户端 close() 不会发 DELETE,只能按空闲时间收。 */
|
|
177
|
+
const sweeper = stateless ? null : setInterval(() => {
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
for (const [sessionId, entry] of sessions) if (now - entry.lastSeen > SESSION_IDLE_TTL_MS) {
|
|
180
|
+
sessions.delete(sessionId);
|
|
181
|
+
closeEntry(entry);
|
|
182
|
+
log(`[${SERVER_NAME}] 会话 ${sessionId} 空闲超时,已回收`);
|
|
183
|
+
}
|
|
184
|
+
}, SESSION_SWEEP_MS);
|
|
185
|
+
sweeper?.unref();
|
|
186
|
+
const httpServer = createServer((req, res) => {
|
|
187
|
+
(async () => {
|
|
188
|
+
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? host}`);
|
|
189
|
+
if (url.pathname === "/health") {
|
|
190
|
+
if (!isAllowedHostHeader(req.headers.host, host)) {
|
|
191
|
+
jsonResponse(res, 403, { error: "forbidden_host" });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const cfg = loadConfig(env);
|
|
195
|
+
jsonResponse(res, 200, {
|
|
196
|
+
status: "ok",
|
|
197
|
+
name: SERVER_NAME,
|
|
198
|
+
version: packageVersion(),
|
|
199
|
+
transport: "streamable-http",
|
|
200
|
+
host,
|
|
201
|
+
port: actualPort,
|
|
202
|
+
path: mcpPath,
|
|
203
|
+
stateless,
|
|
204
|
+
sessions: sessions.size,
|
|
205
|
+
environment: {
|
|
206
|
+
kind: cfg.environment.kind,
|
|
207
|
+
hostOs: cfg.environment.hostOs,
|
|
208
|
+
networkingMode: cfg.environment.networkingMode,
|
|
209
|
+
clientLocations: cfg.environment.clientLocations
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (url.pathname !== mcpPath) {
|
|
215
|
+
jsonResponse(res, 404, {
|
|
216
|
+
error: "not_found",
|
|
217
|
+
path: url.pathname,
|
|
218
|
+
mcpPath
|
|
219
|
+
});
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (!isAllowedHostHeader(req.headers.host, host)) {
|
|
223
|
+
jsonResponse(res, 403, {
|
|
224
|
+
error: "forbidden_host",
|
|
225
|
+
host: req.headers.host ?? null
|
|
226
|
+
});
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (!isAllowedOrigin(req.headers.origin, host)) {
|
|
230
|
+
jsonResponse(res, 403, {
|
|
231
|
+
error: "forbidden_origin",
|
|
232
|
+
origin: req.headers.origin ?? null
|
|
233
|
+
});
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (!authOk(req, options.token)) {
|
|
237
|
+
jsonResponse(res, 401, { error: "unauthorized" }, { "www-authenticate": "Bearer realm=\"lceda-connect\"" });
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const rawSessionId = req.headers["mcp-session-id"];
|
|
241
|
+
const sessionHeader = typeof rawSessionId === "string" ? rawSessionId : void 0;
|
|
242
|
+
if (req.method === "POST") {
|
|
243
|
+
let body;
|
|
244
|
+
try {
|
|
245
|
+
body = await readBody(req);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
const code = error instanceof Error ? error.message : String(error);
|
|
248
|
+
jsonResponse(res, code === "BODY_TOO_LARGE" ? 413 : 400, { error: code.toLowerCase() });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (stateless) {
|
|
252
|
+
const entry = await buildSession();
|
|
253
|
+
try {
|
|
254
|
+
await entry.transport.handleRequest(req, res, body);
|
|
255
|
+
} finally {
|
|
256
|
+
await closeEntry(entry);
|
|
257
|
+
}
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (sessionHeader) {
|
|
261
|
+
const entry = sessions.get(sessionHeader);
|
|
262
|
+
if (!entry) {
|
|
263
|
+
jsonResponse(res, 404, {
|
|
264
|
+
jsonrpc: "2.0",
|
|
265
|
+
error: {
|
|
266
|
+
code: -32e3,
|
|
267
|
+
message: "Session not found"
|
|
268
|
+
},
|
|
269
|
+
id: null
|
|
270
|
+
});
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
entry.lastSeen = Date.now();
|
|
274
|
+
await entry.transport.handleRequest(req, res, body);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (isInitializeRequest(body)) {
|
|
278
|
+
await (await buildSession()).transport.handleRequest(req, res, body);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
jsonResponse(res, 400, {
|
|
282
|
+
jsonrpc: "2.0",
|
|
283
|
+
error: {
|
|
284
|
+
code: -32e3,
|
|
285
|
+
message: "Bad Request: No valid session ID"
|
|
286
|
+
},
|
|
287
|
+
id: null
|
|
288
|
+
});
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (req.method === "GET" || req.method === "DELETE") {
|
|
292
|
+
if (stateless) {
|
|
293
|
+
res.setHeader("allow", "POST");
|
|
294
|
+
jsonResponse(res, 405, { error: "method_not_allowed" });
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (!sessionHeader) {
|
|
298
|
+
jsonResponse(res, 400, {
|
|
299
|
+
jsonrpc: "2.0",
|
|
300
|
+
error: {
|
|
301
|
+
code: -32e3,
|
|
302
|
+
message: "Missing session ID"
|
|
303
|
+
},
|
|
304
|
+
id: null
|
|
305
|
+
});
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const entry = sessions.get(sessionHeader);
|
|
309
|
+
if (!entry) {
|
|
310
|
+
jsonResponse(res, 404, {
|
|
311
|
+
jsonrpc: "2.0",
|
|
312
|
+
error: {
|
|
313
|
+
code: -32e3,
|
|
314
|
+
message: "Session not found"
|
|
315
|
+
},
|
|
316
|
+
id: null
|
|
317
|
+
});
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
entry.lastSeen = Date.now();
|
|
321
|
+
await entry.transport.handleRequest(req, res);
|
|
322
|
+
if (req.method === "DELETE") {
|
|
323
|
+
sessions.delete(sessionHeader);
|
|
324
|
+
await closeEntry(entry);
|
|
325
|
+
}
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
res.setHeader("allow", "GET, POST, DELETE");
|
|
329
|
+
jsonResponse(res, 405, { error: "method_not_allowed" });
|
|
330
|
+
})().catch((error) => {
|
|
331
|
+
log(`[${SERVER_NAME}] HTTP 请求处理失败:${error instanceof Error ? error.message : String(error)}`);
|
|
332
|
+
if (!res.headersSent) jsonResponse(res, 500, { error: "internal_error" });
|
|
333
|
+
else res.end();
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
await new Promise((resolve, reject) => {
|
|
337
|
+
httpServer.once("error", reject);
|
|
338
|
+
httpServer.listen(port, host, () => {
|
|
339
|
+
httpServer.off("error", reject);
|
|
340
|
+
resolve();
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
const address = httpServer.address();
|
|
344
|
+
actualPort = typeof address === "object" && address !== null ? address.port : port;
|
|
345
|
+
const displayHost = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
|
|
346
|
+
const url = `http://${displayHost.includes(":") ? `[${displayHost}]` : displayHost}:${actualPort}${mcpPath}`;
|
|
347
|
+
log(`[${SERVER_NAME}] HTTP MCP 已监听 ${host}:${actualPort}(端点 ${mcpPath},${stateless ? "无状态" : `有状态,空闲 ${Math.round(SESSION_IDLE_TTL_MS / 6e4)} 分钟后回收`}${options.token ? ",需要 Bearer token" : ""})`);
|
|
348
|
+
log(`[${SERVER_NAME}] 本机客户端: ${url}`);
|
|
349
|
+
if (isLoopbackAddress(host)) log(`[${SERVER_NAME}] 提示:只绑回环时 WSL/其它命名空间不可达;跨 WSL 请用 --host <WSL 虚拟网卡地址> 或 --host wsl`);
|
|
350
|
+
return {
|
|
351
|
+
kind: "http",
|
|
352
|
+
url,
|
|
353
|
+
host,
|
|
354
|
+
port: actualPort,
|
|
355
|
+
path: mcpPath,
|
|
356
|
+
close: async () => {
|
|
357
|
+
if (sweeper) clearInterval(sweeper);
|
|
358
|
+
for (const entry of sessions.values()) await closeEntry(entry);
|
|
359
|
+
sessions.clear();
|
|
360
|
+
await new Promise((resolve, reject) => {
|
|
361
|
+
httpServer.close((error) => error ? reject(error) : resolve());
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
export { startHttpTransport as a, isAllowedOrigin as i, DEFAULT_HTTP_PORT as n, isAllowedHostHeader as r, DEFAULT_HTTP_PATH as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as startHttpTransport } from "./http-Cdp6S2rm.mjs";
|
package/dist/index.d.mts
CHANGED
|
@@ -1,24 +1,152 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* 执行环境自动发现。
|
|
5
|
+
*
|
|
6
|
+
* 本工具的在线能力(CDP / bridge)依赖「客户端在哪个操作系统、哪个网络命名空间里」:
|
|
7
|
+
* - Windows 原生:客户端与 MCP 共享回环,127.0.0.1 直连。
|
|
8
|
+
* - WSL2 NAT(默认):MCP 在 Linux 虚拟机里,Windows 的回环与它不互通;只有
|
|
9
|
+
* 宿主地址(默认网关)上的监听才可能可达。
|
|
10
|
+
* - WSL2 mirrored:共享宿主网络命名空间,回环重新可用。
|
|
11
|
+
* 因此先判环境,再决定探测哪些地址、能否启动客户端、失败时该给什么建议。
|
|
12
|
+
*
|
|
13
|
+
* 判定只读:读 /proc、读环境变量、跑一次 `wslinfo --networking-mode`(WSL 自带,
|
|
14
|
+
* 不存在时跳过)。不启动、不修改任何东西。所有外部探测都可注入,便于测试。
|
|
15
|
+
*/
|
|
16
|
+
type EnvironmentKind = "windows-native" | "wsl2" | "linux-native" | "macos-native" | "unknown";
|
|
17
|
+
/** 客户端(嘉立创EDA专业版)所在的操作系统。 */
|
|
18
|
+
type HostOs = "windows" | "linux" | "macos" | "unknown";
|
|
19
|
+
type NetworkingMode = "nat" | "mirrored" | "unknown";
|
|
20
|
+
/**
|
|
21
|
+
* 客户端实例相对 MCP 进程的宿主位置。只对 WSL2 有意义:
|
|
22
|
+
* - host:客户端跑在 Windows 宿主机上(WSL2 默认)
|
|
23
|
+
* - vm:客户端跑在 WSL2 这个 Linux 虚拟机里
|
|
24
|
+
* `--lceda-in vm,host` 的顺序就是启动/探测优先级。
|
|
25
|
+
*/
|
|
26
|
+
type ClientLocation = "host" | "vm";
|
|
27
|
+
type ClientLocationSource = "default" | "argv" | "config-file" | "env";
|
|
28
|
+
/** 当前 Node 进程寻址文件的方式;WSL2 是 posix(/mnt/c/...),宿主是 Windows。 */
|
|
29
|
+
type PathStyle = "windows" | "posix";
|
|
30
|
+
type EnvironmentKindSource = "argv" | "config-file" | "env" | "auto-detected";
|
|
31
|
+
type Confidence = "certain" | "likely" | "unknown";
|
|
32
|
+
interface EnvironmentEvidence {
|
|
33
|
+
source: string;
|
|
34
|
+
value: string;
|
|
35
|
+
}
|
|
36
|
+
interface EnvironmentInfo {
|
|
37
|
+
kind: EnvironmentKind;
|
|
38
|
+
hostOs: HostOs;
|
|
39
|
+
platform: NodeJS.Platform;
|
|
40
|
+
pathStyle: PathStyle;
|
|
41
|
+
isWsl: boolean;
|
|
42
|
+
/** WSL 发行版名(WSL_DISTRO_NAME),非 WSL 时为 null。 */
|
|
43
|
+
distro: string | null;
|
|
44
|
+
networkingMode: NetworkingMode;
|
|
45
|
+
/** WSL2 下 Windows 宿主的可达地址;原生环境为 null。 */
|
|
46
|
+
windowsHost: string | null;
|
|
47
|
+
/** 客户端实例位置,按优先级排序;原生环境固定为 ["host"]。 */
|
|
48
|
+
clientLocations: ClientLocation[];
|
|
49
|
+
clientLocationSource: ClientLocationSource;
|
|
50
|
+
/** true 表示用户显式指定了环境,没有做自动发现。 */
|
|
51
|
+
explicit: boolean;
|
|
52
|
+
source: EnvironmentKindSource;
|
|
53
|
+
confidence: Confidence;
|
|
54
|
+
evidence: EnvironmentEvidence[];
|
|
55
|
+
warnings: string[];
|
|
56
|
+
}
|
|
57
|
+
/** 环境判定可注入的探针;默认实现读 /proc 并调用 wslinfo。 */
|
|
58
|
+
interface EnvironmentProbes {
|
|
59
|
+
platform?: NodeJS.Platform;
|
|
60
|
+
env?: NodeJS.ProcessEnv;
|
|
61
|
+
readText?: (file: string) => string | null;
|
|
62
|
+
exists?: (file: string) => boolean;
|
|
63
|
+
runText?: (command: string, args: readonly string[], timeoutMs: number) => string | null;
|
|
64
|
+
}
|
|
65
|
+
interface DetectEnvironmentOptions {
|
|
66
|
+
/** 显式环境:`--env` / 配置文件 environment.kind / LCEDA_ENV。 */
|
|
67
|
+
kind?: string | null;
|
|
68
|
+
/** 显式宿主地址:`--windows-host` / 配置文件 / LCEDA_WINDOWS_HOST。 */
|
|
69
|
+
windowsHost?: string | null;
|
|
70
|
+
/** 显式环境来自哪里;默认 argv。 */
|
|
71
|
+
kindSource?: EnvironmentKindSource;
|
|
72
|
+
/** 宿主地址是否也是显式给出的(用于证据与警告)。 */
|
|
73
|
+
windowsHostSource?: EnvironmentKindSource;
|
|
74
|
+
/**
|
|
75
|
+
* 客户端实例位置优先级:`vm` / `host` / `vm,host`。
|
|
76
|
+
* 省略时 WSL2 默认 host,其它环境固定 host。
|
|
77
|
+
*/
|
|
78
|
+
clientLocations?: string | readonly string[] | null;
|
|
79
|
+
clientLocationSource?: ClientLocationSource;
|
|
80
|
+
probes?: EnvironmentProbes;
|
|
81
|
+
/** 是否复用自动发现的缓存(默认 true;注入探针或测试时请设 false)。 */
|
|
82
|
+
cache?: boolean;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 客户端可执行文件发现。
|
|
86
|
+
*
|
|
87
|
+
* 候选路径同时取决于两件事:执行环境(windows-native / wsl2 / linux / macos)和
|
|
88
|
+
* 客户端实例位置(WSL2 下 host=Windows 宿主、vm=WSL 虚拟机内)。只做
|
|
89
|
+
* 「列候选 → 逐个 stat」,不猜不存在的位置;找不到时把试过的路径完整吐出来。
|
|
90
|
+
* 跨环境时统一成当前进程能执行的路径(WSL2 + host 是 /mnt/d/...),同时保留
|
|
91
|
+
* Windows 形式的 hostPath 供 pid 记录与提示。
|
|
92
|
+
*/
|
|
93
|
+
interface ExeCandidate {
|
|
94
|
+
path: string;
|
|
95
|
+
source: string;
|
|
96
|
+
}
|
|
97
|
+
interface ExeCandidateResult extends ExeCandidate {
|
|
98
|
+
exists: boolean;
|
|
99
|
+
}
|
|
100
|
+
type ClientExeStatus = "explicit" | "discovered" | "explicit_missing" | "not_found";
|
|
101
|
+
interface ClientExeDiscovery {
|
|
102
|
+
status: ClientExeStatus;
|
|
103
|
+
/** 当前进程可直接执行的路径;找不到时为 null。 */
|
|
104
|
+
path: string | null;
|
|
105
|
+
/** 宿主(Windows)风格路径,便于日志 / pid 记录;未知时 null。 */
|
|
106
|
+
hostPath: string | null;
|
|
107
|
+
source: string;
|
|
108
|
+
/** 这个结果对应的实例位置。 */
|
|
109
|
+
location: ClientLocation;
|
|
110
|
+
candidates: ExeCandidateResult[];
|
|
111
|
+
warnings: string[];
|
|
112
|
+
environment: EnvironmentInfo["kind"];
|
|
113
|
+
}
|
|
114
|
+
interface ClientExePlan {
|
|
115
|
+
/** 按 clientLocations 优先级排列,每项对应一个实例位置。 */
|
|
116
|
+
locations: ClientExeDiscovery[];
|
|
117
|
+
/** 第一个找到的可用路径(显式路径优先命中)。 */
|
|
118
|
+
primary: ClientExeDiscovery | null;
|
|
119
|
+
warnings: string[];
|
|
120
|
+
}
|
|
121
|
+
/** 立创EDA专业版历史默认安装路径(仅 Windows 原生环境;可用 `--exe` / LCEDA_PRO_EXE 覆盖)。 */
|
|
4
122
|
export declare const DEFAULT_PRO_EXE = "D:\\Program Files\\lceda-pro\\lceda-pro.exe";
|
|
5
123
|
export declare const DEFAULT_CDP_PORT = 9222;
|
|
6
124
|
interface CdpConfig {
|
|
125
|
+
/** 显式/历史默认的可执行文件路径;实际可执行路径见 `client.exe.path`。 */
|
|
7
126
|
exePath: string;
|
|
8
127
|
port: number;
|
|
9
128
|
/** CLI 探测端点的回环地址,不控制客户端的监听地址。 */
|
|
10
129
|
bindHost: string;
|
|
130
|
+
/** 当前环境下的首选探测地址(WSL2 可能是宿主地址,而不是回环)。 */
|
|
131
|
+
host: string;
|
|
132
|
+
/** 按客户端位置优先级展开的探测地址列表。 */
|
|
133
|
+
hosts: readonly string[];
|
|
11
134
|
runtimeDir: string;
|
|
12
135
|
pidFile: string;
|
|
13
136
|
logFile: string;
|
|
14
137
|
/** 旧版 easyeda-cdp-connect 的 pid 文件,迁移期只读兼容。 */
|
|
15
138
|
legacyPidFile: string;
|
|
16
139
|
startWaitMs: number;
|
|
140
|
+
/** 附加扫描端口区间(配置 LCEDA_CDP_SCAN=9222-9240)。 */
|
|
141
|
+
scan?: readonly [number, number];
|
|
17
142
|
}
|
|
18
143
|
interface BridgeConfig {
|
|
19
144
|
portFirst: number;
|
|
20
145
|
portLast: number;
|
|
146
|
+
/** 服务监听地址(契约固定回环,不可跨命名空间)。 */
|
|
21
147
|
bindHost: string;
|
|
148
|
+
/** 探测地址;默认回环,允许配置覆盖(例如端口转发场景)。 */
|
|
149
|
+
host: string;
|
|
22
150
|
serviceId: string;
|
|
23
151
|
runtimeDir: string;
|
|
24
152
|
logFile: string;
|
|
@@ -31,9 +159,19 @@ interface BridgeConfig {
|
|
|
31
159
|
/** LCEDA_BRIDGE_SCRIPT 覆盖 */
|
|
32
160
|
bridgeScript?: string;
|
|
33
161
|
}
|
|
162
|
+
interface ClientConfig {
|
|
163
|
+
/** 按实例位置优先级排列的发现结果。 */
|
|
164
|
+
plan: ClientExePlan;
|
|
165
|
+
/** 首个可用结果;没有则是 null。 */
|
|
166
|
+
exe: ClientExeDiscovery | null;
|
|
167
|
+
}
|
|
34
168
|
interface AppConfig {
|
|
35
169
|
cdp: CdpConfig;
|
|
36
170
|
bridge: BridgeConfig;
|
|
171
|
+
/** 执行环境(自动发现或显式指定)。 */
|
|
172
|
+
environment: EnvironmentInfo;
|
|
173
|
+
/** 客户端可执行文件发现结果。 */
|
|
174
|
+
client: ClientConfig;
|
|
37
175
|
}
|
|
38
176
|
interface CdpOverrides {
|
|
39
177
|
exePath?: string;
|
|
@@ -47,13 +185,23 @@ interface ConfigOverrides {
|
|
|
47
185
|
cdp?: CdpOverrides;
|
|
48
186
|
bridge?: BridgeOverrides;
|
|
49
187
|
}
|
|
188
|
+
interface LoadConfigOptions {
|
|
189
|
+
/** 环境探测注入(测试用)。 */
|
|
190
|
+
detect?: DetectEnvironmentOptions;
|
|
191
|
+
/** 客户端发现是否走 where/which 深探(较慢,默认关闭)。 */
|
|
192
|
+
deepExe?: boolean;
|
|
193
|
+
/** 注入文件存在性判断(测试用)。 */
|
|
194
|
+
exists?: (file: string) => boolean;
|
|
195
|
+
}
|
|
50
196
|
/**
|
|
51
|
-
* 配置优先级:命令行参数 > 环境变量 >
|
|
197
|
+
* 配置优先级:命令行参数 > 配置文件 > .env > 环境变量 > 内置默认值
|
|
198
|
+
* (前四级由 core/runtimeConfig 合并成 env,这里只处理 env + 显式 overrides)。
|
|
199
|
+
*
|
|
52
200
|
* 目录约定:
|
|
53
201
|
* - CDP 运行态放在 ~/.lceda-connect/cdp(可用 LCEDA_CONNECT_HOME 改根)
|
|
54
202
|
* - Bridge 运行态沿用 %LOCALAPPDATA%\lceda-bridge,保证老的 state.json / bridge.log 继续可用
|
|
55
203
|
*/
|
|
56
|
-
export declare function loadConfig(env?: NodeJS.ProcessEnv, overrides?: ConfigOverrides): AppConfig;
|
|
204
|
+
export declare function loadConfig(env?: NodeJS.ProcessEnv, overrides?: ConfigOverrides, options?: LoadConfigOptions): AppConfig;
|
|
57
205
|
/** 运行时共享类型。 */
|
|
58
206
|
interface ProcInfo {
|
|
59
207
|
pid: number;
|
|
@@ -2760,12 +2908,84 @@ type ToolsetSelection = string | readonly string[];
|
|
|
2760
2908
|
* `offline`/`live` 聚合名以及 `cdp,bridge,eda,api,...` 精确领域名。
|
|
2761
2909
|
*/
|
|
2762
2910
|
export declare function allTools(selection?: ToolsetSelection): ToolDef[];
|
|
2763
|
-
|
|
2911
|
+
/**
|
|
2912
|
+
* 传输适配层的公共契约。
|
|
2913
|
+
*
|
|
2914
|
+
* 组装(tools/resources/roots/写路径策略)只发生在 `createServer` / `createServerFactory`;
|
|
2915
|
+
* stdio 与 Streamable HTTP 只做「把协议的字节搬到 McpServer 上」这一件事。
|
|
2916
|
+
* 这样新增传输(SSE、WebSocket、进程内 pipe)时只需要写一个适配器,
|
|
2917
|
+
* 不在传输层复制任何业务/权限逻辑。
|
|
2918
|
+
*/
|
|
2919
|
+
interface McpServerFactoryOptions {
|
|
2764
2920
|
env?: NodeJS.ProcessEnv;
|
|
2765
2921
|
tools?: ToolDef[];
|
|
2766
2922
|
toolsets?: ToolsetSelection;
|
|
2767
2923
|
}
|
|
2924
|
+
/** 每次调用返回一个独立的 McpServer;HTTP 每个 session 一个,stdio 一个。 */
|
|
2925
|
+
type McpServerFactory = () => McpServer;
|
|
2926
|
+
interface TransportStartOptions {
|
|
2927
|
+
env: NodeJS.ProcessEnv;
|
|
2928
|
+
toolsets?: ToolsetSelection;
|
|
2929
|
+
/** 嵌入/测试用:覆盖默认的 createServerFactory。 */
|
|
2930
|
+
serverFactory?: McpServerFactory;
|
|
2931
|
+
}
|
|
2932
|
+
/** 传输实例的生命周期句柄;`close` 必须可重复调用。 */
|
|
2933
|
+
interface TransportHandle {
|
|
2934
|
+
/** 监听地址(HTTP);stdio 没有。 */
|
|
2935
|
+
readonly url?: string;
|
|
2936
|
+
close(): Promise<void>;
|
|
2937
|
+
}
|
|
2938
|
+
type CreateServerOptions = McpServerFactoryOptions;
|
|
2768
2939
|
export declare function createServer(options?: CreateServerOptions): McpServer;
|
|
2769
|
-
/**
|
|
2770
|
-
|
|
2940
|
+
/**
|
|
2941
|
+
* 组装工厂:传输适配器只依赖这个函数,不依赖 createServer 的细节。
|
|
2942
|
+
* 每次调用必须返回独立的 McpServer(HTTP 每个 session 一个)。
|
|
2943
|
+
*/
|
|
2944
|
+
export declare function createServerFactory(options?: CreateServerOptions): McpServerFactory;
|
|
2945
|
+
/**
|
|
2946
|
+
* stdio 传输适配器(薄层)。
|
|
2947
|
+
*
|
|
2948
|
+
* 只做三件事:从工厂拿一个 McpServer、接到 StdioServerTransport、把就绪日志写 stderr。
|
|
2949
|
+
* stdout 归 JSON-RPC 协议所有,这里不写任何日志。
|
|
2950
|
+
*/
|
|
2951
|
+
interface StdioTransportOptions extends TransportStartOptions {
|
|
2952
|
+
/** 诊断日志出口,默认 stderr。 */
|
|
2953
|
+
log?: (line: string) => void;
|
|
2954
|
+
}
|
|
2955
|
+
interface StdioTransportHandle extends TransportHandle {
|
|
2956
|
+
readonly kind: "stdio";
|
|
2957
|
+
}
|
|
2958
|
+
export declare function startStdioTransport(options: StdioTransportOptions): Promise<StdioTransportHandle>;
|
|
2959
|
+
/** 兼容旧入口:连接后即返回(进程由 stdio 传输保持存活)。 */
|
|
2960
|
+
export declare function serveStdio(env?: NodeJS.ProcessEnv, toolsets?: TransportStartOptions["toolsets"]): Promise<void>;
|
|
2961
|
+
/**
|
|
2962
|
+
* MCP Streamable HTTP 传输(2025-03-26 规范)。
|
|
2963
|
+
*
|
|
2964
|
+
* 默认只监听 127.0.0.1;要跨 WSL/宿主访问必须显式 `--host`(建议只绑 WSL 虚拟网卡
|
|
2965
|
+
* 地址,不要 0.0.0.0)。可选 `--token` 要求 Authorization: Bearer。
|
|
2966
|
+
* 规范要求:Host/Origin 校验、未知 session 返回 404、无 SSE 能力时对 GET 返回 405、
|
|
2967
|
+
* 请求体超限返回 413(连接不 reset)。`/health` 只返回非敏感状态。
|
|
2968
|
+
*/
|
|
2969
|
+
interface HttpTransportOptions extends TransportStartOptions {
|
|
2970
|
+
/** 监听地址;默认 127.0.0.1。 */
|
|
2971
|
+
host?: string;
|
|
2972
|
+
/** 监听端口;默认 3939,0 表示随机端口。 */
|
|
2973
|
+
port?: number;
|
|
2974
|
+
/** MCP 端点路径;默认 /mcp。 */
|
|
2975
|
+
path?: string;
|
|
2976
|
+
/** 可选的 Bearer token。 */
|
|
2977
|
+
token?: string;
|
|
2978
|
+
/** 无状态模式:每个 POST 独立 server,不下发 session id。 */
|
|
2979
|
+
stateless?: boolean;
|
|
2980
|
+
/** 诊断日志出口,默认 stderr。 */
|
|
2981
|
+
log?: (line: string) => void;
|
|
2982
|
+
}
|
|
2983
|
+
interface HttpTransportHandle extends TransportHandle {
|
|
2984
|
+
kind: "http";
|
|
2985
|
+
url: string;
|
|
2986
|
+
host: string;
|
|
2987
|
+
port: number;
|
|
2988
|
+
path: string;
|
|
2989
|
+
}
|
|
2990
|
+
export declare function startHttpTransport(options: HttpTransportOptions): Promise<HttpTransportHandle>;
|
|
2771
2991
|
export { type AppConfig, type BridgeAction, type BridgeConfig, type BridgeHealth, type BridgeOptions, type CdpAction, type CdpConfig, type CdpInfo, type CdpOptions, type CommandContext, type ConfigOverrides, type ProcInfo, type TcpListener, type ToolContext, type ToolDef, index_d_exports as eprj3, index_d_exports$1 as epro2, index_d_exports$2 as format, index_d_exports$3 as sim };
|