This project is a demonstration of integrating Spring AI with OpenAI to create a Java-specialized AI assistant. The chatbot is capable of:
- Answering questions about Java development
- Memorizing conversation context
- Accessing custom tools (date/time)
- Integrating MCP (Model Context Protocol) clients to extend functionality
- Role: Main client for interacting with the AI model (OpenAI)
- Usage: Building prompts and calling the language model
- Configuration: Defined in the controller with advisors for memory management
- Role: Stores and manages conversation message history
- Configuration: Limited to a maximum of 10 messages to optimize API calls
- Benefit: Allows the model to maintain conversation context
- Role: Advisor that automatically injects history into each model call
- Usage: Ensures that each message to the AI includes previous context
- Role: Template to define system role and behavior
- Configuration: Defines the AI as a Java expert and enforces Markdown responses
- Role: Tool provider via MCP protocol
- Usage: Integrates external tools available via the MCP server
- Configuration: Connected to the server https://mcp.context7.com
- Role: Decorator to expose Java methods as tools for the AI
- Example:
DateTimeToolsexposesgetCurrentDateTime()to the AI
ChatController
├── POST /api/v1/chat → Simple chat with memory
├── POST /api/v1/chat/tools → Chat with custom tools (DateTimeTools)
└── POST /api/v1/chat/mcp → Chat with MCP tools + custom tools
- Java 25+
- Maven
- OpenAI API key (defined via the
API_KEYenvironment variable)
export API_KEY="your-openai-key"# With Maven
./mvnw spring-boot:run
# Or with the compiled JAR
java -jar target/SpringAIArticle-0.0.1-SNAPSHOT.jarThe application will start on http://localhost:8080
curl -X POST http://localhost:8080/api/v1/chat \
-H "Content-Type: application/json" \
-d '"Hello, my name is Alexandre, and you re Bottest!"'curl -X POST http://localhost:8080/api/v1/chat/tools \
-H "Content-Type: application/json" \
-d '"What time is it now?"'curl -X POST http://localhost:8080/api/v1/chat/mcp \
-H "Content-Type: application/json" \
-d '"Answer these two requests: - What are the latest Spring Boot versions? Find them in MCP Context7. - Get the time and display it."'- Framework: Spring Boot 4.0.3
- AI: Spring AI 2.0.0 + OpenAI
- Protocol: MCP (Model Context Protocol)
- Language: Java 25