Claude can interact with external tools by providing a tools array in the MessageParameter.
When Claude decides to use a tool, the MessageResponse will have a stop_reason of tool_use and a content block of type tool_use. This block includes:
id: A unique identifier to match tool results later.name: The name of the tool being called.input: An object containing the arguments for the tool.
Disabling Parallel Tool Use
By default, Claude may use multiple tools. To restrict Claude to at most one tool (when type is .auto) or exactly one tool (when type is .any or .tool), set disableParallelToolUse: true in the ToolChoice object.
let maxTokens = 1024
let weatherTool = MessageParameter.Tool.function(
name: "get_weather",
description: "Get the current weather in a given location",
inputSchema: .init(
type: .object,
properties: [
"location": .init(type: .string, description: "The city and state, e.g. San Francisco, CA"),
"unit": .init(type: .string, description: "The unit of temperature, either celsius or fahrenheit")
],
required: ["location"]
)
)
let messageParameter = MessageParameter.Message(role: .user, content: "What is the weather like in San Francisco?")
let parameters = MessageParameter(model: .claude3Opus, messages: [messageParameter], maxTokens: maxTokens, tools: [weatherTool])
let message = try await service.createMessage(parameters)
// To disable parallel tool use:
let toolChoice = ToolChoice(type: .auto, disableParallelToolUse: true)
let parametersWithLimitedTools = MessageParameter(
model: model,
messages: messages,
maxTokens: maxTokens,
toolChoice: toolChoice
)