@herbertgao/pi-extensions 2026.9.11 → 2026.9.13
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 +4 -4
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +8 -0
- package/node_modules/@herbertgao/pi-subagents/package.json +1 -1
- package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +5 -4
- package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +39 -16
- package/node_modules/@juicesharp/rpiv-ask-user-question/package.json +2 -2
- package/node_modules/@narumitw/pi-btw/README.md +21 -4
- package/node_modules/@narumitw/pi-btw/dist/index.ts +1668 -958
- package/node_modules/@narumitw/pi-btw/dist/index.ts.map +4 -4
- package/node_modules/@narumitw/pi-btw/docs/workflows.md +10 -2
- package/node_modules/@narumitw/pi-btw/package.json +1 -1
- package/node_modules/@narumitw/pi-btw/src/btw.ts +17 -79
- package/node_modules/@narumitw/pi-btw/src/conversation-context.ts +74 -0
- package/node_modules/@narumitw/pi-btw/src/fullscreen-ui.ts +196 -7
- package/node_modules/@narumitw/pi-btw/src/main-thread-updates.ts +40 -0
- package/node_modules/@narumitw/pi-btw/src/menu.ts +34 -2
- package/node_modules/@narumitw/pi-btw/src/settings.ts +49 -0
- package/node_modules/@narumitw/pi-btw/src/transcript-pager.ts +9 -1
- package/node_modules/@narumitw/pi-btw/src/workspace-layout.ts +559 -0
- package/node_modules/pi-mcp-adapter/CHANGELOG.md +62 -0
- package/node_modules/pi-mcp-adapter/README.md +75 -7
- package/node_modules/pi-mcp-adapter/bearer-command-resolver.ts +193 -0
- package/node_modules/pi-mcp-adapter/cli.js +56 -5
- package/node_modules/pi-mcp-adapter/commands.ts +96 -5
- package/node_modules/pi-mcp-adapter/config.ts +73 -6
- package/node_modules/pi-mcp-adapter/direct-tool-surface.ts +22 -28
- package/node_modules/pi-mcp-adapter/direct-tools.ts +13 -5
- package/node_modules/pi-mcp-adapter/dist/abort.d.ts +2 -0
- package/node_modules/pi-mcp-adapter/dist/abort.js +36 -0
- package/node_modules/pi-mcp-adapter/dist/abort.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.d.ts +6 -0
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js +194 -0
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/config.d.ts +5 -0
- package/node_modules/pi-mcp-adapter/dist/config.js +75 -6
- package/node_modules/pi-mcp-adapter/dist/config.js.map +1 -1
- package/node_modules/pi-mcp-adapter/dist/consent-manager.d.ts +18 -0
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js +88 -0
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.d.ts +17 -0
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js +316 -0
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/errors.d.ts +131 -0
- package/node_modules/pi-mcp-adapter/dist/errors.js +278 -0
- package/node_modules/pi-mcp-adapter/dist/errors.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/http-ca.d.ts +8 -0
- package/node_modules/pi-mcp-adapter/dist/http-ca.js +88 -0
- package/node_modules/pi-mcp-adapter/dist/http-ca.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/jev-client.d.ts +15 -0
- package/node_modules/pi-mcp-adapter/dist/jev-client.js +340 -0
- package/node_modules/pi-mcp-adapter/dist/jev-client.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.d.ts +77 -0
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js +2 -0
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.d.ts +22 -0
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js +85 -0
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.d.ts +2 -0
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js +55 -0
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/lifecycle.d.ts +56 -0
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js +410 -0
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/logger.d.ts +51 -0
- package/node_modules/pi-mcp-adapter/dist/logger.js +131 -0
- package/node_modules/pi-mcp-adapter/dist/logger.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.d.ts +26 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js +103 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.d.ts +118 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js +1098 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.d.ts +168 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js +1117 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js +7 -140
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js.map +1 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.d.ts +53 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js +441 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.d.ts +148 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js +689 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.d.ts +6 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js +166 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.d.ts +102 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js +369 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.d.ts +95 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js +242 -0
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js +2 -11
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js.map +1 -1
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.d.ts +6 -0
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js +505 -0
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.d.ts +10 -0
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js +312 -0
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.d.ts +13 -0
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js +97 -0
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.d.ts +17 -0
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js +203 -0
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.d.ts +11 -0
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js +172 -0
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/server-manager.d.ts +157 -0
- package/node_modules/pi-mcp-adapter/dist/server-manager.js +1743 -0
- package/node_modules/pi-mcp-adapter/dist/server-manager.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/session-approvals.d.ts +34 -0
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js +140 -0
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/session-recovery.d.ts +26 -0
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js +142 -0
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/state.d.ts +73 -0
- package/node_modules/pi-mcp-adapter/dist/state.js +2 -0
- package/node_modules/pi-mcp-adapter/dist/state.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/types.d.ts +36 -0
- package/node_modules/pi-mcp-adapter/dist/types.js +18 -0
- package/node_modules/pi-mcp-adapter/dist/types.js.map +1 -1
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.d.ts +17 -0
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js +219 -0
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.d.ts +15 -0
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js +85 -0
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js.map +1 -0
- package/node_modules/pi-mcp-adapter/dist/utils.d.ts +1 -0
- package/node_modules/pi-mcp-adapter/dist/utils.js +13 -0
- package/node_modules/pi-mcp-adapter/dist/utils.js.map +1 -1
- package/node_modules/pi-mcp-adapter/elicitation-handler.ts +29 -19
- package/node_modules/pi-mcp-adapter/examples/jev-accessibility-loop.mjs +85 -0
- package/node_modules/pi-mcp-adapter/examples/jev-semantic-filter.mjs +34 -0
- package/node_modules/pi-mcp-adapter/index.ts +129 -6
- package/node_modules/pi-mcp-adapter/init.ts +6 -19
- package/node_modules/pi-mcp-adapter/jev-client.ts +273 -0
- package/node_modules/pi-mcp-adapter/jev-contracts.ts +53 -0
- package/node_modules/pi-mcp-adapter/jev-key-store.ts +79 -0
- package/node_modules/pi-mcp-adapter/lifecycle.ts +14 -3
- package/node_modules/pi-mcp-adapter/mcp-bearer-store.ts +7 -142
- package/node_modules/pi-mcp-adapter/mcp-code.ts +119 -11
- package/node_modules/pi-mcp-adapter/mcp-output-guard.ts +4 -0
- package/node_modules/pi-mcp-adapter/mcp-references.ts +5 -3
- package/node_modules/pi-mcp-adapter/mcp-script-worker.mjs +5 -0
- package/node_modules/pi-mcp-adapter/mcp-tasks.ts +467 -0
- package/node_modules/pi-mcp-adapter/metadata-cache.ts +2 -13
- package/node_modules/pi-mcp-adapter/namespace-tools.ts +1 -1
- package/node_modules/pi-mcp-adapter/package.json +17 -9
- package/node_modules/pi-mcp-adapter/proxy-modes.ts +319 -166
- package/node_modules/pi-mcp-adapter/request-headers-command.ts +6 -3
- package/node_modules/pi-mcp-adapter/search-ranking.ts +38 -7
- package/node_modules/pi-mcp-adapter/secure-keyring.ts +170 -0
- package/node_modules/pi-mcp-adapter/semantic-search.ts +186 -0
- package/node_modules/pi-mcp-adapter/server-manager.ts +293 -50
- package/node_modules/pi-mcp-adapter/session-approvals.ts +14 -0
- package/node_modules/pi-mcp-adapter/skills/mcp-scripting/SKILL.md +2 -0
- package/node_modules/pi-mcp-adapter/state.ts +3 -1
- package/node_modules/pi-mcp-adapter/tool-approval.ts +26 -4
- package/node_modules/pi-mcp-adapter/tool-metadata.ts +6 -14
- package/node_modules/pi-mcp-adapter/tool-registrar.ts +8 -6
- package/node_modules/pi-mcp-adapter/tool-result-renderer.ts +4 -1
- package/node_modules/pi-mcp-adapter/types.ts +53 -0
- package/node_modules/pi-mcp-adapter/ui-server.ts +17 -9
- package/node_modules/pi-mcp-adapter/utils.ts +16 -0
- package/node_modules/pi-multi-account/CHANGELOG.md +23 -0
- package/node_modules/pi-multi-account/README.md +38 -11
- package/node_modules/pi-multi-account/index.ts +371 -96
- package/node_modules/pi-multi-account/package.json +5 -4
- package/node_modules/pi-multi-account/provider-payload-stream.ts +36 -28
- package/node_modules/pi-multi-account/usage.ts +31 -2
- package/node_modules/pi-typesafe/README.md +3 -1
- package/node_modules/pi-typesafe/dist/auth.d.ts +10 -3
- package/node_modules/pi-typesafe/dist/auth.js +13 -7
- package/node_modules/pi-typesafe/dist/backends.d.ts +31 -0
- package/node_modules/pi-typesafe/dist/backends.js +33 -0
- package/node_modules/pi-typesafe/dist/client.d.ts +3 -9
- package/node_modules/pi-typesafe/dist/client.js +63 -39
- package/node_modules/pi-typesafe/dist/credentials.d.ts +11 -5
- package/node_modules/pi-typesafe/dist/credentials.js +15 -8
- package/node_modules/pi-typesafe/dist/extension.js +4 -1
- package/node_modules/pi-typesafe/dist/index.d.ts +1 -1
- package/node_modules/pi-typesafe/dist/index.js +1 -1
- package/node_modules/pi-typesafe/dist/login.d.ts +13 -7
- package/node_modules/pi-typesafe/dist/login.js +17 -8
- package/node_modules/pi-typesafe/dist/schema.js +19 -13
- package/node_modules/pi-typesafe/package.json +1 -1
- package/package.json +11 -10
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { truncateAtWord } from "./utils.js";
|
|
2
|
+
import { throwIfAborted } from "./abort.js";
|
|
3
|
+
export function registerSamplingHandler(client, options) {
|
|
4
|
+
client.setRequestHandler("sampling/createMessage", request => {
|
|
5
|
+
return handleSamplingRequest(options, request);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export async function handleSamplingRequest(options, request) {
|
|
9
|
+
const params = request.params;
|
|
10
|
+
const signal = options.getSignal();
|
|
11
|
+
throwIfAborted(signal);
|
|
12
|
+
if ("task" in params && params.task) {
|
|
13
|
+
throw new Error("MCP sampling tasks are not supported");
|
|
14
|
+
}
|
|
15
|
+
if (params.includeContext && params.includeContext !== "none") {
|
|
16
|
+
throw new Error("MCP sampling context inclusion is not supported");
|
|
17
|
+
}
|
|
18
|
+
if (params.tools?.length) {
|
|
19
|
+
throw new Error("MCP sampling tool use is not supported");
|
|
20
|
+
}
|
|
21
|
+
if (params.toolChoice) {
|
|
22
|
+
throw new Error("MCP sampling tool choice is not supported");
|
|
23
|
+
}
|
|
24
|
+
if (params.stopSequences?.length) {
|
|
25
|
+
throw new Error("MCP sampling stop sequences are not supported");
|
|
26
|
+
}
|
|
27
|
+
const messages = params.messages.map(convertSamplingMessage);
|
|
28
|
+
const model = resolveSamplingModel(options, params.modelPreferences);
|
|
29
|
+
throwIfAborted(signal);
|
|
30
|
+
await confirmSampling(options, "Approve MCP sampling request", formatRequestApproval(options.serverName, `${model.provider}/${model.id}`, params.systemPrompt, messages));
|
|
31
|
+
throwIfAborted(signal);
|
|
32
|
+
const result = await options.modelRegistry.complete(model, {
|
|
33
|
+
...(params.systemPrompt !== undefined ? { systemPrompt: params.systemPrompt } : {}),
|
|
34
|
+
messages,
|
|
35
|
+
}, {
|
|
36
|
+
maxTokens: params.maxTokens,
|
|
37
|
+
...(params.temperature !== undefined ? { temperature: params.temperature } : {}),
|
|
38
|
+
...(params.metadata !== undefined ? { metadata: params.metadata } : {}),
|
|
39
|
+
...(signal ? { signal } : {}),
|
|
40
|
+
});
|
|
41
|
+
const converted = convertAssistantResult(result);
|
|
42
|
+
throwIfAborted(signal);
|
|
43
|
+
await confirmSampling(options, "Return MCP sampling response", formatResponseApproval(options.serverName, converted));
|
|
44
|
+
return converted;
|
|
45
|
+
}
|
|
46
|
+
function formatRequestApproval(serverName, modelName, systemPrompt, messages) {
|
|
47
|
+
const lines = [`${serverName} wants to sample ${messages.length} message${messages.length === 1 ? "" : "s"} with ${modelName}.`];
|
|
48
|
+
if (systemPrompt) {
|
|
49
|
+
lines.push(`System: ${truncateAtWord(systemPrompt, 400)}`);
|
|
50
|
+
}
|
|
51
|
+
for (const [index, message] of messages.entries()) {
|
|
52
|
+
lines.push(`${index + 1}. ${message.role}: ${truncateAtWord(messageText(message), 400)}`);
|
|
53
|
+
}
|
|
54
|
+
return lines.join("\n\n");
|
|
55
|
+
}
|
|
56
|
+
function formatResponseApproval(serverName, response) {
|
|
57
|
+
const text = response.content.type === "text" ? response.content.text : `[${response.content.type} content]`;
|
|
58
|
+
return `${serverName} will receive this response from ${response.model}:\n\n${truncateAtWord(text, 1000)}`;
|
|
59
|
+
}
|
|
60
|
+
function messageText(message) {
|
|
61
|
+
if (typeof message.content === "string")
|
|
62
|
+
return message.content;
|
|
63
|
+
return message.content.map((block) => {
|
|
64
|
+
if (block.type === "text")
|
|
65
|
+
return block.text;
|
|
66
|
+
if (block.type === "image")
|
|
67
|
+
return `[image: ${block.mimeType}]`;
|
|
68
|
+
if (block.type === "thinking")
|
|
69
|
+
return "[thinking]";
|
|
70
|
+
if (block.type === "toolCall")
|
|
71
|
+
return `[tool call: ${block.name}]`;
|
|
72
|
+
return "[content]";
|
|
73
|
+
}).join("\n");
|
|
74
|
+
}
|
|
75
|
+
function resolveSamplingModel(options, modelPreferences) {
|
|
76
|
+
const candidates = [];
|
|
77
|
+
const availableModels = options.modelRegistry.getAvailable();
|
|
78
|
+
for (const hint of modelPreferences?.hints ?? []) {
|
|
79
|
+
const normalizedHint = hint.name?.trim().toLowerCase();
|
|
80
|
+
if (!normalizedHint)
|
|
81
|
+
continue;
|
|
82
|
+
for (const model of availableModels) {
|
|
83
|
+
const searchableNames = [`${model.provider}/${model.id}`, model.id, model.name];
|
|
84
|
+
if (searchableNames.some((name) => name.toLowerCase().includes(normalizedHint))) {
|
|
85
|
+
addSamplingCandidate(candidates, model);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const currentModel = options.getCurrentModel();
|
|
90
|
+
if (currentModel)
|
|
91
|
+
addSamplingCandidate(candidates, currentModel);
|
|
92
|
+
for (const model of availableModels) {
|
|
93
|
+
addSamplingCandidate(candidates, model);
|
|
94
|
+
}
|
|
95
|
+
const signal = options.getSignal();
|
|
96
|
+
throwIfAborted(signal);
|
|
97
|
+
const model = candidates[0];
|
|
98
|
+
if (model)
|
|
99
|
+
return model;
|
|
100
|
+
throw new Error("No Pi model is available for MCP sampling");
|
|
101
|
+
}
|
|
102
|
+
function addSamplingCandidate(candidates, model) {
|
|
103
|
+
if (!candidates.some((candidate) => candidate.provider === model.provider && candidate.id === model.id)) {
|
|
104
|
+
candidates.push(model);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function confirmSampling(options, title, message) {
|
|
108
|
+
if (options.autoApprove)
|
|
109
|
+
return;
|
|
110
|
+
if (!options.ui) {
|
|
111
|
+
throw new Error("MCP sampling requires interactive approval. Set settings.samplingAutoApprove to true to allow it without UI.");
|
|
112
|
+
}
|
|
113
|
+
const approved = await options.ui.confirm(title, message);
|
|
114
|
+
if (!approved) {
|
|
115
|
+
throw new Error("MCP sampling request was declined");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function convertSamplingMessage(message) {
|
|
119
|
+
const blocks = Array.isArray(message.content) ? message.content : [message.content];
|
|
120
|
+
if (message.role === "user") {
|
|
121
|
+
return {
|
|
122
|
+
role: "user",
|
|
123
|
+
content: blocks.map(convertUserContent),
|
|
124
|
+
timestamp: Date.now(),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
role: "assistant",
|
|
129
|
+
content: blocks.map(convertAssistantContent),
|
|
130
|
+
api: "mcp-sampling",
|
|
131
|
+
provider: "mcp",
|
|
132
|
+
model: "sampling-request",
|
|
133
|
+
usage: zeroUsage(),
|
|
134
|
+
stopReason: "stop",
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function convertUserContent(block) {
|
|
139
|
+
if (block.type === "text") {
|
|
140
|
+
return { type: "text", text: block.text };
|
|
141
|
+
}
|
|
142
|
+
throw new Error(`MCP sampling ${block.type} content is not supported`);
|
|
143
|
+
}
|
|
144
|
+
function convertAssistantContent(block) {
|
|
145
|
+
if (block.type === "text") {
|
|
146
|
+
return { type: "text", text: block.text };
|
|
147
|
+
}
|
|
148
|
+
throw new Error(`MCP sampling assistant ${block.type} content is not supported`);
|
|
149
|
+
}
|
|
150
|
+
function convertAssistantResult(message) {
|
|
151
|
+
if (message.stopReason === "error") {
|
|
152
|
+
throw new Error(message.errorMessage ?? "MCP sampling model call failed");
|
|
153
|
+
}
|
|
154
|
+
if (message.stopReason === "aborted") {
|
|
155
|
+
throw new Error(message.errorMessage ?? "MCP sampling model call was aborted");
|
|
156
|
+
}
|
|
157
|
+
const text = message.content
|
|
158
|
+
.map((block) => {
|
|
159
|
+
if (block.type === "text")
|
|
160
|
+
return block.text;
|
|
161
|
+
if (block.type === "thinking")
|
|
162
|
+
return undefined;
|
|
163
|
+
throw new Error(`MCP sampling result ${block.type} content is not supported`);
|
|
164
|
+
})
|
|
165
|
+
.filter((value) => value !== undefined)
|
|
166
|
+
.join("\n\n")
|
|
167
|
+
.trim();
|
|
168
|
+
if (!text) {
|
|
169
|
+
throw new Error("MCP sampling result did not contain text content");
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
role: "assistant",
|
|
173
|
+
content: { type: "text", text },
|
|
174
|
+
model: `${message.provider}/${message.model}`,
|
|
175
|
+
stopReason: mapStopReason(message.stopReason),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function mapStopReason(reason) {
|
|
179
|
+
if (reason === "stop")
|
|
180
|
+
return "endTurn";
|
|
181
|
+
if (reason === "length")
|
|
182
|
+
return "maxTokens";
|
|
183
|
+
if (reason === "toolUse")
|
|
184
|
+
return "toolUse";
|
|
185
|
+
return reason;
|
|
186
|
+
}
|
|
187
|
+
function zeroUsage() {
|
|
188
|
+
return {
|
|
189
|
+
input: 0,
|
|
190
|
+
output: 0,
|
|
191
|
+
cacheRead: 0,
|
|
192
|
+
cacheWrite: 0,
|
|
193
|
+
totalTokens: 0,
|
|
194
|
+
cost: {
|
|
195
|
+
input: 0,
|
|
196
|
+
output: 0,
|
|
197
|
+
cacheRead: 0,
|
|
198
|
+
cacheWrite: 0,
|
|
199
|
+
total: 0,
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=sampling-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sampling-handler.js","sourceRoot":"","sources":["../sampling-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAyB5C,MAAM,UAAU,uBAAuB,CAAC,MAAc,EAAE,OAA+B;IACrF,MAAM,CAAC,iBAAiB,CAAC,wBAAwB,EAAE,OAAO,CAAC,EAAE;QAC3D,OAAO,qBAAqB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAA+B,EAC/B,OAA6B;IAE7B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IACnC,cAAc,CAAC,MAAM,CAAC,CAAC;IAEvB,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAC1D,CAAC;IACD,IAAI,MAAM,CAAC,cAAc,IAAI,MAAM,CAAC,cAAc,KAAK,MAAM,EAAE,CAAC;QAC9D,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IACD,IAAI,MAAM,CAAC,UA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/B,KAAK,EAAE,GAAG,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,KAAK,EAAE;QAC7C,UAAU,EAAE,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC;KAC9C,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,MAAsC;IAC3D,IAAI,MAAM,KAAK,MAAM;QAAE,OAAO,SAAS,CAAC;IACxC,IAAI,MAAM,KAAK,QAAQ;QAAE,OAAO,WAAW,CAAC;IAC5C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,SAAS;IAChB,OAAO;QACL,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,WAAW,EAAE,CAAC;QACd,IAAI,EAAE;YACJ,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,CAAC;YACT,SAAS,EAAE,CAAC;YACZ,UAAU,EAAE,CAAC;YACb,KAAK,EAAE,CAAC;SACT;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface SecureKeyringStore {
|
|
2
|
+
read(account: string): string | undefined;
|
|
3
|
+
write(account: string, payload: string): void;
|
|
4
|
+
remove(account: string): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function createSecureKeyringStore(service: string): SecureKeyringStore;
|
|
7
|
+
export declare function resetTestSecureKeyring(): void;
|
|
8
|
+
export declare function getTestSecureKeyringReadCount(): number;
|
|
9
|
+
export declare function getTestSecureKeyringEntries(): [string, string][];
|
|
10
|
+
export declare function setTestSecureKeyringEntry(service: string, account: string, payload: string): void;
|
|
11
|
+
export declare function removeTestSecureKeyringEntry(service: string, account: string): void;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const TEST_STORE_ENV = "PI_MCP_ADAPTER_TEST_AUTH_STORE";
|
|
8
|
+
const memoryEntries = new Map();
|
|
9
|
+
let memoryReadCount = 0;
|
|
10
|
+
let KeyringEntryClass;
|
|
11
|
+
function key(service, account) {
|
|
12
|
+
return `${service}\0${account}`;
|
|
13
|
+
}
|
|
14
|
+
function nativeStore(service) {
|
|
15
|
+
const entry = (account) => {
|
|
16
|
+
try {
|
|
17
|
+
KeyringEntryClass ??= loadKeyringEntryClass();
|
|
18
|
+
return new KeyringEntryClass(service, account);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new Error("OS secure credential storage is unavailable. Configure or unlock the OS credential store and retry.", { cause: error });
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const run = (operation, account, payload) => {
|
|
25
|
+
try {
|
|
26
|
+
const credential = entry(account);
|
|
27
|
+
if (operation === "read")
|
|
28
|
+
return credential.getPassword() ?? undefined;
|
|
29
|
+
if (operation === "write")
|
|
30
|
+
credential.setPassword(payload);
|
|
31
|
+
else
|
|
32
|
+
credential.deleteCredential();
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (!shouldRecoverKeyring(error))
|
|
37
|
+
throw error;
|
|
38
|
+
return recoverKeyring(operation, service, account, payload);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
read: account => run("read", account),
|
|
43
|
+
write: (account, payload) => { run("write", account, payload); },
|
|
44
|
+
remove: account => { run("remove", account); },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function shouldRecoverKeyring(error) {
|
|
48
|
+
if (process.platform !== "linux" || process.env.PI_MCP_ADAPTER_DISABLE_KEYRING_RECOVERY === "1")
|
|
49
|
+
return false;
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
for (let depth = 0; depth < 16 && error !== undefined && !seen.has(error); depth++) {
|
|
52
|
+
seen.add(error);
|
|
53
|
+
const candidate = typeof error === "object" && error !== null
|
|
54
|
+
? error
|
|
55
|
+
: { message: error };
|
|
56
|
+
if ([candidate.name, candidate.message, candidate.code].some(value => typeof value === "string" && /\b(?:KeyRevoked|key\s+(?:has been\s+)?revoked)\b/i.test(value)))
|
|
57
|
+
return true;
|
|
58
|
+
error = candidate.cause;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
function recoverKeyring(operation, service, account, payload) {
|
|
63
|
+
try {
|
|
64
|
+
const adjacentHelper = new URL("./mcp-keyring-helper.cjs", import.meta.url);
|
|
65
|
+
const helper = existsSync(adjacentHelper) ? adjacentHelper : new URL("../mcp-keyring-helper.cjs", import.meta.url);
|
|
66
|
+
const result = spawnSync("keyctl", ["session", "-", process.execPath, fileURLToPath(helper)], {
|
|
67
|
+
input: `${JSON.stringify({ operation, service, account, payload })}\n`,
|
|
68
|
+
encoding: "utf8",
|
|
69
|
+
stdio: "pipe",
|
|
70
|
+
shell: false,
|
|
71
|
+
timeout: 10_000,
|
|
72
|
+
killSignal: "SIGKILL",
|
|
73
|
+
maxBuffer: 1024 * 1024,
|
|
74
|
+
windowsHide: true,
|
|
75
|
+
});
|
|
76
|
+
if (result.error || result.status !== 0 || result.signal)
|
|
77
|
+
throw new Error();
|
|
78
|
+
const response = JSON.parse(result.stdout);
|
|
79
|
+
if (typeof response !== "object" || response === null || Array.isArray(response))
|
|
80
|
+
throw new Error();
|
|
81
|
+
const value = response;
|
|
82
|
+
if (value.ok !== true)
|
|
83
|
+
throw new Error();
|
|
84
|
+
const allowedKeys = operation === "read" ? ["ok", "found", "value"] : ["ok"];
|
|
85
|
+
if (Object.keys(response).some(key => !allowedKeys.includes(key)))
|
|
86
|
+
throw new Error();
|
|
87
|
+
if (operation === "read") {
|
|
88
|
+
if (value.found === true && typeof value.value === "string")
|
|
89
|
+
return value.value;
|
|
90
|
+
if (value.found === false && value.value === undefined)
|
|
91
|
+
return undefined;
|
|
92
|
+
throw new Error();
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
throw new Error("Linux keyring recovery failed. Configure or unlock the OS credential store and retry.");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export function createSecureKeyringStore(service) {
|
|
101
|
+
const mode = process.env[TEST_STORE_ENV];
|
|
102
|
+
if (mode === "memory" || mode === "sizelimited") {
|
|
103
|
+
return {
|
|
104
|
+
read(account) { memoryReadCount++; return memoryEntries.get(key(service, account)); },
|
|
105
|
+
write(account, payload) {
|
|
106
|
+
if (mode === "sizelimited" && payload.length > 1280)
|
|
107
|
+
throw new Error("secure credential value exceeds platform limit");
|
|
108
|
+
memoryEntries.set(key(service, account), payload);
|
|
109
|
+
},
|
|
110
|
+
remove(account) { memoryEntries.delete(key(service, account)); },
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (mode === "unavailable") {
|
|
114
|
+
return {
|
|
115
|
+
read() { memoryReadCount++; throw new Error("simulated secure credential store unavailable"); },
|
|
116
|
+
write() { throw new Error("simulated secure credential store unavailable"); },
|
|
117
|
+
remove() { throw new Error("simulated secure credential store unavailable"); },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return nativeStore(service);
|
|
121
|
+
}
|
|
122
|
+
function loadKeyringEntryClass(keyringRequire = require, platform = process.platform, arch = process.arch) {
|
|
123
|
+
try {
|
|
124
|
+
return keyringRequire("@napi-rs/keyring").Entry;
|
|
125
|
+
}
|
|
126
|
+
catch (loaderError) {
|
|
127
|
+
try {
|
|
128
|
+
return loadKeyringNativeBindingFallback(keyringRequire, platform, arch).Entry;
|
|
129
|
+
}
|
|
130
|
+
catch (fallbackError) {
|
|
131
|
+
const message = fallbackError instanceof Error ? fallbackError.message : String(fallbackError);
|
|
132
|
+
throw new Error(`Failed to load @napi-rs/keyring; absolute-path native binding fallback also failed: ${message}`, { cause: loaderError });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function loadKeyringNativeBindingFallback(keyringRequire, platform, arch) {
|
|
137
|
+
let lastError;
|
|
138
|
+
for (const suffix of getKeyringNativeBindingSuffixes(platform, arch)) {
|
|
139
|
+
try {
|
|
140
|
+
const packageName = `@napi-rs/keyring-${suffix}`;
|
|
141
|
+
const packageJsonPath = keyringRequire.resolve(`${packageName}/package.json`);
|
|
142
|
+
return keyringRequire(join(dirname(packageJsonPath), `keyring.${suffix}.node`));
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
lastError = error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
149
|
+
}
|
|
150
|
+
function getKeyringNativeBindingSuffixes(platform, arch) {
|
|
151
|
+
if (platform === "darwin")
|
|
152
|
+
return arch === "arm64" ? ["darwin-arm64"] : arch === "x64" ? ["darwin-x64"] : [];
|
|
153
|
+
if (platform === "win32")
|
|
154
|
+
return arch === "arm64" ? ["win32-arm64-msvc"] : arch === "x64" ? ["win32-x64-msvc"] : arch === "ia32" ? ["win32-ia32-msvc"] : [];
|
|
155
|
+
if (platform === "linux") {
|
|
156
|
+
if (arch === "arm64")
|
|
157
|
+
return ["linux-arm64-gnu", "linux-arm64-musl"];
|
|
158
|
+
if (arch === "arm")
|
|
159
|
+
return ["linux-arm-gnueabihf"];
|
|
160
|
+
if (arch === "riscv64")
|
|
161
|
+
return ["linux-riscv64-gnu"];
|
|
162
|
+
if (arch === "x64")
|
|
163
|
+
return ["linux-x64-gnu", "linux-x64-musl"];
|
|
164
|
+
}
|
|
165
|
+
return platform === "freebsd" && arch === "x64" ? ["freebsd-x64"] : [];
|
|
166
|
+
}
|
|
167
|
+
export function resetTestSecureKeyring() { memoryEntries.clear(); memoryReadCount = 0; }
|
|
168
|
+
export function getTestSecureKeyringReadCount() { return memoryReadCount; }
|
|
169
|
+
export function getTestSecureKeyringEntries() { return [...memoryEntries.entries()]; }
|
|
170
|
+
export function setTestSecureKeyringEntry(service, account, payload) { memoryEntries.set(key(service, account), payload); }
|
|
171
|
+
export function removeTestSecureKeyringEntry(service, account) { memoryEntries.delete(key(service, account)); }
|
|
172
|
+
//# sourceMappingURL=secure-keyring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secure-keyring.js","sourceRoot":"","sources":["../secure-keyring.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAiBzC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,cAAc,GAAG,gCAAgC,CAAC;AACxD,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;AAChD,IAAI,eAAe,GAAG,CAAC,CAAC;AACxB,IAAI,iBAAsD,CAAC;AAE3D,SAAS,GAAG,CAAC,OAAe,EAAE,OAAe;IAC3C,OAAO,GAAG,OAAO,KAAK,OAAO,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,WAAW,CAAC,OAAe;IAClC,MAAM,KAAK,GAAG,CAAC,OAAe,EAAgB,EAAE;QAC9C,IAAI,CAAC;YACH,iBAAiB,KAAK,qBAAqB,EAAE,CAAC;YAC9C,OAAO,IAAI,iBAAiB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,qGAAqG,EAAE,EA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AAe,GAAG,CAAC,CAAC,CAAC,CAAC;AAC9F,MAAM,UAAU,6BAA6B,KAAa,OAAO,eAAe,CAAC,CAAC,CAAC;AACnF,MAAM,UAAU,2BAA2B,KAAyB,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AAC1G,MAAM,UAAU,yBAAyB,CAAC,OAAe,EAAE,OAAe,EAAE,OAAe,IAAU,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;AACzJ,MAAM,UAAU,4BAA4B,CAAC,OAAe,EAAE,OAAe,IAAU,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { Client, type GetPromptResult, type ListToolsResult, type ReadResourceResult, type McpSubscription, type SubscriptionFilter, type RequestOptions, type UrlElicitationRequiredError } from "@modelcontextprotocol/client";
|
|
2
|
+
import { type McpTool, type McpResource, type McpPrompt, type ServerDefinition, type ServerStreamResultPatchNotification, type Transport, type McpTraceSettings, type McpListenState } from "./types.ts";
|
|
3
|
+
import { type McpOAuthRuntime } from "./mcp-auth-flow.ts";
|
|
4
|
+
import { type AuthStorageOptions } from "./mcp-auth.ts";
|
|
5
|
+
import { type ServerSamplingConfig } from "./sampling-handler.ts";
|
|
6
|
+
import { type ServerElicitationConfig } from "./elicitation-handler.ts";
|
|
7
|
+
import { type RawRequestChannel } from "./mcp-tasks.ts";
|
|
8
|
+
import type { TaskEnabledSession } from "@modelcontextprotocol/ext-tasks/client";
|
|
9
|
+
export declare function isUnauthorizedHttpError(error: unknown): boolean;
|
|
10
|
+
export interface ServerConnection {
|
|
11
|
+
client: Client;
|
|
12
|
+
transport: Transport;
|
|
13
|
+
definition: ServerDefinition;
|
|
14
|
+
tools: McpTool[];
|
|
15
|
+
/** Cache hints from the server's aggregated tools/list result. */
|
|
16
|
+
toolListHints?: Partial<Pick<ListToolsResult, "ttlMs" | "cacheScope">> | undefined;
|
|
17
|
+
/** Monotonic guard against older refresh responses replacing newer notifications. */
|
|
18
|
+
toolsRevision?: number;
|
|
19
|
+
resources: McpResource[];
|
|
20
|
+
/** True when resources were advertised but resources/list failed. */
|
|
21
|
+
resourceDiscoveryFailed?: boolean;
|
|
22
|
+
prompts: McpPrompt[];
|
|
23
|
+
/** True when prompts were advertised but prompts/list failed. */
|
|
24
|
+
promptDiscoveryFailed?: boolean;
|
|
25
|
+
instructions?: string;
|
|
26
|
+
lastUsedAt: number;
|
|
27
|
+
inFlight: number;
|
|
28
|
+
status: "connected" | "closed" | "needs-auth";
|
|
29
|
+
/** Catalog subscription health, tracked independently from transport health. */
|
|
30
|
+
listenState: McpListenState;
|
|
31
|
+
/** ext-tasks requester session, attached when `tasks: true` and the server advertises the extension. */
|
|
32
|
+
taskSession?: TaskEnabledSession;
|
|
33
|
+
/** Raw JSON-RPC channel backing the task session on 2026-07-28 connections. */
|
|
34
|
+
taskChannel?: RawRequestChannel;
|
|
35
|
+
listenSubscription?: McpSubscription;
|
|
36
|
+
/** Last requested filter; the server's honored filter may be a subset. */
|
|
37
|
+
listenFilter?: SubscriptionFilter;
|
|
38
|
+
/** True when recovery has an active listen but could not confirm every catalog list. */
|
|
39
|
+
listenCatalogStale?: boolean;
|
|
40
|
+
listenPromise?: Promise<void>;
|
|
41
|
+
listenRetryAfter?: number;
|
|
42
|
+
listenStopped?: boolean;
|
|
43
|
+
recentResourceUris?: Map<string, number>;
|
|
44
|
+
resourceReadRefreshUris?: Set<string>;
|
|
45
|
+
/** True once this needs-auth episode discarded the cached credential. */
|
|
46
|
+
credentialsInvalidated?: boolean;
|
|
47
|
+
}
|
|
48
|
+
type UiStreamListener = (serverName: string, notification: ServerStreamResultPatchNotification["params"]) => void;
|
|
49
|
+
type MetadataListChangedListener = (serverName: string, reason: string) => void;
|
|
50
|
+
type ListenStateChangedListener = (serverName: string, state: McpListenState) => void;
|
|
51
|
+
type ResourceUpdatedListener = (serverName: string, uri: string) => void;
|
|
52
|
+
export type ToolRefreshResult = "updated" | "unchanged" | "superseded" | "refresh-timeout";
|
|
53
|
+
export declare function isTransientHttpConnectError(error: unknown): boolean;
|
|
54
|
+
export declare class McpServerManager {
|
|
55
|
+
private readonly defaultCwd?;
|
|
56
|
+
private connections;
|
|
57
|
+
private connectPromises;
|
|
58
|
+
private connectOAuthAuthorities;
|
|
59
|
+
private reconnectPromises;
|
|
60
|
+
private reconnectOAuthAuthorities;
|
|
61
|
+
private reconnectAttempts;
|
|
62
|
+
private uiStreamListeners;
|
|
63
|
+
private samplingConfig;
|
|
64
|
+
private metadataListChangedListener;
|
|
65
|
+
private listenStateChangedListener;
|
|
66
|
+
private resourceUpdatedListeners;
|
|
67
|
+
private pendingMetadataPublications;
|
|
68
|
+
private elicitationConfig;
|
|
69
|
+
private authStorageOptions;
|
|
70
|
+
private oauthRuntime;
|
|
71
|
+
private acceptedUrlElicitations;
|
|
72
|
+
private defaultRequestTimeoutMs;
|
|
73
|
+
private runtimeSignal;
|
|
74
|
+
private closePromises;
|
|
75
|
+
private closeGenerations;
|
|
76
|
+
private connectAttempts;
|
|
77
|
+
private traceSettings;
|
|
78
|
+
private traceWriter;
|
|
79
|
+
private stopped;
|
|
80
|
+
/** Default cwd for stdio servers without an explicit config `cwd`. */
|
|
81
|
+
constructor(defaultCwd?: string | undefined);
|
|
82
|
+
setSamplingConfig(config: ServerSamplingConfig | undefined): void;
|
|
83
|
+
setMetadataListChangedListener(listener: MetadataListChangedListener | undefined): void;
|
|
84
|
+
setListenStateChangedListener(listener: ListenStateChangedListener | undefined): void;
|
|
85
|
+
publishMetadataChanged(name: string, expectedConnection: ServerConnection, reason: string): boolean;
|
|
86
|
+
setElicitationConfig(config: ServerElicitationConfig | undefined): void;
|
|
87
|
+
setRuntimeSignal(signal: AbortSignal | undefined): void;
|
|
88
|
+
setDefaultRequestTimeoutMs(timeoutMs: number | undefined): void;
|
|
89
|
+
setTraceConfig(settings: McpTraceSettings | undefined): void;
|
|
90
|
+
setAuthStorageOptions(options: AuthStorageOptions): void;
|
|
91
|
+
setOAuthRuntime(runtime: McpOAuthRuntime): void;
|
|
92
|
+
getRequestOptions(name: string, signal?: AbortSignal): RequestOptions | undefined;
|
|
93
|
+
private getResolvedRequestTimeoutMs;
|
|
94
|
+
private buildRequestOptions;
|
|
95
|
+
private arbitrateConnectionAttempt;
|
|
96
|
+
connect(name: string, definition: ServerDefinition, signal?: AbortSignal): Promise<ServerConnection>;
|
|
97
|
+
/**
|
|
98
|
+
* Reconnect a server whose connection was proven stale (e.g. by a 404
|
|
99
|
+
* "session no longer exists" response). Single-flight per server name —
|
|
100
|
+
* concurrent callers that raced to the same failure share one reconnect —
|
|
101
|
+
* and identity-guarded: `staleConnection` is only torn down if it is
|
|
102
|
+
* still the manager's current connection for `name`. If a concurrent
|
|
103
|
+
* reconnect (or an unrelated connect()) already replaced it with a fresh
|
|
104
|
+
* connection, that fresh connection is returned untouched.
|
|
105
|
+
*/
|
|
106
|
+
reconnect(name: string, definition: ServerDefinition, staleConnection: ServerConnection, signal?: AbortSignal): Promise<ServerConnection>;
|
|
107
|
+
/**
|
|
108
|
+
* Fetch the authoritative tool catalog for a connection that still appears
|
|
109
|
+
* connected. The SDK response cache is explicitly bypassed so this request
|
|
110
|
+
* also proves the remote Streamable HTTP session is usable. Results are
|
|
111
|
+
* identity-guarded: a late response from a replaced connection is ignored.
|
|
112
|
+
*/
|
|
113
|
+
refreshTools(name: string, expectedConnection: ServerConnection, signal?: AbortSignal): Promise<ToolRefreshResult>;
|
|
114
|
+
private retryPendingMetadataPublication;
|
|
115
|
+
private setListenState;
|
|
116
|
+
private catalogListenFilter;
|
|
117
|
+
private currentListenFilter;
|
|
118
|
+
private watchListenSubscription;
|
|
119
|
+
/** Quietly repairs a modern catalog listen at an existing activity boundary. */
|
|
120
|
+
ensureListen(name: string, expectedConnection: ServerConnection): Promise<void>;
|
|
121
|
+
private reconcileCatalogAfterListen;
|
|
122
|
+
prepareResourceUse(name: string, uri: string, expectedConnection: ServerConnection): Promise<boolean>;
|
|
123
|
+
registerResourceUpdatedListener(token: string, serverName: string, uri: string, listener: ResourceUpdatedListener): void;
|
|
124
|
+
removeResourceUpdatedListener(token: string): void;
|
|
125
|
+
private doReconnect;
|
|
126
|
+
private createConnection;
|
|
127
|
+
private enrichHttpConnectionError;
|
|
128
|
+
private connectClientWithAbort;
|
|
129
|
+
private buildClientCapabilities;
|
|
130
|
+
private createClient;
|
|
131
|
+
private handleToolsListChanged;
|
|
132
|
+
private handlePromptsListChanged;
|
|
133
|
+
private handleResourcesListChanged;
|
|
134
|
+
handleUrlElicitationRequired(serverName: string, error: UrlElicitationRequiredError): Promise<"accept" | "decline" | "cancel">;
|
|
135
|
+
private rememberUrlElicitation;
|
|
136
|
+
private connectHttpClient;
|
|
137
|
+
private fetchAllTools;
|
|
138
|
+
private fetchAllPrompts;
|
|
139
|
+
private fetchAllResources;
|
|
140
|
+
private attachAdapterNotificationHandlers;
|
|
141
|
+
registerUiStreamListener(streamToken: string, listener: UiStreamListener): void;
|
|
142
|
+
removeUiStreamListener(streamToken: string): void;
|
|
143
|
+
getPrompt(name: string, promptName: string, args?: Record<string, string>, signal?: AbortSignal): Promise<GetPromptResult>;
|
|
144
|
+
readResource(name: string, uri: string, signal?: AbortSignal): Promise<ReadResourceResult>;
|
|
145
|
+
close(name: string): Promise<void>;
|
|
146
|
+
private disposeConnection;
|
|
147
|
+
closeAll(): Promise<void>;
|
|
148
|
+
private containsCleanupFailure;
|
|
149
|
+
isConnecting(name: string): boolean;
|
|
150
|
+
getConnection(name: string): ServerConnection | undefined;
|
|
151
|
+
getAllConnections(): Map<string, ServerConnection>;
|
|
152
|
+
touch(name: string): void;
|
|
153
|
+
incrementInFlight(name: string): void;
|
|
154
|
+
decrementInFlight(name: string): void;
|
|
155
|
+
isIdle(name: string, timeoutMs: number): boolean;
|
|
156
|
+
}
|
|
157
|
+
export {};
|