Package org.machanism.machai.ai.provider.anthropic
This package contains AnthropicProvider,
which adapts the Anthropic Java SDK to Machai's Genai
interface. It enables applications to send prompts to Anthropic Claude models, register
custom function tools, configure web search, connect MCP servers, and track token usage.
Key Components
AnthropicProvider— The primary entry point. Builds and sends requests to the Anthropic Beta Messages API, handles tool-use loops, parses responses, and captures usage statistics.
Configuration Properties
chatModel(required): Claude model identifier, e.g."claude-3-opus-20240229".ANTHROPIC_API_KEY(required): Anthropic API key or auth token.ANTHROPIC_BASE_URL(optional): Override the default Anthropic API base URL (useful for proxy or compatible endpoints).GENAI_TIMEOUT(optional): Request timeout in seconds. 0 or absent means SDK default.MAX_OUTPUT_TOKENS(optional): Maximum tokens in the model response. Defaults to the value defined inAbstractAIProvider.MAX_TOOL_CALLS(optional): Maximum number of tool calls per response. 0 leaves the limit unset.cacheThreshold(optional): Tool result character length above which prompt caching (BetaCacheControlEphemeral) is applied automatically.
Usage Example
// Obtain a Configurator with the required properties
Configurator config = ...; // contains ANTHROPIC_API_KEY, chatModel, etc.
AnthropicProvider provider = new AnthropicProvider();
provider.init("claude-3-opus-20240229", config);
// Optionally register a custom tool
provider.addTool(
"get_weather",
"Returns current weather for a given city",
(params, projectDir) -> fetchWeather(params),
new ParamDescriptor("city", "string", "City name", true)
);
// Send a prompt and receive the response
provider.prompt("What is the weather in Berlin today?");
String response = provider.perform();
Tool-Use Loop
When the model responds with one or more tool-use blocks, AnthropicProvider automatically
invokes the matching registered ToolFunction, appends the
result to the conversation, and re-submits the request until the model returns a plain text
response.
Web Search Support
Web search can be enabled by calling addWebSearch with a supported tool version
("20260209" or "20250305") and optional user location hints (city, country,
region). The corresponding BetaWebSearchTool is then included in every request.
MCP Server Support
External MCP (Model Context Protocol) servers can be registered via addMcpServer,
supplying a name, URL, optional authorization token, and description. All registered servers
are forwarded with each request via MessageCreateParams.
- Since:
- 1.1.13
- Author:
- Viktor Tovstyi
-
Classes