Class McpToolExecutor

java.lang.Object
dev.langchain4j.mcp.McpToolExecutor
All Implemented Interfaces:
ToolExecutor

public class McpToolExecutor extends Object implements ToolExecutor
Since:
1.4.0
  • Constructor Details

    • McpToolExecutor

      public McpToolExecutor(McpClient mcpClient)
    • McpToolExecutor

      public McpToolExecutor(McpClient mcpClient, String fixedToolName)
  • Method Details

    • execute

      public String execute(ToolExecutionRequest executionRequest, Object memoryId)
      Description copied from interface: ToolExecutor
      Executes a tool request.
      Specified by:
      execute in interface ToolExecutor
      Parameters:
      executionRequest - The tool execution request. Contains tool name and arguments.
      memoryId - The ID of the chat memory. .
      Returns:
      The result of the tool execution that will be sent to the LLM.
    • executeWithContext

      public ToolExecutionResult executeWithContext(ToolExecutionRequest executionRequest, InvocationContext invocationContext)
      Description copied from interface: ToolExecutor
      Executes a tool request. Override this method if you wish to:
      - access the InvocationParameters when passing extra data into the tool
      - propagate the tool result object (ToolExecutionResult.result()) into the ToolExecution
      
      Specified by:
      executeWithContext in interface ToolExecutor
      Parameters:
      executionRequest - The tool execution request. Contains tool name and arguments.
      invocationContext - The AI Service invocation context, contains ChatMemory ID (see MemoryId for more details), and InvocationParameters.
      Returns:
      The result of the tool execution that will be sent to the LLM.
    • executeAsync

      public CompletableFuture<ToolExecutionResult> executeAsync(ToolExecutionRequest executionRequest, InvocationContext invocationContext)
      Non-blocking counterpart of ToolExecutor.executeWithContext(ToolExecutionRequest, InvocationContext), invoked by the asynchronous AI Service tool loop (AI Service methods returning CompletableFuture or CompletionStage), which composes the returned future instead of waiting on a thread.

      The default implementation returns a failed future carrying AsyncNotSupportedException: asynchronous AI Services are opt-in, and silently executing a tool synchronously there would block the thread delivering model responses without any visible signal. This failure is not passed to the tool error handlers (and thus never reaches the LLM) — it fails the AI Service invocation, making the gap visible.

      Override this method to use this tool with an asynchronous AI Service. If the tool performs I/O that can be initiated without holding a thread (e.g. it delegates to an asynchronous client), return that future. If blocking execution is acceptable, it can simply return CompletableFuture.completedFuture(executeWithContext(request, context)).

      Errors may be signaled either synchronously (thrown from this method) or via a failed future; the AI Service applies the configured tool error handlers to both identically.

      Non-blocking: delegates to McpClient.executeToolAsync(ToolExecutionRequest, InvocationContext), so no thread is held while the tool executes on the MCP server.

      Specified by:
      executeAsync in interface ToolExecutor
      Parameters:
      executionRequest - The tool execution request. Contains tool name and arguments.
      invocationContext - The AI Service invocation context, contains ChatMemory ID (see MemoryId for more details), and InvocationParameters.
      Returns:
      a CompletableFuture of the result of the tool execution that will be sent to the LLM