Package com.google.genkit.plugins.mcp
Class MCPClient
java.lang.Object
com.google.genkit.plugins.mcp.MCPClient
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 Summary
ConstructorsConstructorDescriptionMCPClient(String serverName, MCPServerConfig config, Duration requestTimeout, boolean rawToolResponses) Creates a new MCP client. -
Method Summary
Modifier and TypeMethodDescriptionCalls an MCP tool directly.voidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Gets resources from the MCP server.Gets the server name.Gets tools from the MCP server as Genkit tools.booleanChecks if connected to the MCP server.readResource(String uri) Reads a resource by URI.
-
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 serverconfig- the server configurationrequestTimeout- timeout for requestsrawToolResponses- whether to return raw MCP responses
-
-
Method Details
-
connect
Connects to the MCP server.- Throws:
GenkitException- if connection fails
-
disconnect
public void disconnect()Disconnects from the MCP server. -
getTools
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
Gets resources from the MCP server.- Returns:
- list of MCP resources
- Throws:
GenkitException- if listing resources fails
-
readResource
Reads a resource by URI.- Parameters:
uri- the resource URI- Returns:
- the resource content
- Throws:
GenkitException- if reading fails
-
callTool
Calls an MCP tool directly.- Parameters:
toolName- the tool namearguments- the tool arguments- Returns:
- the tool result
- Throws:
GenkitException- if the call fails
-
getServerName
Gets the server name.- Returns:
- the server name
-
isConnected
public boolean isConnected()Checks if connected to the MCP server.- Returns:
- true if connected
-