Class GenericGenaiAdapter<E,S>

java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
org.machanism.machai.mcp.server.GenericGenaiAdapter<E,S>
Type Parameters:
E - the type representing the server exchange/context
S - the type representing the tool specification
All Implemented Interfaces:
Genai
Direct Known Subclasses:
AbstractPromptGenaiAdapter

public class GenericGenaiAdapter<E,S> extends AbstractAIProvider
A generic adapter for integrating GenAI tools with different server implementations.

This class abstracts the process of registering tools and their schemas, allowing for flexible integration with various server types by parameterizing the exchange and specification types.

Since:
1.2.0
Author:
Viktor Tovstyi
  • Field Details

    • log

      private final org.slf4j.Logger log
      Logger used to report tool registration and invocation failures.
    • toolSpecifications

      private final List<S> toolSpecifications
      Collection receiving the transport-specific tool specifications.
    • builder

      Factory used to create transport-specific tool specifications.
  • Constructor Details

    • GenericGenaiAdapter

      GenericGenaiAdapter(List<S> toolSpecifications, AbstractMcpServer.ToolSpecificationBuilder<E> builder)
      Constructs a new GenericGenaiAdapter.
      Parameters:
      toolSpecifications - the list to which tool specifications will be added
      builder - the builder responsible for creating tool and specification objects
  • Method Details

    • addTool

      protected void addTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc)
      Register tool implementation for the adapter. If you need to implement a custom tool use FunctionTools.
      Specified by:
      addTool in class AbstractAIProvider
      Parameters:
      name - the name of the tool
      description - the description of the tool
      function - the function to execute when the tool is called
      paramsDesc - the parameter descriptions for the tool, each in the format "name:type:required:description"
    • formatSpecification

      private static String formatSpecification(Object specification)
    • addPropDescription

      private void addPropDescription(Map<String,com.anthropic.core.JsonValue> properties, List<String> required, ParamDescriptor pDesc)
      Adds a property description to the tool schema.
      Parameters:
      properties - the map of property names to their JSON schema values
      required - the list of required property names
      pDesc - the parameter description in the format "name:type:required:description"
    • toHumanReadable

      public static String toHumanReadable(String toolName)
      Converts a given tool name into a human-readable kebab-case format.

      Examples:

      • "myToolName" becomes "my-tool-name"
      • "Already-Kebab" becomes "already-kebab"
      • null or "" returns ""
      Parameters:
      toolName - the original tool name string to convert
      Returns:
      the human-readable kebab-case representation, or an empty string if input is null or empty
    • perform

      public String perform()
      This adapter registers callbacks rather than performing a standalone action.
      Returns:
      always null; tool execution occurs through registered MCP handlers