Skip to content
reaatechREAATECH

@reaatech/structured-function-calling-ai-adapter-anthropic

pending npm

Normalizes tool definitions and execution results between provider-agnostic formats and Anthropic's specific tool-use schema. It provides utility functions to map between these formats and requires `@reaatech/structured-function-calling-ai-core` as a runtime dependency.

@reaatech/structured-function-calling-ai-adapter-anthropic

Normalize tool calls to and from Anthropic’s tool-use format.

Installation

terminal
pnpm add @reaatech/structured-function-calling-ai-adapter-anthropic

API

toToolCall

typescript
function toToolCall(tu: AnthropicToolUseBlock): ToolCall;

Converts an Anthropic ToolUseBlock into a provider-agnostic ToolCall. Treats null input as an empty object {}.

AnthropicToolUseBlock is { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> | null }.

fromToolCallResult

typescript
function fromToolCallResult(tc: ToolCall, result: unknown): AnthropicToolResultBlock;

Converts a ToolCall and execution result into Anthropic’s ToolResultBlock shape. Serializes result with JSON.stringify.

AnthropicToolResultBlock is { type: 'tool_result'; tool_use_id: string; content: string }.

toAnthropicTools

typescript
function toAnthropicTools(defs: readonly ToolDefinition[]): AnthropicTool[];

Converts an array of ToolDefinitions to Anthropic tool format. Uses convertToJsonSchema (target defaults to jsonSchema7) for schema conversion.

AnthropicTool is { name: string; description: string; input_schema: Record<string, unknown> }.

Types

typescript
type ToolCall = { id: string; name: string; arguments: Record<string, unknown> };
type AnthropicToolUseBlock = { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> | null };
type AnthropicToolResultBlock = { type: 'tool_result'; tool_use_id: string; content: string };
type AnthropicTool = { name: string; description: string; input_schema: Record<string, unknown> };

Usage example

typescript
import { toToolCall, fromToolCallResult, toAnthropicTools } from '@reaatech/structured-function-calling-ai-adapter-anthropic';
 
// Convert from Anthropic format
const toolCall = toToolCall({
  type: 'tool_use',
  id: 'msg_xyz',
  name: 'get_weather',
  input: { city: 'NYC', units: 'metric' },
});
// → { id: 'msg_xyz', name: 'get_weather', arguments: { city: 'NYC', units: 'metric' } }
 
// Convert tool definitions to Anthropic tools
const anthropicTools = toAnthropicTools(registry.list());
 
// Convert result back to Anthropic format
const result = fromToolCallResult(toolCall, { temp: 22, conditions: 'sunny' });
// → { type: 'tool_result', tool_use_id: 'msg_xyz', content: '{"temp":22,"conditions":"sunny"}' }

Dependencies

  • @reaatech/structured-function-calling-ai-core (workspace:*)