Navigation
Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment - MCP Implementation

Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment

Expertly crafted Spring AI MCP Server example project accelerates development with streamlined AI integration, ready-to-deploy solutions, and best practices for modern enterprise apps.

Research And Data
4.6(189 reviews)
283 saves
132 comments

This tool saved users approximately 7997 hours last month!

About Spring AI MCP Server

What is Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment?

Spring AI MCP Server is a framework that simplifies the integration of AI tools and services using the Model Context Protocol (MCP). This example implementation demonstrates a server capable of executing computational tasks and accessing external APIs through standardized interface calls. The server includes pre-built modules for mathematical expression evaluation and weather data retrieval, while adhering to best practices for deployment and configuration.

How to use Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment?

1. Configure API keys in application.properties for third-party services like QWeather.
2. Package the application using Maven with mvn clean package.
3. Define the server in Windsurf client configuration with proper JVM arguments for STDIO mode.
4. Invoke services via MCP protocol using tool names "calculate" for expressions and "getWeather" for location-based forecasts.

Spring AI MCP Server Features

Key Features of Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment?

  • Standardized tool interface compatible with Claude Desktop and Windsurf platforms
  • Expression parser supporting complex mathematical operations through Spring SpEL
  • Real-time weather data access via QWeather API with free tier support
  • System information endpoint providing server metadata
  • Logging configuration with debug-level visibility for integration troubleshooting

Use cases of Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment?

1. Intelligent chatbots requiring real-time computation of user-provided mathematical expressions
2. AI-driven weather advisory systems needing localized forecasts
3. Rapid prototyping of MCP-compliant tool endpoints for model training pipelines

Spring AI MCP Server FAQ

FAQ from Spring AI MCP Server: Streamlined AI Integration & Rapid Deployment?

  • Q: How do I ensure Windsurf properly receives output?
    A: Verify spring.ai.mcp.server.stdio=true and empty console logging pattern
  • Q: What API limits apply to weather requests?
    A: QWeather provides 100 free calls/day for basic weather data
  • Q: Can I add custom tool endpoints?
    A: Yes, by following Spring AI's MCP service registration patterns
  • Q: Where are server logs stored?
    A: Configured at logging.file.name=mcp-weather-stdio-server.log

Content

Spring AI MCP Server 示例项目

这个demo使用Spring AI框架实现一个简单的Model Context Protocol (MCP) Server,包含两个功能模块:计算器服务和天气查询服务。

demo介绍

Model Context Protocol (MCP) 是Spring AI提供的一种协议,允许大语言模型(LLM)通过标准化的接口调用外部工具和服务。这个Demo实现了两个MCP服务:

  1. 计算器服务 :接收数学表达式并返回计算结果
  2. 天气查询服务 :接收城市名称并返回该城市的实时天气信息

技术栈

  • Java 17
  • Spring Boot 3.4.4
  • Spring AI 1.0.0-M6
  • Maven
  • Hutool 5.8.36

项目结构

src/main/java/donts/ai/
├── DontsAiApplication.java         # 应用程序入口
├── demo/                           # 演示功能包
│   ├── mcp_resources/              # MCP资源配置
│   │   ├── ResourceConfig.java     # 资源配置类
│   │   └── SystemResource.java     # 系统资源类
│   └── tools/                      # 工具服务
│       ├── ToolConfig.java         # 工具配置类
│       ├── calculator/             # 计算器服务
│       │   ├── CalculatorFunctionRequest.java
│       │   └── CalculatorMcpServer.java
│       └── weather/                # 天气查询服务
│           ├── WeatherApiProperties.java
│           ├── WeatherFunctionRequest.java
│           ├── WeatherFunctionResponse.java
│           └── WeatherMcpServer.java

功能说明

计算器服务

计算器服务使用Spring Expression Language (SpEL)计算数学表达式。为了避免Java整数除法的问题(如3/5=0),添加了预处理逻辑,将所有整数转换为浮点数。

示例:

  • 输入:3 + 4 * 2
  • 输出:表达式:3 + 4 * 2\n计算结果:11.0

天气查询服务

天气查询服务使用和风天气API获取城市的实时天气信息。首先通过城市名称查询城市ID,然后使用城市ID获取实时天气数据。

示例:

  • 输入:昆明
  • 输出:城市昆明的实时天气信息(JSON格式)

MCP 资源

MCP 资源(Resource)是 Model Context Protocol 协议中的一个重要概念,代表服务器想要提供给客户端的任何类型的数据。本项目实现了以下资源:

系统信息资源

系统信息资源提供关于运行环境的基本信息,包括:

  • Java 运行时版本
  • 操作系统信息(名称、版本、架构)
  • 处理器数量
  • 当前时间戳

资源URI:system://info MIME类型:application/json

示例输出:

{
  "javaVersion": "17.0.10",
  "osName": "Mac OS X",
  "osVersion": "14.4",
  "osArch": "aarch64",
  "processors": 10,
  "timestamp": 1711301234567
}

配置说明

注意,我之后要使用windsurf来调用这个MCP Server,由于windsurf只支持STDIO的输出模式,所以需要把web application type设置为none,并且需要设置logging.pattern.console=空的,防止日志打印到控制台,否则windsurf就接受不到tool输出的结果

windsurf MCP官方文档

application.properties

# Required STDIO Configuration
spring.main.web-application-type=none
spring.main.banner-mode=off
logging.pattern.console=

# Server Configuration
spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.name=my-weather-server
spring.ai.mcp.server.version=0.0.1
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.resource-change-notification=true
spring.ai.mcp.server.tool-change-notification=true
spring.ai.mcp.server.prompt-change-notification=true

# 日志配置
logging.file.name=mcp-weather-stdio-server.log
logging.level.root=INFO
logging.level.donts.ai=DEBUG

# 天气API配置
weather.api.api-key=你的API密钥

MCP客户端配置

windsurf支持的格式和Claude Desktop一样

{
  "mcpServers": {
    "donts-ai-mcp-weather": {
      "command": "java",
      "args": [
        "-Dspring.ai.mcp.server.stdio=true",
        "-Dspring.main.web-application-type=none",
        "-Dlogging.pattern.console=",
        "-jar",
        "/yourRealPath/to/donts-ai-0.0.1-SNAPSHOT.jar"
      ]
    }
  }
}

构建和运行

打包

打包后不需要手动运行

mvn clean package

使用说明

当Client需要调用的时候,LLM就可以通过MCP协议调用我们的工具:

  1. 计算表达式
* 工具名称:`calculate`
* 参数:`{"expression": "3 + 4 * 2"}`
  1. 查询城市天气
* 工具名称:`getWeather`
* 参数:`{"city": "昆明"}`

注意事项

  1. demo使用了和风天气的API,调用查询服务前,需要在application.properties中配置和风天气API密钥,到和风天气控制台申请APIKey ,每天有免费的调用次数,用来做个demo已经够用了
  2. 这个小demo参考了Spring AI官方示例

参考资料

Related MCP Servers & Clients