How to Build a Java MCP Server: Step-by-Step Guide for AI Integration

Learn how to build a production-ready Java MCP server for AI and API integration. This step-by-step guide covers setup, tool registration, testing, and best practices for secure, scalable LLM workflows—plus how Apidog fits into your API quality strategy.

Ashley Goolam

Ashley Goolam

20 January 2026

How to Build a Java MCP Server: Step-by-Step Guide for AI Integration

Modern AI development increasingly depends on integrating large language models (LLMs) with external tools and real-time data. The Model Context Protocol (MCP) offers a standardized way for LLMs to securely access APIs, functions, and resources during conversations. This guide will show backend engineers and API developers how to build a robust Java MCP server using the official Java SDK—enabling seamless AI-to-API connectivity for your applications.

button

What Is a Java MCP Server? (And Why Build One)

An MCP server acts as a bridge between AI clients (like LLMs) and custom tools, APIs, or datasets. By exposing tools, resources, and interaction prompts, MCP servers empower AI agents to:

A Java MCP server using the official SDK provides:


Prerequisites: Setting Up Your Java MCP Environment

Before creating your MCP server, ensure you have:


Step 1: Start Your Java MCP Project

  1. Open your terminal and run:

    git clone https://github.com/danvega/javaone-mcp.git
    cd javaone-mcp
    
  2. Build the project with Maven:

    mvn clean package
    

    The executable JAR will be in target/javaone-mcp-0.0.2.jar.

clone github repo

Option B: Create a Project from Scratch

  1. Launch IntelliJ IDEA and create a New Project using Maven.
  2. Set your project name (e.g., java-mcp-server) and confirm.

create a new project

  1. Open your pom.xml and add dependencies:
    <dependencies>
      <!-- MCP SDK -->
      <dependency>
        <groupId>io.modelcontextprotocol.sdk</groupId>
        <artifactId>mcp</artifactId>
        <version>0.9.0</version>
      </dependency>
      <!-- Logging -->
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.16</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.16</version>
      </dependency>
    </dependencies>
    

Step 2: Explore the Project Structure

The sample project includes:

project structure


Step 3: Define Your Data Model

Create a minimal record for your data, for example, presentations or games:

public record Presentation(String title, String url) { }

This holds two fields: title (e.g., "GTA 6") and url.


Step 4: Implement MCP Tools

Define your MCP tools in PresentationTools.java so AI clients can query available data:

import io.modelcontextprotocol.sdk.McpSchema;
import java.util.ArrayList;
import java.util.List;

public class PresentationTools {
    private final List<Presentation> presentations = new ArrayList<>();

    public PresentationTools() {
        presentations.add(new Presentation("GTA 6", "https://example.com/gta6"));
        presentations.add(new Presentation("FC 26", "https://example.com/fc26"));
        presentations.add(new Presentation("Call of Duty: Black Ops 7", "https://example.com/codbo7"));
    }

    @McpSchema.Tool(name = "get_presentations", description = "Returns a list of all available games")
    public List<Presentation> getPresentations() {
        return presentations;
    }

    @McpSchema.Tool(name = "get_game", description = "Returns a specific game by title")
    public Presentation getGame(String title) {
        return presentations.stream()
            .filter(p -> p.title().equalsIgnoreCase(title))
            .findFirst()
            .orElse(null);
    }
}

These methods empower AI clients to fetch all games or query by title.


Step 5: Configure and Launch the MCP Server

Set up your main application in Application.java:

import io.modelcontextprotocol.sdk.McpServer;
import io.modelcontextprotocol.sdk.McpSchema;
import io.modelcontextprotocol.sdk.McpSyncServer;

public class Application {
    public static void main(String[] args) {
        PresentationTools tools = new PresentationTools();
        var syncToolSpecification = new McpServerFeatures.SyncToolSpecification(
            new McpSchema.Tool("get_presentations", "Returns a list of all available games", null),
            (exchange, arguments) -> tools.getPresentations()
        );

        McpSyncServer syncServer = McpServer.sync(new StdioTransportProvider())
            .serverInfo("javaone-mcp-server", "0.0.1")
            .capabilities(McpSchema.ServerCapabilities.builder().tools(true).logging().build())
            .tools(syncToolSpecification)
            .build();

        syncServer.start();
    }
}

This registers the get_presentations tool and starts the server with STDIO transport.


Step 6: Build and Run Your Java MCP Server

  1. Build the project:
    mvn clean package
    

run the server

  1. Start the server:
    java -jar target/javaone-mcp-0.0.2.jar
    
    Your server is now running and ready for AI integration.

Step 7: Test with MCP Inspector

The MCP Inspector helps debug and validate your server.


Step 8: Connect to AI Clients (Claude Desktop or Cursor)

Integrate your Java MCP server with advanced AI clients:

edit cursor mcp configuration


Step 9: Validate Integration

available games on my mcp server


Step 10: Extend Your Java MCP Server for More Power

1. Add More Data

Enhance PresentationTools.java:

presentations.add(new Presentation("Spring Boot 3.5 Deep Dive", "https://example.com/spring-boot", 2025));

Add a search tool:

@McpSchema.Tool(name = "search_presentations", description = "Search presentations by title")
public List<Presentation> searchPresentations(String query) {
    return presentations.stream()
        .filter(p -> p.title().toLowerCase().contains(query.toLowerCase()))
        .collect(Collectors.toList());
}

Register the search tool in Application.java:

var searchToolSpec = new McpServerFeatures.SyncToolSpecification(
    new McpSchema.Tool("search_presentations", "Search presentations by title", null),
    (exchange, arguments) -> {
        String query = arguments.get("query").asText();
        return tools.searchPresentations(query);
    }
);

McpSyncServer syncServer = McpServer.sync(new StdioTransportProvider())
    .serverInfo("javaone-mcp-server", "0.0.1")
    .capabilities(McpSchema.ServerCapabilities.builder().tools(true).logging().build())
    .tools(syncToolSpecification, searchToolSpec)
    .build();

new tool added to java mcp server

Try a search command:

new tool results

3. Go Further: Advanced Features


Troubleshooting Common Issues


Security & Authorization

The Java MCP SDK is security-agnostic by default, letting you connect your preferred security solution (Spring Security, JWT, etc.) for fine-grained access control. Use SDK hooks to enforce custom authorization or integrate with your enterprise policies.


Ensuring Quality: Testing Your MCP Server

For a production-ready MCP server:


Apidog: API Testing for MCP-Driven Workflows

MCP servers often work alongside classic REST, GraphQL, or WebSocket APIs. While MCP is for AI-to-tool communication, Apidog helps you ensure your API endpoints remain robust as your system evolves.

With Apidog you can:

This lets you confidently expose your APIs for both human and AI clients, ensuring reliability across your MCP-integrated architecture.

Automated API Test Case Generation in Apidog

Automated API Test Case Generation in Apidog

button

Frequently Asked Questions

Q1. What is MCP?
MCP (Model Context Protocol) standardizes communication between AI clients and servers exposing tools/data.

Q2. Which Java versions are supported?
Java 17 or higher is required for the official Java MCP SDK.

Q3. How do clients connect to the MCP server?
Via STDIO or streaming protocols like SSE or HTTP streaming.

Q4. Can MCP servers coexist with existing web APIs?
Yes. MCP endpoints can run alongside REST/GraphQL APIs on shared infrastructure.

Q5. Do I need Spring for MCP?
No. Pure Java or lightweight transport layers work; Spring integration is optional.


Conclusion

Building a Java MCP server with the Java SDK allows API teams and backend engineers to expose powerful capabilities directly to AI agents, transforming how business logic and data are accessed in modern applications. By following these steps, you can quickly develop, test, and extend your MCP server for real-world AI workflows.

For comprehensive API quality and contract validation, integrate Apidog into your workflow—ensuring all your endpoints (MCP and classic APIs) deliver reliable, secure, and testable results for both human and AI clients.

button

Explore more

How to Use Claude Code for CI/CD Workflows

How to Use Claude Code for CI/CD Workflows

Technical guide to integrating Claude Code into CI/CD pipelines. Covers container setup, GitHub Actions/GitLab CI integration, skill development, and practical workflows for DevOps automation.

21 January 2026

How to Use Claude Code Skills for API Request/Networking (data-fetching)

How to Use Claude Code Skills for API Request/Networking (data-fetching)

Technical guide to using Claude Code skills for API networking. Covers setup, core request patterns, advanced scenarios, and practical examples for building AI-driven data-fetching workflows.

21 January 2026

How to Use Claude Code Skills for Building UI

How to Use Claude Code Skills for Building UI

Technical guide to using Claude Code skills for UI development. Covers setup, core tools, advanced patterns, and debugging for building production-ready React/Vue/Svelte components.

21 January 2026

Practice API Design-first in Apidog

Discover an easier way to build and use APIs