mcp-use 1.3.4-canary.2 → 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 +686 -22
- 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 +10 -8
- 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,54 @@
|
|
|
1
|
+
import type { StreamEvent } from "@langchain/core/tracers/log_stream";
|
|
2
|
+
interface ExecuteCodeResult {
|
|
3
|
+
result: unknown;
|
|
4
|
+
logs: string[];
|
|
5
|
+
error: string | null;
|
|
6
|
+
execution_time: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function printBox(content: string, title?: string, language?: string, bgColor?: boolean): void;
|
|
9
|
+
/**
|
|
10
|
+
* Extract code from tool input if present
|
|
11
|
+
*/
|
|
12
|
+
export declare function extractCodeFromToolInput(input: unknown): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Parse execute_code tool result
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseExecuteCodeResult(output: unknown): ExecuteCodeResult | null;
|
|
17
|
+
/**
|
|
18
|
+
* Render content with appropriate formatting
|
|
19
|
+
*/
|
|
20
|
+
export declare function renderContent(content: unknown): string;
|
|
21
|
+
/**
|
|
22
|
+
* Unwrap tool input if it's wrapped in an "input" field with JSON string
|
|
23
|
+
*/
|
|
24
|
+
export declare function unwrapToolInput(input: unknown): unknown;
|
|
25
|
+
/**
|
|
26
|
+
* Handle tool start event with pretty printing
|
|
27
|
+
*/
|
|
28
|
+
export declare function handleToolStart(event: StreamEvent): void;
|
|
29
|
+
/**
|
|
30
|
+
* Extract content from LangChain ToolMessage structure
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractToolMessageContent(output: unknown): {
|
|
33
|
+
toolName: string;
|
|
34
|
+
status: string;
|
|
35
|
+
content: unknown;
|
|
36
|
+
} | null;
|
|
37
|
+
/**
|
|
38
|
+
* Format search_tools result as a tree structure
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatSearchToolsAsTree(tools: Array<{
|
|
41
|
+
server: string;
|
|
42
|
+
name: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
}>): string;
|
|
45
|
+
/**
|
|
46
|
+
* Handle tool end event with pretty printing
|
|
47
|
+
*/
|
|
48
|
+
export declare function handleToolEnd(event: StreamEvent): void;
|
|
49
|
+
/**
|
|
50
|
+
* Stream events with pretty printing
|
|
51
|
+
*/
|
|
52
|
+
export declare function prettyStreamEvents(streamEventsGenerator: AsyncGenerator<StreamEvent, void, void>): AsyncGenerator<void, string, void>;
|
|
53
|
+
export {};
|
|
54
|
+
//# sourceMappingURL=display.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"display.d.ts","sourceRoot":"","sources":["../../../src/agents/display.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAWtE,UAAU,iBAAiB;IACzB,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;CACxB;AAkDD,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM,EACjB,OAAO,UAAQ,QA2ChB;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAMtE;AAmBD;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,OAAO,GACd,iBAAiB,GAAG,IAAI,CAiB1B;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAUtD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAevD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,QAqBjD;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,OAAO,GACd;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,CAsC/D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,KAAK,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,GACnE,MAAM,CAgDR;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,QAyK/C;AAED;;GAEG;AACH,wBAAuB,kBAAkB,CACvC,qBAAqB,EAAE,cAAc,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,GAC7D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,CAoCpC"}
|
|
@@ -559,15 +559,467 @@ var init_langfuse = __esm({
|
|
|
559
559
|
}
|
|
560
560
|
});
|
|
561
561
|
|
|
562
|
+
// src/agents/display.ts
|
|
563
|
+
var display_exports = {};
|
|
564
|
+
__export(display_exports, {
|
|
565
|
+
extractCodeFromToolInput: () => extractCodeFromToolInput,
|
|
566
|
+
extractToolMessageContent: () => extractToolMessageContent,
|
|
567
|
+
formatSearchToolsAsTree: () => formatSearchToolsAsTree,
|
|
568
|
+
handleToolEnd: () => handleToolEnd,
|
|
569
|
+
handleToolStart: () => handleToolStart,
|
|
570
|
+
parseExecuteCodeResult: () => parseExecuteCodeResult,
|
|
571
|
+
prettyStreamEvents: () => prettyStreamEvents,
|
|
572
|
+
printBox: () => printBox,
|
|
573
|
+
renderContent: () => renderContent,
|
|
574
|
+
unwrapToolInput: () => unwrapToolInput
|
|
575
|
+
});
|
|
576
|
+
function stripAnsi(str) {
|
|
577
|
+
return (0, import_strip_ansi.default)(str);
|
|
578
|
+
}
|
|
579
|
+
function wrapAnsiLine(line, maxWidth) {
|
|
580
|
+
const stripped = stripAnsi(line);
|
|
581
|
+
if (stripped.length <= maxWidth) return [line];
|
|
582
|
+
const result = [];
|
|
583
|
+
let visibleCount = 0;
|
|
584
|
+
let current = "";
|
|
585
|
+
let i = 0;
|
|
586
|
+
while (i < line.length) {
|
|
587
|
+
const char = line[i];
|
|
588
|
+
if (char === "\x1B") {
|
|
589
|
+
let sequence = char;
|
|
590
|
+
i++;
|
|
591
|
+
while (i < line.length) {
|
|
592
|
+
const nextChar = line[i];
|
|
593
|
+
sequence += nextChar;
|
|
594
|
+
i++;
|
|
595
|
+
if (nextChar === "m") break;
|
|
596
|
+
}
|
|
597
|
+
current += sequence;
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
current += char;
|
|
601
|
+
visibleCount++;
|
|
602
|
+
i++;
|
|
603
|
+
if (visibleCount >= maxWidth) {
|
|
604
|
+
result.push(current);
|
|
605
|
+
current = "";
|
|
606
|
+
visibleCount = 0;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (current) result.push(current);
|
|
610
|
+
return result;
|
|
611
|
+
}
|
|
612
|
+
function printBox(content, title, language, bgColor = false) {
|
|
613
|
+
const width = TERMINAL_WIDTH;
|
|
614
|
+
let displayContent = content;
|
|
615
|
+
if (language) {
|
|
616
|
+
try {
|
|
617
|
+
displayContent = (0, import_cli_highlight.highlight)(content, { language, ignoreIllegals: true });
|
|
618
|
+
} catch {
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
const lines = displayContent.split("\n").flatMap((line) => wrapAnsiLine(line, width - 4));
|
|
622
|
+
console.log(import_chalk.default.gray("\u250C" + "\u2500".repeat(width - 2) + "\u2510"));
|
|
623
|
+
if (title) {
|
|
624
|
+
const stripped = stripAnsi(title);
|
|
625
|
+
const lineText = `${title} `;
|
|
626
|
+
const padding = Math.max(0, width - 4 - stripped.length - 2);
|
|
627
|
+
console.log(
|
|
628
|
+
import_chalk.default.gray("\u2502 ") + import_chalk.default.bold.white(lineText) + " ".repeat(padding) + import_chalk.default.gray(" \u2502")
|
|
629
|
+
);
|
|
630
|
+
console.log(import_chalk.default.gray("\u251C" + "\u2500".repeat(width - 2) + "\u2524"));
|
|
631
|
+
}
|
|
632
|
+
lines.forEach((line) => {
|
|
633
|
+
const stripped = stripAnsi(line);
|
|
634
|
+
const padding = Math.max(0, width - 4 - stripped.length);
|
|
635
|
+
const finalLine = bgColor ? import_chalk.default.bgGray(line + " ".repeat(padding)) : line + " ".repeat(padding);
|
|
636
|
+
console.log(import_chalk.default.gray("\u2502 ") + finalLine + import_chalk.default.gray(" \u2502"));
|
|
637
|
+
});
|
|
638
|
+
console.log(import_chalk.default.gray("\u2514" + "\u2500".repeat(width - 2) + "\u2518"));
|
|
639
|
+
}
|
|
640
|
+
function extractCodeFromToolInput(input) {
|
|
641
|
+
if (typeof input === "object" && input !== null && "code" in input) {
|
|
642
|
+
const inputObj = input;
|
|
643
|
+
return typeof inputObj.code === "string" ? inputObj.code : null;
|
|
644
|
+
}
|
|
645
|
+
return null;
|
|
646
|
+
}
|
|
647
|
+
function isExecuteCodeResult(obj) {
|
|
648
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
649
|
+
const result = obj;
|
|
650
|
+
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);
|
|
651
|
+
}
|
|
652
|
+
function parseExecuteCodeResult(output) {
|
|
653
|
+
try {
|
|
654
|
+
if (typeof output === "string") {
|
|
655
|
+
const parsed = JSON.parse(output);
|
|
656
|
+
if (isExecuteCodeResult(parsed)) {
|
|
657
|
+
return parsed;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
if (isExecuteCodeResult(output)) {
|
|
661
|
+
return output;
|
|
662
|
+
}
|
|
663
|
+
} catch (e) {
|
|
664
|
+
}
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
function renderContent(content) {
|
|
668
|
+
if (content === null || content === void 0) {
|
|
669
|
+
return "null";
|
|
670
|
+
}
|
|
671
|
+
if (typeof content === "object") {
|
|
672
|
+
return JSON.stringify(content, null, 2);
|
|
673
|
+
}
|
|
674
|
+
return String(content);
|
|
675
|
+
}
|
|
676
|
+
function unwrapToolInput(input) {
|
|
677
|
+
if (typeof input === "object" && input !== null && "input" in input) {
|
|
678
|
+
const inputObj = input;
|
|
679
|
+
if (typeof inputObj.input === "string") {
|
|
680
|
+
try {
|
|
681
|
+
return JSON.parse(inputObj.input);
|
|
682
|
+
} catch (e) {
|
|
683
|
+
return inputObj.input;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return input;
|
|
688
|
+
}
|
|
689
|
+
function handleToolStart(event) {
|
|
690
|
+
const toolName = event.name || "unknown";
|
|
691
|
+
let input = event.data?.input || {};
|
|
692
|
+
input = unwrapToolInput(input);
|
|
693
|
+
const code = extractCodeFromToolInput(input);
|
|
694
|
+
if (code) {
|
|
695
|
+
printBox(code, `${toolName} - input`, "javascript", false);
|
|
696
|
+
const otherParams = { ...input };
|
|
697
|
+
delete otherParams.code;
|
|
698
|
+
if (Object.keys(otherParams).length > 0) {
|
|
699
|
+
printBox(renderContent(otherParams), "Other Parameters", "json", false);
|
|
700
|
+
}
|
|
701
|
+
} else {
|
|
702
|
+
printBox(renderContent(input), `${toolName} - input`, "json", false);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function extractToolMessageContent(output) {
|
|
706
|
+
try {
|
|
707
|
+
if (typeof output === "object" && output !== null && "name" in output && "content" in output) {
|
|
708
|
+
const outputObj = output;
|
|
709
|
+
const toolName = (typeof outputObj.name === "string" ? outputObj.name : null) || "unknown";
|
|
710
|
+
const lcKwargs = outputObj.lc_kwargs;
|
|
711
|
+
const status = lcKwargs?.status || outputObj.status || "unknown";
|
|
712
|
+
let content = outputObj.content;
|
|
713
|
+
if (typeof content === "string") {
|
|
714
|
+
try {
|
|
715
|
+
content = JSON.parse(content);
|
|
716
|
+
} catch (e) {
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return { toolName, status, content };
|
|
720
|
+
}
|
|
721
|
+
} catch (e) {
|
|
722
|
+
}
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
function formatSearchToolsAsTree(tools) {
|
|
726
|
+
if (!Array.isArray(tools) || tools.length === 0) {
|
|
727
|
+
return "(no tools found)";
|
|
728
|
+
}
|
|
729
|
+
const toolsByServer = {};
|
|
730
|
+
for (const tool of tools) {
|
|
731
|
+
const server = tool.server || "unknown";
|
|
732
|
+
if (!toolsByServer[server]) {
|
|
733
|
+
toolsByServer[server] = [];
|
|
734
|
+
}
|
|
735
|
+
toolsByServer[server].push(tool);
|
|
736
|
+
}
|
|
737
|
+
const lines = [];
|
|
738
|
+
const servers = Object.keys(toolsByServer).sort();
|
|
739
|
+
for (let i = 0; i < servers.length; i++) {
|
|
740
|
+
const server = servers[i];
|
|
741
|
+
const serverTools = toolsByServer[server];
|
|
742
|
+
const isLastServer = i === servers.length - 1;
|
|
743
|
+
const serverPrefix = isLastServer ? "\u2514\u2500" : "\u251C\u2500";
|
|
744
|
+
lines.push(
|
|
745
|
+
`${serverPrefix} ${import_chalk.default.cyan(server)} (${serverTools.length} tools)`
|
|
746
|
+
);
|
|
747
|
+
for (let j = 0; j < serverTools.length; j++) {
|
|
748
|
+
const tool = serverTools[j];
|
|
749
|
+
const isLastTool = j === serverTools.length - 1;
|
|
750
|
+
const indent = isLastServer ? " " : "\u2502 ";
|
|
751
|
+
const toolPrefix = isLastTool ? "\u2514\u2500" : "\u251C\u2500";
|
|
752
|
+
let toolLine = `${indent}${toolPrefix} ${tool.name}`;
|
|
753
|
+
if (tool.description) {
|
|
754
|
+
toolLine += import_chalk.default.dim(` - ${tool.description}`);
|
|
755
|
+
}
|
|
756
|
+
lines.push(toolLine);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return lines.join("\n");
|
|
760
|
+
}
|
|
761
|
+
function handleToolEnd(event) {
|
|
762
|
+
const output = event.data?.output;
|
|
763
|
+
const toolMessage = extractToolMessageContent(output);
|
|
764
|
+
if (toolMessage) {
|
|
765
|
+
const { toolName, status, content } = toolMessage;
|
|
766
|
+
if (toolName === "execute_code") {
|
|
767
|
+
let actualContent = content;
|
|
768
|
+
if (typeof content === "object" && content !== null && "content" in content) {
|
|
769
|
+
const innerContent = content.content;
|
|
770
|
+
if (Array.isArray(innerContent) && innerContent.length > 0) {
|
|
771
|
+
if (innerContent[0].type === "text" && innerContent[0].text) {
|
|
772
|
+
actualContent = innerContent[0].text;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
const execResult2 = parseExecuteCodeResult(actualContent);
|
|
777
|
+
if (execResult2) {
|
|
778
|
+
const timeMs = execResult2.execution_time ? Math.round(execResult2.execution_time * 1e3) : 0;
|
|
779
|
+
const timeStr = `${timeMs}ms`;
|
|
780
|
+
const isError2 = execResult2.error !== null && execResult2.error !== void 0 && execResult2.error !== "";
|
|
781
|
+
const statusText = isError2 ? import_chalk.default.red("error") : import_chalk.default.green("success");
|
|
782
|
+
const title2 = `${toolName} - ${statusText} - ${timeStr}`;
|
|
783
|
+
if (execResult2.result !== null && execResult2.result !== void 0) {
|
|
784
|
+
const resultStr = renderContent(execResult2.result);
|
|
785
|
+
const language3 = typeof execResult2.result === "object" ? "json" : void 0;
|
|
786
|
+
printBox(resultStr, title2, language3, false);
|
|
787
|
+
} else {
|
|
788
|
+
printBox("(no result)", title2, void 0, false);
|
|
789
|
+
}
|
|
790
|
+
if (execResult2.logs && execResult2.logs.length > 0) {
|
|
791
|
+
printBox(execResult2.logs.join("\n"), `Logs`, void 0, false);
|
|
792
|
+
}
|
|
793
|
+
if (execResult2.error) {
|
|
794
|
+
printBox(execResult2.error, import_chalk.default.red("Error"), void 0, false);
|
|
795
|
+
}
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (toolName === "search_tools") {
|
|
800
|
+
let actualContent = content;
|
|
801
|
+
if (typeof content === "object" && content !== null && !Array.isArray(content) && "content" in content) {
|
|
802
|
+
const innerContent = content.content;
|
|
803
|
+
if (Array.isArray(innerContent) && innerContent.length > 0) {
|
|
804
|
+
if (innerContent[0].type === "text" && innerContent[0].text) {
|
|
805
|
+
try {
|
|
806
|
+
actualContent = JSON.parse(innerContent[0].text);
|
|
807
|
+
} catch (e) {
|
|
808
|
+
actualContent = innerContent[0].text;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
if (Array.isArray(actualContent)) {
|
|
814
|
+
const treeStr = formatSearchToolsAsTree(actualContent);
|
|
815
|
+
const statusText = status === "success" ? import_chalk.default.green("Success") : import_chalk.default.red("Error");
|
|
816
|
+
const title2 = `${statusText}: ${toolName} - Result`;
|
|
817
|
+
printBox(treeStr, title2, void 0, false);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
const contentObj = typeof content === "object" && content !== null ? content : null;
|
|
822
|
+
const isError = contentObj && "isError" in contentObj && contentObj.isError === true || status === "error";
|
|
823
|
+
let displayContent = content;
|
|
824
|
+
if (typeof content === "object" && content !== null && "content" in content) {
|
|
825
|
+
displayContent = content.content;
|
|
826
|
+
if (Array.isArray(displayContent) && displayContent.length > 0) {
|
|
827
|
+
if (displayContent[0].type === "text" && displayContent[0].text) {
|
|
828
|
+
displayContent = displayContent[0].text;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
const contentStr = renderContent(displayContent);
|
|
833
|
+
const language2 = typeof displayContent === "object" ? "json" : void 0;
|
|
834
|
+
const statusLabel = status === "success" ? import_chalk.default.green("Success") : isError ? import_chalk.default.red("Error") : "Result";
|
|
835
|
+
const title = `${statusLabel}: ${toolName} - Result`;
|
|
836
|
+
printBox(contentStr, title, language2, false);
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const execResult = parseExecuteCodeResult(output);
|
|
840
|
+
if (execResult) {
|
|
841
|
+
const timeMs = execResult.execution_time ? Math.round(execResult.execution_time * 1e3) : 0;
|
|
842
|
+
const timeStr = `${timeMs}ms`;
|
|
843
|
+
if (execResult.result !== null && execResult.result !== void 0) {
|
|
844
|
+
const resultStr = renderContent(execResult.result);
|
|
845
|
+
const language2 = typeof execResult.result === "object" ? "json" : void 0;
|
|
846
|
+
printBox(resultStr, `Result - ${timeStr}`, language2, false);
|
|
847
|
+
}
|
|
848
|
+
if (execResult.logs && execResult.logs.length > 0) {
|
|
849
|
+
printBox(execResult.logs.join("\n"), `Logs`, void 0, false);
|
|
850
|
+
}
|
|
851
|
+
if (execResult.error) {
|
|
852
|
+
printBox(execResult.error, import_chalk.default.red("Error"), void 0, false);
|
|
853
|
+
}
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const outputStr = renderContent(output);
|
|
857
|
+
const language = typeof output === "object" ? "json" : void 0;
|
|
858
|
+
printBox(outputStr, "Result", language, false);
|
|
859
|
+
}
|
|
860
|
+
async function* prettyStreamEvents(streamEventsGenerator) {
|
|
861
|
+
let finalResponse = "";
|
|
862
|
+
let isFirstTextChunk = true;
|
|
863
|
+
let hasStreamedText = false;
|
|
864
|
+
for await (const event of streamEventsGenerator) {
|
|
865
|
+
if (event.event === "on_tool_start") {
|
|
866
|
+
if (hasStreamedText) {
|
|
867
|
+
process.stdout.write("\n");
|
|
868
|
+
hasStreamedText = false;
|
|
869
|
+
isFirstTextChunk = true;
|
|
870
|
+
}
|
|
871
|
+
handleToolStart(event);
|
|
872
|
+
} else if (event.event === "on_tool_end") {
|
|
873
|
+
handleToolEnd(event);
|
|
874
|
+
} else if (event.event === "on_chat_model_stream") {
|
|
875
|
+
if (event.data?.chunk?.text) {
|
|
876
|
+
const text = event.data.chunk.text;
|
|
877
|
+
if (typeof text === "string" && text.length > 0) {
|
|
878
|
+
if (isFirstTextChunk) {
|
|
879
|
+
process.stdout.write("\n\u{1F916} ");
|
|
880
|
+
isFirstTextChunk = false;
|
|
881
|
+
}
|
|
882
|
+
process.stdout.write(text);
|
|
883
|
+
finalResponse += text;
|
|
884
|
+
hasStreamedText = true;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
yield;
|
|
889
|
+
}
|
|
890
|
+
return finalResponse;
|
|
891
|
+
}
|
|
892
|
+
var import_chalk, import_cli_highlight, import_strip_ansi, TERMINAL_WIDTH;
|
|
893
|
+
var init_display = __esm({
|
|
894
|
+
"src/agents/display.ts"() {
|
|
895
|
+
"use strict";
|
|
896
|
+
import_chalk = __toESM(require("chalk"), 1);
|
|
897
|
+
import_cli_highlight = require("cli-highlight");
|
|
898
|
+
import_strip_ansi = __toESM(require("strip-ansi"), 1);
|
|
899
|
+
TERMINAL_WIDTH = process.stdout.columns || 120;
|
|
900
|
+
__name(stripAnsi, "stripAnsi");
|
|
901
|
+
__name(wrapAnsiLine, "wrapAnsiLine");
|
|
902
|
+
__name(printBox, "printBox");
|
|
903
|
+
__name(extractCodeFromToolInput, "extractCodeFromToolInput");
|
|
904
|
+
__name(isExecuteCodeResult, "isExecuteCodeResult");
|
|
905
|
+
__name(parseExecuteCodeResult, "parseExecuteCodeResult");
|
|
906
|
+
__name(renderContent, "renderContent");
|
|
907
|
+
__name(unwrapToolInput, "unwrapToolInput");
|
|
908
|
+
__name(handleToolStart, "handleToolStart");
|
|
909
|
+
__name(extractToolMessageContent, "extractToolMessageContent");
|
|
910
|
+
__name(formatSearchToolsAsTree, "formatSearchToolsAsTree");
|
|
911
|
+
__name(handleToolEnd, "handleToolEnd");
|
|
912
|
+
__name(prettyStreamEvents, "prettyStreamEvents");
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
|
|
562
916
|
// src/agents/index.ts
|
|
563
917
|
var agents_exports = {};
|
|
564
918
|
__export(agents_exports, {
|
|
565
919
|
BaseAgent: () => BaseAgent,
|
|
566
920
|
MCPAgent: () => MCPAgent,
|
|
921
|
+
PROMPTS: () => PROMPTS,
|
|
567
922
|
RemoteAgent: () => RemoteAgent
|
|
568
923
|
});
|
|
569
924
|
module.exports = __toCommonJS(agents_exports);
|
|
570
925
|
|
|
926
|
+
// src/connectors/base.ts
|
|
927
|
+
init_logging();
|
|
928
|
+
|
|
929
|
+
// src/client/connectors/codeMode.ts
|
|
930
|
+
var CODE_MODE_AGENT_PROMPT = `
|
|
931
|
+
## MCP Code Mode Tool Usage Guide
|
|
932
|
+
|
|
933
|
+
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:
|
|
934
|
+
|
|
935
|
+
### 1. Tool Discovery Phase
|
|
936
|
+
**Always start by discovering available tools:**
|
|
937
|
+
- Tools are organized by server namespace (e.g., \`server_name.tool_name\`)
|
|
938
|
+
- Use the \`search_tools(query, detail_level)\` function to find available tools
|
|
939
|
+
- You can access \`__tool_namespaces\` to see all available server namespaces
|
|
940
|
+
|
|
941
|
+
\`\`\`javascript
|
|
942
|
+
// Find all GitHub-related tools
|
|
943
|
+
const tools = await search_tools("github");
|
|
944
|
+
for (const tool of tools) {
|
|
945
|
+
console.log(\`\${tool.server}.\${tool.name}: \${tool.description}\`);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// Get only tool names for quick overview
|
|
949
|
+
const tools = await search_tools("", "names");
|
|
950
|
+
\`\`\`
|
|
951
|
+
|
|
952
|
+
### 2. Interface Introspection
|
|
953
|
+
**Understand tool contracts before using them:**
|
|
954
|
+
- Use \`search_tools\` to get tool descriptions and input schemas
|
|
955
|
+
- Look for "Access as: server.tool(args)" patterns in descriptions
|
|
956
|
+
|
|
957
|
+
### 3. Code Execution Guidelines
|
|
958
|
+
**When writing code:**
|
|
959
|
+
- Use \`await server.tool({ param: value })\` syntax for all tool calls
|
|
960
|
+
- Tools are async functions that return promises
|
|
961
|
+
- You have access to standard JavaScript globals: \`console\`, \`JSON\`, \`Math\`, \`Date\`, etc.
|
|
962
|
+
- All console output (\`console.log\`, \`console.error\`, etc.) is automatically captured and returned
|
|
963
|
+
- Build properly structured input objects based on interface definitions
|
|
964
|
+
- Handle errors appropriately with try/catch blocks
|
|
965
|
+
- Chain tool calls by using results from previous calls
|
|
966
|
+
|
|
967
|
+
### 4. Best Practices
|
|
968
|
+
- **Discover first, code second**: Always explore available tools before writing execution code
|
|
969
|
+
- **Respect namespaces**: Use full \`server.tool\` names to avoid conflicts
|
|
970
|
+
- **Minimize Context**: Process large data in code, return only essential results
|
|
971
|
+
- **Error handling**: Wrap tool calls in try/catch for robustness
|
|
972
|
+
- **Data flow**: Chain tools by passing outputs as inputs to subsequent tools
|
|
973
|
+
|
|
974
|
+
### 5. Available Runtime Context
|
|
975
|
+
- \`search_tools(query, detail_level)\`: Function to discover tools
|
|
976
|
+
- \`__tool_namespaces\`: Array of available server namespaces
|
|
977
|
+
- All registered tools as \`server.tool\` functions
|
|
978
|
+
- Standard JavaScript built-ins for data processing
|
|
979
|
+
|
|
980
|
+
### Example Workflow
|
|
981
|
+
|
|
982
|
+
\`\`\`javascript
|
|
983
|
+
// 1. Discover available tools
|
|
984
|
+
const github_tools = await search_tools("github pull request");
|
|
985
|
+
console.log(\`Available GitHub PR tools: \${github_tools.map(t => t.name)}\`);
|
|
986
|
+
|
|
987
|
+
// 2. Call tools with proper parameters
|
|
988
|
+
const pr = await github.get_pull_request({
|
|
989
|
+
owner: "facebook",
|
|
990
|
+
repo: "react",
|
|
991
|
+
number: 12345
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
// 3. Process results
|
|
995
|
+
let result;
|
|
996
|
+
if (pr.state === 'open' && pr.labels.some(l => l.name === 'bug')) {
|
|
997
|
+
// 4. Chain with other tools
|
|
998
|
+
await slack.post_message({
|
|
999
|
+
channel: "#bugs",
|
|
1000
|
+
text: \`\u{1F41B} Bug PR needs review: \${pr.title}\`
|
|
1001
|
+
});
|
|
1002
|
+
result = "Notification sent";
|
|
1003
|
+
} else {
|
|
1004
|
+
result = "No action needed";
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// 5. Return structured results
|
|
1008
|
+
return {
|
|
1009
|
+
pr_number: pr.number,
|
|
1010
|
+
pr_title: pr.title,
|
|
1011
|
+
action_taken: result
|
|
1012
|
+
};
|
|
1013
|
+
\`\`\`
|
|
1014
|
+
|
|
1015
|
+
Remember: Always discover and understand available tools before attempting to use them in code execution.
|
|
1016
|
+
`;
|
|
1017
|
+
|
|
1018
|
+
// src/client/prompts.ts
|
|
1019
|
+
var PROMPTS = {
|
|
1020
|
+
CODE_MODE: CODE_MODE_AGENT_PROMPT
|
|
1021
|
+
};
|
|
1022
|
+
|
|
571
1023
|
// src/agents/base.ts
|
|
572
1024
|
var BaseAgent = class {
|
|
573
1025
|
static {
|
|
@@ -2162,18 +2614,35 @@ var MCPAgent = class {
|
|
|
2162
2614
|
logger.info(
|
|
2163
2615
|
`\u{1F50C} Found ${Object.keys(this.sessions).length} existing sessions`
|
|
2164
2616
|
);
|
|
2165
|
-
|
|
2617
|
+
const nonCodeModeSessions = Object.keys(this.sessions).filter(
|
|
2618
|
+
(name) => name !== "code_mode"
|
|
2619
|
+
);
|
|
2620
|
+
if (nonCodeModeSessions.length === 0) {
|
|
2166
2621
|
logger.info("\u{1F504} No active sessions found, creating new ones...");
|
|
2167
2622
|
this.sessions = await this.client.createAllSessions();
|
|
2168
2623
|
logger.info(
|
|
2169
2624
|
`\u2705 Created ${Object.keys(this.sessions).length} new sessions`
|
|
2170
2625
|
);
|
|
2171
2626
|
}
|
|
2172
|
-
|
|
2627
|
+
if (this.client.codeMode) {
|
|
2628
|
+
const codeModeSession = this.sessions["code_mode"];
|
|
2629
|
+
if (codeModeSession) {
|
|
2630
|
+
this._tools = await this.adapter.createToolsFromConnectors([
|
|
2631
|
+
codeModeSession.connector
|
|
2632
|
+
]);
|
|
2633
|
+
logger.info(`\u{1F6E0}\uFE0F Created ${this._tools.length} code mode tools`);
|
|
2634
|
+
} else {
|
|
2635
|
+
throw new Error(
|
|
2636
|
+
"Code mode enabled but code_mode session not found"
|
|
2637
|
+
);
|
|
2638
|
+
}
|
|
2639
|
+
} else {
|
|
2640
|
+
this._tools = await LangChainAdapter.createTools(this.client);
|
|
2641
|
+
logger.info(
|
|
2642
|
+
`\u{1F6E0}\uFE0F Created ${this._tools.length} LangChain tools from client`
|
|
2643
|
+
);
|
|
2644
|
+
}
|
|
2173
2645
|
this._tools.push(...this.additionalTools);
|
|
2174
|
-
logger.info(
|
|
2175
|
-
`\u{1F6E0}\uFE0F Created ${this._tools.length} LangChain tools from client`
|
|
2176
|
-
);
|
|
2177
2646
|
} else {
|
|
2178
2647
|
logger.info(
|
|
2179
2648
|
`\u{1F517} Connecting to ${this.connectors.length} direct connectors...`
|
|
@@ -2738,6 +3207,8 @@ var MCPAgent = class {
|
|
|
2738
3207
|
tags: this.getTags(),
|
|
2739
3208
|
// Set trace name for LangChain/Langfuse
|
|
2740
3209
|
runName: this.metadata.trace_name || "mcp-use-agent",
|
|
3210
|
+
// Set recursion limit to 3x maxSteps to account for model calls + tool executions
|
|
3211
|
+
recursionLimit: this.maxSteps * 3,
|
|
2741
3212
|
// Pass sessionId for Langfuse if present in metadata
|
|
2742
3213
|
...this.metadata.session_id && {
|
|
2743
3214
|
sessionId: this.metadata.session_id
|
|
@@ -2951,8 +3422,8 @@ var MCPAgent = class {
|
|
|
2951
3422
|
this._agentExecutor = null;
|
|
2952
3423
|
this._tools = [];
|
|
2953
3424
|
if (this.client) {
|
|
2954
|
-
logger.info("\u{1F504} Closing
|
|
2955
|
-
await this.client.
|
|
3425
|
+
logger.info("\u{1F504} Closing client and cleaning up resources");
|
|
3426
|
+
await this.client.close();
|
|
2956
3427
|
this.sessions = {};
|
|
2957
3428
|
} else {
|
|
2958
3429
|
for (const connector of this.connectors) {
|
|
@@ -2968,6 +3439,26 @@ var MCPAgent = class {
|
|
|
2968
3439
|
logger.info("\u{1F44B} Agent closed successfully");
|
|
2969
3440
|
}
|
|
2970
3441
|
}
|
|
3442
|
+
/**
|
|
3443
|
+
* Yields with pretty-printed output for code mode.
|
|
3444
|
+
* This method formats and displays tool executions in a user-friendly way with syntax highlighting.
|
|
3445
|
+
*/
|
|
3446
|
+
async *prettyStreamEvents(query, maxSteps, manageConnector = true, externalHistory, outputSchema) {
|
|
3447
|
+
const { prettyStreamEvents: prettyStream } = await Promise.resolve().then(() => (init_display(), display_exports));
|
|
3448
|
+
const finalResponse = "";
|
|
3449
|
+
for await (const _ of prettyStream(
|
|
3450
|
+
this.streamEvents(
|
|
3451
|
+
query,
|
|
3452
|
+
maxSteps,
|
|
3453
|
+
manageConnector,
|
|
3454
|
+
externalHistory,
|
|
3455
|
+
outputSchema
|
|
3456
|
+
)
|
|
3457
|
+
)) {
|
|
3458
|
+
yield;
|
|
3459
|
+
}
|
|
3460
|
+
return finalResponse;
|
|
3461
|
+
}
|
|
2971
3462
|
/**
|
|
2972
3463
|
* Yields LangChain StreamEvent objects from the underlying streamEvents() method.
|
|
2973
3464
|
* This provides token-level streaming and fine-grained event updates.
|
|
@@ -2995,11 +3486,11 @@ var MCPAgent = class {
|
|
|
2995
3486
|
throw new Error("MCP agent failed to initialize");
|
|
2996
3487
|
}
|
|
2997
3488
|
this.maxSteps = maxSteps ?? this.maxSteps;
|
|
2998
|
-
const display_query = query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, " ")}...` : query.replace(/\n/g, " ");
|
|
3489
|
+
const display_query = typeof query === "string" && query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, " ")}...` : typeof query === "string" ? query.replace(/\n/g, " ") : String(query);
|
|
2999
3490
|
logger.info(`\u{1F4AC} Received query for streamEvents: '${display_query}'`);
|
|
3000
3491
|
if (this.memoryEnabled) {
|
|
3001
|
-
logger.info(`\u{1F504} Adding user message to history: ${
|
|
3002
|
-
this.addToHistory(new import_langchain2.HumanMessage(query));
|
|
3492
|
+
logger.info(`\u{1F504} Adding user message to history: ${display_query}`);
|
|
3493
|
+
this.addToHistory(new import_langchain2.HumanMessage({ content: query }));
|
|
3003
3494
|
}
|
|
3004
3495
|
const historyToUse = externalHistory ?? this.conversationHistory;
|
|
3005
3496
|
const langchainHistory = [];
|
|
@@ -3027,6 +3518,8 @@ var MCPAgent = class {
|
|
|
3027
3518
|
tags: this.getTags(),
|
|
3028
3519
|
// Set trace name for LangChain/Langfuse
|
|
3029
3520
|
runName: this.metadata.trace_name || "mcp-use-agent",
|
|
3521
|
+
// Set recursion limit to 3x maxSteps to account for model calls + tool executions
|
|
3522
|
+
recursionLimit: this.maxSteps * 3,
|
|
3030
3523
|
// Pass sessionId for Langfuse if present in metadata
|
|
3031
3524
|
...this.metadata.session_id && {
|
|
3032
3525
|
sessionId: this.metadata.session_id
|
|
@@ -3126,6 +3619,7 @@ var MCPAgent = class {
|
|
|
3126
3619
|
} else if (this.memoryEnabled && finalResponse) {
|
|
3127
3620
|
this.addToHistory(new import_langchain2.AIMessage(finalResponse));
|
|
3128
3621
|
}
|
|
3622
|
+
console.log("\n\n");
|
|
3129
3623
|
logger.info(`\u{1F389} StreamEvents complete - ${eventCount} events emitted`);
|
|
3130
3624
|
success = true;
|
|
3131
3625
|
} catch (e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/src/agents/index.js
CHANGED
|
@@ -1,28 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseAgent
|
|
3
|
+
} from "../../chunk-ZFZPZ4GE.js";
|
|
1
4
|
import {
|
|
2
5
|
MCPAgent,
|
|
3
6
|
RemoteAgent
|
|
4
|
-
} from "../../chunk-
|
|
7
|
+
} from "../../chunk-35A6O3YH.js";
|
|
8
|
+
import {
|
|
9
|
+
PROMPTS
|
|
10
|
+
} from "../../chunk-WERYJ6PF.js";
|
|
11
|
+
import "../../chunk-DSBKVAWD.js";
|
|
5
12
|
import "../../chunk-YURRUCIM.js";
|
|
6
13
|
import "../../chunk-34R6SIER.js";
|
|
7
|
-
import
|
|
8
|
-
__name
|
|
9
|
-
} from "../../chunk-3GQAWCBQ.js";
|
|
10
|
-
|
|
11
|
-
// src/agents/base.ts
|
|
12
|
-
var BaseAgent = class {
|
|
13
|
-
static {
|
|
14
|
-
__name(this, "BaseAgent");
|
|
15
|
-
}
|
|
16
|
-
session;
|
|
17
|
-
/**
|
|
18
|
-
* @param session MCP session used for tool invocation
|
|
19
|
-
*/
|
|
20
|
-
constructor(session) {
|
|
21
|
-
this.session = session;
|
|
22
|
-
}
|
|
23
|
-
};
|
|
14
|
+
import "../../chunk-3GQAWCBQ.js";
|
|
24
15
|
export {
|
|
25
16
|
BaseAgent,
|
|
26
17
|
MCPAgent,
|
|
18
|
+
PROMPTS,
|
|
27
19
|
RemoteAgent
|
|
28
20
|
};
|