@mcp-use/agent 2.0.0-beta.13 → 2.0.0-beta.15

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.js CHANGED
@@ -478,6 +478,62 @@ function toolImageFollowupHeader(toolName, count) {
478
478
  }
479
479
 
480
480
  // src/llm/messageFormat.ts
481
+ function messageText(content) {
482
+ if (typeof content === "string") return content;
483
+ if (Array.isArray(content)) {
484
+ return content.map(
485
+ (part) => part && typeof part === "object" && "text" in part && typeof part.text === "string" ? part.text : ""
486
+ ).join("");
487
+ }
488
+ return JSON.stringify(content ?? "");
489
+ }
490
+ function langChainMessageType(message) {
491
+ try {
492
+ if (typeof message._getType === "function") return message._getType();
493
+ if (typeof message.getType === "function") return message.getType();
494
+ } catch {
495
+ }
496
+ return message.type ?? "";
497
+ }
498
+ function convertExternalHistoryToProvider(messages) {
499
+ return messages.map((raw, index) => {
500
+ const message = raw;
501
+ const type = langChainMessageType(message);
502
+ const content = messageText(message.content);
503
+ if (type === "human" || type === "user") {
504
+ return { role: "user", content };
505
+ }
506
+ if (type === "system") {
507
+ return { role: "system", content };
508
+ }
509
+ if (type === "ai" || type === "assistant") {
510
+ const toolCalls = message.tool_calls?.map((call, callIndex) => ({
511
+ id: call.id ?? `external_${index}_${callIndex}`,
512
+ name: call.name,
513
+ args: call.args ?? {}
514
+ }));
515
+ return {
516
+ role: "assistant",
517
+ content,
518
+ ...toolCalls?.length ? { toolCalls } : {}
519
+ };
520
+ }
521
+ if (type === "tool") {
522
+ const toolIsError = message.status === "error" || isToolResultError(message.content);
523
+ return {
524
+ role: "tool",
525
+ content,
526
+ toolCallId: message.tool_call_id ?? `external_${index}`,
527
+ ...message.name ? { toolName: message.name } : {},
528
+ toolResult: message.content,
529
+ ...toolIsError ? { toolIsError: true } : {}
530
+ };
531
+ }
532
+ throw new TypeError(
533
+ `Unsupported external history message type at index ${index}: ${type || "unknown"}`
534
+ );
535
+ });
536
+ }
481
537
  function extractText(m) {
482
538
  const raw = typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((x) => x?.text ?? "").join("\n") : JSON.stringify(m.content ?? "");
483
539
  if (raw.trim()) return raw.trim();
@@ -878,7 +934,11 @@ async function* streamChat(params) {
878
934
  try {
879
935
  args = JSON.parse(entry.argsJson);
880
936
  } catch {
881
- args = {};
937
+ yield {
938
+ type: "error",
939
+ message: `Anthropic returned invalid JSON arguments for tool "${entry.name}".`
940
+ };
941
+ return;
882
942
  }
883
943
  }
884
944
  yield {
@@ -1941,14 +2001,21 @@ function appendToolOutputsToInput(input, callId, toolName, result) {
1941
2001
  });
1942
2002
  }
1943
2003
  }
2004
+ function responsesReasoningFields(config) {
2005
+ const effort = config.reasoningEffort;
2006
+ if (!effort || effort === "none") return {};
2007
+ return {
2008
+ include: ["reasoning.encrypted_content"],
2009
+ reasoning: { effort }
2010
+ };
2011
+ }
1944
2012
  function buildResponsesBody(params, stream) {
1945
2013
  const body = {
1946
2014
  model: params.config.model,
1947
2015
  input: params.input,
1948
2016
  store: false,
1949
- include: ["reasoning.encrypted_content"],
1950
- reasoning: { effort: params.config.reasoningEffort ?? "low" },
1951
- stream
2017
+ stream,
2018
+ ...responsesReasoningFields(params.config)
1952
2019
  };
1953
2020
  if (params.instructions) body.instructions = params.instructions;
1954
2021
  if (params.tools && params.tools.length > 0) {
@@ -2455,12 +2522,12 @@ async function* streamNativeAgent(driver, options) {
2455
2522
  }
2456
2523
  async function* streamNativeAgentSteps(driver, options) {
2457
2524
  let finalText = "";
2458
- let pendingStep = null;
2525
+ const pendingSteps = /* @__PURE__ */ new Map();
2459
2526
  for await (const ev of streamNativeAgent(driver, options)) {
2460
2527
  if (ev.type === "text-delta") {
2461
2528
  finalText += ev.delta;
2462
2529
  } else if (ev.type === "tool-call-ready") {
2463
- pendingStep = {
2530
+ const pendingStep = {
2464
2531
  action: {
2465
2532
  tool: ev.toolName,
2466
2533
  toolInput: ev.args,
@@ -2468,14 +2535,17 @@ async function* streamNativeAgentSteps(driver, options) {
2468
2535
  },
2469
2536
  observation: ""
2470
2537
  };
2538
+ pendingSteps.set(ev.toolCallId, pendingStep);
2471
2539
  yield pendingStep;
2472
- } else if (ev.type === "tool-result" && pendingStep) {
2540
+ } else if (ev.type === "tool-result") {
2541
+ const pendingStep = pendingSteps.get(ev.toolCallId);
2542
+ if (!pendingStep) continue;
2473
2543
  const observation = typeof ev.result === "string" ? ev.result : JSON.stringify(ev.result ?? "");
2474
2544
  yield {
2475
2545
  action: pendingStep.action,
2476
2546
  observation
2477
2547
  };
2478
- pendingStep = null;
2548
+ pendingSteps.delete(ev.toolCallId);
2479
2549
  } else if (ev.type === "error") {
2480
2550
  throw new Error(ev.message);
2481
2551
  }
@@ -2567,27 +2637,34 @@ function providerConfigFromOptions(provider, model, config) {
2567
2637
  }
2568
2638
 
2569
2639
  // src/version.ts
2570
- var VERSION = "2.0.0-beta.12";
2640
+ var VERSION = "2.0.0-beta.15";
2571
2641
  function getPackageVersion() {
2572
2642
  return VERSION;
2573
2643
  }
2574
2644
 
2575
2645
  // src/agents/normalize_run_options.ts
2576
- function normalizeRunOptions(queryOrOptions, maxSteps, manageConnector, _externalHistory, outputSchema, signal) {
2646
+ function normalizeRunOptions(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema, signal) {
2577
2647
  if (typeof queryOrOptions === "object" && queryOrOptions !== null) {
2578
2648
  return {
2579
2649
  prompt: queryOrOptions.prompt,
2580
2650
  maxSteps: queryOrOptions.maxSteps,
2581
2651
  manageConnector: queryOrOptions.manageConnector,
2652
+ externalHistory: queryOrOptions.externalHistory,
2582
2653
  messages: queryOrOptions.messages,
2583
2654
  schema: queryOrOptions.schema,
2584
2655
  signal: queryOrOptions.signal
2585
2656
  };
2586
2657
  }
2658
+ if (externalHistory !== void 0 && !Array.isArray(externalHistory)) {
2659
+ throw new TypeError(
2660
+ "externalHistory must be an array of LangChain messages"
2661
+ );
2662
+ }
2587
2663
  return {
2588
2664
  prompt: queryOrOptions,
2589
2665
  maxSteps,
2590
2666
  manageConnector,
2667
+ externalHistory,
2591
2668
  schema: outputSchema,
2592
2669
  signal
2593
2670
  };
@@ -2959,11 +3036,6 @@ var MCPAgent = class {
2959
3036
  }
2960
3037
  if (this.initialized) return;
2961
3038
  if (this.isSimplifiedMode) {
2962
- if (!this.client && this.mcpServersConfig) {
2963
- const { MCPClient } = await import("@mcp-use/client");
2964
- this.client = new MCPClient({ mcpServers: this.mcpServersConfig });
2965
- this.clientOwnedByAgent = true;
2966
- }
2967
3039
  if (this.llmString) {
2968
3040
  this.driver = createLlmDriver(
2969
3041
  parseLLMStringToProviderConfig(this.llmString, this.llmConfig)
@@ -2972,6 +3044,11 @@ var MCPAgent = class {
2972
3044
  } else if (this.explicitProviderConfig) {
2973
3045
  this.driver = createLlmDriver(this.explicitProviderConfig);
2974
3046
  }
3047
+ if (!this.client && this.mcpServersConfig && !this.hasLiveConnections()) {
3048
+ const { MCPClient } = await import("@mcp-use/client");
3049
+ this.client = new MCPClient({ mcpServers: this.mcpServersConfig });
3050
+ this.clientOwnedByAgent = true;
3051
+ }
2975
3052
  if (!this.driver) {
2976
3053
  throw new Error("LLM driver not configured.");
2977
3054
  }
@@ -3078,6 +3155,11 @@ var MCPAgent = class {
3078
3155
  if (this.memoryEnabled && this.conversationMessages.length > 0) {
3079
3156
  messages.push(...this.conversationMessages);
3080
3157
  }
3158
+ if (options.externalHistory?.length) {
3159
+ messages.push(
3160
+ ...convertExternalHistoryToProvider(options.externalHistory)
3161
+ );
3162
+ }
3081
3163
  if (options.messages?.length) {
3082
3164
  messages.push(...options.messages);
3083
3165
  }