Class AnthropicProvider

java.lang.Object
org.machanism.machai.process.provider.AbstractAIProvider
org.machanism.machai.genai.provider.AnthropicProvider
All Implemented Interfaces:
ProcessProvider
Direct Known Subclasses:
CodeMieProvider.ClaudeProviderExtension

public class AnthropicProvider extends AbstractAIProvider
Anthropic-backed implementation of Machai's ProcessProvider abstraction.

This provider adapts the Anthropic Java SDK to the Machai provider interface. It manages prompt collection, request construction for the Anthropic Beta Messages API, custom tool execution, optional web search integration, optional MCP server forwarding, and usage tracking.

Since:
1.1.13
Author:
Viktor Tovstyi
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
      Logger used for provider diagnostics and request lifecycle messages.
    • ANTHROPIC_API_KEY

      public static final String ANTHROPIC_API_KEY
      Configuration property name that contains the Anthropic API key or authorization token.
      See Also:
    • ANTHROPIC_BASE_URL

      public static final String ANTHROPIC_BASE_URL
      Configuration property that overrides the Anthropic API base URL.
      See Also:
    • inputs

      private final List<com.anthropic.models.beta.messages.BetaMessageParam> inputs
      Accumulated Anthropic message inputs for the current conversation.
    • toolMap

      private final Map<com.anthropic.models.beta.messages.BetaTool.Builder,ToolFunction> toolMap
      Mapping between Anthropic tool definitions and local functions.
    • webSearchTool

      private Object webSearchTool
      Anthropic web search tool instance registered for outgoing requests, or null.
    • mcpServers

      private List<com.anthropic.models.beta.messages.BetaRequestMcpServerUrlDefinition> mcpServers
      MCP server definitions forwarded to Anthropic with each request.
  • Constructor Details

    • AnthropicProvider

      public AnthropicProvider()
      Creates an Anthropic provider instance.
  • Method Details

    • addMcpServer

      protected void addMcpServer(String name, String url, String authorization, String description)
      Registers an MCP server definition for future requests.
      Overrides:
      addMcpServer in class AbstractAIProvider
      Parameters:
      name - server name exposed to the model
      url - server endpoint URL
      authorization - optional authorization token
      description - optional description, currently unused by the SDK model
    • addWebSearch

      protected void addWebSearch(String type, String city, String country, String region)
      Registers a web search tool and optional location hints.
      Overrides:
      addWebSearch in class AbstractAIProvider
      Parameters:
      type - web search tool version
      city - optional city hint
      country - optional country hint
      region - optional region hint
      Throws:
      IllegalArgumentException - if type is unsupported
    • prompt

      public void prompt(String text)
      Adds a non-blank user prompt. @param text prompt text
      Specified by:
      prompt in interface ProcessProvider
      Overrides:
      prompt in class AbstractAIProvider
      Parameters:
      text - the prompt text
    • perform

      public String perform()
      Sends the accumulated conversation and returns the final text response. @return response text
      Returns:
      the provider response as a string
    • call

      private com.anthropic.models.beta.messages.BetaMessage call(com.anthropic.models.beta.messages.MessageCreateParams params)
      Submits a request to the Anthropic Beta Messages API and records its token usage.
      Parameters:
      params - immutable request parameters to submit
      Returns:
      the message returned by Anthropic
    • parseResponse

      private String parseResponse(com.anthropic.models.beta.messages.BetaMessage response)
      Converts an Anthropic response into text, executing local tool calls and recursively submitting their results until no tool call remains.
      Parameters:
      response - response to process
      Returns:
      final assistant text, or null when the response contains no text
    • captureUsage

      private void captureUsage(com.anthropic.models.beta.messages.BetaMessage response)
      Captures token usage and updates global usage statistics. @param response response message
    • handleFunctionCall

      private void handleFunctionCall(com.anthropic.models.beta.messages.BetaToolUseBlock toolUse)
      Adds a received tool-use block to the conversation, invokes its local handler, and appends the corresponding tool-result user message.
      Parameters:
      toolUse - tool invocation returned by Anthropic
    • callFunction

      private Object callFunction(com.anthropic.models.beta.messages.BetaToolUseBlock toolUse)
      Locates and invokes the locally registered handler for an Anthropic tool invocation.
      Parameters:
      toolUse - tool invocation to execute
      Returns:
      handler result, or null when no matching handler is registered
    • createResponseBuilder

      private com.anthropic.models.beta.messages.MessageCreateParams createResponseBuilder(List<com.anthropic.models.beta.messages.BetaMessageParam> inputs)
      Builds a Messages API request from the current conversation and provider configuration, including registered local tools, MCP servers, and web search when configured.
      Parameters:
      inputs - conversation messages to include in the request
      Returns:
      configured immutable request parameters
    • clear

      public void clear()
      Clears accumulated conversation inputs.
      Specified by:
      clear in interface ProcessProvider
      Overrides:
      clear in class AbstractAIProvider
    • addTool

      protected void addTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc)
      Registers a local function tool.
      Specified by:
      addTool in class AbstractAIProvider
      Parameters:
      name - tool name
      description - tool description
      function - local callback
      paramsDesc - tool input descriptors
    • getClient

      protected com.anthropic.client.AnthropicClient getClient()
      Creates and configures an Anthropic client. @return configured client
    • getToolNames

      public List<String> getToolNames()
      Returns the names of locally registered Anthropic tools.
      Returns:
      a list of registered tool names in registration order