Class MCPClient

java.lang.Object
com.google.genkit.plugins.mcp.MCPClient

public class MCPClient extends Object
MCP Client that manages connections to MCP servers and provides access to their tools and resources.

This client wraps the MCP Java SDK and converts MCP tools to Genkit tools, allowing them to be used seamlessly in Genkit applications.

Example usage:


 MCPClient client = new MCPClient("filesystem",
 		MCPServerConfig.stdio("npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"),
 		Duration.ofSeconds(30), false);

 client.connect();
 List<Tool<?, ?>> tools = client.getTools(registry);
 client.disconnect();
 
  • Constructor Details

    • MCPClient

      public MCPClient(String serverName, MCPServerConfig config, Duration requestTimeout, boolean rawToolResponses)
      Creates a new MCP client.
      Parameters:
      serverName - the name to identify this server
      config - the server configuration
      requestTimeout - timeout for requests
      rawToolResponses - whether to return raw MCP responses
  • Method Details

    • connect

      public void connect() throws GenkitException
      Connects to the MCP server.
      Throws:
      GenkitException - if connection fails
    • disconnect

      public void disconnect()
      Disconnects from the MCP server.
    • getTools

      public List<Tool<?,?>> getTools(Registry registry) throws GenkitException
      Gets tools from the MCP server as Genkit tools.
      Parameters:
      registry - the Genkit registry for tool registration
      Returns:
      list of Genkit tools
      Throws:
      GenkitException - if listing tools fails
    • getResources

      public List<MCPResource> getResources() throws GenkitException
      Gets resources from the MCP server.
      Returns:
      list of MCP resources
      Throws:
      GenkitException - if listing resources fails
    • readResource

      public MCPResourceContent readResource(String uri) throws GenkitException
      Reads a resource by URI.
      Parameters:
      uri - the resource URI
      Returns:
      the resource content
      Throws:
      GenkitException - if reading fails
    • callTool

      public Object callTool(String toolName, Map<String,Object> arguments) throws GenkitException
      Calls an MCP tool directly.
      Parameters:
      toolName - the tool name
      arguments - the tool arguments
      Returns:
      the tool result
      Throws:
      GenkitException - if the call fails
    • getServerName

      public String getServerName()
      Gets the server name.
      Returns:
      the server name
    • isConnected

      public boolean isConnected()
      Checks if connected to the MCP server.
      Returns:
      true if connected