-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcallTool.ts
165 lines (152 loc) · 5.3 KB
/
callTool.ts
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
import { ContextItem, Tool, ToolExtras } from "..";
import { MCPManagerSingleton } from "../context/mcp";
import { canParseUrl } from "../util/url";
import { BuiltInToolNames } from "./builtIn";
import { createNewFileImpl } from "./implementations/createNewFile";
import { globToolImpl } from "./implementations/globTool";
import { grepToolImpl } from "./implementations/grepTool";
import { lsToolImpl } from "./implementations/lsTool";
import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile";
import { readFileImpl } from "./implementations/readFile";
import { runTerminalCommandImpl } from "./implementations/runTerminalCommand";
import { searchWebImpl } from "./implementations/searchWeb";
import { viewDiffImpl } from "./implementations/viewDiff";
async function callHttpTool(
url: string,
args: any,
extras: ToolExtras,
): Promise<ContextItem[]> {
const response = await extras.fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
arguments: args,
}),
});
if (!response.ok) {
throw new Error(`Failed to call tool: ${url}`);
}
const data = await response.json();
return data.output;
}
export function encodeMCPToolUri(mcpId: string, toolName: string): string {
return `mcp://${encodeURIComponent(mcpId)}/${encodeURIComponent(toolName)}`;
}
export function decodeMCPToolUri(uri: string): [string, string] | null {
const url = new URL(uri);
if (url.protocol !== "mcp:") {
return null;
}
return [
decodeURIComponent(url.hostname),
decodeURIComponent(url.pathname).slice(1), // to remove leading '/'
];
}
async function callToolFromUri(
uri: string,
args: any,
extras: ToolExtras,
): Promise<ContextItem[]> {
const parseable = canParseUrl(uri);
if (!parseable) {
throw new Error(`Invalid URI: ${uri}`);
}
const parsedUri = new URL(uri);
switch (parsedUri?.protocol) {
case "http:":
case "https:":
return callHttpTool(uri, args, extras);
case "mcp:":
const decoded = decodeMCPToolUri(uri);
if (!decoded) {
throw new Error(`Invalid MCP tool URI: ${uri}`);
}
const [mcpId, toolName] = decoded;
const client = MCPManagerSingleton.getInstance().getConnection(mcpId);
if (!client) {
throw new Error("MCP connection not found");
}
const response = await client.client.callTool({
name: toolName,
arguments: args,
});
if (response.isError === true) {
throw new Error(`Failed to call tool: ${toolName}`);
}
const contextItems: ContextItem[] = [];
(response.content as any).forEach((item: any) => {
if (item.type === "text") {
contextItems.push({
name: extras.tool.displayTitle,
description: "Tool output",
content: item.text,
icon: extras.tool.faviconUrl,
});
} else if (item.type === "resource") {
// TODO resource change subscribers https://modelcontextprotocol.io/docs/concepts/resources
if (item.resource?.blob) {
contextItems.push({
name: extras.tool.displayTitle,
description: "MCP Item Error",
content:
"Error: tool call received unsupported blob resource item",
icon: extras.tool.faviconUrl,
});
}
// TODO account for mimetype? // const mimeType = item.resource.mimeType
// const uri = item.resource.uri;
contextItems.push({
name: extras.tool.displayTitle,
description: "Tool output",
content: item.resource.text,
icon: extras.tool.faviconUrl,
});
} else {
contextItems.push({
name: extras.tool.displayTitle,
description: "MCP Item Error",
content: `Error: tool call received unsupported item of type "${item.type}"`,
icon: extras.tool.faviconUrl,
});
}
});
return contextItems;
default:
throw new Error(`Unsupported protocol: ${parsedUri?.protocol}`);
}
}
export async function callTool(
tool: Tool,
args: any,
extras: ToolExtras,
): Promise<ContextItem[]> {
const uri = tool.uri ?? tool.function.name;
switch (uri) {
case BuiltInToolNames.ReadFile:
return await readFileImpl(args, extras);
case BuiltInToolNames.CreateNewFile:
return await createNewFileImpl(args, extras);
case BuiltInToolNames.RunTerminalCommand:
return await runTerminalCommandImpl(args, extras);
case BuiltInToolNames.SearchWeb:
return await searchWebImpl(args, extras);
case BuiltInToolNames.ViewDiff:
return await viewDiffImpl(args, extras);
case BuiltInToolNames.LSTool:
return await lsToolImpl(args, extras);
case BuiltInToolNames.GlobTool:
return await globToolImpl(args, extras);
case BuiltInToolNames.GrepTool:
return await grepToolImpl(args, extras);
case BuiltInToolNames.ReadCurrentlyOpenFile:
return await readCurrentlyOpenFileImpl(args, extras);
// case BuiltInToolNames.ViewRepoMap:
// return await viewRepoMapImpl(args, extras);
// case BuiltInToolNames.ViewSubdirectory:
// return await viewSubdirectoryImpl(args, extras);
default:
return await callToolFromUri(uri, args, extras);
}
}