MongoDB Integration in VS Code Using MongoDB MCP: A Step-by-Step Tutorial
Written by Om-Shree-0709 on .
With the advent of the Model Context Protocol (MCP), MongoDB developers can now interact directly with their databases through AI-powered agents like GitHub Copilot—right inside VS Code.
In this guide, I will demonstrate how to set up MongoDB MCP in Visual Studio Code and perform database tasks using natural-language prompts. 1
Prerequisites
Ensure the following are available:
- Visual Studio Code (v1.99+) with GitHub Copilot and Copilot Chat
- Node.js v22+ (
node -v
) - (Optional) Docker if deploying containerized MCP (like Github 2)
- Access to the
mongodb-mcp-server
(auto-installed vianpx
)
If you already have this, the next thing is figuring out which method you want to use to connect MongoDB to VS Code.
Two Ways to Connect MongoDB to VS Code
Method 1: Direct via Command Palette3
-
Open Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
) → runMCP: Add Servers
-
Choose Command Standard I/O
-
Enter:
Name it "mongodb"
-
VS Code opens
settings.json
: Add this block: -
Save to remove any MCP-related error (figure 3)
-
If your connection string is valid, the server starts (figure 4):
Method 2: Via MongoDB Atlas UI4
-
In your Atlas console, click Connect
-
Select MCP Connection
-
Choose VS Code, copy the generated MCP snippet (figure 7)
-
Paste into your
settings.json
undermcp.servers.mongodb
and save.
Between these two options, I personally prefer the 2nd option because it is more straightforward. Use first option if you like to host the MCP locally or anticipate the need to modify it.
Using MongoDB in VS Code
Now that MCP has been setup, you need to create a connection in VS Code.
-
Open Command Palette → Use commands like
MongoDB: Connect
,Create Connection
,Remove Connection
-
In Copilot’s Agent Chat, try prompts like:
“Show me my database collections.”
The Copilot agent uses MCP to perform schema introspection, queries, or updates.
That's it! You have a working MongoDB MCP server and it is connected to VS Code!
What Happens Behind the Scenes
As with everything that you do with MCP (esp. if running on your own machine or when you give it access to production resources), it is important to understand how the server works.
-
Copilot agent receives your natural‑language prompt and decides if it requires database interaction, so it invokes the MCP server.
-
The MCP client (embedded in Copilot’s host) translates your intent into a structured MCP tool call such as
find
,aggregate
, or atlas-list-clusters—and sends it over MongoDB. -
The MCP server interprets that tool call, executes the corresponding MongoDB command, and returns the result.
-
The MCP client then feeds the data back into the Copilot agent, which post-processes it and presents you with the final response inside VS Code’s UI, chat, or editor pane github.com.
-
Throughout the process, context and state—like schema insights or previous commands—are preserved to maintain conversational flow and support follow‑up queries.
Security Tips
-
Use environment variables instead of inline credentials
-
Enable read-only mode:
or use
MDB_MCP_READ_ONLY=true
-
Disable mutation tools:
-
Disable telemetry:
-
Secure MongoDB: enable TLS/SSL, authentication, IP binding, and RBAC
Example Repository
To learn from a working implementation, explore:
GitHub: Om-Shree-0709/MongoDB-MCP Includes full setup,
.vscode/mcp.json
, and deployment templates
Conclusion
MongoDB MCP enables conversational database access right from VS Code:
- Issue natural language queries and admin commands
- Secure access via flags, read-only mode, and tool restrictions
- Compatible with both Atlas and local deployments using
npx
or Docker
References
Footnotes
Written by Om-Shree-0709 (@Om-Shree-0709)