@witqq/agent-sdk 0.5.2 → 0.6.0

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.
@@ -325,6 +325,14 @@ function extractSchemaFromDef(schema) {
325
325
  }
326
326
 
327
327
  // src/backends/claude.ts
328
+ var MCP_SERVER_NAME = "agent-sdk-tools";
329
+ var MCP_TOOL_PREFIX = `mcp__${MCP_SERVER_NAME}__`;
330
+ function mcpToolName(toolName) {
331
+ return `${MCP_TOOL_PREFIX}${toolName}`;
332
+ }
333
+ function stripMcpPrefix(name) {
334
+ return name.startsWith(MCP_TOOL_PREFIX) ? name.slice(MCP_TOOL_PREFIX.length) : name;
335
+ }
328
336
  var sdkModule = null;
329
337
  async function loadSDK() {
330
338
  if (sdkModule) return sdkModule;
@@ -348,11 +356,13 @@ var ANTHROPIC_API_VERSION = "2023-06-01";
348
356
  var ANTHROPIC_OAUTH_BETA = "oauth-2025-04-20";
349
357
  function buildMcpServer(sdk, tools, toolResultCapture) {
350
358
  if (tools.length === 0) return void 0;
351
- const mcpTools = tools.map(
352
- (tool) => sdk.tool(
359
+ const mcpTools = tools.map((tool) => {
360
+ const zodSchema = tool.parameters;
361
+ const inputSchema = zodSchema.shape ?? zodToJsonSchema(tool.parameters);
362
+ return sdk.tool(
353
363
  tool.name,
354
364
  tool.description ?? "",
355
- zodToJsonSchema(tool.parameters),
365
+ inputSchema,
356
366
  async (args) => {
357
367
  const result = await tool.execute(args);
358
368
  if (toolResultCapture) {
@@ -367,10 +377,10 @@ function buildMcpServer(sdk, tools, toolResultCapture) {
367
377
  ]
368
378
  };
369
379
  }
370
- )
371
- );
380
+ );
381
+ });
372
382
  return sdk.createSdkMcpServer({
373
- name: "agent-sdk-tools",
383
+ name: MCP_SERVER_NAME,
374
384
  version: "1.0.0",
375
385
  tools: mcpTools
376
386
  });
@@ -407,7 +417,8 @@ function buildCanUseTool(config) {
407
417
  const onPermission = config.supervisor?.onPermission;
408
418
  if (!onPermission) return void 0;
409
419
  const permissionStore = config.permissionStore;
410
- return async (toolName, input, options) => {
420
+ return async (rawToolName, input, options) => {
421
+ const toolName = stripMcpPrefix(rawToolName);
411
422
  if (permissionStore && await permissionStore.isApproved(toolName)) {
412
423
  return {
413
424
  behavior: "allow",
@@ -489,14 +500,10 @@ function mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker) {
489
500
  const betaMessage = msg.message;
490
501
  if (!betaMessage?.content) return null;
491
502
  const events = [];
492
- const textParts = betaMessage.content.filter((b) => b.type === "text" && b.text).map((b) => b.text).join("");
493
- if (textParts) {
494
- events.push({ type: "text_delta", text: textParts });
495
- }
496
503
  for (const block of betaMessage.content) {
497
504
  if (block.type === "tool_use") {
498
505
  const toolCallId = String(block.id ?? "");
499
- const toolName = block.name ?? "unknown";
506
+ const toolName = stripMcpPrefix(block.name ?? "unknown");
500
507
  if (toolCallTracker) {
501
508
  toolCallTracker.trackStart(toolCallId, toolName);
502
509
  }
@@ -519,7 +526,7 @@ function mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker) {
519
526
  }
520
527
  case "tool_use_summary": {
521
528
  const summary = msg.summary;
522
- const toolName = msg.tool_name ?? "unknown";
529
+ const toolName = stripMcpPrefix(msg.tool_name ?? "unknown");
523
530
  const precedingIds = msg.preceding_tool_use_ids;
524
531
  let toolCallId = "";
525
532
  if (precedingIds && precedingIds.length > 0) {
@@ -528,15 +535,12 @@ function mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker) {
528
535
  } else if (toolCallTracker) {
529
536
  toolCallId = toolCallTracker.consumeToolCallId(toolName);
530
537
  }
531
- if (summary) {
532
- return {
533
- type: "tool_call_end",
534
- toolCallId,
535
- toolName,
536
- result: summary
537
- };
538
- }
539
- return null;
538
+ return {
539
+ type: "tool_call_end",
540
+ toolCallId,
541
+ toolName,
542
+ result: summary ?? null
543
+ };
540
544
  }
541
545
  case "stream_event": {
542
546
  const event = msg.event;
@@ -564,10 +568,7 @@ function mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker) {
564
568
  return null;
565
569
  }
566
570
  case "tool_progress": {
567
- const toolName = msg.tool_name;
568
- if (!toolName) return null;
569
- const toolCallId = toolCallTracker?.peekToolCallId(toolName) ?? "";
570
- return { type: "tool_call_start", toolCallId, toolName, args: {} };
571
+ return null;
571
572
  }
572
573
  case "result": {
573
574
  if (msg.subtype === "success") {
@@ -662,6 +663,9 @@ var ClaudeAgent = class extends BaseAgent {
662
663
  if (opts.canUseTool && !opts.permissionMode) {
663
664
  opts.permissionMode = "default";
664
665
  }
666
+ if (this.config.availableTools) {
667
+ opts.allowedTools = [...this.config.availableTools];
668
+ }
665
669
  return opts;
666
670
  }
667
671
  async buildMcpConfig(opts, toolResultCapture) {
@@ -670,8 +674,10 @@ var ClaudeAgent = class extends BaseAgent {
670
674
  const mcpServer = buildMcpServer(sdk, this.tools, toolResultCapture);
671
675
  if (mcpServer) {
672
676
  opts.mcpServers = {
673
- "agent-sdk-tools": mcpServer
677
+ [MCP_SERVER_NAME]: mcpServer
674
678
  };
679
+ const mcpToolNames = this.tools.map((t) => mcpToolName(t.name));
680
+ opts.allowedTools = [...opts.allowedTools ?? [], ...mcpToolNames];
675
681
  }
676
682
  return opts;
677
683
  }
@@ -696,7 +702,7 @@ var ClaudeAgent = class extends BaseAgent {
696
702
  if (betaMessage?.content) {
697
703
  for (const block of betaMessage.content) {
698
704
  if (block.type === "tool_use") {
699
- const toolName = block.name ?? "unknown";
705
+ const toolName = stripMcpPrefix(block.name ?? "unknown");
700
706
  toolCalls.push({
701
707
  toolName,
702
708
  args: block.input ?? {},
@@ -834,13 +840,10 @@ var ClaudeAgent = class extends BaseAgent {
834
840
  try {
835
841
  for await (const msg of q) {
836
842
  if (signal.aborted) throw new AbortError();
837
- const event = mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker);
838
- if (event) {
839
- if (Array.isArray(event)) {
840
- for (const e of event) yield e;
841
- } else {
842
- yield event;
843
- }
843
+ const events = mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker);
844
+ if (events) {
845
+ const mapped = Array.isArray(events) ? events : [events];
846
+ for (const e of mapped) yield e;
844
847
  }
845
848
  if (msg.type === "result" && msg.subtype === "success") {
846
849
  const r = msg;