-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathproxy-modes.ts
More file actions
834 lines (746 loc) · 30.2 KB
/
proxy-modes.ts
File metadata and controls
834 lines (746 loc) · 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import type { AgentToolResult, ToolInfo } from "@earendil-works/pi-coding-agent";
import { checkSync } from "recheck";
import type { McpExtensionState } from "./state.ts";
import type { ToolMetadata, McpContent } from "./types.ts";
import { getServerPrefix, parseUiPromptHandoff } from "./types.ts";
import { lazyConnect, updateServerMetadata, updateMetadataCache, getFailureAgeSeconds, updateStatusBar } from "./init.ts";
import { buildToolMetadata, getToolNames, findToolByName, formatSchema } from "./tool-metadata.ts";
import { transformMcpContent } from "./tool-registrar.ts";
import { maybeStartUiSession, type UiSessionRuntime } from "./ui-session.ts";
import { formatAuthRequiredMessage, truncateAtWord } from "./utils.ts";
import { authenticate, supportsOAuth } from "./mcp-auth-flow.ts";
type ProxyToolResult = AgentToolResult<Record<string, unknown>>;
const MAX_REGEX_SEARCH_QUERY_LENGTH = 256;
const REGEX_SAFETY_CHECK_PARAMS = {
attackTimeout: 50,
incubationTimeout: 50,
timeout: 250,
} as const;
type AutoAuthResult =
| { status: "skipped" }
| { status: "success" }
| { status: "failed"; message: string };
function getAuthRequiredMessage(
state: McpExtensionState,
serverName: string,
defaultMessage = `Server "${serverName}" requires OAuth authentication. Run /mcp-auth ${serverName} first.`,
): string {
return formatAuthRequiredMessage(state.config, serverName, defaultMessage);
}
function getAuthFailedMessage(state: McpExtensionState, serverName: string, message: string): string {
const customGuidance = state.config.settings?.authRequiredMessage;
if (customGuidance) {
return `OAuth authentication failed for "${serverName}": ${message}. ${getAuthRequiredMessage(state, serverName)}`;
}
return `OAuth authentication failed for "${serverName}": ${message}. Run /mcp-auth ${serverName} first.`;
}
async function attemptAutoAuth(
state: McpExtensionState,
serverName: string,
): Promise<AutoAuthResult> {
if (state.config.settings?.autoAuth !== true) {
return { status: "skipped" };
}
const definition = state.config.mcpServers[serverName];
if (!definition || !supportsOAuth(definition) || !definition.url) {
return { status: "skipped" };
}
const grantType = definition.oauth ? definition.oauth.grantType ?? "authorization_code" : "authorization_code";
if (!state.ui && grantType !== "client_credentials") {
return {
status: "failed",
message: getAuthRequiredMessage(
state,
serverName,
`Server "${serverName}" requires OAuth authentication. Run /mcp-auth ${serverName} in an interactive session.`,
),
};
}
try {
await authenticate(serverName, definition.url, definition);
return { status: "success" };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
status: "failed",
message: getAuthFailedMessage(state, serverName, message),
};
}
}
export function executeUiMessages(state: McpExtensionState): ProxyToolResult {
const sessions = state.completedUiSessions;
if (sessions.length === 0) {
return {
content: [{ type: "text" as const, text: "No UI session messages available." }],
details: { sessions: 0 },
};
}
const output: string[] = [];
output.push(`UI Session Messages (${sessions.length} session${sessions.length > 1 ? "s" : ""}):\n`);
const allPrompts: string[] = [];
const allIntents = sessions.flatMap((session) => session.messages.intents);
const parsedHandoffs: Array<{ intent: string; params: Record<string, unknown>; raw: string }> = [];
for (const session of sessions) {
const timestamp = session.completedAt.toLocaleTimeString();
output.push(`\n## ${session.serverName} / ${session.toolName} (${timestamp}, ${session.reason})`);
const plainPrompts: string[] = [];
for (const prompt of session.messages.prompts) {
allPrompts.push(prompt);
const handoff = parseUiPromptHandoff(prompt);
if (handoff) {
parsedHandoffs.push(handoff);
} else {
plainPrompts.push(prompt);
}
}
if (plainPrompts.length > 0) {
output.push("\n### Prompts:");
for (const prompt of plainPrompts) {
output.push(`- ${prompt}`);
}
}
const intentsForSession = [
...session.messages.intents,
...session.messages.prompts
.map((prompt) => parseUiPromptHandoff(prompt))
.filter((handoff): handoff is NonNullable<typeof handoff> => !!handoff)
.map((handoff) => ({ intent: handoff.intent, params: handoff.params })),
];
if (intentsForSession.length > 0) {
output.push("\n### Intents:");
for (const intent of intentsForSession) {
const params = intent.params ? ` (${JSON.stringify(intent.params)})` : "";
output.push(`- ${intent.intent}${params}`);
}
}
if (session.messages.notifications.length > 0) {
output.push("\n### Notifications:");
for (const notification of session.messages.notifications) {
output.push(`- ${notification}`);
}
}
}
const count = sessions.length;
state.completedUiSessions = [];
return {
content: [{ type: "text" as const, text: output.join("\n") }],
details: {
sessions: count,
prompts: allPrompts,
intents: [...allIntents, ...parsedHandoffs.map(({ intent, params }) => ({ intent, params }))],
handoffs: parsedHandoffs,
cleared: true,
},
};
}
export function executeStatus(state: McpExtensionState): ProxyToolResult {
const servers: Array<{ name: string; status: string; toolCount: number; failedAgo: number | null }> = [];
for (const name of Object.keys(state.config.mcpServers)) {
const connection = state.manager.getConnection(name);
const metadata = state.toolMetadata.get(name);
const toolCount = metadata?.length ?? 0;
const failedAgo = getFailureAgeSeconds(state, name);
let status = "not connected";
if (connection?.status === "connected") {
status = "connected";
} else if (connection?.status === "needs-auth") {
status = "needs-auth";
} else if (failedAgo !== null) {
status = "failed";
} else if (metadata !== undefined) {
status = "cached";
}
servers.push({ name, status, toolCount, failedAgo });
}
const totalTools = servers.reduce((sum, s) => sum + s.toolCount, 0);
const connectedCount = servers.filter(s => s.status === "connected").length;
let text = `MCP: ${connectedCount}/${servers.length} servers, ${totalTools} tools\n\n`;
for (const server of servers) {
if (server.status === "connected") {
text += `✓ ${server.name} (${server.toolCount} tools)\n`;
continue;
}
if (server.status === "needs-auth") {
text += `⚠ ${server.name} (needs auth)\n`;
continue;
}
if (server.status === "cached") {
text += `○ ${server.name} (${server.toolCount} tools, cached)\n`;
continue;
}
if (server.status === "failed") {
text += `✗ ${server.name} (failed ${server.failedAgo ?? 0}s ago)\n`;
continue;
}
text += `○ ${server.name} (not connected)\n`;
}
if (servers.length > 0) {
text += `\nmcp({ server: "name" }) to list tools, mcp({ search: "..." }) to search`;
}
return {
content: [{ type: "text" as const, text: text.trim() }],
details: { mode: "status", servers, totalTools, connectedCount },
};
}
export function executeDescribe(state: McpExtensionState, toolName: string): ProxyToolResult {
let serverName: string | undefined;
let toolMeta: ToolMetadata | undefined;
for (const [server, metadata] of state.toolMetadata.entries()) {
const found = findToolByName(metadata, toolName);
if (found) {
serverName = server;
toolMeta = found;
break;
}
}
if (!serverName || !toolMeta) {
return {
content: [{ type: "text" as const, text: `Tool "${toolName}" not found. Use mcp({ search: "..." }) to search.` }],
details: { mode: "describe", error: "tool_not_found", requestedTool: toolName },
};
}
let text = `${toolMeta.name}\n`;
text += `Server: ${serverName}\n`;
if (toolMeta.resourceUri) {
text += `Type: Resource (reads from ${toolMeta.resourceUri})\n`;
}
text += `\n${toolMeta.description || "(no description)"}\n`;
if (toolMeta.inputSchema && !toolMeta.resourceUri) {
text += `\nParameters:\n${formatSchema(toolMeta.inputSchema)}`;
} else if (toolMeta.resourceUri) {
text += `\nNo parameters required (resource tool).`;
} else {
text += `\nNo parameters defined.`;
}
return {
content: [{ type: "text" as const, text: text.trim() }],
details: { mode: "describe", tool: toolMeta, server: serverName },
};
}
export function executeSearch(
state: McpExtensionState,
query: string,
regex?: boolean,
server?: string,
includeSchemas?: boolean,
): ProxyToolResult {
const showSchemas = includeSchemas !== false;
const matches: Array<{ server: string; tool: ToolMetadata }> = [];
let pattern: RegExp;
try {
if (regex) {
if (query.length > MAX_REGEX_SEARCH_QUERY_LENGTH) {
return {
content: [{ type: "text" as const, text: `Regex query is too long; maximum length is ${MAX_REGEX_SEARCH_QUERY_LENGTH} characters.` }],
details: { mode: "search", error: "query_too_long", query, maxLength: MAX_REGEX_SEARCH_QUERY_LENGTH },
};
}
pattern = new RegExp(query, "i");
let safety;
try {
safety = checkSync(query, "i", REGEX_SAFETY_CHECK_PARAMS);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
content: [{ type: "text" as const, text: "Regex query rejected because safety analysis failed." }],
details: { mode: "search", error: "unsafe_pattern", query, reason: message },
};
}
if (safety.status !== "safe") {
return {
content: [{ type: "text" as const, text: `Regex query rejected as unsafe (${safety.status}).` }],
details: { mode: "search", error: "unsafe_pattern", query, safetyStatus: safety.status },
};
}
} else {
const terms = query.trim().split(/\s+/).filter(t => t.length > 0);
if (terms.length === 0) {
return {
content: [{ type: "text" as const, text: "Search query cannot be empty" }],
details: { mode: "search", error: "empty_query" },
};
}
const escaped = terms.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
pattern = new RegExp(escaped.join("|"), "i");
}
} catch {
return {
content: [{ type: "text" as const, text: `Invalid regex: ${query}` }],
details: { mode: "search", error: "invalid_pattern", query },
};
}
for (const [serverName, metadata] of state.toolMetadata.entries()) {
if (server && serverName !== server) continue;
for (const tool of metadata) {
if (pattern.test(tool.name) || pattern.test(tool.description)) {
matches.push({
server: serverName,
tool,
});
}
}
}
const totalCount = matches.length;
if (totalCount === 0) {
const msg = server
? `No tools matching "${query}" in "${server}"`
: `No tools matching "${query}"`;
return {
content: [{ type: "text" as const, text: msg }],
details: { mode: "search", matches: [], count: 0, query },
};
}
let text = `Found ${totalCount} tool${totalCount === 1 ? "" : "s"} matching "${query}":\n\n`;
for (const match of matches) {
if (showSchemas) {
text += `${match.tool.name}\n`;
text += ` ${match.tool.description || "(no description)"}\n`;
if (match.tool.inputSchema && !match.tool.resourceUri) {
text += `\n Parameters:\n${formatSchema(match.tool.inputSchema, " ")}\n`;
} else if (match.tool.resourceUri) {
text += ` No parameters (resource tool).\n`;
}
text += "\n";
} else {
text += `- ${match.tool.name}`;
if (match.tool.description) {
text += ` - ${truncateAtWord(match.tool.description, 50)}`;
}
text += "\n";
}
}
return {
content: [{ type: "text" as const, text: text.trim() }],
details: {
mode: "search",
matches: matches.map(m => ({ server: m.server, tool: m.tool.name })),
count: totalCount,
query,
},
};
}
export function executeList(state: McpExtensionState, server: string): ProxyToolResult {
if (!state.config.mcpServers[server]) {
return {
content: [{ type: "text" as const, text: `Server "${server}" not found. Use mcp({}) to see available servers.` }],
details: { mode: "list", server, tools: [], count: 0, error: "not_found" },
};
}
const metadata = state.toolMetadata.get(server);
const toolNames = metadata?.map(m => m.name) ?? [];
const connection = state.manager.getConnection(server);
if (toolNames.length === 0) {
if (connection?.status === "connected") {
return {
content: [{ type: "text" as const, text: `Server "${server}" has no tools.` }],
details: { mode: "list", server, tools: [], count: 0 },
};
}
if (metadata !== undefined) {
return {
content: [{ type: "text" as const, text: `Server "${server}" has no cached tools (not connected).` }],
details: { mode: "list", server, tools: [], count: 0, cached: true },
};
}
return {
content: [{ type: "text" as const, text: `Server "${server}" is configured but not connected. Use mcp({ connect: "${server}" }) or /mcp reconnect ${server} to retry.` }],
details: { mode: "list", server, tools: [], count: 0, error: "not_connected" },
};
}
const cachedNote = connection?.status === "connected" ? "" : " (not connected, cached)";
let text = `${server} (${toolNames.length} tools${cachedNote}):\n\n`;
const descMap = new Map<string, string>();
if (metadata) {
for (const m of metadata) {
descMap.set(m.name, m.description);
}
}
for (const tool of toolNames) {
const desc = descMap.get(tool) ?? "";
const truncated = truncateAtWord(desc, 50);
text += `- ${tool}`;
if (truncated) text += ` - ${truncated}`;
text += "\n";
}
return {
content: [{ type: "text" as const, text: text.trim() }],
details: { mode: "list", server, tools: toolNames, count: toolNames.length },
};
}
export async function executeConnect(state: McpExtensionState, serverName: string): Promise<ProxyToolResult> {
const definition = state.config.mcpServers[serverName];
if (!definition) {
return {
content: [{ type: "text" as const, text: `Server "${serverName}" not found. Use mcp({}) to see available servers.` }],
details: { mode: "connect", error: "not_found", server: serverName },
};
}
try {
if (state.ui) {
state.ui.setStatus("mcp", `MCP: connecting to ${serverName}...`);
}
let connection = await state.manager.connect(serverName, definition);
if (connection.status === "needs-auth") {
const autoAuth = await attemptAutoAuth(state, serverName);
if (autoAuth.status === "failed") {
return {
content: [{ type: "text" as const, text: autoAuth.message }],
details: { mode: "connect", error: "auth_required", server: serverName, message: autoAuth.message },
};
}
if (autoAuth.status === "success") {
await state.manager.close(serverName);
connection = await state.manager.connect(serverName, definition);
}
if (connection.status === "needs-auth") {
const message = getAuthRequiredMessage(state, serverName);
return {
content: [{ type: "text" as const, text: message }],
details: { mode: "connect", error: "auth_required", server: serverName, message },
};
}
}
const prefix = state.config.settings?.toolPrefix ?? "server";
const { metadata } = buildToolMetadata(connection.tools, connection.resources, definition, serverName, prefix);
state.toolMetadata.set(serverName, metadata);
updateMetadataCache(state, serverName);
state.failureTracker.delete(serverName);
updateStatusBar(state);
return executeList(state, serverName);
} catch (error) {
state.failureTracker.set(serverName, Date.now());
updateStatusBar(state);
const message = error instanceof Error ? error.message : String(error);
return {
content: [{ type: "text" as const, text: `Failed to connect to "${serverName}": ${message}` }],
details: { mode: "connect", error: "connect_failed", server: serverName, message },
};
}
}
export async function executeCall(
state: McpExtensionState,
toolName: string,
args?: Record<string, unknown>,
serverOverride?: string,
getPiTools?: () => ToolInfo[],
): Promise<ProxyToolResult> {
let serverName: string | undefined = serverOverride;
let toolMeta: ToolMetadata | undefined;
let autoAuthAttempted = false;
const prefixMode = state.config.settings?.toolPrefix ?? "server";
if (serverName && !state.config.mcpServers[serverName]) {
return {
content: [{ type: "text" as const, text: `Server "${serverName}" not found. Use mcp({}) to see available servers.` }],
details: { mode: "call", error: "server_not_found", server: serverName },
};
}
if (serverName) {
toolMeta = findToolByName(state.toolMetadata.get(serverName), toolName);
} else {
for (const [server, metadata] of state.toolMetadata.entries()) {
const found = findToolByName(metadata, toolName);
if (found) {
serverName = server;
toolMeta = found;
break;
}
}
}
if (serverName && !toolMeta) {
const connected = await lazyConnect(state, serverName);
if (connected) {
toolMeta = findToolByName(state.toolMetadata.get(serverName), toolName);
} else {
const needsAuthConnection = state.manager.getConnection(serverName);
if (needsAuthConnection?.status === "needs-auth") {
if (!autoAuthAttempted) {
autoAuthAttempted = true;
const autoAuth = await attemptAutoAuth(state, serverName);
if (autoAuth.status === "failed") {
return {
content: [{ type: "text" as const, text: autoAuth.message }],
details: { mode: "call", error: "auth_required", server: serverName, message: autoAuth.message },
};
}
if (autoAuth.status === "success") {
await state.manager.close(serverName);
state.failureTracker.delete(serverName);
const connectedAfterAuth = await lazyConnect(state, serverName);
if (connectedAfterAuth) {
toolMeta = findToolByName(state.toolMetadata.get(serverName), toolName);
if (!toolMeta) {
return {
content: [{ type: "text" as const, text: `Tool "${toolName}" not found on "${serverName}" after reconnect.` }],
details: { mode: "call", error: "tool_not_found_after_reconnect", requestedTool: toolName },
};
}
}
}
}
if (!toolMeta && state.manager.getConnection(serverName)?.status === "needs-auth") {
const message = getAuthRequiredMessage(state, serverName);
return {
content: [{ type: "text" as const, text: message }],
details: { mode: "call", error: "auth_required", server: serverName, message },
};
}
}
if (!toolMeta) {
const failedAgo = getFailureAgeSeconds(state, serverName);
if (failedAgo !== null) {
return {
content: [{ type: "text" as const, text: `Server "${serverName}" not available (last failed ${failedAgo}s ago)` }],
details: { mode: "call", error: "server_backoff", server: serverName },
};
}
}
}
}
let prefixMatchedServer: string | undefined;
if (!serverName && !toolMeta && prefixMode !== "none") {
const candidates = Object.keys(state.config.mcpServers)
.map(name => ({ name, prefix: getServerPrefix(name, prefixMode) }))
.filter(c => c.prefix && toolName.startsWith(c.prefix + "_"))
.sort((a, b) => b.prefix.length - a.prefix.length);
for (const { name: configuredServer } of candidates) {
const existingConnection = state.manager.getConnection(configuredServer);
const failedAgo = getFailureAgeSeconds(state, configuredServer);
if (failedAgo !== null && existingConnection?.status !== "needs-auth") continue;
let connected = await lazyConnect(state, configuredServer);
if (!connected && state.manager.getConnection(configuredServer)?.status === "needs-auth" && !autoAuthAttempted) {
autoAuthAttempted = true;
const autoAuth = await attemptAutoAuth(state, configuredServer);
if (autoAuth.status === "failed") {
return {
content: [{ type: "text" as const, text: autoAuth.message }],
details: { mode: "call", error: "auth_required", server: configuredServer, message: autoAuth.message },
};
}
if (autoAuth.status === "success") {
await state.manager.close(configuredServer);
state.failureTracker.delete(configuredServer);
connected = await lazyConnect(state, configuredServer);
}
}
if (!connected) continue;
if (!prefixMatchedServer) prefixMatchedServer = configuredServer;
toolMeta = findToolByName(state.toolMetadata.get(configuredServer), toolName);
if (toolMeta) {
serverName = configuredServer;
break;
}
}
}
if (!serverName || !toolMeta) {
const nativeTool = !serverOverride
? getPiTools?.().find((tool) => tool.name === toolName && tool.name !== "mcp")
: undefined;
if (nativeTool) {
return {
content: [{ type: "text" as const, text: `"${toolName}" is a native Pi tool. Call ${toolName} directly instead of using mcp({ tool: "${toolName}" }).` }],
details: { mode: "call", error: "native_tool", requestedTool: toolName },
};
}
const hintServer = serverName ?? prefixMatchedServer;
const available = hintServer ? getToolNames(state, hintServer) : [];
let msg = `Tool "${toolName}" not found.`;
if (available.length > 0) {
msg += ` Server "${hintServer}" has: ${available.join(", ")}`;
} else {
msg += ` Use mcp({ search: "..." }) to search.`;
}
return {
content: [{ type: "text" as const, text: msg }],
details: { mode: "call", error: "tool_not_found", requestedTool: toolName, hintServer },
};
}
let connection = state.manager.getConnection(serverName);
if (connection?.status === "needs-auth") {
if (!autoAuthAttempted) {
autoAuthAttempted = true;
const autoAuth = await attemptAutoAuth(state, serverName);
if (autoAuth.status === "failed") {
return {
content: [{ type: "text" as const, text: autoAuth.message }],
details: { mode: "call", error: "auth_required", server: serverName, message: autoAuth.message },
};
}
if (autoAuth.status === "success") {
await state.manager.close(serverName);
state.failureTracker.delete(serverName);
connection = state.manager.getConnection(serverName);
}
}
if (connection?.status === "needs-auth") {
const message = getAuthRequiredMessage(state, serverName);
return {
content: [{ type: "text" as const, text: message }],
details: { mode: "call", error: "auth_required", server: serverName, message },
};
}
}
if (!connection || connection.status !== "connected") {
const failedAgo = getFailureAgeSeconds(state, serverName);
if (failedAgo !== null) {
return {
content: [{ type: "text" as const, text: `Server "${serverName}" not available (last failed ${failedAgo}s ago)` }],
details: { mode: "call", error: "server_backoff", server: serverName },
};
}
const definition = state.config.mcpServers[serverName];
if (!definition) {
return {
content: [{ type: "text" as const, text: `Server "${serverName}" not connected` }],
details: { mode: "call", error: "server_not_connected", server: serverName },
};
}
try {
if (state.ui) {
state.ui.setStatus("mcp", `MCP: connecting to ${serverName}...`);
}
connection = await state.manager.connect(serverName, definition);
if (connection.status === "needs-auth") {
if (!autoAuthAttempted) {
autoAuthAttempted = true;
const autoAuth = await attemptAutoAuth(state, serverName);
if (autoAuth.status === "failed") {
return {
content: [{ type: "text" as const, text: autoAuth.message }],
details: { mode: "call", error: "auth_required", server: serverName, message: autoAuth.message },
};
}
if (autoAuth.status === "success") {
await state.manager.close(serverName);
connection = await state.manager.connect(serverName, definition);
}
}
if (connection.status === "needs-auth") {
const message = getAuthRequiredMessage(state, serverName);
return {
content: [{ type: "text" as const, text: message }],
details: { mode: "call", error: "auth_required", server: serverName, message },
};
}
}
state.failureTracker.delete(serverName);
updateServerMetadata(state, serverName);
updateMetadataCache(state, serverName);
updateStatusBar(state);
toolMeta = findToolByName(state.toolMetadata.get(serverName), toolName);
if (!toolMeta) {
const available = getToolNames(state, serverName);
const hint = available.length > 0
? `Available tools on "${serverName}": ${available.join(", ")}`
: `Server "${serverName}" has no tools.`;
return {
content: [{ type: "text" as const, text: `Tool "${toolName}" not found on "${serverName}" after reconnect. ${hint}` }],
details: { mode: "call", error: "tool_not_found_after_reconnect", requestedTool: toolName },
};
}
} catch (error) {
state.failureTracker.set(serverName, Date.now());
updateStatusBar(state);
const message = error instanceof Error ? error.message : String(error);
return {
content: [{ type: "text" as const, text: `Failed to connect to "${serverName}": ${message}` }],
details: { mode: "call", error: "connect_failed", message },
};
}
}
let uiSession: UiSessionRuntime | null = null;
try {
state.manager.touch(serverName);
state.manager.incrementInFlight(serverName);
if (toolMeta.resourceUri) {
const result = await connection.client.readResource({ uri: toolMeta.resourceUri });
const content = (result.contents ?? []).map(c => ({
type: "text" as const,
text: "text" in c ? c.text : ("blob" in c ? `[Binary data: ${(c as { mimeType?: string }).mimeType ?? "unknown"}]` : JSON.stringify(c)),
}));
return {
content: content.length > 0 ? content : [{ type: "text" as const, text: "(empty resource)" }],
details: { mode: "call", resourceUri: toolMeta.resourceUri, server: serverName },
};
}
uiSession = toolMeta.uiResourceUri
? await maybeStartUiSession(state, {
serverName,
toolName: toolMeta.originalName,
toolArgs: args ?? {},
uiResourceUri: toolMeta.uiResourceUri,
streamMode: toolMeta.uiStreamMode,
})
: null;
const resultPromise = connection.client.callTool({
name: toolMeta.originalName,
arguments: args ?? {},
_meta: uiSession?.requestMeta,
});
if (toolMeta.uiResourceUri) {
const result = await resultPromise;
uiSession?.sendToolResult(result as unknown as import("@modelcontextprotocol/sdk/types.js").CallToolResult);
const mcpContent = (result.content ?? []) as McpContent[];
const content = transformMcpContent(mcpContent);
const mcpText = content
.filter((c) => c.type === "text")
.map((c) => (c as { text: string }).text)
.join("\n");
if (result.isError) {
let errorWithSchema = `Error: ${mcpText || "Tool execution failed"}`;
if (toolMeta.inputSchema) {
errorWithSchema += `\n\nExpected parameters:\n${formatSchema(toolMeta.inputSchema)}`;
}
return {
content: [{ type: "text" as const, text: errorWithSchema }],
details: { mode: "call", error: "tool_error", mcpResult: result },
};
}
const resultText = mcpText || "(empty result)";
const uiMessage = uiSession?.reused
? "Updated the open UI."
: "📺 Interactive UI is now open in your browser. I'll respond to your prompts and intents as you interact with it.";
return {
content: [{ type: "text" as const, text: `${resultText}\n\n${uiMessage}` }],
details: { mode: "call", mcpResult: result, server: serverName, tool: toolMeta.originalName, uiOpen: true },
};
}
const result = await resultPromise;
const mcpContent = (result.content ?? []) as McpContent[];
const content = transformMcpContent(mcpContent);
if (result.isError) {
const errorText = content
.filter((c) => c.type === "text")
.map((c) => (c as { text: string }).text)
.join("\n") || "Tool execution failed";
let errorWithSchema = `Error: ${errorText}`;
if (toolMeta.inputSchema) {
errorWithSchema += `\n\nExpected parameters:\n${formatSchema(toolMeta.inputSchema)}`;
}
return {
content: [{ type: "text" as const, text: errorWithSchema }],
details: { mode: "call", error: "tool_error", mcpResult: result },
};
}
return {
content: content.length > 0 ? content : [{ type: "text" as const, text: "(empty result)" }],
details: { mode: "call", mcpResult: result, server: serverName, tool: toolMeta.originalName },
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
uiSession?.sendToolCancelled(message);
let errorWithSchema = `Failed to call tool: ${message}`;
if (toolMeta.inputSchema) {
errorWithSchema += `\n\nExpected parameters:\n${formatSchema(toolMeta.inputSchema)}`;
}
return {
content: [{ type: "text" as const, text: errorWithSchema }],
details: { mode: "call", error: "call_failed", message },
};
} finally {
if (uiSession?.reused) {
uiSession.close();
}
state.manager.decrementInFlight(serverName);
state.manager.touch(serverName);
}
}