Class AnthropicProvider

java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
org.machanism.machai.ai.provider.impl.AnthropicProvider
All Implemented Interfaces:
Genai
Direct Known Subclasses:
CodeMieProvider.ClaudeProviderExtension

public class AnthropicProvider extends AbstractAIProvider
Anthropic-backed implementation of Machai's Genai 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 Genai
      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)
    • parseResponse

      private String parseResponse(com.anthropic.models.beta.messages.BetaMessage response)
    • 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)
    • callFunction

      private Object callFunction(com.anthropic.models.beta.messages.BetaToolUseBlock toolUse)
    • createResponseBuilder

      private com.anthropic.models.beta.messages.MessageCreateParams createResponseBuilder(List<com.anthropic.models.beta.messages.BetaMessageParam> inputs)
    • clear

      public void clear()
      Clears accumulated conversation inputs.
      Specified by:
      clear in interface Genai
      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