DEV Community

Cover image for From Zero to AI Agent: My Journey into Java-based Intelligent Applications
Sebastiao Gazolla Jr
Sebastiao Gazolla Jr

Posted on

From Zero to AI Agent: My Journey into Java-based Intelligent Applications

How I went from "AI is just hype" to building production AI agents in Java

Mid-2024. I was scrolling through yet another "AI will replace developers" article when I decided to build something with AI. Not just calling an API and pretending it's magic, but really understanding how to make Java applications that think (I mean, agentic apps).

That curiosity turned into months of late-night coding, debugging mysterious errors, and slowly figuring out how to build AI agents that actually work. Or at least, start to work.

Why Java? (Yes, really)

Everyone kept telling me to use Python for AI. "That's where all the libraries are," they said. "Java is too verbose for AI," they said.

But here's the thing - I'm a seasoned Java developer and I know how to build applications in Java. And honestly? Most of the AI demos I saw were impressive but felt fragile.

I wanted to build something with good old Java code.

The Model Context Protocol Discovery

This is where things got interesting. In late 2024 Anthropic announced the Model Context Protocol (MCP). Think of it as a standardized way for AI models to talk to external tools and data sources.

So I trashed all my code that said "when user says X, call API Y,". MCP lets you connect AI models to tools dynamically. The AI can discover what tools are available and figure out how to use them based on natural language requests.

Here's what blew my mind: with MCP, I could say "save the weather forecast for NYC to a file" and the AI would:

  1. Understand it needs weather data
  2. Find the weather tool
  3. Get the forecast for NYC
  4. Find the file-writing tool
  5. Save the data

No hardcoded logic. No if-else chains. Just natural language instructions.

My First "Hello World" Moment

After finding the official Java SDK for MCP servers and clients and take a look in documentation, I came out with this:

import java.time.Duration;
import java.util.Map;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;

public class SimpleClientMCP {
    public static void main(String[] args) {
        int requestTimeoutSeconds = 30;
        String basePath = System.getProperty("user.home") + "/Documents";

        StdioClientTransport stdioTransport = new StdioClientTransport(
            ServerParameters.builder("cmd.exe")
                .args("/c", "npx @modelcontextprotocol/server-filesystem " + basePath)
                .build());

        McpSyncClient client = McpClient.sync(stdioTransport)
            .requestTimeout(Duration.ofSeconds(requestTimeoutSeconds))
            .build();

        client.initialize();

        CallToolRequest request = new CallToolRequest("write_file", Map.of(
            "path", basePath + "\\test.txt",
            "content", "Hello from Java MCP client!"
        ));

        CallToolResult result = client.callTool(request);
        System.out.println(result.toString());
    }
}
Enter fullscreen mode Exit fullscreen mode

When I ran this and saw "Hello from Java MCP client!" appear in a file created by my Java code through an AI protocol, it was awesome. But I noticed that nothing will repent magically.

What I Learned Right Away

Let me be honest about this sample:

Tool discovery was manual. I had to hardcode which servers to connect to and manually map user requests to specific tools. It worked, but it wasn't intelligent.

No natural language understanding. My first version was basically "if user says 'save file' then call write_file tool." Very brittle and not really AI-powered.

To make the magic happen, I needed to code a way to let LLM understand that it has to execute a tool and call it.

Where This Journey Led

That simple file-writing example was just the beginning. Over the next few months, I built:

  • A system that can connect to multiple MCP servers simultaneously
  • Natural language tool selection using LLMs
  • A chat interface where users can request "complex" operations in plain English
  • Integration with multiple AI providers (Groq, Gemini, OpenAI)

But most importantly, I learned that building AI agents isn't about the AI part being perfect. It's about building systems with new functionality that we didn't achieve before.

What's Coming Next

In the next posts, I'll walk through:

  • How MCP actually works under the hood
  • Building your first multi-server MCP client
  • Adding natural language tool selection with LLMs
  • The debugging nightmares I lived through (and how to avoid them)
  • Architecture patterns to make LLMs select tools.

This isn't going to be a series of perfect demos. I'll share what I learned building agentic apps.

Why Share This?

I'm writing this series because when I started, there weren't many resources about building production AI applications in Java.

If you're a Java developer curious about AI, or an AI enthusiast who wants to build something without using any AI framework, maybe these posts will save you some of the time I spent figuring things out the hard way.

Next post, I'll dive into the Model Context Protocol itself. What it is, why it matters, and how it's different from everything else you've probably used.


Have you tried building AI applications in Java? What challenges did you hit? I'd love to hear about your experiences in the comments.

Top comments (0)