A Model Context Protocol (MCP) server built with Spring AI that exposes MongoDB operations as tools for AI models. This project demonstrates how to create a standardized interface between AI applications and MongoDB, allowing AI models to perform database operations through a simple todo list application.
This is an MCP server that bridges AI models and MongoDB. Instead of AI models needing to understand database connection strings, query syntax, or data modeling, they can simply call tools like todo-add-task or todo-get-tasks. The server handles all the translation between AI requests and actual MongoDB operations.
The Model Context Protocol (MCP) is an open standard that enables AI applications to securely connect to various data sources and tools. This project shows you how to build an MCP server using Spring AI and Spring Data MongoDB.
- Java 17+ - for running the Spring Boot application
- Maven - for building and dependency management
- MongoDB Atlas account - M0 free tier works perfectly
- Sign up for MongoDB Atlas if you don't have an account
- MCP Inspector (optional, for testing) - install with
npx @modelcontextprotocol/inspector
- Clone the repository
git clone <repository-url>
cd springai-mcp-
Configure MongoDB connection
Set your MongoDB connection string as an environment variable:
export SPRING_DATA_MONGODB_URI="your-mongodb-atlas-connection-string"- Build the project
mvn clean installStart the Spring Boot application:
mvn spring-boot:runThe MCP server will be available at http://localhost:8080/mcp
- Install and run the MCP Inspector:
npx @modelcontextprotocol/inspector-
In the browser interface:
- Change transport type to Streamable HTTP
- Enter URL:
http://localhost:8080/mcp - Click Connect
-
Navigate to the Tools tab to see available tools and test them
The server exposes three MCP tools for managing todo tasks:
Adds a new task to the MongoDB database.
Parameters:
name(required): The name or description of the task
Example:
{
"name": "Write documentation"
}Marks a task as complete by its name.
Parameters:
name(required): The name of the task to mark as complete
Example:
{
"name": "Write documentation"
}Retrieves tasks with optional filtering by completion status.
Parameters:
filter(optional): Filter by completion status - accepts"complete","incomplete", or"all"(default)
Example:
{
"filter": "incomplete"
}src/main/java/com/timkelly/springaimcp/
├── Task.java # MongoDB document model
├── TodoRepository.java # Spring Data MongoDB repository
├── TodoService.java # Business logic layer
└── MongoDbTools.java # MCP tools definition
Key configuration in application.properties:
spring.application.name=springai-mcp
spring.ai.mcp.server.name=mongodb-mcp
spring.ai.mcp.server.version=1.0.0
spring.ai.mcp.server.protocol=streamable
spring.ai.mcp.server.stdio=false
spring.ai.mcp.server.type=sync
spring.data.mongodb.uri=${SPRING_DATA_MONGODB_URI}
spring.data.mongodb.database=todoWant to dive deeper into MongoDB's AI capabilities? Check out the Vector Search Fundamentals course to learn about embeddings, semantic search, and vector databases (earn a skill badge while you're at it).