mongo-mcp
Enables CRUD operations on MongoDB databases and collections, including listing databases and collections, via MCP tools.
README
mongo-mcp
A Model Context Protocol (MCP) server for MongoDB operations, using stdio for communication.
This server exposes tools to perform Create, Read, Update, and Delete (CRUD) operations on a MongoDB server, as well as tools for listing databases and collections. It is designed to be run with the MongoDB connection URL provided as a command-line argument or environment variable.
Prerequisites
- Node.js (v20 or later recommended, as used in Dockerfile)
- npm
- A running MongoDB instance
- Docker (for containerized deployment)
Setup and Running Locally
-
Clone the repository (if applicable) and navigate to the
mongo-mcpdirectory. -
Install Dependencies:
npm install -
Build the TypeScript Code:
npm run build -
Run the Server: Provide the MongoDB connection URL as the first argument after
node dist/index.js:node dist/index.js "your_mongodb_connection_string"Example:
node dist/index.js "mongodb://localhost:27017/mydatabase"Alternatively, you can set the
MONGO_URLenvironment variable, which the server will use if no command-line argument is provided:export MONGO_URL="mongodb://localhost:27017/mydatabase" node dist/index.js
Running with Docker
-
Build the Docker Image: From the
mongo-mcpdirectory:docker build -t mongo-mcp-server . -
Run the Docker Container: Provide the MongoDB connection URL as a command-line argument to the container after the image name. If your MongoDB is running on the host machine (e.g.,
localhost), usehost.docker.internal(on Docker Desktop for Mac/Windows) or your host's network IP address for the MongoDB host in the connection string.docker run -i --rm mongo-mcp-server "your_mongodb_connection_string"Example (connecting to MongoDB on host from Docker Desktop):
docker run -i --rm mongo-mcp-server "mongodb://host.docker.internal:27017/mydatabase"Example (connecting to a remote MongoDB):
docker run -i --rm mongo-mcp-server "mongodb://user:password@remote_mongo_host:27017/mydatabase"The
-iflag is crucial for stdio communication.
MCP Configuration (e.g., for .cursor/mcp.json)
To use this server with an MCP client like Cursor, you can add a configuration to your mcp.json file (typically located in .cursor/mcp.json in your workspace). Below are examples for running the server locally and via Docker.
General Structure for .cursor/mcp.json:
{
"mcpServers": {
// ... other server configurations ...
"my_mongodb_server_local": {
"command": "node",
"args": [
"/full/path/to/your/mongo-mcp/dist/index.js", // <-- IMPORTANT: Update this path
"mongodb://localhost:27017/your_default_database" // <-- Update MongoDB URL
],
"transport": "stdio",
"notes": "MongoDB server running locally via node."
},
"my_mongodb_server_docker": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mongo-mcp-server", // Assumes image is built with this tag
"mongodb://host.docker.internal:27017/your_default_database" // <-- Update MongoDB URL
],
"transport": "stdio",
"notes": "MongoDB server running via Docker. Adjust DB URL for non-Docker Desktop or remote DBs."
}
// ... possibly more server configurations ...
}
}
Instructions for adding to your mcp.json:
- Choose a name for your server configuration (e.g.,
my_mongodb_server_localormongo_dev). - Decide on the execution method (local
nodeordocker). - Copy the relevant example into the
mcpServersobject in your.cursor/mcp.json. - Update placeholders:
- For local execution, change
/full/path/to/your/mongo-mcp/dist/index.jsto the correct absolute path on your system. - For both, update the MongoDB connection string (
"mongodb://...") to point to your desired MongoDB instance and database.
- For local execution, change
Available Tools
The server exposes the following tools (tool names are exact):
list_databases: Lists all databases in the MongoDB instance.- Input: (No parameters)
list_collections: Lists all collections in a specified database.- Input:
{ "databaseName": "string" }
- Input:
find_documents: Finds documents in a collection.- Input:
{ "databaseName": "string", "collectionName": "string", "query": { ... } (optional), "projection": { ... } (optional), "limit": number (optional), "skip": number (optional), "sort": { ... } (optional) }
- Input:
insert_document: Inserts a single document into a collection.- Input:
{ "databaseName": "string", "collectionName": "string", "document": { ... } }
- Input:
update_document: Updates a single document matching the filter.- Input:
{ "databaseName": "string", "collectionName": "string", "filter": { ... }, "update": { ... } }
- Input:
delete_document: Deletes a single document matching the filter.- Input:
{ "databaseName": "string", "collectionName": "string", "filter": { ... } }
- Input:
Development
- Run in development mode (uses
tsxfor live reloading TypeScript execution):(Thenpm run dev -- "your_mongodb_connection_string"--ensures arguments are passed to thetsxscript. If no MongoDB URL is provided as an argument, it will look for theMONGO_URLenvironment variable.)
Publishing (Hypothetical NPX Command)
If this package were published to npm (e.g., as mongodb-mcp, based on package.json), an npx command might look like this in an MCP configuration:
{
"mcpServers": {
"mongo_published_npm": {
"command": "npx",
"args": [
"-y", // Or --yes, to auto-confirm npx execution if needed
"mongodb-mcp", // Package name from npm
"mongodb://localhost:27017/mydatabase" // Argument for the MongoDB URL
],
"transport": "stdio"
}
}
}
This server is not currently configured for publishing, but this illustrates a potential pattern if it were.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。