eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

MCP (Model Context Protocol) servers are the basis of modern AI handling and development. To that end, knowing how to configure one and integrate it into projects can be essential.

In this tutorial, we’ll learn how to build an MCP server with Spring AI and connect it to an AI harness such as Claude Desktop. To start, we create a simple hello-world tool, test it using the MCP Inspector, and then register it with Claude Desktop as a custom connector.

Once the basics are in place, we’ll take it one step further and expose a rich, interactive HTML UI — a fortune wheel that randomly picks today’s sport. In that implementation, we’ll see how Spring AI’s @McpTool and @McpResource annotations let the harness render the page inline in the chat and feed the result back into the conversation.

2. Creating an MCP Server

The Model Context Protocol (MCP) provides a standardized way for AI assistants such as Claude Code or Claude Desktop to invoke some custom functionality whenever the model sees fit. An MCP server is the lightweight backend that exposes this functionality via tools, resources, and prompts.

Let’s start by creating a simple hello-world MCP server. First, we add the spring-ai-starter-mcp-server-webmvc dependency to the pom.xml:

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

Additionally, we use spring-ai-bom for version management:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>2.0.0-M6</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Then, let’s configure the server port and the MCP transport protocol that the server exposes. Spring AI supports three transport options:

  • stdio for local process-based communication
  • sse for the legacy HTTP transport based on Server-Sent Events
  • streamable for the newer Streamable HTTP transport that handles both requests and streamed responses over a single endpoint

Here, we use the streamable protocol. So, let’s configure it in application.properties:

server.port=3001
spring.ai.mcp.server.protocol=STREAMABLE

Finally, we add a tool that says hello from the server back to the user:

@SpringBootApplication
class McpUiApplication {

    public static void main(String[] args) {
        SpringApplication.run(McpUiApplication.class, args);
    }

    @McpTool(
        title = "Say Hello",
        name = "say-hello",
        description = "A simple tool that returns a greeting message."
    )
    String sayHello() {
        return "Hello from the MCP UI Application!";
    }
}

As expected, this tool is exposed to the AI agent, and the greeting message is returned to the user whenever the model decides to invoke it.

3. Testing the MCP Server

There are several ways to connect to and test an MCP server. Let’s try to use some of them to do that and trigger the Say Hello tool.

3.1. Test Using MCP Inspector

One convenient option is the MCP Inspector, an interactive developer tool designed for testing and debugging MCP servers:

npx @modelcontextprotocol/inspector

This command starts a local proxy server along with a web-based UI that we can open in the browser to test the MCP. Using this interface, we can perform different actions:

  1. connect to an MCP server
  2. see the resources, prompts, and tools it exposes
  3. trigger tools

The interface is fairly straightforward:

MCP Inspector

As we can see, we are connected to the MCP server we created using Spring AI, and we successfully ran its Say Hello tool.

3.2. Test Using Claude Desktop

Alternatively, we can use an AI harness to test the MCP server. After all, the end goal here is to use the MCP server from a desktop application such as Claude Desktop.

Firstly, we need to register it in the claude_desktop_config.json file, the location of which varies:

  • on Windows, it’s %APPDATA%\Claude\claude_desktop_config.json
  • on macOS, it’s ~/Library/Application Support/Claude/claude_desktop_config.json

Claude Desktop natively supports only the stdio transport for local MCP servers, but the server speaks streamable HTTP. To bridge that gap, we use mcp-remote — a small npm utility that runs as a local stdio process and forwards every MCP message to a remote HTTP endpoint. Simply put, Claude Desktop talks to mcp-remote over standard input-output, and mcp-remote relays the traffic to the Spring AI server at http://localhost:3001/mcp:

"mcpServers": {
  "sport-spinner": {
    "command": "npx",
    "args": [
      "mcp-remote",
      "http://localhost:3001/mcp",
      "--transport",
      "http-only",
      "--allow-http"
    ]
  }
}

As we can see, we also need to assign a name to the MCP application. Let’s call it sport-spinner, since we extend it to recommend a sport for the user to practice on any given day.

Finally, if we restart Claude Desktop, we should be able to see the MCP server as a custom connector:

Enable connector

Therefore, the model can now decide to invoke the tools exposed by the MCP server:

Example of usage

Looks like we managed to connect to the MCP application and return a String that AI Harness can use or display to the user. Now, let’s upgrade the application to return HTML that can be rendered in Claude Desktop.

4. Embedding HTML

So far, the MCP server only returns plain text. However, modern AI harnesses such as Claude Desktop can also render rich, interactive UIs that the server exposes as HTML resources. We can achieve this by combining an @McpTool that triggers the UI with an @McpResource that serves the actual HTML.

Let’s enhance the sport-spinner to display a real fortune wheel that picks today’s sport. First, we add an HTML file under src/main/resources/static/sport-spinner.html, containing the HTML, CSS, and JavaScript that animates the wheel.

4.1. Exposing the HTML as an MCP Resource

Let’s create a new Spring bean that loads the HTML file and exposes it via the @McpResource annotation:

@Component
class SportSpinnerUI {

    @Value("classpath:/static/sport-spinner.html")
    private Resource sportSpinnerResource;

    @McpResource(
        name = "Sport Spinner App Resource",
        uri = "ui://sport/sport-spinner.html",
        mimeType = "text/html;profile=mcp-app",
        metaProvider = CspMetaProvider.class)
    public String getSportSpinnerResource() throws IOException {
        return sportSpinnerResource.getContentAsString(Charset.defaultCharset());
    }
}

A few things worth highlighting here:

  • the uri uniquely identifies the resource within the MCP server
  • the mimeType uses the text/html;profile=mcp-app profile – signaling to the client that this HTML should be rendered as an interactive MCP app rather than displayed as raw text
  • the metaProvider attaches additional metadata to the resource — in this case, a Content Security Policy that allow-lists unpkg.com so the page can load external scripts from that CDN

Let’s have a look at the CspMetaProvider:

class CspMetaProvider implements MetaProvider {
    @Override
    public Map<String, Object> getMeta() {
        return Map.of("ui",
            Map.of("csp",
                Map.of("resourceDomains", List.of("https://unpkg.com"))));
    }
}

At this point, we should be ready to connect the UI to the tool.

4.2. Linking the Tool to the UI Resource

Finally, let’s add an MCP tool that instructs the harness to open the wheel:

@McpTool(
    title = "Spin Sport Wheel",
    name = "spin-sport-wheel",
    description = "Opens a fortune wheel that spins and randomly picks today's sport.",
    metaProvider = SportSpinnerToolMetaProvider.class)
public String spinSportWheel() {
    return "Opening the sport spinner wheel.";
}

The key element here is metaProvider, which links this tool to the HTML resource we defined earlier:

class SportSpinnerToolMetaProvider implements MetaProvider {
    @Override
    public Map<String, Object> getMeta() {
        return Map.of("ui",
            Map.of("resourceUri", "ui://sport/sport-spinner.html"));
    }
}

When the model invokes spin-sport-wheel, the harness reads this metadata, fetches the resource at ui://sport/sport-spinner.html, and renders it inline in the chat, giving the user a fully interactive wheel instead of a plain text reply.

Let’s restart Claude Desktop and ask it what sport we should practice today:

Fortune Wheel spin UI

As expected, the HTML from the Spring Boot application is now rendered directly inside the Claude Desktop chat. At this point, we can spin the wheel to let it pick today’s sport, with the result fed back into the conversation so the model can react to it.

5. Conclusion

In this tutorial, we learned how to build an MCP server with Spring AI and connect it to an AI harness such as Claude Desktop.

Specifically, we started with a simple hello-world tool and tested it using the MCP Inspector and Claude Desktop. Then, we took it one step further by exposing a rich, interactive HTML UI through Spring AI’s @McpTool and @McpResource annotations. As a result, we made the harness render a fortune wheel inline in the chat and feed the result back into the conversation.

As always, the full source code is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments