Created
March 24, 2025 20:51
-
-
Save jesuino/a6ba7fdce74ea764da9b275cada25629 to your computer and use it in GitHub Desktop.
MCP Java SDK: Simple Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.fxapps.ollamamcp; | |
import java.util.Map; | |
import io.modelcontextprotocol.client.McpClient; | |
import io.modelcontextprotocol.client.transport.ServerParameters; | |
import io.modelcontextprotocol.client.transport.StdioClientTransport; | |
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest; | |
import io.modelcontextprotocol.spec.McpSchema.TextContent; | |
public class App { | |
private static final String[] SERVER_PARAMS = { | |
"jdbc@quarkiverse/quarkus-mcp-servers" | |
}; | |
public static void main(String[] args) { | |
var params = ServerParameters.builder("jbang") | |
.args(SERVER_PARAMS) | |
.build(); | |
var transport = new StdioClientTransport(params); | |
var client = McpClient.sync(transport).build(); | |
client.initialize(); | |
System.out.println("Available tools:"); | |
client.listTools() | |
.tools() | |
.stream() | |
.map(tool -> tool.name() + ": " + tool.description()) | |
.forEach(System.out::println); | |
System.out.println("Tools Parameters:"); | |
client.listTools() | |
.tools() | |
.stream() | |
.forEach(tool -> { | |
System.out.println(tool.name() + "\n-"); | |
var properties = tool.inputSchema().properties(); | |
properties.forEach((k, v) -> { | |
System.out.println(k + ": " + v); | |
}); | |
if (properties.isEmpty()) { | |
System.out.println("no params"); | |
} | |
System.out.println("-----"); | |
}); | |
var sql = """ | |
CREATE TABLE users ( | |
id INT PRIMARY KEY, | |
name VARCHAR(100), | |
age INT | |
); | |
"""; | |
var result = client.callTool(new CallToolRequest("write_query", | |
Map.of("query", sql))).content(); | |
if (result.size() > 0 && | |
result.get(0) instanceof TextContent content) { | |
System.out.println(content.text()); | |
} | |
client.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.fxapps.ollamamcp</groupId> | |
<artifactId>mcp-client</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<name>ollama-mcp</name> | |
<!-- FIXME change it to the project's website --> | |
<url>http://www.example.com</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven.compiler.release>24</maven.compiler.release> | |
<maven.compiler.source>24</maven.compiler.source> | |
<maven.compiler.target>24</maven.compiler.target> | |
<mcpsdk.version>0.8.0</mcpsdk.version> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>io.modelcontextprotocol.sdk</groupId> | |
<artifactId>mcp-bom</artifactId> | |
<version>${mcpsdk.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>io.modelcontextprotocol.sdk</groupId> | |
<artifactId>mcp</artifactId> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment