@wenaixi/searchfuse 0.0.0-stage → 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/CHANGELOG.md +131 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/README.zh-CN.md +170 -0
- package/SECURITY.md +20 -0
- package/claude/.claude-plugin/plugin.json +17 -0
- package/claude/SKILL.md +53 -0
- package/claude/hooks/hooks.json +3 -0
- package/claude/hooks/register.ts +552 -0
- package/cordis.patch.yml +16 -0
- package/dist/config-schema.js +40 -0
- package/dist/errorCause-BY0A0XKN.js +17 -0
- package/dist/kernel.js +62 -0
- package/dist/main.js +1371 -0
- package/dist/mcp-protocol.js +1 -0
- package/dist/mcp-registry.js +155 -0
- package/dist/protocol-ClnjMnGV.js +259 -0
- package/dist/proxy-DodOSGiI.js +50 -0
- package/dist/search-BIW13Csk.js +4061 -0
- package/dist/serve-Bfqz-nCB.js +279 -0
- package/dist/serve-ensure.js +140 -0
- package/docs/commit.md +77 -0
- package/docs/dsh.md +205 -0
- package/docs/harness-setup.md +78 -0
- package/docs/parity-matrix.md +61 -0
- package/docs/searxng-setup.md +117 -0
- package/docs/security.md +59 -0
- package/docs/settings-redesign-decisions.md +407 -0
- package/docs/superpowers/plans/2026-11-24-serve-lifecycle.md +880 -0
- package/docs/superpowers/reports/2026-11-24-serve-lifecycle-ledger.md +211 -0
- package/docs/superpowers/specs/2026-11-20-searchfuse-dual-host-design.md +172 -0
- package/docs/superpowers/specs/2026-11-21-searchfuse-tri-host-design.md +659 -0
- package/docs/superpowers/specs/2026-11-21-tri-host-takeover-design.md +363 -0
- package/docs/superpowers/specs/2026-11-22-settings-card-refactor-design.md +117 -0
- package/docs/superpowers/specs/2026-11-24-serve-lifecycle-design.md +155 -0
- package/docs/testing.md +92 -0
- package/docs/troubleshooting.md +258 -0
- package/dsh/client.js +2917 -0
- package/dsh/fetch-schema.json +1 -0
- package/dsh/index.js +2640 -0
- package/dsh/search-schema.json +65 -0
- package/dsh/spawnHidden.d.ts +7 -0
- package/dsh/spawnHidden.js +8 -0
- package/omp/extension.ts +331 -0
- package/package.json +95 -4
- package/skills/searchfuse/SKILL.md +139 -0
- package/skills/searchfuse/references/cli.md +78 -0
- package/skills/searchfuse/references/configure.md +405 -0
- package/skills/searchfuse/references/output-schema.md +182 -0
- package/skills/searchfuse/references/runtime.md +58 -0
- package/skills/searchfuse/scripts/run.ps1 +279 -0
- package/skills/searchfuse/scripts/run.sh +315 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as McpConnection, n as McpProtocolError, r as isServerNameValid, i as parseSseMessages, a as publicToolName } from "./protocol-ClnjMnGV.js";
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { a as publicToolName, r as isServerNameValid, t as McpConnection } from "./protocol-ClnjMnGV.js";
|
|
2
|
+
import { t as formatErrorWithCause } from "./errorCause-BY0A0XKN.js";
|
|
3
|
+
// src/mcp/registry.ts
|
|
4
|
+
/**
|
|
5
|
+
* DSH 工具运行时接受的输出形状。
|
|
6
|
+
*
|
|
7
|
+
* `assertSupportedJsonSchema` 只允许 JSON Schema 的一小部分子集,而 MCP
|
|
8
|
+
* 工具的结果是任意的内容数组。把它包进这个记录里才使它可注册;这个形状
|
|
9
|
+
* 照抄宿主自己的 MCP 客户端,好让同一种工具无论由哪一侧注册看起来都一样。
|
|
10
|
+
*/
|
|
11
|
+
var MCP_OUTPUT_SCHEMA = {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: { content: {
|
|
14
|
+
type: "array",
|
|
15
|
+
items: {}
|
|
16
|
+
} },
|
|
17
|
+
required: ["content"],
|
|
18
|
+
additionalProperties: false
|
|
19
|
+
};
|
|
20
|
+
/** 把 MCP 结果的文本块摊平成模型读到的那段文本。 */
|
|
21
|
+
function extractText(content) {
|
|
22
|
+
if (!Array.isArray(content)) return typeof content === "string" ? content : "";
|
|
23
|
+
return content.map((block) => {
|
|
24
|
+
if (typeof block === "string") return block;
|
|
25
|
+
const record = block;
|
|
26
|
+
if (record.type === "text" && typeof record.text === "string") return record.text;
|
|
27
|
+
return "";
|
|
28
|
+
}).filter((line) => line !== "").join("\n\n");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 把每个已启用服务端的工具注册到 context 上。
|
|
32
|
+
*
|
|
33
|
+
* 每个服务端返回一份报告,这样插件能告诉用户发生了什么,而不必以启动失败
|
|
34
|
+
* 收场。数字与措辞刻意按服务端分开:「context7 需要密钥」和「grep 连不上」
|
|
35
|
+
* 是两种不同的修法。
|
|
36
|
+
*/
|
|
37
|
+
async function registerMcpServers(ctx, options) {
|
|
38
|
+
const reserved = new Set(options.reservedNames ?? []);
|
|
39
|
+
const claimed = new Set(reserved);
|
|
40
|
+
const reports = [];
|
|
41
|
+
/** 为已注册的工具保持打开的连接,由调用方关闭。 */
|
|
42
|
+
const keepAlive = [];
|
|
43
|
+
/** 本轮注册的工具对应的撤销函数。 */
|
|
44
|
+
const disposeTools_ = [];
|
|
45
|
+
let closed = false;
|
|
46
|
+
const closeAll = async () => {
|
|
47
|
+
if (closed) return;
|
|
48
|
+
closed = true;
|
|
49
|
+
await Promise.all(keepAlive.map((c) => c.close().catch(() => void 0)));
|
|
50
|
+
};
|
|
51
|
+
const handshakes = await Promise.all(Object.entries(options.servers).map(async ([serverKey, server]) => {
|
|
52
|
+
if (server.enabled === false) return;
|
|
53
|
+
if (!isServerNameValid(serverKey)) return {
|
|
54
|
+
ok: false,
|
|
55
|
+
serverKey,
|
|
56
|
+
error: `server key "${serverKey}" must match [A-Za-z0-9_-]{1,32}`
|
|
57
|
+
};
|
|
58
|
+
let connection;
|
|
59
|
+
try {
|
|
60
|
+
connection = new McpConnection(server);
|
|
61
|
+
const advertised = await connection.listTools();
|
|
62
|
+
return {
|
|
63
|
+
ok: true,
|
|
64
|
+
serverKey,
|
|
65
|
+
connection,
|
|
66
|
+
advertised
|
|
67
|
+
};
|
|
68
|
+
} catch (error) {
|
|
69
|
+
await connection?.close().catch(() => void 0);
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
serverKey,
|
|
73
|
+
error: formatErrorWithCause(error)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}));
|
|
77
|
+
for (const handshake of handshakes) {
|
|
78
|
+
if (handshake === void 0) continue;
|
|
79
|
+
const { serverKey } = handshake;
|
|
80
|
+
const report = {
|
|
81
|
+
serverKey,
|
|
82
|
+
url: options.servers[serverKey].url,
|
|
83
|
+
connected: false,
|
|
84
|
+
tools: []
|
|
85
|
+
};
|
|
86
|
+
if (!handshake.ok) {
|
|
87
|
+
report.error = handshake.error;
|
|
88
|
+
reports.push(report);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const connection = handshake.connection;
|
|
92
|
+
const allowed = options.allowTools?.[serverKey];
|
|
93
|
+
const eligible = handshake.advertised.filter((tool) => allowed === void 0 || allowed.includes(tool.name));
|
|
94
|
+
for (const tool of eligible) {
|
|
95
|
+
const overrideKey = `${serverKey}/${tool.name}`;
|
|
96
|
+
const name = options.toolNames?.[overrideKey] ?? publicToolName(serverKey, tool.name);
|
|
97
|
+
if (claimed.has(name)) {
|
|
98
|
+
(report.skipped ??= []).push(`"${name}" is already taken`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if ((() => {
|
|
102
|
+
try {
|
|
103
|
+
return typeof ctx.tools?.get === "function" ? ctx.tools.get(name) !== void 0 : false;
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
})()) {
|
|
108
|
+
(report.skipped ??= []).push(`"${name}" is already registered by the host`);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
claimed.add(name);
|
|
112
|
+
const toolConnection = connection;
|
|
113
|
+
try {
|
|
114
|
+
const registered = ctx.tools.register({
|
|
115
|
+
name,
|
|
116
|
+
description: tool.description ?? `Tool ${tool.name} provided by the MCP server "${serverKey}".`,
|
|
117
|
+
parameters: tool.inputSchema ?? {
|
|
118
|
+
type: "object",
|
|
119
|
+
properties: {}
|
|
120
|
+
},
|
|
121
|
+
output: {
|
|
122
|
+
schema: MCP_OUTPUT_SCHEMA,
|
|
123
|
+
render: (_args, value) => [{
|
|
124
|
+
type: "text",
|
|
125
|
+
text: extractText(value?.content) || "(no content)"
|
|
126
|
+
}]
|
|
127
|
+
},
|
|
128
|
+
isConcurrencySafe: () => true,
|
|
129
|
+
async execute(args, exec) {
|
|
130
|
+
return toolConnection.callTool(tool.name, args ?? {}, exec?.signal);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
if (typeof registered === "function") disposeTools_.push(registered);
|
|
134
|
+
report.tools.push(name);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
report.error = `tool name "${name}" could not be registered: ${error instanceof Error ? error.message : String(error)}`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
report.connected = true;
|
|
140
|
+
keepAlive.push(connection);
|
|
141
|
+
reports.push(report);
|
|
142
|
+
}
|
|
143
|
+
const disposeTools = () => {
|
|
144
|
+
for (const undo of disposeTools_) try {
|
|
145
|
+
undo();
|
|
146
|
+
} catch {}
|
|
147
|
+
disposeTools_.length = 0;
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
reports,
|
|
151
|
+
closeAll,
|
|
152
|
+
disposeTools
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export { MCP_OUTPUT_SCHEMA, extractText, registerMcpServers };
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { t as resolveProxyForUrl } from "./proxy-DodOSGiI.js";
|
|
2
|
+
import { EnvHttpProxyAgent } from "undici";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
// src/mcp/protocol.ts
|
|
5
|
+
var HANDSHAKE_PROTOCOL_VERSION = "2025-06-18";
|
|
6
|
+
/** 宿主自己的工具名契约,这里等价复制,好让同一个名字在两边含义一致。 */
|
|
7
|
+
var MAX_PUBLIC_NAME_LENGTH = 64;
|
|
8
|
+
var INVALID_NAME_CHARS = /[^A-Za-z0-9_-]/g;
|
|
9
|
+
var HASH_LENGTH = 12;
|
|
10
|
+
var SERVER_NAME_PATTERN = /^[A-Za-z0-9_-]{1,32}$/;
|
|
11
|
+
function isServerNameValid(name) {
|
|
12
|
+
return SERVER_NAME_PATTERN.test(name);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* `mcp__<server>__<tool>`,折叠到宿主接受的字符集,超长时用稳定的哈希缩短。
|
|
16
|
+
*/
|
|
17
|
+
function publicToolName(serverName, rawName) {
|
|
18
|
+
const normalized = `mcp__${serverName}__${rawName}`.replace(INVALID_NAME_CHARS, "_");
|
|
19
|
+
if (normalized.length <= MAX_PUBLIC_NAME_LENGTH) return normalized;
|
|
20
|
+
const hash = createHash("sha256").update(`${serverName}\0${rawName}`).digest("hex").slice(0, HASH_LENGTH);
|
|
21
|
+
return `${normalized.slice(0, 51)}_${hash}`;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 从 SSE 响应体里读出 JSON-RPC 消息。
|
|
25
|
+
*
|
|
26
|
+
* 注释行(`:`)是保活,空行分隔事件,而 `data:` 载荷可能拆成好几行。
|
|
27
|
+
* 解析不了 JSON 的内容直接跳过,而不是当作致命错误:服务端有权发出
|
|
28
|
+
* 本客户端不认识的事件类型,一帧跑偏不能把后面真正的答案弄丢。
|
|
29
|
+
*/
|
|
30
|
+
function parseSseMessages(body) {
|
|
31
|
+
const messages = [];
|
|
32
|
+
for (const event of body.split(/\r?\n\r?\n/)) {
|
|
33
|
+
const dataLines = event.split(/\r?\n/).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).replace(/^ /, ""));
|
|
34
|
+
if (dataLines.length === 0) continue;
|
|
35
|
+
try {
|
|
36
|
+
messages.push(JSON.parse(dataLines.join("\n")));
|
|
37
|
+
} catch {}
|
|
38
|
+
}
|
|
39
|
+
return messages;
|
|
40
|
+
}
|
|
41
|
+
var McpProtocolError = class extends Error {
|
|
42
|
+
code;
|
|
43
|
+
constructor(message, code) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "McpProtocolError";
|
|
46
|
+
this.code = code;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* 一条服务端连接。
|
|
51
|
+
*
|
|
52
|
+
* 不在多次运行之间共享:MCP 服务端可能是无状态的,而对一个本就并发扇出
|
|
53
|
+
* 请求的搜索引擎来说,占着一只 socket 不关也换不来任何好处。
|
|
54
|
+
*/
|
|
55
|
+
var McpConnection = class {
|
|
56
|
+
sessionId;
|
|
57
|
+
protocolVersion = HANDSHAKE_PROTOCOL_VERSION;
|
|
58
|
+
initialized = false;
|
|
59
|
+
config;
|
|
60
|
+
/** 一次工具调用允许的时间。用户配的 `timeoutMs` 管的是这个。 */
|
|
61
|
+
toolTimeoutMs;
|
|
62
|
+
/**
|
|
63
|
+
* 握手允许的时间。
|
|
64
|
+
*
|
|
65
|
+
* 与工具调用分开,因为它们是两种完全不同性质的等待:握手只是几次 HTTP
|
|
66
|
+
* 往返,实测一两秒就够;而一次 `tools/call` 可能真的在服务端算很久。用同一
|
|
67
|
+
* 个 30 秒的上限管两件事,代价是一个死掉的服务端会把插件启动拖住 30 秒——
|
|
68
|
+
* 而它其实在第一秒就已经没救了。
|
|
69
|
+
*
|
|
70
|
+
* 用户显式配的 `timeoutMs` 仍然压过这里的默认值:有人跑在慢链路上,10 秒
|
|
71
|
+
* 也许不够。
|
|
72
|
+
*/
|
|
73
|
+
handshakeTimeoutMs;
|
|
74
|
+
constructor(config, options = {}) {
|
|
75
|
+
this.config = config;
|
|
76
|
+
this.toolTimeoutMs = options.timeoutMs ?? config.timeoutMs ?? 3e4;
|
|
77
|
+
this.handshakeTimeoutMs = config.timeoutMs ?? Math.min(this.toolTimeoutMs, 1e4);
|
|
78
|
+
}
|
|
79
|
+
get serverUrl() {
|
|
80
|
+
return this.config.url;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 所有出站请求的唯一入口:按 `config.proxy` 决定走不走代理。
|
|
84
|
+
*
|
|
85
|
+
* 为什么必须集中一处:这个文件里有四个 fetch 调用点(请求、通知、
|
|
86
|
+
* 握手、删会话)。漏掉任何一个都会让「配了代理却仍直连」以最难查的
|
|
87
|
+
* 形式出现——大部分调用正常,只有某一种操作失败。
|
|
88
|
+
*
|
|
89
|
+
* 三路语义与引擎侧一致(见 `util/explicitProxy.ts`):
|
|
90
|
+
* - `undefined`:按机器上的 proxy 环境变量解析
|
|
91
|
+
* - `null`:直连,且绝不查环境变量
|
|
92
|
+
* - URL:只用这一个代理
|
|
93
|
+
*/
|
|
94
|
+
async proxyFetch(url, init) {
|
|
95
|
+
const explicit = this.config.proxy;
|
|
96
|
+
const proxyUrl = explicit === void 0 ? resolveProxyForUrl(new URL(url), process.env) : explicit === null ? null : explicit;
|
|
97
|
+
if (proxyUrl === null) return fetch(url, init);
|
|
98
|
+
const dispatcher = new EnvHttpProxyAgent({
|
|
99
|
+
httpProxy: proxyUrl,
|
|
100
|
+
httpsProxy: proxyUrl,
|
|
101
|
+
noProxy: ""
|
|
102
|
+
});
|
|
103
|
+
return fetch(url, {
|
|
104
|
+
...init,
|
|
105
|
+
dispatcher
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
headers(extra = {}) {
|
|
109
|
+
const headers = {
|
|
110
|
+
"content-type": "application/json",
|
|
111
|
+
accept: "application/json, text/event-stream",
|
|
112
|
+
...this.config.headers,
|
|
113
|
+
...extra
|
|
114
|
+
};
|
|
115
|
+
if (this.sessionId) headers["mcp-session-id"] = this.sessionId;
|
|
116
|
+
if (this.initialized) headers["mcp-protocol-version"] = this.protocolVersion;
|
|
117
|
+
return headers;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 本次调用的取消信号:本次调用的超时,加上调用方传进来的那个。
|
|
121
|
+
*
|
|
122
|
+
* 两者用 AbortSignal.any 合并,谁先到都算取消。于是超时保护照旧,而用户
|
|
123
|
+
* 中途点停止也能真的把在飞的请求掐断,而不是让它跑完再自己超时。
|
|
124
|
+
*/
|
|
125
|
+
signalFor(external) {
|
|
126
|
+
const timeout = AbortSignal.timeout(this.initialized ? this.toolTimeoutMs : this.handshakeTimeoutMs);
|
|
127
|
+
return external === void 0 ? timeout : AbortSignal.any([timeout, external]);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 一次 JSON-RPC 请求。返回与之匹配的响应消息。
|
|
131
|
+
*
|
|
132
|
+
* 404 是协议表达「会话已没了」的方式,所以它会触发一次重新握手加一次重试。
|
|
133
|
+
* 其他情况一律不重试:401 或格式不对的响应意味着服务端拒绝了这次调用,
|
|
134
|
+
* 重复一遍只会再失败一次。
|
|
135
|
+
*/
|
|
136
|
+
async request(body, id, allowReconnect = true, external) {
|
|
137
|
+
const response = await this.proxyFetch(this.config.url, {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers: this.headers(),
|
|
140
|
+
body: JSON.stringify(body),
|
|
141
|
+
signal: this.signalFor(external)
|
|
142
|
+
});
|
|
143
|
+
if (response.status === 202) return {};
|
|
144
|
+
if (response.status === 404 && allowReconnect) {
|
|
145
|
+
this.sessionId = void 0;
|
|
146
|
+
this.initialized = false;
|
|
147
|
+
await this.handshake();
|
|
148
|
+
if (Object.hasOwn(body, "id")) return this.request(body, id, false, external);
|
|
149
|
+
return {};
|
|
150
|
+
}
|
|
151
|
+
const text = await response.text();
|
|
152
|
+
if (!response.ok) throw new McpProtocolError(`MCP server ${this.config.url} answered ${response.status}.${text ? ` ${text.slice(0, 300)}` : ""}`);
|
|
153
|
+
const messages = (response.headers.get("content-type") ?? "").includes("event-stream") ? parseSseMessages(text) : (() => {
|
|
154
|
+
try {
|
|
155
|
+
return [JSON.parse(text)];
|
|
156
|
+
} catch {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
})();
|
|
160
|
+
const matched = messages.find((message) => message.id === id) ?? messages.at(-1);
|
|
161
|
+
if (matched?.error !== void 0) throw new McpProtocolError(matched.error.message ?? "MCP server returned an error with no message", matched.error.code);
|
|
162
|
+
return matched ?? {};
|
|
163
|
+
}
|
|
164
|
+
async notify(method) {
|
|
165
|
+
await this.proxyFetch(this.config.url, {
|
|
166
|
+
method: "POST",
|
|
167
|
+
headers: this.headers(),
|
|
168
|
+
body: JSON.stringify({
|
|
169
|
+
jsonrpc: "2.0",
|
|
170
|
+
method
|
|
171
|
+
}),
|
|
172
|
+
signal: AbortSignal.timeout(this.handshakeTimeoutMs)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/** 先 initialize,再 notifications/initialized。单次运行内幂等。 */
|
|
176
|
+
async handshake() {
|
|
177
|
+
if (this.initialized) return;
|
|
178
|
+
const initId = 1;
|
|
179
|
+
const response = await this.proxyFetch(this.config.url, {
|
|
180
|
+
method: "POST",
|
|
181
|
+
headers: {
|
|
182
|
+
"content-type": "application/json",
|
|
183
|
+
accept: "application/json, text/event-stream",
|
|
184
|
+
...this.config.headers
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify({
|
|
187
|
+
jsonrpc: "2.0",
|
|
188
|
+
id: initId,
|
|
189
|
+
method: "initialize",
|
|
190
|
+
params: {
|
|
191
|
+
protocolVersion: HANDSHAKE_PROTOCOL_VERSION,
|
|
192
|
+
capabilities: {},
|
|
193
|
+
clientInfo: {
|
|
194
|
+
name: "searchfuse",
|
|
195
|
+
version: "1.0.0"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}),
|
|
199
|
+
signal: AbortSignal.timeout(this.handshakeTimeoutMs)
|
|
200
|
+
});
|
|
201
|
+
const assigned = response.headers.get("mcp-session-id");
|
|
202
|
+
if (assigned) this.sessionId = assigned;
|
|
203
|
+
const text = await response.text();
|
|
204
|
+
if (!response.ok) throw new McpProtocolError(`MCP server ${this.config.url} refused initialize with ${response.status}.${text ? ` ${text.slice(0, 300)}` : ""}`);
|
|
205
|
+
if (!(response.headers.get("content-type") ?? "").includes("event-stream") && text !== "") try {
|
|
206
|
+
const version = JSON.parse(text).result?.protocolVersion;
|
|
207
|
+
if (typeof version === "string" && version !== "") this.protocolVersion = version;
|
|
208
|
+
} catch {}
|
|
209
|
+
this.initialized = true;
|
|
210
|
+
await this.notify("notifications/initialized");
|
|
211
|
+
}
|
|
212
|
+
async listTools() {
|
|
213
|
+
await this.handshake();
|
|
214
|
+
const collected = [];
|
|
215
|
+
let cursor;
|
|
216
|
+
for (let page = 0; page < 20; page += 1) {
|
|
217
|
+
const result = (await this.request({
|
|
218
|
+
jsonrpc: "2.0",
|
|
219
|
+
id: 2,
|
|
220
|
+
method: "tools/list",
|
|
221
|
+
params: cursor === void 0 ? {} : { cursor }
|
|
222
|
+
}, 2)).result;
|
|
223
|
+
for (const tool of result?.tools ?? []) collected.push(tool);
|
|
224
|
+
cursor = result?.nextCursor;
|
|
225
|
+
if (cursor === void 0 || cursor === "") break;
|
|
226
|
+
}
|
|
227
|
+
return collected;
|
|
228
|
+
}
|
|
229
|
+
async callTool(name, args, signal) {
|
|
230
|
+
if (signal?.aborted) throw new McpProtocolError(`MCP call ${name} was cancelled before it started.`);
|
|
231
|
+
await this.handshake();
|
|
232
|
+
return (await this.request({
|
|
233
|
+
jsonrpc: "2.0",
|
|
234
|
+
id: 3,
|
|
235
|
+
method: "tools/call",
|
|
236
|
+
params: {
|
|
237
|
+
name,
|
|
238
|
+
arguments: args
|
|
239
|
+
}
|
|
240
|
+
}, 3, true, signal)).result;
|
|
241
|
+
}
|
|
242
|
+
/** 显式拆除。405 意味着服务端把会话留给自己管。 */
|
|
243
|
+
async close() {
|
|
244
|
+
if (!this.sessionId) return;
|
|
245
|
+
try {
|
|
246
|
+
await this.proxyFetch(this.config.url, {
|
|
247
|
+
method: "DELETE",
|
|
248
|
+
headers: {
|
|
249
|
+
"mcp-session-id": this.sessionId,
|
|
250
|
+
...this.config.headers
|
|
251
|
+
},
|
|
252
|
+
signal: AbortSignal.timeout(5e3)
|
|
253
|
+
});
|
|
254
|
+
} catch {}
|
|
255
|
+
this.sessionId = void 0;
|
|
256
|
+
this.initialized = false;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
export { publicToolName as a, parseSseMessages as i, McpProtocolError as n, isServerNameValid as r, McpConnection as t };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// src/util/proxy.ts
|
|
2
|
+
function firstNonEmpty(values) {
|
|
3
|
+
for (const value of values) {
|
|
4
|
+
if (value === void 0) continue;
|
|
5
|
+
const trimmed = value.trim();
|
|
6
|
+
if (trimmed !== "") return trimmed;
|
|
7
|
+
}
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
function proxyUriForProtocol(protocol, env) {
|
|
11
|
+
if (protocol === "http:") return firstNonEmpty([env.http_proxy, env.HTTP_PROXY]);
|
|
12
|
+
if (protocol === "https:") return firstNonEmpty([
|
|
13
|
+
env.https_proxy,
|
|
14
|
+
env.HTTPS_PROXY,
|
|
15
|
+
env.http_proxy,
|
|
16
|
+
env.HTTP_PROXY
|
|
17
|
+
]);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
function urlPort(url) {
|
|
21
|
+
if (url.port) return Number.parseInt(url.port, 10);
|
|
22
|
+
return url.protocol === "https:" ? 443 : 80;
|
|
23
|
+
}
|
|
24
|
+
function isExcludedByNoProxy(url, env) {
|
|
25
|
+
const raw = env.no_proxy ?? env.NO_PROXY;
|
|
26
|
+
if (raw === void 0) return false;
|
|
27
|
+
const trimmed = raw.trim();
|
|
28
|
+
if (trimmed === "") return false;
|
|
29
|
+
if (trimmed === "*") return true;
|
|
30
|
+
const hostname = url.hostname.toLowerCase();
|
|
31
|
+
const port = urlPort(url);
|
|
32
|
+
for (const part of trimmed.split(/[,\s]/)) {
|
|
33
|
+
if (!part) continue;
|
|
34
|
+
if (part === "*") return true;
|
|
35
|
+
const parsed = part.match(/^(.+):(\d+)$/);
|
|
36
|
+
const entryHost = (parsed ? parsed[1] : part).replace(/^\*?\./, "").toLowerCase();
|
|
37
|
+
const entryPort = parsed ? Number.parseInt(parsed[2], 10) : void 0;
|
|
38
|
+
if (entryPort !== void 0 && entryPort !== port) continue;
|
|
39
|
+
if (hostname === entryHost || hostname.endsWith(`.${entryHost}`)) return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/** Which system HTTP proxy to use for this URL, or null to go direct. */
|
|
44
|
+
function resolveProxyForUrl(url, env) {
|
|
45
|
+
const proxyUrl = proxyUriForProtocol(url.protocol, env);
|
|
46
|
+
if (!proxyUrl) return null;
|
|
47
|
+
if (isExcludedByNoProxy(url, env)) return null;
|
|
48
|
+
return proxyUrl;
|
|
49
|
+
}
|
|
50
|
+
export { resolveProxyForUrl as t };
|