Building a Maoyan Movie MCP Server with Spring AI 1.0.0-M7
This article explains the Model Context Protocol (MCP), why it simplifies AI tool integration, and provides a step‑by‑step guide to create a Spring AI 1.0.0‑M7 Maven project, configure the MCP server with HTTP SSE, implement weather and Maoyan movie services, run the server, and test it with the MCP Inspector and Cherry Studio client.
Model Context Protocol (MCP) is an open standard that lets large language models (LLM) call external tools uniformly. Before MCP each tool required separate code; MCP reduces integration cost.
Anthropic released MCP in 2024; companies like Gaode, Baidu, and Alipay have adopted it.
MCP supports three transport modes: Stdio, HTTP SSE, and Streamable HTTP. Streamable HTTP is the most flexible for distributed applications.
Environment preparation
Node.js (latest)
Java 17
Spring AI 1.0.0‑M7
LLM with tool‑use capability (gpt‑4.1, gpt‑4o)
Cherry Studio client
Maoyan movie box‑office HTTP API
Project setup
Create a Maven project and add the Spring AI BOM (1.0.0‑M7) and required dependencies:
<project ...>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencyManagement>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
</dependencies>
</project>Add application.yml to enable the MCP server with SSE transport:
spring:
ai:
mcp:
server:
enabled: true
name: webmvc-mcp-server
version: 1.0.0
type: ASYNC
sse-endpoint: /sse
sse-message-endpoint: /mcp/messages
transport:
mode: sseImplementing tool services
Weather service (simulated):
@Service
public class WeatherService {
@Tool(description = "Get weather information by city name")
public String getWeather(String cityName) {
Map<String, Object> result = new HashMap<>();
result.put("city", cityName);
result.put("temperature", "22°C");
result.put("description", "晴转多云");
return JSON.toJSONString(result);
}
}Maoyan movie service (fetches the first ten items from the public API):
@Service
public class MaoyanMovieService {
private String maoyanMovieUrl = "https://piaofang.maoyan.com/dashboard-ajax/movie?...";
@Tool(description = "获取当日的猫眼电影票房列表数据", name = "猫眼票房数据列表")
public String getMaoyanMovieList() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(maoyanMovieUrl)
.addHeader("User-Agent", "Mozilla/5.0 ...")
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful())
throw new Exception("请求失败: " + response);
String jsonString = response.body().string();
JSONObj json = JSON.parseObject(jsonString);
JSONObj movieList = json.getJSONObject("movieList");
JSONArray list = movieList.getJSONArray("list");
List<Object> objects = list.subList(0, 10);
StringBuilder sb = new StringBuilder();
sb.append("当前票房列表json数据如下:")
.append(JSON.toJSONString(objects))
.append("
各项含义解释如下:movieInfo字段代表电影ID、名称、上映时间;")
.append("avgSeatView代表上座率,avgShowView代表场均人次,boxRate代表票房占比,showCount代表排片场次;")
.append("showCountRate代表排片占比,sumBoxDesc代表总票房信息。");
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
return "电影列表获取数据异常,请稍后再试";
}
}
}Application entry point
@SpringBootApplication
public class McpApplication {
public static void main(String[] args) {
SpringApplication.run(McpApplication.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService,
MaoyanMovieService maoyanMovieService) {
return MethodToolCallbackProvider.builder()
.toolObjects(weatherService, maoyanMovieService)
.build();
}
}Run the application; the MCP server is exposed at /sse. Use npx @modelcontextprotocol/inspector to launch the MCP Inspector UI and verify that the two tools return the expected JSON.
Configure Cherry Studio (client) with the server’s SSE endpoint, enable tools, and test queries such as “What’s the weather in Beijing?” and “Show today’s Maoyan box‑office list”. The UI displays the tool calls and their results, confirming successful integration.
Other companies (Gaode, Alipay) already provide MCP servers for map, payment, and other services; their public endpoints are listed for reference.
Overall, the article demonstrates that with a few Maven dependencies, a simple Spring Boot project, and the MCP SDK, developers can expose any business API as an LLM‑callable tool, dramatically lowering the barrier for building AI‑augmented applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubiquitous Tech
A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
