Interface McpClient

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
DefaultMcpClient

public interface McpClient extends AutoCloseable
Represents a client that can communicate with an MCP server over a given transport protocol, retrieve and execute tools using the server.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Performs a health check that returns normally if the MCP server is reachable and properly responding to ping requests.
    dev.langchain4j.service.tool.ToolExecutionResult
    executeTool(dev.langchain4j.agent.tool.ToolExecutionRequest executionRequest)
    Executes a tool on the MCP server and returns the result.
    dev.langchain4j.service.tool.ToolExecutionResult
    executeTool(dev.langchain4j.agent.tool.ToolExecutionRequest executionRequest, dev.langchain4j.invocation.InvocationContext invocationContext)
    Executes a tool on the MCP server and returns the result.
    getPrompt(String name, Map<String,Object> arguments)
    Render the contents of a prompt.
    key()
    Returns the unique key of this client.
    Obtain a list of prompts available on the MCP server.
    Obtains the current list of resources available on the MCP server.
    listResources(dev.langchain4j.invocation.InvocationContext invocationContext)
    Obtains the current list of resources available on the MCP server.
    Obtains the current list of resource templates (dynamic resources) available on the MCP server.
    listResourceTemplates(dev.langchain4j.invocation.InvocationContext invocationContext)
    Obtains the current list of resource templates (dynamic resources) available on the MCP server.
    List<dev.langchain4j.agent.tool.ToolSpecification>
    Obtains a list of tools from the MCP server.
    List<dev.langchain4j.agent.tool.ToolSpecification>
    listTools(dev.langchain4j.invocation.InvocationContext invocationContext)
    Obtains a list of tools from the MCP server.
    Retrieves the contents of the resource with the specified URI.
    readResource(String uri, dev.langchain4j.invocation.InvocationContext invocationContext)
    Retrieves the contents of the resource with the specified URI.
    void
    Sets the roots that are made available to the server upon its request.
    void
    Subscribes to updates for the resource with the specified URI.
    void
    Unsubscribes from updates for the resource with the specified URI.

    Methods inherited from interface AutoCloseable

    close
  • Method Details

    • key

      String key()
      Returns the unique key of this client.
    • listTools

      List<dev.langchain4j.agent.tool.ToolSpecification> listTools()
      Obtains a list of tools from the MCP server.
    • listTools

      List<dev.langchain4j.agent.tool.ToolSpecification> listTools(dev.langchain4j.invocation.InvocationContext invocationContext)
      Obtains a list of tools from the MCP server.
    • executeTool

      dev.langchain4j.service.tool.ToolExecutionResult executeTool(dev.langchain4j.agent.tool.ToolExecutionRequest executionRequest)
      Executes a tool on the MCP server and returns the result. Currently, this expects a tool execution to only contain text-based results or JSON structured content.
    • executeTool

      dev.langchain4j.service.tool.ToolExecutionResult executeTool(dev.langchain4j.agent.tool.ToolExecutionRequest executionRequest, dev.langchain4j.invocation.InvocationContext invocationContext)
      Executes a tool on the MCP server and returns the result. Currently, this expects a tool execution to only contain text-based results or JSON structured content.
    • listResources

      List<McpResource> listResources()
      Obtains the current list of resources available on the MCP server.
    • listResources

      List<McpResource> listResources(dev.langchain4j.invocation.InvocationContext invocationContext)
      Obtains the current list of resources available on the MCP server.
    • listResourceTemplates

      List<McpResourceTemplate> listResourceTemplates()
      Obtains the current list of resource templates (dynamic resources) available on the MCP server.
    • listResourceTemplates

      List<McpResourceTemplate> listResourceTemplates(dev.langchain4j.invocation.InvocationContext invocationContext)
      Obtains the current list of resource templates (dynamic resources) available on the MCP server.
    • readResource

      McpReadResourceResult readResource(String uri)
      Retrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates).
    • readResource

      McpReadResourceResult readResource(String uri, dev.langchain4j.invocation.InvocationContext invocationContext)
      Retrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates).
    • subscribeToResource

      void subscribeToResource(String uri)
      Subscribes to updates for the resource with the specified URI. When the resource changes, the server will send a notifications/resources/updated notification. The client will invoke the onResourceUpdated callback (if configured) with the URI of the updated resource.
    • unsubscribeFromResource

      void unsubscribeFromResource(String uri)
      Unsubscribes from updates for the resource with the specified URI.
    • listPrompts

      List<McpPrompt> listPrompts()
      Obtain a list of prompts available on the MCP server.
    • getPrompt

      McpGetPromptResult getPrompt(String name, Map<String,Object> arguments)
      Render the contents of a prompt.
    • checkHealth

      void checkHealth()
      Performs a health check that returns normally if the MCP server is reachable and properly responding to ping requests. If this method throws an exception, the health of this MCP client is considered degraded.
    • setRoots

      void setRoots(List<McpRoot> roots)
      Sets the roots that are made available to the server upon its request. After calling this method, the client also sends a `notifications/roots/list_changed` message to the server.