Day 13 of Spring AI 14‑Day Series: Using Model Context Protocol to Plug Into Tool Ecosystems
This article explains how the Model Context Protocol (MCP) standardizes AI model access to external tools like file systems, databases, and browsers, showing how to integrate ready‑made MCP servers with Spring AI, expose your own tools as MCP servers, and choose between stdio and SSE transports.
What is MCP
MCP (Model Context Protocol) is an open protocol that standardizes how AI models connect to external tools and data sources, acting as a "USB" for the tool ecosystem. Community‑provided MCP Servers (file system, Git, database, map, browser automation, etc.) can be plugged in with a single integration.
Comparison of tool sources
Tool source : Day 12 @Tool uses a custom Java method; MCP uses a ready‑made MCP Server.
Suitable for : Day 12 @Tool fits internal business logic; MCP fits generic tools and cross‑language/process scenarios.
Reuse : Day 12 @Tool is reusable only within the project; MCP tools are reusable across the entire MCP ecosystem.
Adding an MCP Server (client)
Add the MCP client starter dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>Configure application.yml to connect to the official file‑system server using the stdio transport:
spring:
ai:
mcp:
client:
type: SYNC # synchronous client
stdio:
connections:
filesystem: # arbitrary connection name
command: npx
args:
- "-y"
- "@modelcontextprotocol/server-filesystem"
- "/tmp" # allowed directoryFor a remote server, use the sse transport and provide a url.
Providing MCP tools to the model
The starter automatically creates a ToolCallbackProvider bean that aggregates all connected servers. Inject this bean into a ChatClient builder:
@Bean
CommandLineRunner run(ChatClient.Builder builder, ToolCallbackProvider mcpTools) {
return args -> {
ChatClient chatClient = builder
.defaultToolCallbacks(mcpTools) // attach all MCP tools
.build();
String answer = chatClient.prompt()
.user("List all files under /tmp")
.call()
.content();
System.out.println(answer);
};
}The model automatically selects the file‑system server tool to fulfill the request, using the same syntax as a local @Tool call.
Exposing your own services as an MCP Server
Include the spring-ai-starter-mcp-server starter. Any method annotated with @Tool in the project is exposed as an MCP Server that other AI applications (e.g., Claude Desktop, other agents) can invoke. Two transport options are supported:
stdio transport : local subprocess, suitable for desktop clients or CLI tools.
sse transport : HTTP remote, suitable for microservice communication.
Quick links
MCP quick‑start: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-overview.html
MCP client starter docs: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html
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.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
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.
