MCPX4J is a Java client library for MCP.RUN, enabling developers to easily integrate and invoke installed tools using a simple API with features like installation caching and customizable JSON decoding.
The MCP.RUN client library for Java!
To use MCPX4J, add the following dependency to your project's pom.xml
:
<dependencies>
<dependency>
<groupId>com.dylibso.mcpx4j</groupId>
<artifactId>mcpx4j</artifactId>
<version>${mcpx4j.version}</version>
</dependency>
</dependencies>
Create a Mcpx
instance using your API key to interact with your installed tools. Refresh installations with refreshInstallations()
. Retrieve a servlet and tool by their IDs, then call the tool with a JSON payload.
import com.github.dylibso.mcpx4j.Mcpx;
Mcpx mcpx = Mcpx.forApiKey(apiKey).build();
mcpx.refreshInstallations();
var servlet = mcpx.get("my-servlet-id");
var tool = servlet.get("my-tool-id");
var bytes = servlet.call("""
{
"method": "tools/call",
"params": {
"name": "my-tool-id",
"arguments": {
...
}
}
}
""");
Mcpx
caches installations, refresh it using refreshInstallations()
. Schedule refreshes in a separate thread.
var scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(mcpx::refreshInstallations, 0, 5, TimeUnit.MINUTES);
Customize the Mcpx
instance with an alternative base URL, JSON decoder, and HTTP client.
var mcpx = Mcpx.forApiKey(apiKey)
.withBaseUrl(jsonDecoder)
...
.build();
Use the default JacksonDecoder
or the Jakarta JSON-P
implementation. Include the corresponding dependencies.
Use the default JdkHttpClientAdapter
or the lighter HttpUrlConnectionClientAdapter
.
Configure servlet options, such as ChicoryHttpConfig
, ChicoryLogger
, and AoT
(Ahead-of-Time compilation), propagated to the Chicory Extism SDK.
dylibso/mcpx4j
January 7, 2025
March 27, 2025
Java