Written by Jack Woehr - SoftWoehr LLC - Seiden Group - ProCern Technology Solutions
After reading this article and exploring the associated code repository, you should be able to write your own agent which leverages the MongoDB MCP server.
The code and this article are illustrative only. This is not production code, but it is a complete working example. In contrast to our working example, in production:
- You will do more for security.
-
You will conform to an advanced protocol such as:
You may use a gateway such as IBM MCP Context Forge.
You will probably containerize your entire project.
Chat with MongoDB in natural language
The MongoDB MCP server allows you to write an agent that you can use:
- In conjunction with a web application.
- In a multi-agent application.
- To be called from a simple chat interface as implemented in our code.
We wrote the code ("we" being "me" and various agentic tasking code assistants, mostly Roo Code, some Kilo, some Cline, and some IBM Project Bob) as part of a design-code-play-repeat cycle on a larger web dashboard project (not yet released).
Architecture
All communication in this simple example is via HTTP.
Chat
The chat sends to the agent the:
- Query.
- Context (i.e., chat history).
- Prompt.
Agent
The agent sends to the model (the AI service) the content relayed from the chat:
- query
- context
- prompt
…and also the tools list which the agent loaded from the MongoDB MCP server when the agent started.
Model
- The model examines the content from the agent and makes an MCP tool call to the agent.
- The agent makes the tool call to the MongoDB MCP server.
- The agent sends back to the model the result of the tool call.
- The model analyzes the result and formats a text response to the query which is then returned to the agent, which relays it back to the chat.
The code
The code is on GitHub.
The agent and chat interface
After starting the MongoDB MCP server (see below), the README.md file explains how to set up and run the agent and the chat interface.
-
Install the required Node packages.
npm i
Copy the
.env.exampleto.envand edit it.-
Start the agent in a terminal window.
npm start
-
Start the chat in another terminal window
npm run chat
Trying out the chat
Data model
In the illustration above, we see a portion of the validation for the pottery collection of the herwheel3_test database that we query using Google Gemini as our model provider. herwheel3_test is not a public database and is not part of the code and is merely used for illustration. Use your own database!
Gemini is smart enough to figure out to search the tags array when queried about "pots tagged as mugs," as shown in this second illustration.
Starting the MongoDB MCP server
There is a script, start_mongodb-mcp-server.py, to start the MongoDB MCP server. You do not need to use it. You may start the mongodb-mcp-server instance as you wish, but make sure it comes up on the TCP port expected by your configuration for the agent.
That Python script depends on python-dotenv, so you should create a Python virtual environment, activate it, and install that package if you want to use the script. There's even a requirements.txt file.
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Run that script with the -h flag to see the arguments.
$ ./start_mongodb-mcp-server.py -h
usage: start_mongodb-mcp-server.py [-h] [--log-path LOG_PATH] DOTENV
Load a dotenv file into the environment and launch mongodb-mcp-server in the background.
positional arguments:
DOTENV Path to a dotenv-style file to load (e.g. .env)
options:
-h, --help show this help message and exit
--log-path LOG_PATH Fully qualified filepath of the log file. Default: same directory as dotenv file with name 'mongodb-mcp-server.log'
Of course, you will need to have installed and configured the MongoDB MCP server. The instructions for doing that are here, and I have provided a .env.mongodb-mcp-server.example with the code. Copy it to some other name, edit it, and provide it as the argument to start_mongodb-mcp-server.py.
We used our MongoDB Atlas installation as the target, so the server configuration we provide with the sample needs three factors:
MDB_MCP_API_CLIENT_ID=API_CLIENT_ID
MDB_MCP_API_CLIENT_SECRET=API_CLIENT_SECRET
MDB_MCP_CONNECTION_STRING=CONNECTION_STRING
- The connection string is of the form MongoDB URI that we are all familiar with.
-
API_CLIENT_IDandAPI_CLIENT_SECRETare Atlas API factors. Please see MongoDB document Manage Programmatic Access to a Project to learn how to create these artifacts.
Conclusion
You should now be able to write your own agent which leverages the MongoDB MCP server, allowing you to incorporate MongoDB in your agentic mix.
A final thought about code assistants
Those developers best equipped to use code assistants are those developers who code well already. Code assistants are good, very good, and are getting better, but you are the ultimate authority on suitability, sustainability, and correctness. With the advent of these tools, our profession has changed forever and irrevocably, and will continue to change in the coming years, but our mission is still the same. Our job will not get easier, but we will be more productive.




Top comments (0)