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.

20
1

MCPX4J

The MCP.RUN client library for Java!

Installation

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>

Quick Start

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": {
            ...
        }  
    }
}
""");

Plugin Updates

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);

Configuration

Customize the Mcpx instance with an alternative base URL, JSON decoder, and HTTP client.

var mcpx = Mcpx.forApiKey(apiKey)
    .withBaseUrl(jsonDecoder)
    ...
    .build();

JSON Decoder

Use the default JacksonDecoder or the Jakarta JSON-P implementation. Include the corresponding dependencies.

HTTP Client

Use the default JdkHttpClientAdapter or the lighter HttpUrlConnectionClientAdapter.

Servlet Options

Configure servlet options, such as ChicoryHttpConfig, ChicoryLogger, and AoT (Ahead-of-Time compilation), propagated to the Chicory Extism SDK.

Repository

DY
dylibso

dylibso/mcpx4j

Created

January 7, 2025

Updated

March 27, 2025

Language

Java

Category

Developer Tools