Understanding MCP and How to Create a MCP Server using Spring Boot?
MCP stands for Model Context Protocol. It is an open-source standard which was introduced in November 2024 by Anthropic.
It defines a JSON-PRC-2.0-based protocol that enables models to communicate with external tools, such as APIs or databases.
My articles are free for everyone. Non-members can read this full article for free HERE.
Why do we need to learn about MCP?
- It is vendor-agnostic. All major players, such as OpenAI and Google DeepMind, are adopting it.
- It supports various transport mechanisms, including STDIO, HTTP-based SSE, etc.
- It boasts a vast community, with MCP servers already in place for primary tools like GitHub, Slack, Stripe, etc.
- The role of developers is shifting towards design and architecture, enabling the creation of secure systems with the help of AI.
- Models can hallucinate and can lead to issues like prompt injection, so secure design is essential.
Creating an MCP Server with Spring Boot
Setup and Dependencies
First, we need to install Spring AI dependencies as follows.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- MCP Server Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
<!-- For REST/SSE support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Auto-Configuration and Settings
Spring AI auto-configures your server. We can use application.properties
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.stdio=false
spring.ai.mcp.server.name=MyMcpServer
spring.ai.mcp.server.version=1.0.0
# Enable capabilities
spring.ai.mcp.server.capabilities.tool=true
spring.ai.mcp.server.capabilities.resource=true
spring.ai.mcp.server.capabilities.prompt=true
spring.ai.mcp.server.capabilities.completion=true
Exposing Tools via Spring Beans
Now we need to create a Service class and add the tools using the @ Tool annotation.
@Service
public class CourseService {
private List<Course> courses = new ArrayList<>();
@Tool(description = "List all courses")
public List<Course> listCourses() { ... }
@Tool(description = "Find a course by title")
public Course findCourse(@ToolParam String title) { ... }
}
Spring detects these and exposes them via MCP auto-registration.
Transport Configuration: STDIO
spring.ai.mcp.server.stdio=true
spring.main.web-application-type=none
spring.main.banner-mode=off
logging.pattern.console=
Now we can either create our own MCP Client using Spring Boot or use existing clients like Claude Desktop. I am going to use the Claude desktop in this article.
Install and Use with Claude
- Install Claude Desktop on your system.
- Build your MCP server, and create a JAR file.
- Now, create a config that Claude Client uses. It should be something like below:
{
"mcpServers": {
"dev-mcp-server": {
"command": "java",
"args": ["-jar", "/path/to/your-server.jar"]
}
}
}
- Add this config to Claude’s desktop.
- Launch your MCP Server and connect to it using Claude Desktop.
What is the difference between an AI Agent and an MCP?
Here are More Contents You Might Like
Thanks for reading. If you enjoy my content and want to show support, you can check out my other articles, give a few claps, and follow me for more such content. I write on Java, Productivity, and Technology.
Until Next Time
Saquib Aftab
Understanding MCP and How to Create a MCP Server using Spring Boot? was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.
This post first appeared on Read More