@mcp-use/agent 2.0.0-beta.14 → 2.0.0-beta.16

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 {
@@ -2462,12 +2522,12 @@ async function* streamNativeAgent(driver, options) {
2462
2522
  }
2463
2523
  async function* streamNativeAgentSteps(driver, options) {
2464
2524
  let finalText = "";
2465
- let pendingStep = null;
2525
+ const pendingSteps = /* @__PURE__ */ new Map();
2466
2526
  for await (const ev of streamNativeAgent(driver, options)) {
2467
2527
  if (ev.type === "text-delta") {
2468
2528
  finalText += ev.delta;
2469
2529
  } else if (ev.type === "tool-call-ready") {
2470
- pendingStep = {
2530
+ const pendingStep = {
2471
2531
  action: {
2472
2532
  tool: ev.toolName,
2473
2533
  toolInput: ev.args,
@@ -2475,14 +2535,17 @@ async function* streamNativeAgentSteps(driver, options) {
2475
2535
  },
2476
2536
  observation: ""
2477
2537
  };
2538
+ pendingSteps.set(ev.toolCallId, pendingStep);
2478
2539
  yield pendingStep;
2479
- } 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;
2480
2543
  const observation = typeof ev.result === "string" ? ev.result : JSON.stringify(ev.result ?? "");
2481
2544
  yield {
2482
2545
  action: pendingStep.action,
2483
2546
  observation
2484
2547
  };
2485
- pendingStep = null;
2548
+ pendingSteps.delete(ev.toolCallId);
2486
2549
  } else if (ev.type === "error") {
2487
2550
  throw new Error(ev.message);
2488
2551
  }
@@ -2574,27 +2637,34 @@ function providerConfigFromOptions(provider, model, config) {
2574
2637
  }
2575
2638
 
2576
2639
  // src/version.ts
2577
- var VERSION = "2.0.0-beta.14";
2640
+ var VERSION = "2.0.0-beta.16";
2578
2641
  function getPackageVersion() {
2579
2642
  return VERSION;
2580
2643
  }
2581
2644
 
2582
2645
  // src/agents/normalize_run_options.ts
2583
- function normalizeRunOptions(queryOrOptions, maxSteps, manageConnector, _externalHistory, outputSchema, signal) {
2646
+ function normalizeRunOptions(queryOrOptions, maxSteps, manageConnector, externalHistory, outputSchema, signal) {
2584
2647
  if (typeof queryOrOptions === "object" && queryOrOptions !== null) {
2585
2648
  return {
2586
2649
  prompt: queryOrOptions.prompt,
2587
2650
  maxSteps: queryOrOptions.maxSteps,
2588
2651
  manageConnector: queryOrOptions.manageConnector,
2652
+ externalHistory: queryOrOptions.externalHistory,
2589
2653
  messages: queryOrOptions.messages,
2590
2654
  schema: queryOrOptions.schema,
2591
2655
  signal: queryOrOptions.signal
2592
2656
  };
2593
2657
  }
2658
+ if (externalHistory !== void 0 && !Array.isArray(externalHistory)) {
2659
+ throw new TypeError(
2660
+ "externalHistory must be an array of LangChain messages"
2661
+ );
2662
+ }
2594
2663
  return {
2595
2664
  prompt: queryOrOptions,
2596
2665
  maxSteps,
2597
2666
  manageConnector,
2667
+ externalHistory,
2598
2668
  schema: outputSchema,
2599
2669
  signal
2600
2670
  };
@@ -3085,6 +3155,11 @@ var MCPAgent = class {
3085
3155
  if (this.memoryEnabled && this.conversationMessages.length > 0) {
3086
3156
  messages.push(...this.conversationMessages);
3087
3157
  }
3158
+ if (options.externalHistory?.length) {
3159
+ messages.push(
3160
+ ...convertExternalHistoryToProvider(options.externalHistory)
3161
+ );
3162
+ }
3088
3163
  if (options.messages?.length) {
3089
3164
  messages.push(...options.messages);
3090
3165
  }