mcp-use 1.3.4-canary.1 → 1.4.0-canary.3
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/dist/.tsbuildinfo +1 -1
- package/dist/{chunk-6EHM3S3M.js → chunk-35A6O3YH.js} +52 -10
- package/dist/chunk-DSBKVAWD.js +263 -0
- package/dist/{chunk-TIUSJAAE.js → chunk-GRLCLVAK.js} +3 -254
- package/dist/{chunk-QKPVHYJU.js → chunk-RE7EYFDV.js} +1 -1
- package/dist/chunk-WERYJ6PF.js +209 -0
- package/dist/chunk-ZFZPZ4GE.js +21 -0
- package/dist/display-LIYVTGEU.js +350 -0
- package/dist/index.cjs +1539 -284
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +673 -9
- package/dist/src/agents/display.d.ts +54 -0
- package/dist/src/agents/display.d.ts.map +1 -0
- package/dist/src/agents/index.cjs +504 -10
- package/dist/src/agents/index.d.ts +1 -0
- package/dist/src/agents/index.d.ts.map +1 -1
- package/dist/src/agents/index.js +10 -18
- package/dist/src/agents/mcp_agent.d.ts +5 -0
- package/dist/src/agents/mcp_agent.d.ts.map +1 -1
- package/dist/src/browser.cjs +406 -10
- package/dist/src/browser.js +5 -3
- package/dist/src/client/codeExecutor.d.ts +6 -0
- package/dist/src/client/codeExecutor.d.ts.map +1 -0
- package/dist/src/client/connectors/codeMode.d.ts +27 -0
- package/dist/src/client/connectors/codeMode.d.ts.map +1 -0
- package/dist/src/client/executors/base.d.ts +56 -0
- package/dist/src/client/executors/base.d.ts.map +1 -0
- package/dist/src/client/executors/e2b.d.ts +46 -0
- package/dist/src/client/executors/e2b.d.ts.map +1 -0
- package/dist/src/client/executors/vm.d.ts +30 -0
- package/dist/src/client/executors/vm.d.ts.map +1 -0
- package/dist/src/client/prompts.cjs +401 -0
- package/dist/src/client/prompts.d.ts +13 -0
- package/dist/src/client/prompts.d.ts.map +1 -0
- package/dist/src/client/prompts.js +9 -0
- package/dist/src/client.d.ts +51 -4
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/react/index.js +3 -2
- package/dist/tsup.config.d.ts.map +1 -1
- package/package.json +14 -4
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseConnector
|
|
3
|
+
} from "./chunk-DSBKVAWD.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-3GQAWCBQ.js";
|
|
7
|
+
|
|
8
|
+
// src/client/connectors/codeMode.ts
|
|
9
|
+
var CODE_MODE_AGENT_PROMPT = `
|
|
10
|
+
## MCP Code Mode Tool Usage Guide
|
|
11
|
+
|
|
12
|
+
You have access to an MCP Code Mode Client that allows you to execute JavaScript/TypeScript code with access to registered tools. Follow this workflow:
|
|
13
|
+
|
|
14
|
+
### 1. Tool Discovery Phase
|
|
15
|
+
**Always start by discovering available tools:**
|
|
16
|
+
- Tools are organized by server namespace (e.g., \`server_name.tool_name\`)
|
|
17
|
+
- Use the \`search_tools(query, detail_level)\` function to find available tools
|
|
18
|
+
- You can access \`__tool_namespaces\` to see all available server namespaces
|
|
19
|
+
|
|
20
|
+
\`\`\`javascript
|
|
21
|
+
// Find all GitHub-related tools
|
|
22
|
+
const tools = await search_tools("github");
|
|
23
|
+
for (const tool of tools) {
|
|
24
|
+
console.log(\`\${tool.server}.\${tool.name}: \${tool.description}\`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Get only tool names for quick overview
|
|
28
|
+
const tools = await search_tools("", "names");
|
|
29
|
+
\`\`\`
|
|
30
|
+
|
|
31
|
+
### 2. Interface Introspection
|
|
32
|
+
**Understand tool contracts before using them:**
|
|
33
|
+
- Use \`search_tools\` to get tool descriptions and input schemas
|
|
34
|
+
- Look for "Access as: server.tool(args)" patterns in descriptions
|
|
35
|
+
|
|
36
|
+
### 3. Code Execution Guidelines
|
|
37
|
+
**When writing code:**
|
|
38
|
+
- Use \`await server.tool({ param: value })\` syntax for all tool calls
|
|
39
|
+
- Tools are async functions that return promises
|
|
40
|
+
- You have access to standard JavaScript globals: \`console\`, \`JSON\`, \`Math\`, \`Date\`, etc.
|
|
41
|
+
- All console output (\`console.log\`, \`console.error\`, etc.) is automatically captured and returned
|
|
42
|
+
- Build properly structured input objects based on interface definitions
|
|
43
|
+
- Handle errors appropriately with try/catch blocks
|
|
44
|
+
- Chain tool calls by using results from previous calls
|
|
45
|
+
|
|
46
|
+
### 4. Best Practices
|
|
47
|
+
- **Discover first, code second**: Always explore available tools before writing execution code
|
|
48
|
+
- **Respect namespaces**: Use full \`server.tool\` names to avoid conflicts
|
|
49
|
+
- **Minimize Context**: Process large data in code, return only essential results
|
|
50
|
+
- **Error handling**: Wrap tool calls in try/catch for robustness
|
|
51
|
+
- **Data flow**: Chain tools by passing outputs as inputs to subsequent tools
|
|
52
|
+
|
|
53
|
+
### 5. Available Runtime Context
|
|
54
|
+
- \`search_tools(query, detail_level)\`: Function to discover tools
|
|
55
|
+
- \`__tool_namespaces\`: Array of available server namespaces
|
|
56
|
+
- All registered tools as \`server.tool\` functions
|
|
57
|
+
- Standard JavaScript built-ins for data processing
|
|
58
|
+
|
|
59
|
+
### Example Workflow
|
|
60
|
+
|
|
61
|
+
\`\`\`javascript
|
|
62
|
+
// 1. Discover available tools
|
|
63
|
+
const github_tools = await search_tools("github pull request");
|
|
64
|
+
console.log(\`Available GitHub PR tools: \${github_tools.map(t => t.name)}\`);
|
|
65
|
+
|
|
66
|
+
// 2. Call tools with proper parameters
|
|
67
|
+
const pr = await github.get_pull_request({
|
|
68
|
+
owner: "facebook",
|
|
69
|
+
repo: "react",
|
|
70
|
+
number: 12345
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// 3. Process results
|
|
74
|
+
let result;
|
|
75
|
+
if (pr.state === 'open' && pr.labels.some(l => l.name === 'bug')) {
|
|
76
|
+
// 4. Chain with other tools
|
|
77
|
+
await slack.post_message({
|
|
78
|
+
channel: "#bugs",
|
|
79
|
+
text: \`\u{1F41B} Bug PR needs review: \${pr.title}\`
|
|
80
|
+
});
|
|
81
|
+
result = "Notification sent";
|
|
82
|
+
} else {
|
|
83
|
+
result = "No action needed";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 5. Return structured results
|
|
87
|
+
return {
|
|
88
|
+
pr_number: pr.number,
|
|
89
|
+
pr_title: pr.title,
|
|
90
|
+
action_taken: result
|
|
91
|
+
};
|
|
92
|
+
\`\`\`
|
|
93
|
+
|
|
94
|
+
Remember: Always discover and understand available tools before attempting to use them in code execution.
|
|
95
|
+
`;
|
|
96
|
+
var CodeModeConnector = class extends BaseConnector {
|
|
97
|
+
static {
|
|
98
|
+
__name(this, "CodeModeConnector");
|
|
99
|
+
}
|
|
100
|
+
mcpClient;
|
|
101
|
+
_tools;
|
|
102
|
+
constructor(client) {
|
|
103
|
+
super();
|
|
104
|
+
this.mcpClient = client;
|
|
105
|
+
this.connected = true;
|
|
106
|
+
this._tools = this._createToolsList();
|
|
107
|
+
}
|
|
108
|
+
async connect() {
|
|
109
|
+
this.connected = true;
|
|
110
|
+
}
|
|
111
|
+
async disconnect() {
|
|
112
|
+
this.connected = false;
|
|
113
|
+
}
|
|
114
|
+
get publicIdentifier() {
|
|
115
|
+
return { name: "code_mode", version: "1.0.0" };
|
|
116
|
+
}
|
|
117
|
+
_createToolsList() {
|
|
118
|
+
return [
|
|
119
|
+
{
|
|
120
|
+
name: "execute_code",
|
|
121
|
+
description: "Execute JavaScript/TypeScript code with access to MCP tools. This is the PRIMARY way to interact with MCP servers in code mode. Write code that discovers tools using search_tools(), calls tools as async functions (e.g., await github.get_pull_request(...)), processes data efficiently, and returns results. Use 'await' for async operations and 'return' to return values. Available in code: search_tools(), __tool_namespaces, and server.tool_name() functions.",
|
|
122
|
+
inputSchema: {
|
|
123
|
+
type: "object",
|
|
124
|
+
properties: {
|
|
125
|
+
code: {
|
|
126
|
+
type: "string",
|
|
127
|
+
description: "JavaScript/TypeScript code to execute. Use 'await' for async operations. Use 'return' to return a value. Available: search_tools(), server.tool_name(), __tool_namespaces"
|
|
128
|
+
},
|
|
129
|
+
timeout: {
|
|
130
|
+
type: "number",
|
|
131
|
+
description: "Execution timeout in milliseconds",
|
|
132
|
+
default: 3e4
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
required: ["code"]
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "search_tools",
|
|
140
|
+
description: "Search and discover available MCP tools across all servers. Use this to find out what tools are available before writing code. Returns tool information including names, descriptions, and schemas. Can filter by query and control detail level.",
|
|
141
|
+
inputSchema: {
|
|
142
|
+
type: "object",
|
|
143
|
+
properties: {
|
|
144
|
+
query: {
|
|
145
|
+
type: "string",
|
|
146
|
+
description: "Search query to filter tools by name or description",
|
|
147
|
+
default: ""
|
|
148
|
+
},
|
|
149
|
+
detail_level: {
|
|
150
|
+
type: "string",
|
|
151
|
+
description: "Detail level: 'names', 'descriptions', or 'full'",
|
|
152
|
+
enum: ["names", "descriptions", "full"],
|
|
153
|
+
default: "full"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
// Override tools getter to return static list immediately
|
|
161
|
+
get tools() {
|
|
162
|
+
return this._tools;
|
|
163
|
+
}
|
|
164
|
+
async initialize() {
|
|
165
|
+
this.toolsCache = this._tools;
|
|
166
|
+
return { capabilities: {}, version: "1.0.0" };
|
|
167
|
+
}
|
|
168
|
+
async callTool(name, args) {
|
|
169
|
+
if (name === "execute_code") {
|
|
170
|
+
const code = args.code;
|
|
171
|
+
const timeout = args.timeout || 3e4;
|
|
172
|
+
const result = await this.mcpClient.executeCode(code, timeout);
|
|
173
|
+
return {
|
|
174
|
+
content: [
|
|
175
|
+
{
|
|
176
|
+
type: "text",
|
|
177
|
+
text: JSON.stringify(result)
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
};
|
|
181
|
+
} else if (name === "search_tools") {
|
|
182
|
+
const query = args.query || "";
|
|
183
|
+
const detailLevel = args.detail_level;
|
|
184
|
+
const result = await this.mcpClient.searchTools(
|
|
185
|
+
query,
|
|
186
|
+
detailLevel && detailLevel in ["names", "descriptions", "full"] ? detailLevel : "full"
|
|
187
|
+
);
|
|
188
|
+
return {
|
|
189
|
+
content: [
|
|
190
|
+
{
|
|
191
|
+
type: "text",
|
|
192
|
+
text: JSON.stringify(result)
|
|
193
|
+
}
|
|
194
|
+
]
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// src/client/prompts.ts
|
|
202
|
+
var PROMPTS = {
|
|
203
|
+
CODE_MODE: CODE_MODE_AGENT_PROMPT
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export {
|
|
207
|
+
CodeModeConnector,
|
|
208
|
+
PROMPTS
|
|
209
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__name
|
|
3
|
+
} from "./chunk-3GQAWCBQ.js";
|
|
4
|
+
|
|
5
|
+
// src/agents/base.ts
|
|
6
|
+
var BaseAgent = class {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "BaseAgent");
|
|
9
|
+
}
|
|
10
|
+
session;
|
|
11
|
+
/**
|
|
12
|
+
* @param session MCP session used for tool invocation
|
|
13
|
+
*/
|
|
14
|
+
constructor(session) {
|
|
15
|
+
this.session = session;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
BaseAgent
|
|
21
|
+
};
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__name
|
|
3
|
+
} from "./chunk-3GQAWCBQ.js";
|
|
4
|
+
|
|
5
|
+
// src/agents/display.ts
|
|
6
|
+
import chalk from "chalk";
|
|
7
|
+
import { highlight } from "cli-highlight";
|
|
8
|
+
import stripAnsiLib from "strip-ansi";
|
|
9
|
+
var TERMINAL_WIDTH = process.stdout.columns || 120;
|
|
10
|
+
function stripAnsi(str) {
|
|
11
|
+
return stripAnsiLib(str);
|
|
12
|
+
}
|
|
13
|
+
__name(stripAnsi, "stripAnsi");
|
|
14
|
+
function wrapAnsiLine(line, maxWidth) {
|
|
15
|
+
const stripped = stripAnsi(line);
|
|
16
|
+
if (stripped.length <= maxWidth) return [line];
|
|
17
|
+
const result = [];
|
|
18
|
+
let visibleCount = 0;
|
|
19
|
+
let current = "";
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (i < line.length) {
|
|
22
|
+
const char = line[i];
|
|
23
|
+
if (char === "\x1B") {
|
|
24
|
+
let sequence = char;
|
|
25
|
+
i++;
|
|
26
|
+
while (i < line.length) {
|
|
27
|
+
const nextChar = line[i];
|
|
28
|
+
sequence += nextChar;
|
|
29
|
+
i++;
|
|
30
|
+
if (nextChar === "m") break;
|
|
31
|
+
}
|
|
32
|
+
current += sequence;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
current += char;
|
|
36
|
+
visibleCount++;
|
|
37
|
+
i++;
|
|
38
|
+
if (visibleCount >= maxWidth) {
|
|
39
|
+
result.push(current);
|
|
40
|
+
current = "";
|
|
41
|
+
visibleCount = 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (current) result.push(current);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
__name(wrapAnsiLine, "wrapAnsiLine");
|
|
48
|
+
function printBox(content, title, language, bgColor = false) {
|
|
49
|
+
const width = TERMINAL_WIDTH;
|
|
50
|
+
let displayContent = content;
|
|
51
|
+
if (language) {
|
|
52
|
+
try {
|
|
53
|
+
displayContent = highlight(content, { language, ignoreIllegals: true });
|
|
54
|
+
} catch {
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const lines = displayContent.split("\n").flatMap((line) => wrapAnsiLine(line, width - 4));
|
|
58
|
+
console.log(chalk.gray("\u250C" + "\u2500".repeat(width - 2) + "\u2510"));
|
|
59
|
+
if (title) {
|
|
60
|
+
const stripped = stripAnsi(title);
|
|
61
|
+
const lineText = `${title} `;
|
|
62
|
+
const padding = Math.max(0, width - 4 - stripped.length - 2);
|
|
63
|
+
console.log(
|
|
64
|
+
chalk.gray("\u2502 ") + chalk.bold.white(lineText) + " ".repeat(padding) + chalk.gray(" \u2502")
|
|
65
|
+
);
|
|
66
|
+
console.log(chalk.gray("\u251C" + "\u2500".repeat(width - 2) + "\u2524"));
|
|
67
|
+
}
|
|
68
|
+
lines.forEach((line) => {
|
|
69
|
+
const stripped = stripAnsi(line);
|
|
70
|
+
const padding = Math.max(0, width - 4 - stripped.length);
|
|
71
|
+
const finalLine = bgColor ? chalk.bgGray(line + " ".repeat(padding)) : line + " ".repeat(padding);
|
|
72
|
+
console.log(chalk.gray("\u2502 ") + finalLine + chalk.gray(" \u2502"));
|
|
73
|
+
});
|
|
74
|
+
console.log(chalk.gray("\u2514" + "\u2500".repeat(width - 2) + "\u2518"));
|
|
75
|
+
}
|
|
76
|
+
__name(printBox, "printBox");
|
|
77
|
+
function extractCodeFromToolInput(input) {
|
|
78
|
+
if (typeof input === "object" && input !== null && "code" in input) {
|
|
79
|
+
const inputObj = input;
|
|
80
|
+
return typeof inputObj.code === "string" ? inputObj.code : null;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
__name(extractCodeFromToolInput, "extractCodeFromToolInput");
|
|
85
|
+
function isExecuteCodeResult(obj) {
|
|
86
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
87
|
+
const result = obj;
|
|
88
|
+
return "result" in result && "logs" in result && Array.isArray(result.logs) && "execution_time" in result && typeof result.execution_time === "number" && "error" in result && (typeof result.error === "string" || result.error === null);
|
|
89
|
+
}
|
|
90
|
+
__name(isExecuteCodeResult, "isExecuteCodeResult");
|
|
91
|
+
function parseExecuteCodeResult(output) {
|
|
92
|
+
try {
|
|
93
|
+
if (typeof output === "string") {
|
|
94
|
+
const parsed = JSON.parse(output);
|
|
95
|
+
if (isExecuteCodeResult(parsed)) {
|
|
96
|
+
return parsed;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (isExecuteCodeResult(output)) {
|
|
100
|
+
return output;
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
__name(parseExecuteCodeResult, "parseExecuteCodeResult");
|
|
107
|
+
function renderContent(content) {
|
|
108
|
+
if (content === null || content === void 0) {
|
|
109
|
+
return "null";
|
|
110
|
+
}
|
|
111
|
+
if (typeof content === "object") {
|
|
112
|
+
return JSON.stringify(content, null, 2);
|
|
113
|
+
}
|
|
114
|
+
return String(content);
|
|
115
|
+
}
|
|
116
|
+
__name(renderContent, "renderContent");
|
|
117
|
+
function unwrapToolInput(input) {
|
|
118
|
+
if (typeof input === "object" && input !== null && "input" in input) {
|
|
119
|
+
const inputObj = input;
|
|
120
|
+
if (typeof inputObj.input === "string") {
|
|
121
|
+
try {
|
|
122
|
+
return JSON.parse(inputObj.input);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return inputObj.input;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return input;
|
|
129
|
+
}
|
|
130
|
+
__name(unwrapToolInput, "unwrapToolInput");
|
|
131
|
+
function handleToolStart(event) {
|
|
132
|
+
const toolName = event.name || "unknown";
|
|
133
|
+
let input = event.data?.input || {};
|
|
134
|
+
input = unwrapToolInput(input);
|
|
135
|
+
const code = extractCodeFromToolInput(input);
|
|
136
|
+
if (code) {
|
|
137
|
+
printBox(code, `${toolName} - input`, "javascript", false);
|
|
138
|
+
const otherParams = { ...input };
|
|
139
|
+
delete otherParams.code;
|
|
140
|
+
if (Object.keys(otherParams).length > 0) {
|
|
141
|
+
printBox(renderContent(otherParams), "Other Parameters", "json", false);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
printBox(renderContent(input), `${toolName} - input`, "json", false);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
__name(handleToolStart, "handleToolStart");
|
|
148
|
+
function extractToolMessageContent(output) {
|
|
149
|
+
try {
|
|
150
|
+
if (typeof output === "object" && output !== null && "name" in output && "content" in output) {
|
|
151
|
+
const outputObj = output;
|
|
152
|
+
const toolName = (typeof outputObj.name === "string" ? outputObj.name : null) || "unknown";
|
|
153
|
+
const lcKwargs = outputObj.lc_kwargs;
|
|
154
|
+
const status = lcKwargs?.status || outputObj.status || "unknown";
|
|
155
|
+
let content = outputObj.content;
|
|
156
|
+
if (typeof content === "string") {
|
|
157
|
+
try {
|
|
158
|
+
content = JSON.parse(content);
|
|
159
|
+
} catch (e) {
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return { toolName, status, content };
|
|
163
|
+
}
|
|
164
|
+
} catch (e) {
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
__name(extractToolMessageContent, "extractToolMessageContent");
|
|
169
|
+
function formatSearchToolsAsTree(tools) {
|
|
170
|
+
if (!Array.isArray(tools) || tools.length === 0) {
|
|
171
|
+
return "(no tools found)";
|
|
172
|
+
}
|
|
173
|
+
const toolsByServer = {};
|
|
174
|
+
for (const tool of tools) {
|
|
175
|
+
const server = tool.server || "unknown";
|
|
176
|
+
if (!toolsByServer[server]) {
|
|
177
|
+
toolsByServer[server] = [];
|
|
178
|
+
}
|
|
179
|
+
toolsByServer[server].push(tool);
|
|
180
|
+
}
|
|
181
|
+
const lines = [];
|
|
182
|
+
const servers = Object.keys(toolsByServer).sort();
|
|
183
|
+
for (let i = 0; i < servers.length; i++) {
|
|
184
|
+
const server = servers[i];
|
|
185
|
+
const serverTools = toolsByServer[server];
|
|
186
|
+
const isLastServer = i === servers.length - 1;
|
|
187
|
+
const serverPrefix = isLastServer ? "\u2514\u2500" : "\u251C\u2500";
|
|
188
|
+
lines.push(
|
|
189
|
+
`${serverPrefix} ${chalk.cyan(server)} (${serverTools.length} tools)`
|
|
190
|
+
);
|
|
191
|
+
for (let j = 0; j < serverTools.length; j++) {
|
|
192
|
+
const tool = serverTools[j];
|
|
193
|
+
const isLastTool = j === serverTools.length - 1;
|
|
194
|
+
const indent = isLastServer ? " " : "\u2502 ";
|
|
195
|
+
const toolPrefix = isLastTool ? "\u2514\u2500" : "\u251C\u2500";
|
|
196
|
+
let toolLine = `${indent}${toolPrefix} ${tool.name}`;
|
|
197
|
+
if (tool.description) {
|
|
198
|
+
toolLine += chalk.dim(` - ${tool.description}`);
|
|
199
|
+
}
|
|
200
|
+
lines.push(toolLine);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return lines.join("\n");
|
|
204
|
+
}
|
|
205
|
+
__name(formatSearchToolsAsTree, "formatSearchToolsAsTree");
|
|
206
|
+
function handleToolEnd(event) {
|
|
207
|
+
const output = event.data?.output;
|
|
208
|
+
const toolMessage = extractToolMessageContent(output);
|
|
209
|
+
if (toolMessage) {
|
|
210
|
+
const { toolName, status, content } = toolMessage;
|
|
211
|
+
if (toolName === "execute_code") {
|
|
212
|
+
let actualContent = content;
|
|
213
|
+
if (typeof content === "object" && content !== null && "content" in content) {
|
|
214
|
+
const innerContent = content.content;
|
|
215
|
+
if (Array.isArray(innerContent) && innerContent.length > 0) {
|
|
216
|
+
if (innerContent[0].type === "text" && innerContent[0].text) {
|
|
217
|
+
actualContent = innerContent[0].text;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const execResult2 = parseExecuteCodeResult(actualContent);
|
|
222
|
+
if (execResult2) {
|
|
223
|
+
const timeMs = execResult2.execution_time ? Math.round(execResult2.execution_time * 1e3) : 0;
|
|
224
|
+
const timeStr = `${timeMs}ms`;
|
|
225
|
+
const isError2 = execResult2.error !== null && execResult2.error !== void 0 && execResult2.error !== "";
|
|
226
|
+
const statusText = isError2 ? chalk.red("error") : chalk.green("success");
|
|
227
|
+
const title2 = `${toolName} - ${statusText} - ${timeStr}`;
|
|
228
|
+
if (execResult2.result !== null && execResult2.result !== void 0) {
|
|
229
|
+
const resultStr = renderContent(execResult2.result);
|
|
230
|
+
const language3 = typeof execResult2.result === "object" ? "json" : void 0;
|
|
231
|
+
printBox(resultStr, title2, language3, false);
|
|
232
|
+
} else {
|
|
233
|
+
printBox("(no result)", title2, void 0, false);
|
|
234
|
+
}
|
|
235
|
+
if (execResult2.logs && execResult2.logs.length > 0) {
|
|
236
|
+
printBox(execResult2.logs.join("\n"), `Logs`, void 0, false);
|
|
237
|
+
}
|
|
238
|
+
if (execResult2.error) {
|
|
239
|
+
printBox(execResult2.error, chalk.red("Error"), void 0, false);
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (toolName === "search_tools") {
|
|
245
|
+
let actualContent = content;
|
|
246
|
+
if (typeof content === "object" && content !== null && !Array.isArray(content) && "content" in content) {
|
|
247
|
+
const innerContent = content.content;
|
|
248
|
+
if (Array.isArray(innerContent) && innerContent.length > 0) {
|
|
249
|
+
if (innerContent[0].type === "text" && innerContent[0].text) {
|
|
250
|
+
try {
|
|
251
|
+
actualContent = JSON.parse(innerContent[0].text);
|
|
252
|
+
} catch (e) {
|
|
253
|
+
actualContent = innerContent[0].text;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (Array.isArray(actualContent)) {
|
|
259
|
+
const treeStr = formatSearchToolsAsTree(actualContent);
|
|
260
|
+
const statusText = status === "success" ? chalk.green("Success") : chalk.red("Error");
|
|
261
|
+
const title2 = `${statusText}: ${toolName} - Result`;
|
|
262
|
+
printBox(treeStr, title2, void 0, false);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const contentObj = typeof content === "object" && content !== null ? content : null;
|
|
267
|
+
const isError = contentObj && "isError" in contentObj && contentObj.isError === true || status === "error";
|
|
268
|
+
let displayContent = content;
|
|
269
|
+
if (typeof content === "object" && content !== null && "content" in content) {
|
|
270
|
+
displayContent = content.content;
|
|
271
|
+
if (Array.isArray(displayContent) && displayContent.length > 0) {
|
|
272
|
+
if (displayContent[0].type === "text" && displayContent[0].text) {
|
|
273
|
+
displayContent = displayContent[0].text;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const contentStr = renderContent(displayContent);
|
|
278
|
+
const language2 = typeof displayContent === "object" ? "json" : void 0;
|
|
279
|
+
const statusLabel = status === "success" ? chalk.green("Success") : isError ? chalk.red("Error") : "Result";
|
|
280
|
+
const title = `${statusLabel}: ${toolName} - Result`;
|
|
281
|
+
printBox(contentStr, title, language2, false);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const execResult = parseExecuteCodeResult(output);
|
|
285
|
+
if (execResult) {
|
|
286
|
+
const timeMs = execResult.execution_time ? Math.round(execResult.execution_time * 1e3) : 0;
|
|
287
|
+
const timeStr = `${timeMs}ms`;
|
|
288
|
+
if (execResult.result !== null && execResult.result !== void 0) {
|
|
289
|
+
const resultStr = renderContent(execResult.result);
|
|
290
|
+
const language2 = typeof execResult.result === "object" ? "json" : void 0;
|
|
291
|
+
printBox(resultStr, `Result - ${timeStr}`, language2, false);
|
|
292
|
+
}
|
|
293
|
+
if (execResult.logs && execResult.logs.length > 0) {
|
|
294
|
+
printBox(execResult.logs.join("\n"), `Logs`, void 0, false);
|
|
295
|
+
}
|
|
296
|
+
if (execResult.error) {
|
|
297
|
+
printBox(execResult.error, chalk.red("Error"), void 0, false);
|
|
298
|
+
}
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const outputStr = renderContent(output);
|
|
302
|
+
const language = typeof output === "object" ? "json" : void 0;
|
|
303
|
+
printBox(outputStr, "Result", language, false);
|
|
304
|
+
}
|
|
305
|
+
__name(handleToolEnd, "handleToolEnd");
|
|
306
|
+
async function* prettyStreamEvents(streamEventsGenerator) {
|
|
307
|
+
let finalResponse = "";
|
|
308
|
+
let isFirstTextChunk = true;
|
|
309
|
+
let hasStreamedText = false;
|
|
310
|
+
for await (const event of streamEventsGenerator) {
|
|
311
|
+
if (event.event === "on_tool_start") {
|
|
312
|
+
if (hasStreamedText) {
|
|
313
|
+
process.stdout.write("\n");
|
|
314
|
+
hasStreamedText = false;
|
|
315
|
+
isFirstTextChunk = true;
|
|
316
|
+
}
|
|
317
|
+
handleToolStart(event);
|
|
318
|
+
} else if (event.event === "on_tool_end") {
|
|
319
|
+
handleToolEnd(event);
|
|
320
|
+
} else if (event.event === "on_chat_model_stream") {
|
|
321
|
+
if (event.data?.chunk?.text) {
|
|
322
|
+
const text = event.data.chunk.text;
|
|
323
|
+
if (typeof text === "string" && text.length > 0) {
|
|
324
|
+
if (isFirstTextChunk) {
|
|
325
|
+
process.stdout.write("\n\u{1F916} ");
|
|
326
|
+
isFirstTextChunk = false;
|
|
327
|
+
}
|
|
328
|
+
process.stdout.write(text);
|
|
329
|
+
finalResponse += text;
|
|
330
|
+
hasStreamedText = true;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
yield;
|
|
335
|
+
}
|
|
336
|
+
return finalResponse;
|
|
337
|
+
}
|
|
338
|
+
__name(prettyStreamEvents, "prettyStreamEvents");
|
|
339
|
+
export {
|
|
340
|
+
extractCodeFromToolInput,
|
|
341
|
+
extractToolMessageContent,
|
|
342
|
+
formatSearchToolsAsTree,
|
|
343
|
+
handleToolEnd,
|
|
344
|
+
handleToolStart,
|
|
345
|
+
parseExecuteCodeResult,
|
|
346
|
+
prettyStreamEvents,
|
|
347
|
+
printBox,
|
|
348
|
+
renderContent,
|
|
349
|
+
unwrapToolInput
|
|
350
|
+
};
|