Interface ToolSource
- All Known Subinterfaces:
ToolSource.Local, ToolSource.Mcp, ToolSource.Programmatic
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:
ToolSource.Local: a local method annotated with@ToolToolSource.Mcp: an MCP server, or several MCP servers (identified by their keys)ToolSource.Programmatic: a tool registered programmatically, without an explicit source
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 ClassesModifier and TypeInterfaceDescriptionstatic interfaceA local@Toolmethod.static interfaceOne or more MCP servers.static interfaceA programmatically registered tool. -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic ToolSourceCreates a source for a local@Toolmethod.static ToolSourceCreates a source for a local@Toolmethod from its descriptive data, without reflection.static ToolSourceCreates a source for a single MCP server.static ToolSourcemcp(Collection<String> serverKeys) Creates a source spanning several MCP servers, for example tools that list or read resources across multiple MCP clients.static ToolSourceCreates a source for a tool registered programmatically, without an explicit source.
-
Method Details
-
local
Creates a source for a local@Toolmethod. Only the declaring class name, the method name and the parameter type names are extracted; theMethoditself is not retained. The declaring class name uses the format returned byClass.getName()(a binary name, for examplecom.acme.Outer$Inner); parameter type names use the format returned byClass.getTypeName()(for exampleint,java.lang.String[], orcom.acme.Outer$Inner[]). These formats allow equality with a source created bylocal(String, String, List).- Parameters:
method- the tool method, must not benull.- Returns:
- the source.
-
local
static ToolSource local(String declaringClassName, String methodName, List<String> parameterTypeNames) Creates a source for a local@Toolmethod from its descriptive data, without reflection. To compare equal to a source created bylocal(Method),declaringClassNamemust use the format returned byClass.getName()(a binary name), and each parameter type name must use the format returned byClass.getTypeName()(including binary names for nested classes and a[]suffix for arrays).- Parameters:
declaringClassName- the class name as returned byClass.getName(), must not be blank.methodName- the method name, must not be blank.parameterTypeNames- the parameter type names as returned byClass.getTypeName(), in declaration order (distinguishes overloads); must not be null or contain blank elements. May be empty.- Returns:
- the source.
-
mcp
Creates a source for a single MCP server.- Parameters:
serverKey- the key identifying the MCP server, must not be blank.- Returns:
- the source.
-
mcp
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 ofToolSource.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)isfalsefor 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
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@Toolmethods is local, however it is registered).- Returns:
- the source.
-