Skip to main content
AIDA Spring 2026
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

MCP Tutorial

Build Your First MCP Server in Java (Codex + Claude CLI)

In this tutorial, you will:

  • build a simple MCP server in Java
  • expose a tool (add_numbers)
  • build the project in IntelliJ
  • connect it to Codex and Claude CLI
  • call it from natural language

What is MCP (short version)

MCP (Model Context Protocol) lets AI tools like Codex and Claude:

call your code as tools

Think of it like:

  • REST → for frontend
  • MCP → for AI

Step 1 — Create a simple Java project

Create a new Maven project in IntelliJ:

  1. Open IntelliJ → New Project
  2. Choose Maven
  3. Set:
    • GroupId: demo
    • ArtifactId: mcp-demo
  4. Click Create

Step 2 — Add MCP dependency

Edit your pom.xml:

<dependencies>
    <dependency>
        <groupId>io.modelcontextprotocol</groupId>
        <artifactId>mcp</artifactId>
        <version>0.18.1</version>
    </dependency>
</dependencies>

Step 3 — Create your MCP server

Create a file:

src/main/java/demo/McpServerApp.java

Add this code:

package demo;

import io.modelcontextprotocol.server.McpServer;

public class McpServerApp {

    public static void main(String[] args) {

        var server = McpServer.builder()
                .name("demo-mcp-server")
                .version("1.0.0")
                .build();

        server.tool("add_numbers", "Add two numbers", input -> {
            int a = input.getInt("a");
            int b = input.getInt("b");

            return "Result: " + (a + b);
        });

        // Start MCP server over STDIO
        server.startStdio();
    }
}

What is STDIO?

STDIO = standard input/output

👉 Instead of HTTP, the AI talks to your program directly via:

  • input stream
  • output stream

✔ simplest setup ✔ perfect for local tools


Step 4 — Build the project in IntelliJ

Use IntelliJ (not terminal):

  1. Open the Maven tool window (right side)
  2. Expand your project → Lifecycle
  3. Double-click clean
  4. Double-click package

You should now have a jar in:

target/mcp-demo-1.0-SNAPSHOT.jar

Step 5 — Register the MCP server in Codex

Run:

codex mcp add demo-server \
  --command java \
  --arg -jar \
  --arg /FULL/PATH/TO/target/mcp-demo-1.0-SNAPSHOT.jar

👉 Replace with your actual path!


Step 6 — Register the same server in Claude CLI

Run:

claude mcp add demo-server \
  --command java \
  --arg -jar \
  --arg /FULL/PATH/TO/target/mcp-demo-1.0-SNAPSHOT.jar

👉 Use the same jar path as above.


Step 7 — Verify in both clients

Codex:

codex mcp list

Claude CLI:

claude mcp list

You should see demo-server in both outputs.


Step 8 — Use your tool from each client

In Codex, try:

Use the add_numbers tool with a=5 and b=7

In Claude CLI, try:

Use the add_numbers tool with a=5 and b=7

👉 Both clients will:

  1. detect your tool
  2. call your Java server
  3. return the result

What just happened?

You built:

AI → MCP → your Java code

Instead of:

Frontend → REST → backend

Key takeaway

MCP is just another interface to your backend logic


Try extending it

Add another tool:

server.tool("get_time", "Get current time", input -> {
    return java.time.Instant.now().toString();
});

Reflection questions

  • Where does the logic live?
  • What is the difference between MCP and REST?
  • Why might AI tools need this instead of HTTP APIs?

⚠️ Common issues

❌ Tool not found

  • Did you run codex mcp add or claude mcp add?
  • Did you restart the client?

❌ Nothing happens

  • Is the .jar path correct?
  • Does java -jar ... run manually?

❌ Crashes immediately

  • Check for missing dependencies
  • Run manually to debug:
java -jar target/mcp-demo-1.0-SNAPSHOT.jar

🧭 What’s next?

You can now:

  • connect MCP to real services (database, APIs)
  • combine with Javalin
  • build AI-powered backend features