Sponsored by Deepsite.site

Spring MCP Server

Created By
deepanshu-rawat68 months ago
A basic implementation of MCP server using Spring Boot
Content

Spring MCP Server

The Model Context Protocol is an open standard that enables developers to build secure, two-way connections between their data sources and AI-powered tools. The architecture is straightforward: developers can either expose their data through MCP servers or build AI applications (MCP clients) that connect to these servers.

Prerequisites

  • Java 21
  • Maven 3.9.9
  • Spring Boot 3.4.4
  • Spring AI 0.4.0
  • Spring Model Context Protocol Server
  • Claude Desktop Application

Getting Started

In this example, we will create a simple Spring Boot application that serves as an MCP server.

1. Create a Spring Boot Application

Create a new Spring Boot application using the Spring Initializr or your preferred method. You can use the following dependencies:

  • Spring Model Context Protocol Server

Creating record

Create a record class that will represent the information you want to expose through the MCP server.

package org.sehn.spring_mcp;

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

Creating services

Create a service class that will provide the information you want to expose through the MCP server.

package org.sehn.spring_mcp;

import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class InfoService {

    private static final Logger logger = LoggerFactory.getLogger(InfoService.class);

    private List<Info> information = new ArrayList<>();

    @Tool(name = "dr_get_information", description = "Get a list of information about Deepanshu Rawat")
    public List<Info> getInformation() {
        return information;
    }

    @Tool(name = "dr_get_info", description = "Get a specific information about Deepanshu Rawat")
    public Info getInfo(String title) {
        return information.stream().filter(info -> info.title().equals(title)).findFirst().orElse(null);
    }

    @PostConstruct
    public void init() {
        information.addAll(List.of(
                new Info("Portfolio", "https://bento.me/deepanshu-rawat6"),
                new Info("GitHub", "https://github.com/deepanshu-rawat6"),
                new Info("LinkedIn", "https://www.linkedin.com/in/deepanshu-rawat6/"),
                new Info("Twitter", "https://twitter.com/deepanshuurawat")
        ));


    }
}

Creating a Tool Callback

In the SpringMCPApplication or your main application class, create a tool callback that will handle the requests from the MCP server.

    @Bean
    public List<ToolCallback> deepanshuTools(InfoService infoService) {
        return List.of(ToolCallbacks.from(infoService));
    }

Adding Configuration

Create a configuration class that will configure the MCP server.

spring.application.name=spring_mcp
spring.main.web-application-type=none
spring.ai.mcp.server.name=spring_mcp
spring.ai.mcp.server.version=0.0.1


spring.main.banner-mode=off
logging.pattern.console=

2. Build the Application

Simply run the following command to build the application:

mvn clean package -DskipTests

3. Further configuration

Now, in order to use the MCP server, you need to add the following configuration in your claude_desktop_config.json:

{
    "mcpServers": {
        "<name_of_the_mcp_server>": {
            "command": "java",
            "args": [
                "-jar",
                "<Path_of_the_jar_file_created_after_building>"
            ]
        }
    }
}

Replace <name_of_the_mcp_server> with the name of your MCP server and <Path_of_the_jar_file_created_after_building> with the path to the jar file created after building the application.

For example, name of my MCP server is spring_mcp.

4. Testing the MCP Server

Now, start Claude Desktop and a few new things will appear underneath the prompt area:

Installed MCP Servers

Share_context_with_Claude

Available MCP tools

Available_MCP_tools

Now, upon hitting a prompt, say for example: Get me the information about Deepanshu Rawat, you will get a alert about third-party tools being used, simply accept the alert for the current chat. And then you will get a response, like this:

response_from_server

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
DeepChatYour AI Partner on Desktop
Tavily Mcp
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
WindsurfThe new purpose-built IDE to harness magic
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Amap Maps高德地图官方 MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Serper MCP ServerA Serper MCP Server
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
ChatWiseThe second fastest AI chatbot™
CursorThe AI Code Editor
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Playwright McpPlaywright MCP server