DEV Community

Cover image for MongoDB Integration in VS Code Using MongoDB MCP: A Step-by-Step Tutorial
Om Shree
Om Shree

Posted on • Originally published at glama.ai

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

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
Enter fullscreen mode Exit fullscreen mode

Name it "mongodb"
figure1

  1. 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/"
       ]
     }
   }
Enter fullscreen mode Exit fullscreen mode

figure2

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

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

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

As with everything else, be sure that you take time to understand what the server does and

  • Use environment variables instead of inline credentials

  • Enable read-only mode:

  npx mongodb-mcp-server --connectionString "..." --readOnly
Enter fullscreen mode Exit fullscreen mode

or use MDB_MCP_READ_ONLY=true

  • Disable mutation tools:
  --disabledTools create update delete
  # or
  MDB_MCP_DISABLED_TOOLS=create,update,delete
Enter fullscreen mode Exit fullscreen mode
  • Disable telemetry:
  --telemetry disabled
  # or
  MDB_MCP_TELEMETRY=disabled
Enter fullscreen mode Exit fullscreen mode
  • 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


  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 

Top comments (2)

Collapse
 
thedeepseeker profile image
Anna kowoski

Nice Article, I’m curious, how well does this MCP thing fit into existing CI/CD pipelines? Can it be used safely for things like automated database migrations, or is it mainly meant for local dev workflows?

Collapse
 
om_shree_0709 profile image
Om Shree

MCP can be integrated into CI/CD pipelines, but for automated tasks like database migrations, it's crucial to assess the security implications and ensure proper access controls are in place. For now it's more commonly used for local dev workflows only/-