Class McpToolProvider

java.lang.Object
dev.langchain4j.mcp.McpToolProvider
All Implemented Interfaces:
dev.langchain4j.service.tool.ToolProvider

public class McpToolProvider extends Object implements dev.langchain4j.service.tool.ToolProvider
A tool provider backed by one or more MCP clients.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    McpToolProvider(List<McpClient> mcpClients, boolean failIfOneServerFails, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter, Function<dev.langchain4j.service.tool.ToolExecutor, dev.langchain4j.service.tool.ToolExecutor> toolWrapper, McpResourcesAsToolsPresenter resourcesAsToolsPresenter, BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, String> toolNameMapper, BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, dev.langchain4j.agent.tool.ToolSpecification> toolSpecificationMapper)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addFilter(BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> filter)
    Adds a tools filter that will act in conjunction (AND) with the eventually existing ones.
    void
    Adds a new MCP client to the list of clients.
     
    dev.langchain4j.service.tool.ToolProviderResult
    provideTools(dev.langchain4j.service.tool.ToolProviderRequest request)
     
    protected dev.langchain4j.service.tool.ToolProviderResult
    provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter)
     
    protected dev.langchain4j.service.tool.ToolProviderResult
    provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter, List<McpClient> consideredMcpClients)
     
    protected dev.langchain4j.service.tool.ToolProviderResult
    provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, List<McpClient> consideredMcpClients)
     
    void
    Removes an MCP client from the list of clients.
    void
    Resets the all the eventually existing tools filters.
    void
    setFilter(BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> filter)
    Sets the tools filter overriding the eventually existing ones.
    void
    setToolNameMapper(BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, String> toolNameMapper)
    Sets the tool name mapper overriding the current one.
    void
    setToolSpecificationMapper(BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, dev.langchain4j.agent.tool.ToolSpecification> toolSpecificationMapper)
    Sets the tool specification mapper overriding the current one.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface dev.langchain4j.service.tool.ToolProvider

    isDynamic
  • Constructor Details

    • McpToolProvider

      protected McpToolProvider(List<McpClient> mcpClients, boolean failIfOneServerFails, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter, Function<dev.langchain4j.service.tool.ToolExecutor, dev.langchain4j.service.tool.ToolExecutor> toolWrapper, McpResourcesAsToolsPresenter resourcesAsToolsPresenter, BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, String> toolNameMapper, BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, dev.langchain4j.agent.tool.ToolSpecification> toolSpecificationMapper)
  • Method Details

    • addMcpClient

      public void addMcpClient(McpClient client)
      Adds a new MCP client to the list of clients.
      Parameters:
      client - the MCP client to add
    • removeMcpClient

      public void removeMcpClient(McpClient client)
      Removes an MCP client from the list of clients.
      Parameters:
      client - the MCP client to remove
    • addFilter

      public void addFilter(BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> filter)
      Adds a tools filter that will act in conjunction (AND) with the eventually existing ones.
      Parameters:
      filter - the filter to add
    • setFilter

      public void setFilter(BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> filter)
      Sets the tools filter overriding the eventually existing ones.
      Parameters:
      filter - the filter to add
    • setToolNameMapper

      public void setToolNameMapper(BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, String> toolNameMapper)
      Sets the tool name mapper overriding the current one. The mapper can be null, in which case the tool names will be used as-is.
    • setToolSpecificationMapper

      public void setToolSpecificationMapper(BiFunction<McpClient, dev.langchain4j.agent.tool.ToolSpecification, dev.langchain4j.agent.tool.ToolSpecification> toolSpecificationMapper)
      Sets the tool specification mapper overriding the current one. The mapper can be null, in which case the tool specifications will be used as-is.
    • resetFilters

      public void resetFilters()
      Resets the all the eventually existing tools filters.
    • provideTools

      public dev.langchain4j.service.tool.ToolProviderResult provideTools(dev.langchain4j.service.tool.ToolProviderRequest request)
      Specified by:
      provideTools in interface dev.langchain4j.service.tool.ToolProvider
    • provideTools

      protected dev.langchain4j.service.tool.ToolProviderResult provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter)
    • provideTools

      protected dev.langchain4j.service.tool.ToolProviderResult provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, List<McpClient> consideredMcpClients)
    • provideTools

      protected dev.langchain4j.service.tool.ToolProviderResult provideTools(dev.langchain4j.service.tool.ToolProviderRequest request, BiPredicate<McpClient, dev.langchain4j.agent.tool.ToolSpecification> mcpToolsFilter, List<McpClient> consideredMcpClients)
    • builder

      public static McpToolProvider.Builder builder()