pi-soly 1.9.3 → 1.11.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/ask/index.ts +12 -11
- package/ask/picker.ts +356 -76
- package/ask/prompt.ts +6 -2
- package/ask/tests/picker.test.ts +273 -82
- package/codemap.ts +276 -0
- package/hotreload.ts +239 -0
- package/init.ts +302 -0
- package/mcp/CHANGELOG.md +384 -0
- package/mcp/LICENSE +21 -0
- package/mcp/OAUTH.md +355 -0
- package/mcp/README.md +410 -0
- package/mcp/__tests__/agent-dir-paths.upstream-test.ts +80 -0
- package/mcp/__tests__/cli.upstream-test.ts +97 -0
- package/mcp/__tests__/commands-onboarding.upstream-test.ts +157 -0
- package/mcp/__tests__/config.upstream-test.ts +303 -0
- package/mcp/__tests__/consent-manager.upstream-test.ts +151 -0
- package/mcp/__tests__/direct-tools-auto-auth.upstream-test.ts +218 -0
- package/mcp/__tests__/direct-tools.upstream-test.ts +299 -0
- package/mcp/__tests__/elicitation-handler.upstream-test.ts +346 -0
- package/mcp/__tests__/elicitation-sdk-integration.upstream-test.ts +140 -0
- package/mcp/__tests__/errors.upstream-test.ts +218 -0
- package/mcp/__tests__/fixtures/elicitation-server.mjs +98 -0
- package/mcp/__tests__/host-html-template.upstream-test.ts +278 -0
- package/mcp/__tests__/index-lifecycle.upstream-test.ts +484 -0
- package/mcp/__tests__/init-elicitation.upstream-test.ts +86 -0
- package/mcp/__tests__/interactive-visualizer-server.upstream-test.ts +28 -0
- package/mcp/__tests__/logger.upstream-test.ts +175 -0
- package/mcp/__tests__/mcp-auth-flow-client-credentials.upstream-test.ts +612 -0
- package/mcp/__tests__/mcp-auth-storage.upstream-test.ts +47 -0
- package/mcp/__tests__/mcp-callback-server-unref.upstream-test.ts +264 -0
- package/mcp/__tests__/mcp-oauth-provider.upstream-test.ts +216 -0
- package/mcp/__tests__/mcp-panel-auth.upstream-test.ts +206 -0
- package/mcp/__tests__/mcp-panel-exclude-tools.upstream-test.ts +69 -0
- package/mcp/__tests__/mcp-panel-keybindings.upstream-test.ts +186 -0
- package/mcp/__tests__/npx-resolver.upstream-test.ts +54 -0
- package/mcp/__tests__/oauth-handler.upstream-test.ts +77 -0
- package/mcp/__tests__/onboarding-state.upstream-test.ts +52 -0
- package/mcp/__tests__/package-manifest.upstream-test.ts +22 -0
- package/mcp/__tests__/proxy-modes-auto-auth.upstream-test.ts +253 -0
- package/mcp/__tests__/proxy-modes-discovery.upstream-test.ts +84 -0
- package/mcp/__tests__/proxy-modes-manual-auth.upstream-test.ts +91 -0
- package/mcp/__tests__/proxy-modes-ui-messages.upstream-test.ts +72 -0
- package/mcp/__tests__/sampling-handler.upstream-test.ts +285 -0
- package/mcp/__tests__/server-manager-http-auth.upstream-test.ts +144 -0
- package/mcp/__tests__/server-manager-sampling.upstream-test.ts +236 -0
- package/mcp/__tests__/tool-metadata.upstream-test.ts +111 -0
- package/mcp/__tests__/tool-result-renderer.upstream-test.ts +147 -0
- package/mcp/__tests__/ui-integration.upstream-test.ts +551 -0
- package/mcp/__tests__/ui-resource-handler.upstream-test.ts +303 -0
- package/mcp/__tests__/ui-server.upstream-test.ts +967 -0
- package/mcp/__tests__/ui-session-messages.upstream-test.ts +72 -0
- package/mcp/__tests__/ui-streaming.upstream-test.ts +543 -0
- package/mcp/agent-dir.ts +20 -0
- package/mcp/app-bridge.bundle.js +67 -0
- package/mcp/cli.js +184 -0
- package/mcp/commands.ts +422 -0
- package/mcp/config.ts +666 -0
- package/mcp/consent-manager.ts +64 -0
- package/mcp/direct-tools.ts +439 -0
- package/mcp/elicitation-handler.ts +347 -0
- package/mcp/errors.ts +219 -0
- package/mcp/glimpse-ui.ts +80 -0
- package/mcp/host-html-template.ts +427 -0
- package/mcp/index.ts +362 -0
- package/mcp/init.ts +362 -0
- package/mcp/lifecycle.ts +93 -0
- package/mcp/logger.ts +169 -0
- package/mcp/mcp-auth-flow.ts +559 -0
- package/mcp/mcp-auth-flow.upstream-test.ts +259 -0
- package/mcp/mcp-auth.ts +302 -0
- package/mcp/mcp-auth.upstream-test.ts +373 -0
- package/mcp/mcp-callback-server.ts +372 -0
- package/mcp/mcp-callback-server.upstream-test.ts +416 -0
- package/mcp/mcp-oauth-provider.ts +369 -0
- package/mcp/mcp-oauth-provider.upstream-test.ts +518 -0
- package/mcp/mcp-panel.ts +829 -0
- package/mcp/mcp-setup-panel.ts +580 -0
- package/mcp/metadata-cache.ts +201 -0
- package/mcp/notify.ts +111 -0
- package/mcp/npx-resolver.ts +424 -0
- package/mcp/oauth-handler.ts +57 -0
- package/mcp/onboarding-state.ts +68 -0
- package/mcp/package.json +106 -0
- package/mcp/panel-keys.ts +37 -0
- package/mcp/proxy-modes.ts +949 -0
- package/mcp/resource-tools.ts +17 -0
- package/mcp/sampling-handler.ts +268 -0
- package/mcp/server-manager.ts +545 -0
- package/mcp/state.ts +41 -0
- package/mcp/tool-metadata.ts +216 -0
- package/mcp/tool-registrar.ts +46 -0
- package/mcp/tool-result-renderer.ts +161 -0
- package/mcp/types.ts +448 -0
- package/mcp/ui-resource-handler.ts +146 -0
- package/mcp/ui-server.ts +623 -0
- package/mcp/ui-session.ts +386 -0
- package/mcp/ui-stream-types.ts +89 -0
- package/mcp/utils.ts +129 -0
- package/mcp/vitest.config.ts +14 -0
- package/migrate.ts +258 -0
- package/notification.ts +218 -0
- package/notifications-log.ts +83 -0
- package/package.json +20 -3
- package/status.ts +140 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// resource-tools.ts - MCP resource name utilities
|
|
2
|
+
|
|
3
|
+
export function resourceNameToToolName(name: string): string {
|
|
4
|
+
let result = name
|
|
5
|
+
.replace(/[^a-zA-Z0-9]/g, "_")
|
|
6
|
+
.replace(/_+/g, "_")
|
|
7
|
+
.replace(/^_+/, "") // Remove leading underscores
|
|
8
|
+
.replace(/_+$/, "") // Remove trailing underscores
|
|
9
|
+
.toLowerCase();
|
|
10
|
+
|
|
11
|
+
// Ensure we have a valid name
|
|
12
|
+
if (!result || /^\d/.test(result)) {
|
|
13
|
+
result = "resource" + (result ? "_" + result : "");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { complete, type Api, type AssistantMessage, type Message, type Model, type TextContent } from "@earendil-works/pi-ai";
|
|
2
|
+
import { truncateAtWord } from "./utils.ts";
|
|
3
|
+
import type { ExtensionUIContext, ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import {
|
|
6
|
+
CreateMessageRequestSchema,
|
|
7
|
+
type CreateMessageRequest,
|
|
8
|
+
type CreateMessageResult,
|
|
9
|
+
type ModelPreferences,
|
|
10
|
+
type SamplingMessage,
|
|
11
|
+
type SamplingMessageContentBlock,
|
|
12
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
|
|
14
|
+
export interface SamplingHandlerOptions {
|
|
15
|
+
serverName: string;
|
|
16
|
+
autoApprove: boolean;
|
|
17
|
+
ui?: ExtensionUIContext;
|
|
18
|
+
modelRegistry: ModelRegistry;
|
|
19
|
+
getCurrentModel: () => Model<Api> | undefined;
|
|
20
|
+
getSignal: () => AbortSignal | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ServerSamplingConfig = Omit<SamplingHandlerOptions, "serverName">;
|
|
24
|
+
|
|
25
|
+
export function registerSamplingHandler(client: Client, options: SamplingHandlerOptions): void {
|
|
26
|
+
client.setRequestHandler(CreateMessageRequestSchema, (request) => {
|
|
27
|
+
return handleSamplingRequest(options, request as CreateMessageRequest);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function handleSamplingRequest(
|
|
32
|
+
options: SamplingHandlerOptions,
|
|
33
|
+
request: CreateMessageRequest,
|
|
34
|
+
): Promise<CreateMessageResult> {
|
|
35
|
+
const params = request.params;
|
|
36
|
+
|
|
37
|
+
if ("task" in params && params.task) {
|
|
38
|
+
throw new Error("MCP sampling tasks are not supported");
|
|
39
|
+
}
|
|
40
|
+
if (params.includeContext && params.includeContext !== "none") {
|
|
41
|
+
throw new Error("MCP sampling context inclusion is not supported");
|
|
42
|
+
}
|
|
43
|
+
if (params.tools?.length) {
|
|
44
|
+
throw new Error("MCP sampling tool use is not supported");
|
|
45
|
+
}
|
|
46
|
+
if (params.toolChoice) {
|
|
47
|
+
throw new Error("MCP sampling tool choice is not supported");
|
|
48
|
+
}
|
|
49
|
+
if (params.stopSequences?.length) {
|
|
50
|
+
throw new Error("MCP sampling stop sequences are not supported");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const messages = params.messages.map(convertSamplingMessage);
|
|
54
|
+
const { model, apiKey, headers } = await resolveSamplingModel(options, params.modelPreferences);
|
|
55
|
+
await confirmSampling(
|
|
56
|
+
options,
|
|
57
|
+
"Approve MCP sampling request",
|
|
58
|
+
formatRequestApproval(options.serverName, `${model.provider}/${model.id}`, params.systemPrompt, messages),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const result = await complete(
|
|
62
|
+
model,
|
|
63
|
+
{
|
|
64
|
+
systemPrompt: params.systemPrompt,
|
|
65
|
+
messages,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
apiKey,
|
|
69
|
+
headers,
|
|
70
|
+
maxTokens: params.maxTokens,
|
|
71
|
+
temperature: params.temperature,
|
|
72
|
+
metadata: params.metadata as Record<string, unknown> | undefined,
|
|
73
|
+
signal: options.getSignal(),
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const converted = convertAssistantResult(result);
|
|
78
|
+
await confirmSampling(
|
|
79
|
+
options,
|
|
80
|
+
"Return MCP sampling response",
|
|
81
|
+
formatResponseApproval(options.serverName, converted),
|
|
82
|
+
);
|
|
83
|
+
return converted;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function formatRequestApproval(
|
|
87
|
+
serverName: string,
|
|
88
|
+
modelName: string,
|
|
89
|
+
systemPrompt: string | undefined,
|
|
90
|
+
messages: Message[],
|
|
91
|
+
): string {
|
|
92
|
+
const lines = [`${serverName} wants to sample ${messages.length} message${messages.length === 1 ? "" : "s"} with ${modelName}.`];
|
|
93
|
+
if (systemPrompt) {
|
|
94
|
+
lines.push(`System: ${truncateAtWord(systemPrompt, 400)}`);
|
|
95
|
+
}
|
|
96
|
+
for (const [index, message] of messages.entries()) {
|
|
97
|
+
lines.push(`${index + 1}. ${message.role}: ${truncateAtWord(messageText(message), 400)}`);
|
|
98
|
+
}
|
|
99
|
+
return lines.join("\n\n");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function formatResponseApproval(serverName: string, response: CreateMessageResult): string {
|
|
103
|
+
const text = response.content.type === "text" ? response.content.text : `[${response.content.type} content]`;
|
|
104
|
+
return `${serverName} will receive this response from ${response.model}:\n\n${truncateAtWord(text, 1000)}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function messageText(message: Message): string {
|
|
108
|
+
if (typeof message.content === "string") return message.content;
|
|
109
|
+
return message.content.map((block) => {
|
|
110
|
+
if (block.type === "text") return block.text;
|
|
111
|
+
if (block.type === "image") return `[image: ${block.mimeType}]`;
|
|
112
|
+
if (block.type === "thinking") return "[thinking]";
|
|
113
|
+
if (block.type === "toolCall") return `[tool call: ${block.name}]`;
|
|
114
|
+
return "[content]";
|
|
115
|
+
}).join("\n");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function resolveSamplingModel(
|
|
119
|
+
options: SamplingHandlerOptions,
|
|
120
|
+
modelPreferences: ModelPreferences | undefined,
|
|
121
|
+
): Promise<{
|
|
122
|
+
model: Model<Api>;
|
|
123
|
+
apiKey?: string;
|
|
124
|
+
headers?: Record<string, string>;
|
|
125
|
+
}> {
|
|
126
|
+
const candidates: Model<Api>[] = [];
|
|
127
|
+
const availableModels = options.modelRegistry.getAvailable();
|
|
128
|
+
|
|
129
|
+
for (const hint of modelPreferences?.hints ?? []) {
|
|
130
|
+
const normalizedHint = hint.name?.trim().toLowerCase();
|
|
131
|
+
if (!normalizedHint) continue;
|
|
132
|
+
for (const model of availableModels) {
|
|
133
|
+
const searchableNames = [`${model.provider}/${model.id}`, model.id, model.name];
|
|
134
|
+
if (searchableNames.some((name) => name.toLowerCase().includes(normalizedHint))) {
|
|
135
|
+
addSamplingCandidate(candidates, model);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const currentModel = options.getCurrentModel();
|
|
141
|
+
if (currentModel) addSamplingCandidate(candidates, currentModel);
|
|
142
|
+
|
|
143
|
+
for (const model of availableModels) {
|
|
144
|
+
addSamplingCandidate(candidates, model);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const errors: string[] = [];
|
|
148
|
+
for (const model of candidates) {
|
|
149
|
+
const auth = await options.modelRegistry.getApiKeyAndHeaders(model);
|
|
150
|
+
if (auth.ok === false) {
|
|
151
|
+
errors.push(`${model.provider}/${model.id}: ${auth.error}`);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
return { model, apiKey: auth.apiKey, headers: auth.headers };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (errors.length > 0) {
|
|
158
|
+
throw new Error(`No configured auth for MCP sampling model. ${errors.join("; ")}`);
|
|
159
|
+
}
|
|
160
|
+
throw new Error("No Pi model is available for MCP sampling");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function addSamplingCandidate(candidates: Model<Api>[], model: Model<Api>): void {
|
|
164
|
+
if (!candidates.some((candidate) => candidate.provider === model.provider && candidate.id === model.id)) {
|
|
165
|
+
candidates.push(model);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function confirmSampling(options: SamplingHandlerOptions, title: string, message: string): Promise<void> {
|
|
170
|
+
if (options.autoApprove) return;
|
|
171
|
+
if (!options.ui) {
|
|
172
|
+
throw new Error("MCP sampling requires interactive approval. Set settings.samplingAutoApprove to true to allow it without UI.");
|
|
173
|
+
}
|
|
174
|
+
const approved = await options.ui.confirm(title, message);
|
|
175
|
+
if (!approved) {
|
|
176
|
+
throw new Error("MCP sampling request was declined");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function convertSamplingMessage(message: SamplingMessage): Message {
|
|
181
|
+
const blocks = Array.isArray(message.content) ? message.content : [message.content];
|
|
182
|
+
if (message.role === "user") {
|
|
183
|
+
return {
|
|
184
|
+
role: "user",
|
|
185
|
+
content: blocks.map(convertUserContent),
|
|
186
|
+
timestamp: Date.now(),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
role: "assistant",
|
|
192
|
+
content: blocks.map(convertAssistantContent),
|
|
193
|
+
api: "mcp-sampling",
|
|
194
|
+
provider: "mcp",
|
|
195
|
+
model: "sampling-request",
|
|
196
|
+
usage: zeroUsage(),
|
|
197
|
+
stopReason: "stop",
|
|
198
|
+
timestamp: Date.now(),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function convertUserContent(block: SamplingMessageContentBlock): TextContent {
|
|
203
|
+
if (block.type === "text") {
|
|
204
|
+
return { type: "text", text: block.text };
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`MCP sampling ${block.type} content is not supported`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function convertAssistantContent(block: SamplingMessageContentBlock): TextContent {
|
|
210
|
+
if (block.type === "text") {
|
|
211
|
+
return { type: "text", text: block.text };
|
|
212
|
+
}
|
|
213
|
+
throw new Error(`MCP sampling assistant ${block.type} content is not supported`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function convertAssistantResult(message: AssistantMessage): CreateMessageResult {
|
|
217
|
+
if (message.stopReason === "error") {
|
|
218
|
+
throw new Error(message.errorMessage ?? "MCP sampling model call failed");
|
|
219
|
+
}
|
|
220
|
+
if (message.stopReason === "aborted") {
|
|
221
|
+
throw new Error(message.errorMessage ?? "MCP sampling model call was aborted");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const text = message.content
|
|
225
|
+
.map((block) => {
|
|
226
|
+
if (block.type === "text") return block.text;
|
|
227
|
+
if (block.type === "thinking") return undefined;
|
|
228
|
+
throw new Error(`MCP sampling result ${block.type} content is not supported`);
|
|
229
|
+
})
|
|
230
|
+
.filter((value): value is string => value !== undefined)
|
|
231
|
+
.join("\n\n")
|
|
232
|
+
.trim();
|
|
233
|
+
|
|
234
|
+
if (!text) {
|
|
235
|
+
throw new Error("MCP sampling result did not contain text content");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
role: "assistant",
|
|
240
|
+
content: { type: "text", text },
|
|
241
|
+
model: `${message.provider}/${message.model}`,
|
|
242
|
+
stopReason: mapStopReason(message.stopReason),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function mapStopReason(reason: AssistantMessage["stopReason"]): CreateMessageResult["stopReason"] {
|
|
247
|
+
if (reason === "stop") return "endTurn";
|
|
248
|
+
if (reason === "length") return "maxTokens";
|
|
249
|
+
if (reason === "toolUse") return "toolUse";
|
|
250
|
+
return reason;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function zeroUsage(): AssistantMessage["usage"] {
|
|
254
|
+
return {
|
|
255
|
+
input: 0,
|
|
256
|
+
output: 0,
|
|
257
|
+
cacheRead: 0,
|
|
258
|
+
cacheWrite: 0,
|
|
259
|
+
totalTokens: 0,
|
|
260
|
+
cost: {
|
|
261
|
+
input: 0,
|
|
262
|
+
output: 0,
|
|
263
|
+
cacheRead: 0,
|
|
264
|
+
cacheWrite: 0,
|
|
265
|
+
total: 0,
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|