springboot-ai -example
Example Spring AI Model Context Protocol (MCP)
claude mcp add --transport stdio duongminhhieu-springboot-ai-mcp-example docker run -i duongminhhieu/springboot-ai-mcp-example
How to use
This MCP server implements a Spring Boot based MCP example that demonstrates how to expose and call tools from an AI model using the MCP (Model-Controller-Prompt) architecture. The server integrates with external data sources (MongoDB and PostgreSQL) and uses @Tool annotated methods registered via ToolCallbackProvider, allowing the AI to discover available capabilities at runtime. A corresponding MCP client is included to discover capabilities, register tools with the AI model, and pass execution requests back to the server. To use it, start both the MongoDB and PostgreSQL instances, then run the server and client components. The AI model can request data, perform database lookups, or trigger tool actions, and the server will execute the tool logic and return results to the client for the final response.
Key capabilities include:
- Tool discovery and dynamic invocation via @Tool annotated methods.
- Dynamic tool registration through ToolCallbackProvider during initialization.
- Data access to MongoDB and PostgreSQL for real-time answer generation.
- End-to-end MCP workflow from user prompt to tool execution and final response delivered to the user interface.
How to install
Prerequisites:
- Docker and Docker Compose
- Java 21 and Maven (for local builds)
- OpenAI API key
- MongoDB and PostgreSQL (via Docker or external instances)
Step-by-step installation:
-
Install Docker and Docker Compose on your system.
-
Start the required databases (MongoDB and PostgreSQL) using Docker Compose as described in the project:
docker compose up -d
- Obtain an OpenAI API key and set it in the configuration (the project references application.yml for client config). You can place your key in a .env file or pass it through environment variables as required by your setup, for example:
export OPENAI_API_KEY=your-openai-key-here
- Build and run the MCP server and client locally (optional if you prefer Docker):
# From the mcp-server directory
cd mcp-server
./mvnw spring-boot:run
# In another terminal, from the mcp-client directory
cd ../mcp-client
./mvnw spring-boot:run
- Alternatively, run the provided server image via Docker (as outlined in the mcp_config):
# Ensure MongoDB and PostgreSQL are accessible (through docker-compose or external instances)
# Run the server image (adjust image name if needed)
docker run -i duongminhhieu/springboot-ai-mcp-example
- Verify the setup by sending a request to the MCP server endpoint (the README demonstrates how to test with Postman or curl).
Additional notes
Tips and notes:
- Ensure your OpenAI API key is valid and accessible to the MCP client/server configuration.
- The MCP workflow relies on tool discovery; make sure tools are properly annotated with @Tool and registered via ToolCallbackProvider during startup.
- For production, configure persistent MongoDB and PostgreSQL connections and consider securing endpoints behind authentication.
- If you update the server code, re-run the build and restart both server and client to refresh tool metadata on the client side.
- Common issues: mismatched API versions between Spring AI and the MCP runtime, missing environment variables, and database connection errors. Check logs for tool discovery errors and database query failures.
Related MCP Servers
mcp-for-beginners
This open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.
xiaozhi-esp32 -java
小智ESP32的Java企业级管理平台,提供设备监控、音色定制、角色切换和对话记录管理的前后端及服务端一体化解决方案
api2mcp4j
This is a revolutionary AI MCP plugin with excellent pluggable and encapsulated features. With just a few lines of configuration, it can easily integrate into your Spring boot web program and give it MCP capabilities,inheriting the powerful engineering capabilities of the Spring series framework
spring-ai
From Java Dev to AI Engineer: Spring AI Fast Track
auto -client
基于Spring AI 封装了 mcp-client 服务,目的使web网页智能体也能通过 stdio 和 HTTP SSE(Server-Sent Events) 与 MCP Server 进行交互。项目实现了自动化的连接管理机制,包括自动初始化连接、健康检查、超时关闭以及链接复用等功能
solon-ai-embedded-examples
solon ai(&mcp) embedded examples。支持 MCP_2025_06_18(mcp streamable)。示例项目包括各种框架嵌入:(Solon、SpringBoot、jFinal、Vert.X、Quarkus、Micronaut)