mcp-mongo-server 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/build/index.js +44 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,6 +13,12 @@ A Model Context Protocol server that provides access to MongoDB databases. This
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
+
### Read-Only Mode
|
|
17
|
+
- Connect to MongoDB in read-only mode with `--read-only` or `-r` flag
|
|
18
|
+
- Prevents write operations (update, insert, createIndex)
|
|
19
|
+
- Uses MongoDB's secondary read preference for optimal read performance
|
|
20
|
+
- Provides additional safety for production database connections
|
|
21
|
+
|
|
16
22
|
### Resources
|
|
17
23
|
- List and access collections via `mongodb://` URIs
|
|
18
24
|
- Each collection has a name, description and schema
|
|
@@ -97,6 +103,14 @@ On Windows: `%APPDATA%/Claude/claude_desktop_config.json`
|
|
|
97
103
|
"mongodb://muhammed:kilic@mongodb.localhost/namespace"
|
|
98
104
|
]
|
|
99
105
|
},
|
|
106
|
+
"mongodb-readonly": {
|
|
107
|
+
"command": "node",
|
|
108
|
+
"args": [
|
|
109
|
+
"~/mcp-mongo-server/build/index.js",
|
|
110
|
+
"mongodb://muhammed:kilic@mongodb.localhost/namespace",
|
|
111
|
+
"--read-only"
|
|
112
|
+
]
|
|
113
|
+
}
|
|
100
114
|
}
|
|
101
115
|
}
|
|
102
116
|
```
|
|
@@ -139,6 +153,24 @@ To use this server with the Claude Desktop app, add the following configuration
|
|
|
139
153
|
"mcp-mongo-server",
|
|
140
154
|
"mongodb://muhammed:kilic@mongodb.localhost/sample_namespace"
|
|
141
155
|
]
|
|
156
|
+
},
|
|
157
|
+
"mongodb-readonly": {
|
|
158
|
+
"command": "npx",
|
|
159
|
+
"args": [
|
|
160
|
+
"-y",
|
|
161
|
+
"mcp-mongo-server",
|
|
162
|
+
"mongodb://muhammed:kilic@mongodb.localhost/sample_namespace",
|
|
163
|
+
"--read-only"
|
|
164
|
+
]
|
|
165
|
+
},
|
|
166
|
+
"mongodb-github": {
|
|
167
|
+
"command": "npx",
|
|
168
|
+
"args": [
|
|
169
|
+
"-y",
|
|
170
|
+
"github:kiliczsh/mcp-mongo-server",
|
|
171
|
+
"mongodb://muhammed:kilic@mongodb.localhost/sample_namespace",
|
|
172
|
+
"--read-only"
|
|
173
|
+
]
|
|
142
174
|
}
|
|
143
175
|
}
|
|
144
176
|
}
|
|
@@ -162,6 +194,21 @@ npx @michaellatman/mcp-get@latest install mcp-mongo-server
|
|
|
162
194
|
|
|
163
195
|
Replace `/sample_namespace` with your database name.
|
|
164
196
|
|
|
197
|
+
## Using Read-Only Mode
|
|
198
|
+
|
|
199
|
+
You can connect to MongoDB in read-only mode by adding the `--read-only` or `-r` flag when starting the server. This is recommended when you need to protect your data from accidental writes or when connecting to production databases.
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# Connect in read-only mode using the command line
|
|
203
|
+
npx mcp-mongo-server mongodb://user:password@mongodb.example.com/database --read-only
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
When in read-only mode:
|
|
207
|
+
1. All write operations (update, insert, createIndex) will be blocked
|
|
208
|
+
2. The server connects using MongoDB's secondary read preference
|
|
209
|
+
3. The connection status indicates read-only mode is active
|
|
210
|
+
4. The `ping` and `serverInfo` responses include read-only status information
|
|
211
|
+
|
|
165
212
|
## License
|
|
166
213
|
|
|
167
214
|
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
|
package/build/index.js
CHANGED
|
@@ -10,19 +10,23 @@
|
|
|
10
10
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
11
11
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
12
12
|
import { CallToolRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, ListPromptsRequestSchema, GetPromptRequestSchema, ListResourceTemplatesRequestSchema, PingRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
-
import { MongoClient } from "mongodb";
|
|
13
|
+
import { MongoClient, ReadPreference } from "mongodb";
|
|
14
14
|
/**
|
|
15
15
|
* MongoDB connection client and database reference
|
|
16
16
|
*/
|
|
17
17
|
let client = null;
|
|
18
18
|
let db = null;
|
|
19
|
+
/**
|
|
20
|
+
* Flag indicating whether the connection is in read-only mode
|
|
21
|
+
*/
|
|
22
|
+
let isReadOnlyMode = false;
|
|
19
23
|
/**
|
|
20
24
|
* Create an MCP server with capabilities for resources (to list/read collections),
|
|
21
25
|
* tools (to query data), and prompts (to analyze collections).
|
|
22
26
|
*/
|
|
23
27
|
const server = new Server({
|
|
24
28
|
name: "mongodb",
|
|
25
|
-
version: "1.1.
|
|
29
|
+
version: "1.1.2",
|
|
26
30
|
}, {
|
|
27
31
|
capabilities: {
|
|
28
32
|
resources: {},
|
|
@@ -33,11 +37,15 @@ const server = new Server({
|
|
|
33
37
|
/**
|
|
34
38
|
* Initialize MongoDB connection
|
|
35
39
|
*/
|
|
36
|
-
async function connectToMongoDB(url) {
|
|
40
|
+
async function connectToMongoDB(url, readOnly = false) {
|
|
37
41
|
try {
|
|
38
|
-
|
|
42
|
+
const options = readOnly
|
|
43
|
+
? { readPreference: ReadPreference.SECONDARY }
|
|
44
|
+
: {};
|
|
45
|
+
client = new MongoClient(url, options);
|
|
39
46
|
await client.connect();
|
|
40
47
|
db = client.db();
|
|
48
|
+
isReadOnlyMode = readOnly;
|
|
41
49
|
return true;
|
|
42
50
|
}
|
|
43
51
|
catch (error) {
|
|
@@ -56,7 +64,9 @@ server.setRequestHandler(PingRequestSchema, async () => {
|
|
|
56
64
|
}
|
|
57
65
|
// Ping MongoDB to verify connection
|
|
58
66
|
await db.command({ ping: 1 });
|
|
59
|
-
return {
|
|
67
|
+
return {
|
|
68
|
+
readOnlyMode: isReadOnlyMode,
|
|
69
|
+
};
|
|
60
70
|
}
|
|
61
71
|
catch (error) {
|
|
62
72
|
if (error instanceof Error) {
|
|
@@ -386,14 +396,14 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
|
386
396
|
properties: {
|
|
387
397
|
nameOnly: {
|
|
388
398
|
type: "boolean",
|
|
389
|
-
description: "Optional: If true, returns only the collection names instead of full collection info"
|
|
399
|
+
description: "Optional: If true, returns only the collection names instead of full collection info",
|
|
390
400
|
},
|
|
391
401
|
filter: {
|
|
392
402
|
type: "object",
|
|
393
|
-
description: "Optional: Filter to apply to the collections"
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
403
|
+
description: "Optional: Filter to apply to the collections",
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
},
|
|
397
407
|
},
|
|
398
408
|
],
|
|
399
409
|
};
|
|
@@ -404,6 +414,12 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
|
404
414
|
*/
|
|
405
415
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
406
416
|
const collection = db.collection(request.params.arguments?.collection);
|
|
417
|
+
// Define write operations that should be blocked in read-only mode
|
|
418
|
+
const writeOperations = ["update", "insert", "createIndex"];
|
|
419
|
+
// Check if the operation is a write operation and we're in read-only mode
|
|
420
|
+
if (isReadOnlyMode && writeOperations.includes(request.params.name)) {
|
|
421
|
+
throw new Error(`ReadonlyError: Operation '${request.params.name}' is not allowed in read-only mode`);
|
|
422
|
+
}
|
|
407
423
|
switch (request.params.name) {
|
|
408
424
|
case "query": {
|
|
409
425
|
const { filter, projection, limit, explain } = request.params.arguments || {};
|
|
@@ -624,6 +640,10 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
624
640
|
bits: buildInfo.bits,
|
|
625
641
|
ok: buildInfo.ok,
|
|
626
642
|
status: {},
|
|
643
|
+
connectionInfo: {
|
|
644
|
+
readOnlyMode: isReadOnlyMode,
|
|
645
|
+
readPreference: isReadOnlyMode ? "secondary" : "primary",
|
|
646
|
+
},
|
|
627
647
|
};
|
|
628
648
|
// Add server status information if requested
|
|
629
649
|
if (serverStatus) {
|
|
@@ -995,11 +1015,23 @@ Use these patterns to construct MongoDB queries.`,
|
|
|
995
1015
|
*/
|
|
996
1016
|
async function main() {
|
|
997
1017
|
const args = process.argv.slice(2);
|
|
998
|
-
|
|
1018
|
+
let connectionUrl = "";
|
|
1019
|
+
let readOnlyMode = false;
|
|
1020
|
+
// Parse command line arguments
|
|
1021
|
+
for (let i = 0; i < args.length; i++) {
|
|
1022
|
+
if (args[i] === "--read-only" || args[i] === "-r") {
|
|
1023
|
+
readOnlyMode = true;
|
|
1024
|
+
}
|
|
1025
|
+
else if (!connectionUrl) {
|
|
1026
|
+
connectionUrl = args[i];
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
if (!connectionUrl) {
|
|
999
1030
|
console.error("Please provide a MongoDB connection URL as a command-line argument");
|
|
1031
|
+
console.error("Usage: command <mongodb-url> [--read-only|-r]");
|
|
1000
1032
|
process.exit(1);
|
|
1001
1033
|
}
|
|
1002
|
-
const connected = await connectToMongoDB(
|
|
1034
|
+
const connected = await connectToMongoDB(connectionUrl, readOnlyMode);
|
|
1003
1035
|
if (!connected) {
|
|
1004
1036
|
console.error("Failed to connect to MongoDB");
|
|
1005
1037
|
process.exit(1);
|