DEV Community

Cover image for Set Up a MongoDB MCP Server in VS Code Like a Pro
Eduardo Calzone
Eduardo Calzone

Posted on

Set Up a MongoDB MCP Server in VS Code Like a Pro

๐Ÿš€ What is the Model Context Protocol (MCP)?

The Model Context Protocol (MCP) is an open standard introduced by Anthropic that aims to simplify how AI applicationsโ€”like chatbots, IDE assistants, or custom agentsโ€”connect with external tools, data sources, and systems.

๐Ÿ”Œ Think of it like โ€œUSB for AI integrationsโ€

Before USB, connecting devices meant dealing with dozens of ports and drivers. Similarly, integrating M different AI apps with N different tools used to require M ร— N custom integrationsโ€”a huge development burden.

MCP changes that to an M + N model:

  • Tool creators only need to build 1 MCP server per tool
  • App developers only need to build 1 MCP client per application

๐Ÿงฉ How it works

MCP follows a client-server architecture:

  • Hosts: AI applications the user interacts with (e.g., Claude, an IDE, a custom agent)
  • Clients: Reside within the host and manage a 1:1 connection to an MCP server
  • Servers: External programs exposing functionality to AI apps via a standard API

๐Ÿ“š MCP Servers expose 3 components:

  1. Tools (model-controlled):

    Functions the LLM can call to perform actions (e.g., calling a weather API)

  2. Resources (app-controlled):

    Data sources the model can read, similar to GET requests in a REST API

    No heavy computation, no side effects

  3. Prompts (user-controlled):

    Predefined templates that help LLMs use tools/resources in the most effective way


This enables a modular, scalable ecosystem for AI integrations โ€” with minimal duplication and maximum reuse.

๐Ÿ› ๏ธ Configuring mongodb-mcp-server in VS Code

On the mongodb-mcp-server npm page, you'll find four different ways to run the server.

In this tutorial, we'll choose the simplest option โ€” using a connection string directly in the mcp.json file.

Let's run the following command in your terminal:

npm i mongodb-mcp-server
Enter fullscreen mode Exit fullscreen mode

Make sure you're using Node.js version 20 or higher. You can check your version with:

node -v
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Create the .vscode/mcp.json file

Inside your project's .vscode folder, create a file named mcp.json with the following content:

{
  "mcpServers": {
    "MongoDB": {
      "command": "npx",
      "args": [
        "-y",
        "mongodb-mcp-server",
        "--connectionString",
        "mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
      ]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Once you've entered your MongoDB connection details, click Start to launch the server.

๐Ÿš€ Installing GitHub Copilot Extensions

We will install the two GitHub Copilot extensions from the VS Code Marketplace:

  1. GitHub Copilot โ€“ the core AI coding assistant.
  2. GitHub Copilot Chat โ€“ enables chat-based interactions within your IDE.

๐Ÿ”‘ Log in to GitHub Copilot

Then, log in to GitHub Copilot following the instructions provided after installation.

This will authenticate your GitHub account and activate Copilot features in your editor.

๐Ÿง  Using Agent Mode with MongoDB

Once you've selected Agent Mode, you can start using natural language to give high-level instructions to your MongoDB database.

For example, you can type prompts like:

"Show me all users who signed up last week"

"Add a new user with the username TestUser7"

The agent will translate these into MongoDB queries and execute them for you.


Let's see what we have in our database in the User collection:

๐Ÿ’ฌ Running Our Instructions with Chat Copilot on MongoDB

Happy coding! ๐Ÿฅณ

Top comments (0)