@jarvis-agent/core 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -34233,25 +34233,31 @@ class Agent {
34233
34233
  this.description = params.description;
34234
34234
  this.tools = params.tools;
34235
34235
  this.llms = params.llms;
34236
- this.mcpClient = params.mcpClient;
34236
+ this.mcpClients = params.mcpClients || (params.mcpClient ? [params.mcpClient] : []);
34237
34237
  this.planDescription = params.planDescription;
34238
34238
  this.requestHandler = params.requestHandler;
34239
34239
  }
34240
34240
  async run(context, agentChain) {
34241
- const mcpClient = this.mcpClient || context.config.defaultMcpClient;
34241
+ const mcpClients = this.mcpClients.length > 0
34242
+ ? this.mcpClients
34243
+ : (context.config.defaultMcpClient ? [context.config.defaultMcpClient] : []);
34242
34244
  const agentContext = new AgentContext(context, this, agentChain);
34243
34245
  try {
34244
34246
  this.agentContext = agentContext;
34245
- mcpClient &&
34246
- !mcpClient.isConnected() &&
34247
- (await mcpClient.connect(context.controller.signal));
34248
- return await this.runWithContext(agentContext, mcpClient, config$1.maxReactNum);
34247
+ for (const client of mcpClients) {
34248
+ if (!client.isConnected()) {
34249
+ await client.connect(context.controller.signal);
34250
+ }
34251
+ }
34252
+ return await this.runWithContext(agentContext, mcpClients, config$1.maxReactNum);
34249
34253
  }
34250
34254
  finally {
34251
- mcpClient && (await mcpClient.close());
34255
+ for (const client of mcpClients) {
34256
+ await client.close();
34257
+ }
34252
34258
  }
34253
34259
  }
34254
- async runWithContext(agentContext, mcpClient, maxReactNum = 100, historyMessages = []) {
34260
+ async runWithContext(agentContext, mcpClients, maxReactNum = 100, historyMessages = []) {
34255
34261
  let loopNum = 0;
34256
34262
  let checkNum = 0;
34257
34263
  this.agentContext = agentContext;
@@ -34275,16 +34281,19 @@ class Agent {
34275
34281
  ];
34276
34282
  agentContext.messages = messages;
34277
34283
  const rlm = new RetryLanguageModel(context.config.llms, this.llms, context.config.globalConfig?.streamFirstTimeout, context.config.globalConfig?.streamTokenTimeout, agentContext);
34284
+ const resolvedMcpClients = Array.isArray(mcpClients)
34285
+ ? mcpClients
34286
+ : (mcpClients ? [mcpClients] : []);
34278
34287
  let agentTools = tools;
34279
34288
  while (loopNum < maxReactNum) {
34280
34289
  await context.checkAborted();
34281
- if (mcpClient) {
34290
+ if (resolvedMcpClients.length > 0) {
34282
34291
  const controlMcp = await this.controlMcpTools(agentContext, messages, loopNum);
34283
34292
  if (controlMcp.mcpTools) {
34284
- const mcpTools = await this.listTools(context, mcpClient, agentNode, controlMcp.mcpParams);
34293
+ const mcpTools = await this.listMcpTools(context, resolvedMcpClients, agentNode, controlMcp.mcpParams);
34285
34294
  const usedTools = extractUsedTool(messages, agentTools);
34286
- const _agentTools = mergeTools(tools, usedTools);
34287
- agentTools = mergeTools(_agentTools, mcpTools);
34295
+ const mergedTools = mergeTools(tools, usedTools);
34296
+ agentTools = mergeTools(mergedTools, mcpTools);
34288
34297
  }
34289
34298
  }
34290
34299
  await this.handleMessages(agentContext, messages, tools);
@@ -34441,33 +34450,32 @@ class Agent {
34441
34450
  async extSysPrompt(agentContext, tools) {
34442
34451
  return "";
34443
34452
  }
34444
- async listTools(context, mcpClient, agentNode, mcpParams) {
34445
- try {
34446
- if (!mcpClient.isConnected()) {
34447
- await mcpClient.connect(context.controller.signal);
34453
+ async listMcpTools(context, clients, agentNode, mcpParams) {
34454
+ const allTools = [];
34455
+ for (const client of clients) {
34456
+ try {
34457
+ if (!client.isConnected()) {
34458
+ await client.connect(context.controller.signal);
34459
+ }
34460
+ const list = await client.listTools({
34461
+ taskId: context.taskId,
34462
+ nodeId: agentNode?.id,
34463
+ environment: config$1.platform,
34464
+ agent_name: agentNode?.name || this.name,
34465
+ params: {},
34466
+ prompt: agentNode?.task || context.chain.taskPrompt,
34467
+ ...(mcpParams || {}),
34468
+ }, context.controller.signal);
34469
+ for (const toolSchema of list) {
34470
+ const execute = this.toolExecuter(client, toolSchema.name);
34471
+ allTools.push(new McpTool(new ToolWrapper(toolSchema, execute)));
34472
+ }
34473
+ }
34474
+ catch (e) {
34475
+ Log.error("Mcp listTools error", e);
34448
34476
  }
34449
- let list = await mcpClient.listTools({
34450
- taskId: context.taskId,
34451
- nodeId: agentNode?.id,
34452
- environment: config$1.platform,
34453
- agent_name: agentNode?.name || this.name,
34454
- params: {},
34455
- prompt: agentNode?.task || context.chain.taskPrompt,
34456
- ...(mcpParams || {}),
34457
- }, context.controller.signal);
34458
- let mcpTools = [];
34459
- for (let i = 0; i < list.length; i++) {
34460
- let toolSchema = list[i];
34461
- let execute = this.toolExecuter(mcpClient, toolSchema.name);
34462
- let toolWrapper = new ToolWrapper(toolSchema, execute);
34463
- mcpTools.push(new McpTool(toolWrapper));
34464
- }
34465
- return mcpTools;
34466
- }
34467
- catch (e) {
34468
- Log.error("Mcp listTools error", e);
34469
- return [];
34470
34477
  }
34478
+ return allTools;
34471
34479
  }
34472
34480
  async controlMcpTools(agentContext, messages, loopNum) {
34473
34481
  return {
@@ -34510,9 +34518,9 @@ class Agent {
34510
34518
  };
34511
34519
  }
34512
34520
  async loadTools(context) {
34513
- if (this.mcpClient) {
34514
- let mcpTools = await this.listTools(context, this.mcpClient);
34515
- if (mcpTools && mcpTools.length > 0) {
34521
+ if (this.mcpClients.length > 0) {
34522
+ const mcpTools = await this.listMcpTools(context, this.mcpClients);
34523
+ if (mcpTools.length > 0) {
34516
34524
  return mergeTools(this.tools, mcpTools);
34517
34525
  }
34518
34526
  }
@@ -34544,8 +34552,11 @@ class Agent {
34544
34552
  get PlanDescription() {
34545
34553
  return this.planDescription;
34546
34554
  }
34555
+ get McpClients() {
34556
+ return this.mcpClients;
34557
+ }
34547
34558
  get McpClient() {
34548
- return this.mcpClient;
34559
+ return this.mcpClients[0];
34549
34560
  }
34550
34561
  get AgentContext() {
34551
34562
  return this.agentContext;
@@ -34554,8 +34565,8 @@ class Agent {
34554
34565
 
34555
34566
  const AGENT_NAME$3 = "File";
34556
34567
  class BaseFileAgent extends Agent {
34557
- constructor(work_path, llms, ext_tools, mcpClient, planDescription) {
34558
- const _tools_ = [];
34568
+ constructor(work_path, llms, ext_tools, mcpClients, planDescription) {
34569
+ const initTools = [];
34559
34570
  const prompt = work_path
34560
34571
  ? `Your working directory is: ${work_path}
34561
34572
  - When viewing file lists and outputting file paths, always include the working directory
@@ -34568,9 +34579,9 @@ class BaseFileAgent extends Agent {
34568
34579
  super({
34569
34580
  name: AGENT_NAME$3,
34570
34581
  description: `You are a file agent, handling file-related tasks such as creating, finding, reading, modifying files, etc.${prompt}`,
34571
- tools: _tools_,
34582
+ tools: initTools,
34572
34583
  llms: llms,
34573
- mcpClient: mcpClient,
34584
+ mcpClients: Array.isArray(mcpClients) ? mcpClients : (mcpClients ? [mcpClients] : []),
34574
34585
  planDescription: planDescription ||
34575
34586
  `File operation agent, handles file-related tasks such as creating, finding, reading, modifying files, etc. Only supports text file output
34576
34587
  - Output file names must be in English
@@ -34578,11 +34589,11 @@ class BaseFileAgent extends Agent {
34578
34589
  - For data-related content, combine with visualization tools for display
34579
34590
  - For visualizations, generate charts first before page generation to minimize repetitive work`,
34580
34591
  });
34581
- let init_tools = this.buildInitTools();
34592
+ let builtTools = this.buildInitTools();
34582
34593
  if (ext_tools && ext_tools.length > 0) {
34583
- init_tools = mergeTools(init_tools, ext_tools);
34594
+ builtTools = mergeTools(builtTools, ext_tools);
34584
34595
  }
34585
- init_tools.forEach((tool) => _tools_.push(tool));
34596
+ builtTools.forEach((tool) => initTools.push(tool));
34586
34597
  }
34587
34598
  async do_file_read(agentContext, path, write_variable) {
34588
34599
  let file_context = await this.file_read(agentContext, path);
@@ -34737,24 +34748,24 @@ class BaseFileAgent extends Agent {
34737
34748
 
34738
34749
  const AGENT_NAME$2 = "Shell";
34739
34750
  class BaseShellAgent extends Agent {
34740
- constructor(llms, ext_tools, mcpClient, planDescription) {
34741
- const _tools_ = [];
34751
+ constructor(llms, ext_tools, mcpClients, planDescription) {
34752
+ const initTools = [];
34742
34753
  super({
34743
34754
  name: AGENT_NAME$2,
34744
34755
  description: `Run commands in a bash shell,
34745
34756
  * You must first call create_session to create a new session when using it for the first time.
34746
34757
  * Please execute delete commands with caution, and never perform dangerous operations like \`rm -rf /\`.
34747
34758
  * Please avoid commands that may produce a very large amount of output.`,
34748
- tools: _tools_,
34759
+ tools: initTools,
34749
34760
  llms: llms,
34750
- mcpClient: mcpClient,
34761
+ mcpClients: Array.isArray(mcpClients) ? mcpClients : (mcpClients ? [mcpClients] : []),
34751
34762
  planDescription: planDescription || "Shell command agent, use to execute shell commands.",
34752
34763
  });
34753
- let init_tools = this.buildInitTools();
34764
+ let builtTools = this.buildInitTools();
34754
34765
  if (ext_tools && ext_tools.length > 0) {
34755
- init_tools = mergeTools(init_tools, ext_tools);
34766
+ builtTools = mergeTools(builtTools, ext_tools);
34756
34767
  }
34757
- init_tools.forEach((tool) => _tools_.push(tool));
34768
+ builtTools.forEach((tool) => initTools.push(tool));
34758
34769
  }
34759
34770
  buildInitTools() {
34760
34771
  return [
@@ -34819,8 +34830,8 @@ class BaseShellAgent extends Agent {
34819
34830
 
34820
34831
  const AGENT_NAME$1 = "Computer";
34821
34832
  class BaseComputerAgent extends Agent {
34822
- constructor(llms, ext_tools, mcpClient, keyboardKeys) {
34823
- const _tools_ = [];
34833
+ constructor(llms, ext_tools, mcpClients, keyboardKeys) {
34834
+ const initTools = [];
34824
34835
  super({
34825
34836
  name: AGENT_NAME$1,
34826
34837
  description: `You are a computer operation agent, who interacts with the computer using mouse and keyboard, completing specified tasks step by step based on the given tasks and screenshots. After each of your operations, you will receive the latest computer screenshot to evaluate the task execution status.
@@ -34828,9 +34839,9 @@ This is a computer GUI interface, observe the execution through screenshots, and
34828
34839
  * COMPUTER OPERATIONS:
34829
34840
  - You can operate the application using shortcuts.
34830
34841
  - If stuck, try alternative approaches`,
34831
- tools: _tools_,
34842
+ tools: initTools,
34832
34843
  llms: llms,
34833
- mcpClient: mcpClient,
34844
+ mcpClients: Array.isArray(mcpClients) ? mcpClients : (mcpClients ? [mcpClients] : []),
34834
34845
  planDescription: "Computer operation agent, interact with the computer using the mouse and keyboard."
34835
34846
  });
34836
34847
  if (!keyboardKeys) {
@@ -34861,11 +34872,11 @@ This is a computer GUI interface, observe the execution through screenshots, and
34861
34872
  ];
34862
34873
  }
34863
34874
  }
34864
- let init_tools = this.buildInitTools(keyboardKeys);
34875
+ let builtTools = this.buildInitTools(keyboardKeys);
34865
34876
  if (ext_tools && ext_tools.length > 0) {
34866
- init_tools = mergeTools(init_tools, ext_tools);
34877
+ builtTools = mergeTools(builtTools, ext_tools);
34867
34878
  }
34868
- init_tools.forEach((tool) => _tools_.push(tool));
34879
+ builtTools.forEach((tool) => initTools.push(tool));
34869
34880
  }
34870
34881
  buildInitTools(keyboardKeys) {
34871
34882
  return [
@@ -35992,7 +36003,7 @@ function run_build_dom_tree() {
35992
36003
  }
35993
36004
 
35994
36005
  class BaseBrowserLabelsAgent extends BaseBrowserAgent {
35995
- constructor(llms, ext_tools, mcpClient) {
36006
+ constructor(llms, ext_tools, mcpClients) {
35996
36007
  let description = `You are a browser operation agent, use structured commands to interact with the browser.
35997
36008
  * This is a browser GUI interface where you need to analyze webpages by taking screenshot and page element structures, and specify action sequences to complete designated tasks.
35998
36009
  * For your first visit, please start by calling either the \`navigate_to\` or \`current_page\` tool. After each action you perform, I will provide you with updated information about the current state, including page screenshots and structured element data that has been specially processed for easier analysis.
@@ -36027,20 +36038,20 @@ class BaseBrowserLabelsAgent extends BaseBrowserAgent {
36027
36038
  - When filling out a form, fields that are not dependent on each other should be filled simultaneously
36028
36039
  - Avoid parallel processing for dependent operations, such as those that need to wait for page loading, DOM changes, redirects, subsequent operations that depend on the results of previous operations, or operations that may interfere with each other and affect the same page elements. In these cases, please do not use parallelization.`;
36029
36040
  }
36030
- const _tools_ = [];
36041
+ const initTools = [];
36031
36042
  super({
36032
36043
  name: AGENT_NAME,
36033
36044
  description: description,
36034
- tools: _tools_,
36045
+ tools: initTools,
36035
36046
  llms: llms,
36036
- mcpClient: mcpClient,
36047
+ mcpClients: Array.isArray(mcpClients) ? mcpClients : (mcpClients ? [mcpClients] : []),
36037
36048
  planDescription: "Browser operation agent, interact with the browser using the mouse and keyboard.",
36038
36049
  });
36039
- let init_tools = this.buildInitTools();
36050
+ let builtTools = this.buildInitTools();
36040
36051
  if (ext_tools && ext_tools.length > 0) {
36041
- init_tools = mergeTools(init_tools, ext_tools);
36052
+ builtTools = mergeTools(builtTools, ext_tools);
36042
36053
  }
36043
- init_tools.forEach((tool) => _tools_.push(tool));
36054
+ builtTools.forEach((tool) => initTools.push(tool));
36044
36055
  }
36045
36056
  async input_text(agentContext, index, text, enter) {
36046
36057
  await this.execute_script(agentContext, typing, [{ index, text, enter }]);
@@ -36745,7 +36756,7 @@ function scroll_by(params) {
36745
36756
  }
36746
36757
 
36747
36758
  class BaseBrowserScreenAgent extends BaseBrowserAgent {
36748
- constructor(llms, ext_tools, mcpClient) {
36759
+ constructor(llms, ext_tools, mcpClients) {
36749
36760
  const description = `You are a browser operation agent, use a mouse and keyboard to interact with a browser.
36750
36761
  * This is a browser GUI interface, observe the webpage execution through screenshots, and specify action sequences to complete designated tasks.
36751
36762
  * For the first visit, please call the \`navigate_to\` or \`current_page\` tool first. After that, each of your actions will return a screenshot of the page.
@@ -36757,20 +36768,20 @@ class BaseBrowserScreenAgent extends BaseBrowserAgent {
36757
36768
  - Wait for elements to load
36758
36769
  - Scroll pages and handle infinite scroll
36759
36770
  - YOU CAN DO ANYTHING ON THE BROWSER - including clicking on elements, filling forms, submitting data, etc.`;
36760
- const _tools_ = [];
36771
+ const initTools = [];
36761
36772
  super({
36762
36773
  name: AGENT_NAME,
36763
36774
  description: description,
36764
- tools: _tools_,
36775
+ tools: initTools,
36765
36776
  llms: llms,
36766
- mcpClient: mcpClient,
36777
+ mcpClients: Array.isArray(mcpClients) ? mcpClients : (mcpClients ? [mcpClients] : []),
36767
36778
  planDescription: "Browser operation agent, interact with the browser using the mouse and keyboard.",
36768
36779
  });
36769
- let init_tools = this.buildInitTools();
36780
+ let builtTools = this.buildInitTools();
36770
36781
  if (ext_tools && ext_tools.length > 0) {
36771
- init_tools = mergeTools(init_tools, ext_tools);
36782
+ builtTools = mergeTools(builtTools, ext_tools);
36772
36783
  }
36773
- init_tools.forEach((tool) => _tools_.push(tool));
36784
+ builtTools.forEach((tool) => initTools.push(tool));
36774
36785
  }
36775
36786
  buildInitTools() {
36776
36787
  return [