Skip to main content
Glama

MongoDB Integration in VS Code Using MongoDB MCP: A Step-by-Step Tutorial

Written by on .

mongodb
mcp
vscode

  1. Prerequisites
    1. Two Ways to Connect MongoDB to VS Code
      1. Method 1: Direct via Command Palette
        1. Method 2: Via MongoDB Atlas UI
        2. Using MongoDB in VS Code
          1. What Happens Behind the Scenes
            1. Security Tips
              1. Example Repository
                1. Conclusion
                  1. References

                  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 via npx)

                  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

                  1. Open Command Palette (Ctrl+Shift+P or Cmd+Shift+P) → run MCP: Add Servers

                  2. Choose Command Standard I/O

                  3. Enter:

                    npx -y mongodb-mcp-server

                    Name it "mongodb" figure1

                  4. VS Code opens settings.json: Add this block:

                    "mcp.servers": { "mongodb": { "command": "npx", "args": [ "-y", "mongodb-mcp-server", "--connectionString", "mongodb+srv://<username>:<password>@cluster0.iuveu72.mongodb.net/" ] } }

                    figure2

                  5. Save to remove any MCP-related error (figure 3) figure3

                  6. If your connection string is valid, the server starts (figure 4): figure4

                  Method 2: Via MongoDB Atlas UI4

                  1. In your Atlas console, click Connect figure5

                  2. Select MCP Connection figure6

                  3. Choose VS Code, copy the generated MCP snippet (figure 7) figure7

                  4. Paste into your settings.json under mcp.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.

                  1. Open Command Palette → Use commands like MongoDB: Connect, Create Connection, Remove Connection

                    figure8

                  2. 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.

                  Image

                  Security Tips

                  • Use environment variables instead of inline credentials

                  • Enable read-only mode:

                    npx mongodb-mcp-server --connectionString "..." --readOnly

                    or use MDB_MCP_READ_ONLY=true

                  • Disable mutation tools:

                    --disabledTools create update delete # or MDB_MCP_DISABLED_TOOLS=create,update,delete
                  • Disable telemetry:

                    --telemetry disabled # or MDB_MCP_TELEMETRY=disabled
                  • 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

                  1. Announcing the MongoDB MCP Server

                  2. GitHub Integration in VS Code Using Docker MCP: A Step-by-Step Tutorial

                  3. Get Started with the MongoDB MCP Server

                  4. Github Official mongodb-mcp-server

                  Written by Om-Shree-0709 (@Om-Shree-0709)