Unlock AI Data Integration with Spring AI’s Model Context Protocol (MCP)
Spring AI now supports the Model Context Protocol (MCP), a universal adapter that lets AI applications seamlessly read databases, files, and APIs, offering a plug‑and‑play alternative to function calling, with simple Maven setup and code examples to get started quickly.
MCP: The "Universal Translator" for AI and the External World
As AI technology rapidly advances, efficiently acquiring and processing external data becomes a key challenge. Spring AI now supports the Model Context Protocol (MCP), opening a new world of data interconnection for AI applications.
Why is MCP special?
Unified Standard : One development, run everywhere. MCP acts as a universal adapter allowing AI apps to converse with any data source in the same way.
Full Compatibility : Handles both structured database tables and unstructured text files.
Plug‑and‑Play : Enables direct viewing of GitHub code or Google Docs within a chat interface.
MCP vs Function Calling: More Than Simple Calls
Function Calling is a one‑off custom task, whereas MCP serves as a general translator that builds bridges between systems, allowing free flow of data and functionality.
Spring AI + MCP: Data Interconnection in a Few Lines
With MCP, AI can directly read local documents, databases, and other sources, greatly expanding practical use cases such as content creation and document processing.
1. Add Dependency
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>0.4.1</version>
</dependency>2. Basic Configuration
spring.ai.openai.base-url=your-api-base-url
spring.ai.openai.api-key=your-api-key3. Core Implementation
@Bean
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder,
List<McpFunctionCallback> functionCallbacks, ConfigurableApplicationContext context) {
return args -> {
var chatClient = chatClientBuilder
.defaultFunctions(functionCallbacks.toArray(new McpFunctionCallback[0]))
.build();
String question1 = "mcp.txt 这篇文档有什么建议吗?方便公众号传播推广";
System.out.println("ASSISTANT: " + chatClient.prompt(question1).call().content());
context.close();
};
}
@Bean
public List<McpFunctionCallback> functionCallbacks(McpSyncClient mcpClient) {
return mcpClient.listTools(null)
.tools()
.stream()
.map(tool -> new McpFunctionCallback(mcpClient, tool))
.toList();
}
@Bean(destroyMethod = "close")
public McpSyncClient mcpClient() {
var stdioParams = ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-filesystem", getDbPath())
.build();
var mcpClient = McpClient.using(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).sync();
var init = mcpClient.initialize();
System.out.println("MCP Initialized: " + init);
return mcpClient;
}
private static String getDbPath() {
return Paths.get(System.getProperty("user.dir"), "target").toString();
}Test Execution
Run the following command to start the Spring Boot application and test MCP functionality:
mvn springboot:runSigned-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.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.
