Interface ToolSource

All Known Subinterfaces:
ToolSource.Local, ToolSource.Mcp, ToolSource.Programmatic

@Experimental public interface ToolSource
Describes where a tool comes from.

A ToolSource is trusted information: it is set by the framework or by the application when the tool is registered, and it is never derived from remote data such as MCP _meta, openWorldHint or ToolSpecification.metadata(). Guardrails can therefore rely on it to make security decisions.

The kinds of sources that LangChain4j sets are:

Later versions may add kinds (for example for A2A agents, or OpenAPI tools). A guardrail, and a guardrail library in particular, should handle a kind it does not know, and a security check should treat such a kind as untrusted. An instance of ToolSource.Local, ToolSource.Mcp or ToolSource.Programmatic can only be created with the factory methods of this interface.

An application or a framework can define a kind of its own, and set it on the tools it registers (for example with AiServiceTool.builder().toolSource(..)). Its implementation should be immutable, with value-based equals and hashCode and a toString: a record, for example record A2aToolSource(String agentUrl) implements ToolSource {}.

Since:
1.25.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    A local @Tool method.
    static interface 
    One or more MCP servers.
    static interface 
    A programmatically registered tool.
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static ToolSource
    local(Method method)
    Creates a source for a local @Tool method.
    static ToolSource
    local(String declaringClassName, String methodName, List<String> parameterTypeNames)
    Creates a source for a local @Tool method from its descriptive data, without reflection.
    static ToolSource
    mcp(String serverKey)
    Creates a source for a single MCP server.
    static ToolSource
    mcp(Collection<String> serverKeys)
    Creates a source spanning several MCP servers, for example tools that list or read resources across multiple MCP clients.
    static ToolSource
    Creates a source for a tool registered programmatically, without an explicit source.
  • Method Details

    • local

      static ToolSource local(Method method)
      Creates a source for a local @Tool method. Only the declaring class name, the method name and the parameter type names are extracted; the Method itself is not retained. The declaring class name uses the format returned by Class.getName() (a binary name, for example com.acme.Outer$Inner); parameter type names use the format returned by Class.getTypeName() (for example int, java.lang.String[], or com.acme.Outer$Inner[]). These formats allow equality with a source created by local(String, String, List).
      Parameters:
      method - the tool method, must not be null.
      Returns:
      the source.
    • local

      static ToolSource local(String declaringClassName, String methodName, List<String> parameterTypeNames)
      Creates a source for a local @Tool method from its descriptive data, without reflection. To compare equal to a source created by local(Method), declaringClassName must use the format returned by Class.getName() (a binary name), and each parameter type name must use the format returned by Class.getTypeName() (including binary names for nested classes and a [] suffix for arrays).
      Parameters:
      declaringClassName - the class name as returned by Class.getName(), must not be blank.
      methodName - the method name, must not be blank.
      parameterTypeNames - the parameter type names as returned by Class.getTypeName(), in declaration order (distinguishes overloads); must not be null or contain blank elements. May be empty.
      Returns:
      the source.
    • mcp

      static ToolSource mcp(String serverKey)
      Creates a source for a single MCP server.
      Parameters:
      serverKey - the key identifying the MCP server, must not be blank.
      Returns:
      the source.
    • mcp

      static ToolSource mcp(Collection<String> serverKeys)
      Creates a source spanning several MCP servers, for example tools that list or read resources across multiple MCP clients. Duplicate keys are collapsed; the iteration order of ToolSource.Mcp.serverKeys() follows the first occurrence of each key in the given collection.

      An empty collection gives a source for MCP servers whose keys are not known (their clients have no key): it is still an MCP source, but ToolSource.Mcp.involves(String) is false for every key.

      Parameters:
      serverKeys - the keys identifying the MCP servers, must not be null or contain blank keys. May be empty.
      Returns:
      the source.
    • programmatic

      static ToolSource programmatic()
      Creates a source for a tool registered programmatically, without an explicit source. LangChain4j AI Services use it for a tool registered without a source whose executor does not run a local method (a tool backed by their default executor of @Tool methods is local, however it is registered).
      Returns:
      the source.