A Java implementation of a Model Context Protocol (MCP) server that provides example tools for Claude Desktop.
This MCP server provides three example tools:
- calculate - Perform basic math operations (add, subtract, multiply, divide)
- reverse_text - Reverse any text string
- word_count - Analyze text and return statistics (word count, character count, line count, sentence count)
- Java 17 or higher
- Gradle 8.5 or higher (wrapper included)
- Clone or navigate to the project directory:
cd /Users/steveo/projects/my-mcp-server-java- Build the project using Gradle:
./gradlew buildThis will create an executable JAR file at build/libs/my-mcp-server-1.0.0.jar
You can test the server by running it directly:
java -jar build/libs/my-mcp-server-1.0.0.jarThe server will start and wait for JSON-RPC messages via stdin/stdout.
Alternatively, you can run it using Gradle:
./gradlew runTo use this server with Claude Desktop, add it to your Claude configuration file:
On macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
On Windows: %APPDATA%\Claude\claude_desktop_config.json
Add the following configuration:
{
"mcpServers": {
"my-mcp-server-java": {
"command": "java",
"args": [
"-jar",
"/Users/steveo/projects/my-mcp-server-java/build/libs/my-mcp-server-1.0.0.jar"
]
}
}
}Important: Update the path above to match your actual installation location.
After adding the configuration:
- Restart Claude Desktop
- The tools should now be available in Claude
- Try asking Claude to:
- "calculate 5 + 3"
- "reverse the text hello world"
- "analyze the word count of this text: The quick brown fox jumps over the lazy dog"
my-mcp-server-java/
├── build.gradle # Gradle build configuration
├── settings.gradle # Gradle settings
├── gradlew # Gradle wrapper script (Unix)
├── gradlew.bat # Gradle wrapper script (Windows)
├── gradle/
│ └── wrapper/ # Gradle wrapper files
└── src/
└── main/
└── java/
└── com/
└── example/
└── mcp/
├── MCPServer.java # Main server implementation
├── Tool.java # Tool interface
├── ToolRegistry.java # Tool management
├── TextContent.java # Response content wrapper
├── CalculateTool.java # Calculator tool
├── ReverseTextTool.java # Text reversal tool
└── WordCountTool.java # Word count tool
To add new tools to your server:
- Create a new class that implements the
Toolinterface - Implement the three required methods:
getName()- returns the tool namegetDefinition(ObjectMapper mapper)- returns the JSON tool definitionexecute(JsonNode arguments)- executes the tool logic
- Register the tool in
MCPServer.registerTools()method - Rebuild the project:
./gradlew build - Restart Claude Desktop to reload the server
Example tool implementation:
public class MyTool implements Tool {
@Override
public String getName() {
return "my_tool";
}
@Override
public JsonNode getDefinition(ObjectMapper mapper) {
ObjectNode tool = mapper.createObjectNode();
tool.put("name", "my_tool");
tool.put("description", "Description of what my tool does");
// Define input schema
ObjectNode inputSchema = mapper.createObjectNode();
inputSchema.put("type", "object");
// ... add properties
tool.set("inputSchema", inputSchema);
return tool;
}
@Override
public List<TextContent> execute(JsonNode arguments) throws Exception {
// Tool implementation
String result = "Tool result";
return Collections.singletonList(new TextContent(result));
}
}# Build the project
./gradlew build
# Run tests
./gradlew test
# Create JAR file
./gradlew shadowJar
# Clean build artifacts
./gradlew cleanThe server uses Java's built-in logging (java.util.logging) which logs to stderr by default to avoid corrupting the JSON-RPC messages sent via stdout. You can check the logs in Claude Desktop's developer console or your terminal.
This is a standalone implementation that demonstrates the MCP protocol without relying on an official Java SDK (which may not be available yet). The server:
- Uses JSON-RPC 2.0 for communication
- Implements the MCP protocol specification
- Communicates via stdin/stdout
- Uses Jackson for JSON processing
- Follows the same architecture as the Python reference implementation
This is example code for demonstration purposes.