A Spring Boot application that integrates OpenAI's GPT models with the Model Context Protocol (MCP) to provide AI-powered chat functionality with tool calling capabilities.
- OpenAI Integration: Leverages GPT-4.1-nano for intelligent chat responses
- MCP Client Support: Connects to MCP servers to extend AI capabilities with custom tools
- Tool Callbacks: Automatic tool discovery and execution via MCP protocol
- RESTful API: Simple HTTP endpoints for stateless chat interactions
- Observability: Built-in logging for prompts, completions, and errors
- Modern Stack: Spring Boot 3.5.10, Spring AI 1.1.2, Java 21
- Java 21 or higher
- Maven 3.9+
- OpenAI API key
- MCP server running on
http://localhost:8081/(or configure your own)
-
Clone the repository
git clone <repository-url> cd mcp-client
-
Set up environment variables
export OPENAI_API_KEY=your_openai_api_key_here -
Build the project
./mvnw clean install
The application is configured via src/main/resources/application.yaml:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4.1-nano
temperature: 0.7
mcp:
client:
streamable-http:
connections:
server1:
url: http://localhost:8081/- OpenAI Model: Default is
gpt-4.1-nano, can be changed to other GPT models - Temperature: Set to
0.7for balanced creativity/determinism - MCP Server URL: Configure the endpoint where your MCP server is running
Important: Ensure your MCP server is running on http://localhost:8081/ before starting this application.
If you encounter the error:
Factory method 'mcpSyncClients' threw exception with message:
Client failed to initialize by explicit API call
This means the MCP server is not reachable. Verify:
- The MCP server is running
- The URL in
application.yamlis correct - There are no network/firewall issues
./mvnw spring-boot:runThe application will start on http://localhost:8080
Endpoint: POST /v1/chat/stateless
Request:
curl -X POST http://localhost:8080/v1/chat/stateless \
-H "Content-Type: text/plain" \
-d "What is the weather like today?"Response:
{
"conversationId": null,
"response": "The current weather is...",
"timeTaken": 1250
}Fields:
response: The AI-generated responsetimeTaken: Time taken to process the request in millisecondsconversationId: Currently unused (reserved for future stateful conversations)
mcp-client/
βββ src/
β βββ main/
β β βββ java/com/navneet/mcp_client/
β β β βββ McpClientApplication.java # Main Spring Boot application
β β β βββ constants/
β β β β βββ AgentConstants.java # System prompts and constants
β β β βββ controller/
β β β β βββ ChatController.java # REST API endpoints
β β β βββ models/
β β β β βββ ChatRequest.java # Request DTOs
β β β β βββ ChatResponse.java # Response DTOs
β β β βββ service/
β β β βββ ChatService.java # Service interface
β β β βββ impl/
β β β βββ ChatServiceImpl.java # Chat service implementation
β β βββ resources/
β β βββ application.yaml # Application configuration
β βββ test/
β βββ java/com/navneet/mcp_client/
β βββ McpClientApplicationTests.java # Integration tests
βββ pom.xml # Maven dependencies
βββ README.md # This file
- Spring Boot 3.5.10: Core framework
- Spring AI 1.1.2: AI integration layer
spring-ai-starter-mcp-client: MCP protocol supportspring-ai-starter-model-openai: OpenAI integration@Toolannotation support: Enables automatic tool discovery and invocation by AI models
- Lombok: Reduces boilerplate code
- Spring Web: RESTful API support
Handles AI-powered chat interactions with MCP tool callbacks support.
Provides time and date utility functions exposed as tools to the AI model:
- Get current time/date in various formats
- Convert between different time representations (milliseconds, seconds, formatted strings)
- All methods are annotated with
@Toolfor automatic Spring AI discovery
Error: Factory method 'mcpSyncClients' threw exception
Solutions:
- Ensure MCP server is running and accessible
- Check the URL in
application.yaml - Verify network connectivity to the MCP server
- Review MCP server logs for errors
Error: Authentication or rate limit issues
Solutions:
- Verify
OPENAI_API_KEYenvironment variable is set correctly - Check your OpenAI account for API quota/credits
- Ensure the model name is correct and accessible with your API key
Solutions:
- Check MCP server is exposing tools correctly
- Review application logs for tool discovery messages
- Ensure
toolcallback.enabledis set totruein configuration
./mvnw test./mvnw clean package
java -jar target/mcp-client-0.0.1-SNAPSHOT.jar- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the terms specified in the LICENSE file.
- Spring AI Documentation
- Model Context Protocol (MCP)
- OpenAI API Documentation
- Spring Boot Documentation
navneet.prabhakar
Built with β€οΈ using Spring AI and MCP