@aexol/spectral 0.2.5 → 0.2.7

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.
Files changed (40) hide show
  1. package/dist/cli.js +10 -47
  2. package/dist/mcp/agent-dir.js +18 -0
  3. package/dist/mcp/app-bridge.bundle.js +67 -0
  4. package/dist/mcp/commands.js +263 -0
  5. package/dist/mcp/config.js +532 -0
  6. package/dist/mcp/consent-manager.js +59 -0
  7. package/dist/mcp/direct-tools.js +354 -0
  8. package/dist/mcp/errors.js +165 -0
  9. package/dist/mcp/glimpse-ui.js +67 -0
  10. package/dist/mcp/host-html-template.js +412 -0
  11. package/dist/mcp/index.js +291 -0
  12. package/dist/mcp/init.js +280 -0
  13. package/dist/mcp/lifecycle.js +79 -0
  14. package/dist/mcp/logger.js +130 -0
  15. package/dist/mcp/mcp-auth-flow.js +283 -0
  16. package/dist/mcp/mcp-auth.js +226 -0
  17. package/dist/mcp/mcp-callback-server.js +225 -0
  18. package/dist/mcp/mcp-oauth-provider.js +243 -0
  19. package/dist/mcp/mcp-panel.js +646 -0
  20. package/dist/mcp/mcp-setup-panel.js +485 -0
  21. package/dist/mcp/metadata-cache.js +158 -0
  22. package/dist/mcp/npx-resolver.js +385 -0
  23. package/dist/mcp/oauth-handler.js +54 -0
  24. package/dist/mcp/onboarding-state.js +56 -0
  25. package/dist/mcp/proxy-modes.js +714 -0
  26. package/dist/mcp/resource-tools.js +14 -0
  27. package/dist/mcp/sampling-handler.js +206 -0
  28. package/dist/mcp/server-manager.js +301 -0
  29. package/dist/mcp/state.js +1 -0
  30. package/dist/mcp/tool-metadata.js +128 -0
  31. package/dist/mcp/tool-registrar.js +43 -0
  32. package/dist/mcp/types.js +93 -0
  33. package/dist/mcp/ui-resource-handler.js +113 -0
  34. package/dist/mcp/ui-server.js +522 -0
  35. package/dist/mcp/ui-session.js +306 -0
  36. package/dist/mcp/ui-stream-types.js +58 -0
  37. package/dist/mcp/utils.js +104 -0
  38. package/dist/mcp/vitest.config.js +13 -0
  39. package/dist/server/pi-bridge.js +9 -30
  40. package/package.json +6 -3
@@ -0,0 +1,14 @@
1
+ // resource-tools.ts - MCP resource name utilities
2
+ export function resourceNameToToolName(name) {
3
+ let result = name
4
+ .replace(/[^a-zA-Z0-9]/g, "_")
5
+ .replace(/_+/g, "_")
6
+ .replace(/^_+/, "") // Remove leading underscores
7
+ .replace(/_+$/, "") // Remove trailing underscores
8
+ .toLowerCase();
9
+ // Ensure we have a valid name
10
+ if (!result || /^\d/.test(result)) {
11
+ result = "resource" + (result ? "_" + result : "");
12
+ }
13
+ return result;
14
+ }
@@ -0,0 +1,206 @@
1
+ import { complete } from "@mariozechner/pi-ai";
2
+ import { truncateAtWord } from "./utils.js";
3
+ import { CreateMessageRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
4
+ export function registerSamplingHandler(client, options) {
5
+ client.setRequestHandler(CreateMessageRequestSchema, (request) => {
6
+ return handleSamplingRequest(options, request);
7
+ });
8
+ }
9
+ export async function handleSamplingRequest(options, request) {
10
+ const params = request.params;
11
+ if ("task" in params && params.task) {
12
+ throw new Error("MCP sampling tasks are not supported");
13
+ }
14
+ if (params.includeContext && params.includeContext !== "none") {
15
+ throw new Error("MCP sampling context inclusion is not supported");
16
+ }
17
+ if (params.tools?.length) {
18
+ throw new Error("MCP sampling tool use is not supported");
19
+ }
20
+ if (params.toolChoice) {
21
+ throw new Error("MCP sampling tool choice is not supported");
22
+ }
23
+ if (params.stopSequences?.length) {
24
+ throw new Error("MCP sampling stop sequences are not supported");
25
+ }
26
+ const messages = params.messages.map(convertSamplingMessage);
27
+ const { model, apiKey, headers } = await resolveSamplingModel(options, params.modelPreferences);
28
+ await confirmSampling(options, "Approve MCP sampling request", formatRequestApproval(options.serverName, `${model.provider}/${model.id}`, params.systemPrompt, messages));
29
+ const result = await complete(model, {
30
+ systemPrompt: params.systemPrompt,
31
+ messages,
32
+ }, {
33
+ apiKey,
34
+ headers,
35
+ maxTokens: params.maxTokens,
36
+ temperature: params.temperature,
37
+ metadata: params.metadata,
38
+ signal: options.getSignal(),
39
+ });
40
+ const converted = convertAssistantResult(result);
41
+ await confirmSampling(options, "Return MCP sampling response", formatResponseApproval(options.serverName, converted));
42
+ return converted;
43
+ }
44
+ function formatRequestApproval(serverName, modelName, systemPrompt, messages) {
45
+ const lines = [`${serverName} wants to sample ${messages.length} message${messages.length === 1 ? "" : "s"} with ${modelName}.`];
46
+ if (systemPrompt) {
47
+ lines.push(`System: ${truncateAtWord(systemPrompt, 400)}`);
48
+ }
49
+ for (const [index, message] of messages.entries()) {
50
+ lines.push(`${index + 1}. ${message.role}: ${truncateAtWord(messageText(message), 400)}`);
51
+ }
52
+ return lines.join("\n\n");
53
+ }
54
+ function formatResponseApproval(serverName, response) {
55
+ const text = response.content.type === "text" ? response.content.text : `[${response.content.type} content]`;
56
+ return `${serverName} will receive this response from ${response.model}:\n\n${truncateAtWord(text, 1000)}`;
57
+ }
58
+ function messageText(message) {
59
+ if (typeof message.content === "string")
60
+ return message.content;
61
+ return message.content.map((block) => {
62
+ if (block.type === "text")
63
+ return block.text;
64
+ if (block.type === "image")
65
+ return `[image: ${block.mimeType}]`;
66
+ if (block.type === "thinking")
67
+ return "[thinking]";
68
+ if (block.type === "toolCall")
69
+ return `[tool call: ${block.name}]`;
70
+ return "[content]";
71
+ }).join("\n");
72
+ }
73
+ async function resolveSamplingModel(options, modelPreferences) {
74
+ const candidates = [];
75
+ const availableModels = options.modelRegistry.getAvailable();
76
+ for (const hint of modelPreferences?.hints ?? []) {
77
+ const normalizedHint = hint.name?.trim().toLowerCase();
78
+ if (!normalizedHint)
79
+ continue;
80
+ for (const model of availableModels) {
81
+ const searchableNames = [`${model.provider}/${model.id}`, model.id, model.name];
82
+ if (searchableNames.some((name) => name.toLowerCase().includes(normalizedHint))) {
83
+ addSamplingCandidate(candidates, model);
84
+ }
85
+ }
86
+ }
87
+ const currentModel = options.getCurrentModel();
88
+ if (currentModel)
89
+ addSamplingCandidate(candidates, currentModel);
90
+ for (const model of availableModels) {
91
+ addSamplingCandidate(candidates, model);
92
+ }
93
+ const errors = [];
94
+ for (const model of candidates) {
95
+ const auth = await options.modelRegistry.getApiKeyAndHeaders(model);
96
+ if (auth.ok) {
97
+ return { model, apiKey: auth.apiKey, headers: auth.headers };
98
+ }
99
+ errors.push(`${model.provider}/${model.id}: ${auth.error}`);
100
+ }
101
+ if (errors.length > 0) {
102
+ throw new Error(`No configured auth for MCP sampling model. ${errors.join("; ")}`);
103
+ }
104
+ throw new Error("No Pi model is available for MCP sampling");
105
+ }
106
+ function addSamplingCandidate(candidates, model) {
107
+ if (!candidates.some((candidate) => candidate.provider === model.provider && candidate.id === model.id)) {
108
+ candidates.push(model);
109
+ }
110
+ }
111
+ async function confirmSampling(options, title, message) {
112
+ if (options.autoApprove)
113
+ return;
114
+ if (!options.ui) {
115
+ throw new Error("MCP sampling requires interactive approval. Set settings.samplingAutoApprove to true to allow it without UI.");
116
+ }
117
+ const approved = await options.ui.confirm(title, message);
118
+ if (!approved) {
119
+ throw new Error("MCP sampling request was declined");
120
+ }
121
+ }
122
+ function convertSamplingMessage(message) {
123
+ const blocks = Array.isArray(message.content) ? message.content : [message.content];
124
+ if (message.role === "user") {
125
+ return {
126
+ role: "user",
127
+ content: blocks.map(convertUserContent),
128
+ timestamp: Date.now(),
129
+ };
130
+ }
131
+ return {
132
+ role: "assistant",
133
+ content: blocks.map(convertAssistantContent),
134
+ api: "mcp-sampling",
135
+ provider: "mcp",
136
+ model: "sampling-request",
137
+ usage: zeroUsage(),
138
+ stopReason: "stop",
139
+ timestamp: Date.now(),
140
+ };
141
+ }
142
+ function convertUserContent(block) {
143
+ if (block.type === "text") {
144
+ return { type: "text", text: block.text };
145
+ }
146
+ throw new Error(`MCP sampling ${block.type} content is not supported`);
147
+ }
148
+ function convertAssistantContent(block) {
149
+ if (block.type === "text") {
150
+ return { type: "text", text: block.text };
151
+ }
152
+ throw new Error(`MCP sampling assistant ${block.type} content is not supported`);
153
+ }
154
+ function convertAssistantResult(message) {
155
+ if (message.stopReason === "error") {
156
+ throw new Error(message.errorMessage ?? "MCP sampling model call failed");
157
+ }
158
+ if (message.stopReason === "aborted") {
159
+ throw new Error(message.errorMessage ?? "MCP sampling model call was aborted");
160
+ }
161
+ const text = message.content
162
+ .map((block) => {
163
+ if (block.type === "text")
164
+ return block.text;
165
+ if (block.type === "thinking")
166
+ return undefined;
167
+ throw new Error(`MCP sampling result ${block.type} content is not supported`);
168
+ })
169
+ .filter((value) => value !== undefined)
170
+ .join("\n\n")
171
+ .trim();
172
+ if (!text) {
173
+ throw new Error("MCP sampling result did not contain text content");
174
+ }
175
+ return {
176
+ role: "assistant",
177
+ content: { type: "text", text },
178
+ model: `${message.provider}/${message.model}`,
179
+ stopReason: mapStopReason(message.stopReason),
180
+ };
181
+ }
182
+ function mapStopReason(reason) {
183
+ if (reason === "stop")
184
+ return "endTurn";
185
+ if (reason === "length")
186
+ return "maxTokens";
187
+ if (reason === "toolUse")
188
+ return "toolUse";
189
+ return reason;
190
+ }
191
+ function zeroUsage() {
192
+ return {
193
+ input: 0,
194
+ output: 0,
195
+ cacheRead: 0,
196
+ cacheWrite: 0,
197
+ totalTokens: 0,
198
+ cost: {
199
+ input: 0,
200
+ output: 0,
201
+ cacheRead: 0,
202
+ cacheWrite: 0,
203
+ total: 0,
204
+ },
205
+ };
206
+ }
@@ -0,0 +1,301 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
3
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4
+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
5
+ import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
6
+ import { serverStreamResultPatchNotificationSchema } from "./types.js";
7
+ import { resolveNpxBinary } from "./npx-resolver.js";
8
+ import { logger } from "./logger.js";
9
+ import { McpOAuthProvider } from "./mcp-oauth-provider.js";
10
+ import { supportsOAuth } from "./mcp-auth-flow.js";
11
+ import { registerSamplingHandler } from "./sampling-handler.js";
12
+ import { interpolateEnvRecord, resolveBearerToken, resolveConfigPath } from "./utils.js";
13
+ export class McpServerManager {
14
+ connections = new Map();
15
+ connectPromises = new Map();
16
+ uiStreamListeners = new Map();
17
+ samplingConfig;
18
+ setSamplingConfig(config) {
19
+ this.samplingConfig = config;
20
+ }
21
+ async connect(name, definition) {
22
+ // Dedupe concurrent connection attempts
23
+ if (this.connectPromises.has(name)) {
24
+ return this.connectPromises.get(name);
25
+ }
26
+ // Reuse existing connection if healthy
27
+ const existing = this.connections.get(name);
28
+ if (existing?.status === "connected") {
29
+ existing.lastUsedAt = Date.now();
30
+ return existing;
31
+ }
32
+ const promise = this.createConnection(name, definition);
33
+ this.connectPromises.set(name, promise);
34
+ try {
35
+ const connection = await promise;
36
+ this.connections.set(name, connection);
37
+ return connection;
38
+ }
39
+ finally {
40
+ this.connectPromises.delete(name);
41
+ }
42
+ }
43
+ async createConnection(name, definition) {
44
+ const client = this.createClient(name);
45
+ let transport;
46
+ if (definition.command) {
47
+ let command = definition.command;
48
+ let args = definition.args ?? [];
49
+ if (command === "npx" || command === "npm") {
50
+ const resolved = await resolveNpxBinary(command, args);
51
+ if (resolved) {
52
+ command = resolved.isJs ? "node" : resolved.binPath;
53
+ args = resolved.isJs ? [resolved.binPath, ...resolved.extraArgs] : resolved.extraArgs;
54
+ logger.debug(`${name} resolved to ${resolved.binPath} (skipping npm parent)`);
55
+ }
56
+ }
57
+ transport = new StdioClientTransport({
58
+ command,
59
+ args,
60
+ env: resolveEnv(definition.env),
61
+ cwd: resolveConfigPath(definition.cwd),
62
+ stderr: definition.debug ? "inherit" : "ignore",
63
+ });
64
+ }
65
+ else if (definition.url) {
66
+ // HTTP transport with fallback
67
+ transport = await this.createHttpTransport(definition, name);
68
+ }
69
+ else {
70
+ throw new Error(`Server ${name} has no command or url`);
71
+ }
72
+ try {
73
+ await client.connect(transport);
74
+ this.attachAdapterNotificationHandlers(name, client);
75
+ // Discover tools and resources
76
+ const [tools, resources] = await Promise.all([
77
+ this.fetchAllTools(client),
78
+ this.fetchAllResources(client),
79
+ ]);
80
+ return {
81
+ client,
82
+ transport,
83
+ definition,
84
+ tools,
85
+ resources,
86
+ lastUsedAt: Date.now(),
87
+ inFlight: 0,
88
+ status: "connected",
89
+ };
90
+ }
91
+ catch (error) {
92
+ // Check for UnauthorizedError - server requires OAuth
93
+ if (error instanceof UnauthorizedError && supportsOAuth(definition)) {
94
+ // Clean up both client and transport before reporting needs-auth.
95
+ await client.close().catch(() => { });
96
+ await transport.close().catch(() => { });
97
+ return {
98
+ client,
99
+ transport,
100
+ definition,
101
+ tools: [],
102
+ resources: [],
103
+ lastUsedAt: Date.now(),
104
+ inFlight: 0,
105
+ status: "needs-auth",
106
+ };
107
+ }
108
+ // Clean up both client and transport on any error
109
+ await client.close().catch(() => { });
110
+ await transport.close().catch(() => { });
111
+ throw error;
112
+ }
113
+ }
114
+ createClient(serverName) {
115
+ const client = new Client({ name: `pi-mcp-${serverName}`, version: "1.0.0" }, this.samplingConfig ? { capabilities: { sampling: {} } } : undefined);
116
+ if (this.samplingConfig) {
117
+ registerSamplingHandler(client, { ...this.samplingConfig, serverName });
118
+ }
119
+ return client;
120
+ }
121
+ async createHttpTransport(definition, serverName) {
122
+ const url = new URL(definition.url);
123
+ // Build headers first (including any bearer token)
124
+ const headers = resolveHeaders(definition.headers) ?? {};
125
+ // For bearer auth, add the token to headers BEFORE creating requestInit
126
+ if (definition.auth === "bearer") {
127
+ const token = resolveBearerToken(definition);
128
+ if (token) {
129
+ headers["Authorization"] = `Bearer ${token}`;
130
+ }
131
+ }
132
+ // Create request init with headers (Authorization now included for bearer auth)
133
+ const requestInit = Object.keys(headers).length > 0 ? { headers } : undefined;
134
+ // For OAuth servers, create an auth provider
135
+ let authProvider;
136
+ if (supportsOAuth(definition)) {
137
+ // Extract OAuth config (handles both object and false cases)
138
+ const oauthConfig = definition.oauth === false ? {} : {
139
+ grantType: definition.oauth?.grantType,
140
+ clientId: definition.oauth?.clientId,
141
+ clientSecret: definition.oauth?.clientSecret,
142
+ scope: definition.oauth?.scope,
143
+ };
144
+ authProvider = new McpOAuthProvider(serverName, definition.url, oauthConfig, {
145
+ onRedirect: async (_authUrl) => {
146
+ // URL is captured by startAuth, no need to log
147
+ },
148
+ });
149
+ }
150
+ // Try StreamableHTTP first (modern MCP servers)
151
+ const streamableTransport = new StreamableHTTPClientTransport(url, {
152
+ requestInit,
153
+ authProvider,
154
+ });
155
+ try {
156
+ // Create a test client to verify the transport works
157
+ const testClient = new Client({ name: "pi-mcp-probe", version: "2.1.2" });
158
+ await testClient.connect(streamableTransport);
159
+ await testClient.close().catch(() => { });
160
+ // Close probe transport before creating fresh one
161
+ await streamableTransport.close().catch(() => { });
162
+ // StreamableHTTP works - create fresh transport for actual use
163
+ return new StreamableHTTPClientTransport(url, { requestInit, authProvider });
164
+ }
165
+ catch (error) {
166
+ // StreamableHTTP failed, close and try SSE fallback
167
+ await streamableTransport.close().catch(() => { });
168
+ // If this was an UnauthorizedError, don't try SSE - the server needs auth
169
+ if (error instanceof UnauthorizedError) {
170
+ throw error;
171
+ }
172
+ // SSE is the legacy transport
173
+ return new SSEClientTransport(url, { requestInit, authProvider });
174
+ }
175
+ }
176
+ async fetchAllTools(client) {
177
+ const allTools = [];
178
+ let cursor;
179
+ do {
180
+ const result = await client.listTools(cursor ? { cursor } : undefined);
181
+ allTools.push(...(result.tools ?? []));
182
+ cursor = result.nextCursor;
183
+ } while (cursor);
184
+ return allTools;
185
+ }
186
+ async fetchAllResources(client) {
187
+ try {
188
+ const allResources = [];
189
+ let cursor;
190
+ do {
191
+ const result = await client.listResources(cursor ? { cursor } : undefined);
192
+ allResources.push(...(result.resources ?? []));
193
+ cursor = result.nextCursor;
194
+ } while (cursor);
195
+ return allResources;
196
+ }
197
+ catch {
198
+ // Server may not support resources
199
+ return [];
200
+ }
201
+ }
202
+ attachAdapterNotificationHandlers(serverName, client) {
203
+ client.setNotificationHandler(serverStreamResultPatchNotificationSchema, (notification) => {
204
+ const listener = this.uiStreamListeners.get(notification.params.streamToken);
205
+ if (!listener)
206
+ return;
207
+ listener(serverName, notification.params);
208
+ });
209
+ }
210
+ registerUiStreamListener(streamToken, listener) {
211
+ this.uiStreamListeners.set(streamToken, listener);
212
+ }
213
+ removeUiStreamListener(streamToken) {
214
+ this.uiStreamListeners.delete(streamToken);
215
+ }
216
+ async readResource(name, uri) {
217
+ const connection = this.connections.get(name);
218
+ if (!connection || connection.status !== "connected") {
219
+ throw new Error(`Server "${name}" is not connected`);
220
+ }
221
+ try {
222
+ this.touch(name);
223
+ this.incrementInFlight(name);
224
+ return await connection.client.readResource({ uri });
225
+ }
226
+ finally {
227
+ this.decrementInFlight(name);
228
+ this.touch(name);
229
+ }
230
+ }
231
+ async close(name) {
232
+ const connection = this.connections.get(name);
233
+ if (!connection)
234
+ return;
235
+ // Delete from map BEFORE async cleanup to prevent a race where a
236
+ // concurrent connect() creates a new connection that our deferred
237
+ // delete() would then remove, orphaning the new server process.
238
+ connection.status = "closed";
239
+ this.connections.delete(name);
240
+ await connection.client.close().catch(() => { });
241
+ await connection.transport.close().catch(() => { });
242
+ }
243
+ async closeAll() {
244
+ const names = [...this.connections.keys()];
245
+ await Promise.all(names.map(name => this.close(name)));
246
+ }
247
+ getConnection(name) {
248
+ return this.connections.get(name);
249
+ }
250
+ getAllConnections() {
251
+ return new Map(this.connections);
252
+ }
253
+ touch(name) {
254
+ const connection = this.connections.get(name);
255
+ if (connection) {
256
+ connection.lastUsedAt = Date.now();
257
+ }
258
+ }
259
+ incrementInFlight(name) {
260
+ const connection = this.connections.get(name);
261
+ if (connection) {
262
+ connection.inFlight = (connection.inFlight ?? 0) + 1;
263
+ }
264
+ }
265
+ decrementInFlight(name) {
266
+ const connection = this.connections.get(name);
267
+ if (connection && connection.inFlight) {
268
+ connection.inFlight--;
269
+ }
270
+ }
271
+ isIdle(name, timeoutMs) {
272
+ const connection = this.connections.get(name);
273
+ if (!connection || connection.status !== "connected")
274
+ return false;
275
+ if (connection.inFlight > 0)
276
+ return false;
277
+ return (Date.now() - connection.lastUsedAt) > timeoutMs;
278
+ }
279
+ }
280
+ /**
281
+ * Resolve environment variables with interpolation.
282
+ */
283
+ function resolveEnv(env) {
284
+ // Copy process.env, filtering out undefined values
285
+ const resolved = {};
286
+ for (const [key, value] of Object.entries(process.env)) {
287
+ if (value !== undefined) {
288
+ resolved[key] = value;
289
+ }
290
+ }
291
+ if (!env)
292
+ return resolved;
293
+ const overrides = interpolateEnvRecord(env);
294
+ return overrides ? { ...resolved, ...overrides } : resolved;
295
+ }
296
+ /**
297
+ * Resolve headers with environment variable interpolation.
298
+ */
299
+ function resolveHeaders(headers) {
300
+ return interpolateEnvRecord(headers);
301
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,128 @@
1
+ import { getToolUiResourceUri } from "@modelcontextprotocol/ext-apps/app-bridge";
2
+ import { formatToolName, isToolExcluded } from "./types.js";
3
+ import { resourceNameToToolName } from "./resource-tools.js";
4
+ import { extractToolUiStreamMode } from "./utils.js";
5
+ export function buildToolMetadata(tools, resources, definition, serverName, prefix) {
6
+ const metadata = [];
7
+ const failedTools = [];
8
+ for (const tool of tools) {
9
+ if (!tool?.name) {
10
+ failedTools.push("(unnamed)");
11
+ continue;
12
+ }
13
+ if (isToolExcluded(tool.name, serverName, prefix, definition.excludeTools)) {
14
+ continue;
15
+ }
16
+ let uiResourceUri;
17
+ try {
18
+ uiResourceUri = getToolUiResourceUri({ _meta: tool._meta });
19
+ }
20
+ catch {
21
+ failedTools.push(tool.name);
22
+ }
23
+ metadata.push({
24
+ name: formatToolName(tool.name, serverName, prefix),
25
+ originalName: tool.name,
26
+ description: tool.description ?? "",
27
+ inputSchema: tool.inputSchema,
28
+ uiResourceUri,
29
+ uiStreamMode: extractToolUiStreamMode(tool._meta),
30
+ });
31
+ }
32
+ if (definition.exposeResources !== false) {
33
+ for (const resource of resources) {
34
+ const baseName = `get_${resourceNameToToolName(resource.name)}`;
35
+ if (isToolExcluded(baseName, serverName, prefix, definition.excludeTools)) {
36
+ continue;
37
+ }
38
+ metadata.push({
39
+ name: formatToolName(baseName, serverName, prefix),
40
+ originalName: baseName,
41
+ description: resource.description ?? `Read resource: ${resource.uri}`,
42
+ resourceUri: resource.uri,
43
+ });
44
+ }
45
+ }
46
+ return { metadata, failedTools };
47
+ }
48
+ export function getToolNames(state, serverName) {
49
+ return state.toolMetadata.get(serverName)?.map(m => m.name) ?? [];
50
+ }
51
+ export function totalToolCount(state) {
52
+ let count = 0;
53
+ for (const metadata of state.toolMetadata.values()) {
54
+ count += metadata.length;
55
+ }
56
+ return count;
57
+ }
58
+ export function findToolByName(metadata, toolName) {
59
+ if (!metadata)
60
+ return undefined;
61
+ const exact = metadata.find(m => m.name === toolName);
62
+ if (exact)
63
+ return exact;
64
+ const normalized = toolName.replace(/-/g, "_");
65
+ return metadata.find(m => m.name.replace(/-/g, "_") === normalized);
66
+ }
67
+ export function formatSchema(schema, indent = " ") {
68
+ if (!schema || typeof schema !== "object") {
69
+ return `${indent}(no schema)`;
70
+ }
71
+ const s = schema;
72
+ if (s.type === "object" && s.properties && typeof s.properties === "object") {
73
+ const props = s.properties;
74
+ const required = Array.isArray(s.required) ? s.required : [];
75
+ if (Object.keys(props).length === 0) {
76
+ return `${indent}(no parameters)`;
77
+ }
78
+ const lines = [];
79
+ for (const [name, propSchema] of Object.entries(props)) {
80
+ const isRequired = required.includes(name);
81
+ const propLine = formatProperty(name, propSchema, isRequired, indent);
82
+ lines.push(propLine);
83
+ }
84
+ return lines.join("\n");
85
+ }
86
+ if (s.type) {
87
+ return `${indent}(${s.type})`;
88
+ }
89
+ return `${indent}(complex schema)`;
90
+ }
91
+ function formatProperty(name, schema, required, indent) {
92
+ if (!schema || typeof schema !== "object") {
93
+ return `${indent}${name}${required ? " *required*" : ""}`;
94
+ }
95
+ const s = schema;
96
+ const parts = [];
97
+ let typeStr = "";
98
+ if (s.type) {
99
+ if (Array.isArray(s.type)) {
100
+ typeStr = s.type.join(" | ");
101
+ }
102
+ else {
103
+ typeStr = String(s.type);
104
+ }
105
+ }
106
+ else if (s.enum) {
107
+ typeStr = "enum";
108
+ }
109
+ else if (s.anyOf || s.oneOf) {
110
+ typeStr = "union";
111
+ }
112
+ if (Array.isArray(s.enum)) {
113
+ const enumVals = s.enum.map(v => JSON.stringify(v)).join(", ");
114
+ typeStr = `enum: ${enumVals}`;
115
+ }
116
+ parts.push(`${indent}${name}`);
117
+ if (typeStr)
118
+ parts.push(`(${typeStr})`);
119
+ if (required)
120
+ parts.push("*required*");
121
+ if (s.description && typeof s.description === "string") {
122
+ parts.push(`- ${s.description}`);
123
+ }
124
+ if (s.default !== undefined) {
125
+ parts.push(`[default: ${JSON.stringify(s.default)}]`);
126
+ }
127
+ return parts.join(" ");
128
+ }
@@ -0,0 +1,43 @@
1
+ // tool-registrar.ts - MCP content transformation
2
+ // NOTE: Tools are NOT registered with Pi - only the unified `mcp` proxy tool is registered.
3
+ // This keeps the LLM context small (1 tool instead of 100s).
4
+ /**
5
+ * Transform MCP content types to Pi content blocks.
6
+ */
7
+ export function transformMcpContent(content) {
8
+ return content.map(c => {
9
+ if (c.type === "text") {
10
+ return { type: "text", text: c.text ?? "" };
11
+ }
12
+ if (c.type === "image") {
13
+ return {
14
+ type: "image",
15
+ data: c.data ?? "",
16
+ mimeType: c.mimeType ?? "image/png",
17
+ };
18
+ }
19
+ if (c.type === "resource") {
20
+ const resourceUri = c.resource?.uri ?? "(no URI)";
21
+ const resourceContent = c.resource?.text ?? (c.resource ? JSON.stringify(c.resource) : "(no content)");
22
+ return {
23
+ type: "text",
24
+ text: `[Resource: ${resourceUri}]\n${resourceContent}`,
25
+ };
26
+ }
27
+ if (c.type === "resource_link") {
28
+ const linkName = c.name ?? c.uri ?? "unknown";
29
+ const linkUri = c.uri ?? "(no URI)";
30
+ return {
31
+ type: "text",
32
+ text: `[Resource Link: ${linkName}]\nURI: ${linkUri}`,
33
+ };
34
+ }
35
+ if (c.type === "audio") {
36
+ return {
37
+ type: "text",
38
+ text: `[Audio content: ${c.mimeType ?? "audio/*"}]`,
39
+ };
40
+ }
41
+ return { type: "text", text: JSON.stringify(c) };
42
+ });
43
+ }