mongo_agent_mcp 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,404 @@
1
+ Metadata-Version: 2.4
2
+ Name: mongo_agent_mcp
3
+ Version: 0.1.0
4
+ Summary: A mongo mcp that can be used to automate the database query with llm
5
+ Requires-Python: >=3.13
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: dotenv>=0.9.9
8
+ Requires-Dist: fastmcp>=3.4.2
9
+ Requires-Dist: pymongo>=4.17.0
10
+
11
+ # MongoDB MCP
12
+
13
+ A MongoDB Model Context Protocol (MCP) Server that allows AI agents and MCP clients to interact with MongoDB databases through standardized tools.
14
+
15
+ ## Features
16
+
17
+ * List all collections
18
+ * Discover collection schemas
19
+ * Fetch collection data
20
+ * Query documents
21
+ * Insert documents
22
+ * Update documents
23
+ * Delete documents
24
+ * MongoDB Atlas support
25
+ * Local MongoDB support
26
+ * MCP stdio transport support
27
+
28
+ ---
29
+
30
+ # Installation
31
+
32
+ ## Using pip
33
+
34
+ ```bash
35
+ pip install mongo-mcp
36
+ ```
37
+
38
+ ## Using uv
39
+
40
+ ```bash
41
+ uv add mongo-mcp
42
+ ```
43
+
44
+ ---
45
+
46
+ # Prerequisites
47
+
48
+ * Python 3.10+
49
+ * MongoDB Local Instance or MongoDB Atlas Cluster
50
+
51
+ Examples:
52
+
53
+ ```txt
54
+ mongodb://localhost:27017
55
+ ```
56
+
57
+ or
58
+
59
+ ```txt
60
+ mongodb+srv://username:password@cluster.mongodb.net
61
+ ```
62
+
63
+ ---
64
+
65
+ # Configuration
66
+
67
+ MongoDB MCP uses environment variables to connect to your database.
68
+
69
+ Create a `.env` file:
70
+
71
+ ```env
72
+ MONGODB_URI=mongodb://localhost:27017
73
+ DATABASE_NAME=shipbihar
74
+ ```
75
+
76
+ ## Environment Variables
77
+
78
+ | Variable | Description | Required |
79
+ | ------------- | ------------------------- | -------- |
80
+ | MONGODB_URI | MongoDB connection string | Yes |
81
+ | DATABASE_NAME | Database name | Yes |
82
+
83
+ ---
84
+
85
+ # Running the MCP Server
86
+
87
+ ```bash
88
+ mongo_mcp
89
+ ```
90
+
91
+ or
92
+
93
+ ```bash
94
+ python -m mongo_mcp.main
95
+ ```
96
+
97
+ The MCP server will start using stdio transport.
98
+
99
+ ---
100
+
101
+ # MCP Client Configuration
102
+
103
+ Example MCP configuration:
104
+
105
+ ```json
106
+ {
107
+ "mcpServers": {
108
+ "mongodb": {
109
+ "command": "mongo_mcp",
110
+ "env": {
111
+ "MONGODB_URI": "mongodb://localhost:27017",
112
+ "DATABASE_NAME": "shipbihar"
113
+ }
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ ---
120
+
121
+ # Available Tools
122
+
123
+ ## all_collections
124
+
125
+ Returns all collections in the configured database.
126
+
127
+ Example Output:
128
+
129
+ ```json
130
+ [
131
+ "users",
132
+ "orders",
133
+ "shipments"
134
+ ]
135
+ ```
136
+
137
+ ---
138
+
139
+ ## fetch_collection_schema
140
+
141
+ Returns an inferred schema from a sample document.
142
+
143
+ Example:
144
+
145
+ ```json
146
+ {
147
+ "_id": "ObjectId",
148
+ "name": "str",
149
+ "email": "str",
150
+ "createdAt": "datetime"
151
+ }
152
+ ```
153
+
154
+ ---
155
+
156
+ ## fetch_collection_data
157
+
158
+ Returns documents from a collection.
159
+
160
+ Parameters:
161
+
162
+ ```json
163
+ {
164
+ "collection_name": "users",
165
+ "limit": 100
166
+ }
167
+ ```
168
+
169
+ ---
170
+
171
+ ## find_document
172
+
173
+ Find a document using a MongoDB query.
174
+
175
+ Example:
176
+
177
+ ```json
178
+ {
179
+ "collection_name": "users",
180
+ "query": {
181
+ "email": "john@example.com"
182
+ }
183
+ }
184
+ ```
185
+
186
+ ---
187
+
188
+ ## insert_document
189
+
190
+ Insert a document.
191
+
192
+ Example:
193
+
194
+ ```json
195
+ {
196
+ "collection_name": "users",
197
+ "document": {
198
+ "name": "John",
199
+ "email": "john@example.com"
200
+ }
201
+ }
202
+ ```
203
+
204
+ ---
205
+
206
+ ## update_document
207
+
208
+ Update matching documents.
209
+
210
+ Example:
211
+
212
+ ```json
213
+ {
214
+ "collection_name": "users",
215
+ "filter_query": {
216
+ "email": "john@example.com"
217
+ },
218
+ "update_data": {
219
+ "role": "admin"
220
+ }
221
+ }
222
+ ```
223
+
224
+ ---
225
+
226
+ ## delete_document
227
+
228
+ Delete matching documents.
229
+
230
+ Example:
231
+
232
+ ```json
233
+ {
234
+ "collection_name": "users",
235
+ "filter_query": {
236
+ "email": "john@example.com"
237
+ }
238
+ }
239
+ ```
240
+
241
+ ---
242
+
243
+ # Common Errors
244
+
245
+ ## Error: DATABASE_NAME is None
246
+
247
+ Error:
248
+
249
+ ```txt
250
+ TypeError: name must be an instance of str, not <class 'NoneType'>
251
+ ```
252
+
253
+ Reason:
254
+
255
+ MongoDB MCP cannot find the `DATABASE_NAME` environment variable.
256
+
257
+ Solution:
258
+
259
+ Create a `.env` file:
260
+
261
+ ```env
262
+ MONGODB_URI=mongodb://localhost:27017
263
+ DATABASE_NAME=your_database_name
264
+ ```
265
+
266
+ or export variables manually.
267
+
268
+ Windows PowerShell:
269
+
270
+ ```powershell
271
+ $env:MONGODB_URI="mongodb://localhost:27017"
272
+ $env:DATABASE_NAME="shipbihar"
273
+ ```
274
+
275
+ Linux/macOS:
276
+
277
+ ```bash
278
+ export MONGODB_URI="mongodb://localhost:27017"
279
+ export DATABASE_NAME="shipbihar"
280
+ ```
281
+
282
+ ---
283
+
284
+ ## Error: Connection Refused
285
+
286
+ Error:
287
+
288
+ ```txt
289
+ ServerSelectionTimeoutError
290
+ ```
291
+
292
+ Reason:
293
+
294
+ MongoDB server is not running.
295
+
296
+ Solution:
297
+
298
+ Start MongoDB:
299
+
300
+ ```bash
301
+ mongod
302
+ ```
303
+
304
+ or verify your Atlas connection string.
305
+
306
+ ---
307
+
308
+ ## Error: Authentication Failed
309
+
310
+ Error:
311
+
312
+ ```txt
313
+ Authentication failed
314
+ ```
315
+
316
+ Reason:
317
+
318
+ Incorrect username or password.
319
+
320
+ Solution:
321
+
322
+ Verify your MongoDB credentials.
323
+
324
+ ---
325
+
326
+ # Security
327
+
328
+ Recommended:
329
+
330
+ * Use dedicated database users
331
+ * Restrict permissions when possible
332
+ * Avoid connecting with admin credentials
333
+ * Store secrets in environment variables
334
+
335
+ Do NOT:
336
+
337
+ * Commit `.env` files to GitHub
338
+ * Hardcode MongoDB passwords in code
339
+
340
+ ---
341
+
342
+ # Development
343
+
344
+ Clone the repository:
345
+
346
+ ```bash
347
+ git clone <repository-url>
348
+ cd mongo-mcp
349
+ ```
350
+
351
+ Create environment:
352
+
353
+ ```bash
354
+ uv venv
355
+ source .venv/bin/activate
356
+ ```
357
+
358
+ Install dependencies:
359
+
360
+ ```bash
361
+ uv sync
362
+ ```
363
+
364
+ Run locally:
365
+
366
+ ```bash
367
+ python -m mongo_mcp.main
368
+ ```
369
+
370
+ ---
371
+
372
+ # Roadmap
373
+
374
+ ## V1
375
+
376
+ * Collection discovery
377
+ * CRUD operations
378
+ * Schema inspection
379
+
380
+ ## V2
381
+
382
+ * Aggregation pipelines
383
+ * Count documents
384
+ * Regex search
385
+
386
+ ## V3
387
+
388
+ * Natural language queries
389
+ * Query optimization
390
+ * Schema caching
391
+
392
+ ---
393
+
394
+ # License
395
+
396
+ MIT License
397
+
398
+ ---
399
+
400
+ # Author
401
+
402
+ Vishnu Bhardwaj
403
+
404
+ Built for AI Agents, MCP Clients, and MongoDB Developers.
@@ -0,0 +1,8 @@
1
+ mongo_mcp/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ mongo_mcp/main.py,sha256=xE1M3lcMLDz-_Kpk-DAtl8S-Yl1F7ULT5n_zVJ3I-ck,124
3
+ mongo_mcp/tools.py,sha256=ooSzk4I46k6HK8CEl_HmelQTDEJwvTqudYmJO6BeBV0,3832
4
+ mongo_agent_mcp-0.1.0.dist-info/METADATA,sha256=eC7p1BoAH-Xefnxj_fCKIQ1VtMBHW_aTIkCs5FnWEhY,5333
5
+ mongo_agent_mcp-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
6
+ mongo_agent_mcp-0.1.0.dist-info/entry_points.txt,sha256=GJ0esSmtZWEkIX6HNHASREt9injyYauZsUyeRLzsfK0,50
7
+ mongo_agent_mcp-0.1.0.dist-info/top_level.txt,sha256=nU4IZ7nMfeJXnfIN3mbwrmwHWzdXqVezA3UYQBjIF1A,10
8
+ mongo_agent_mcp-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ mongo_mcp = mongo_mcp.main:main
@@ -0,0 +1 @@
1
+ mongo_mcp
mongo_mcp/__init__.py ADDED
File without changes
mongo_mcp/main.py ADDED
@@ -0,0 +1,11 @@
1
+ from mongo_mcp.tools import mcp
2
+
3
+ def main():
4
+ mcp.run(transport="stdio")
5
+
6
+
7
+
8
+
9
+
10
+ if __name__ == "__main__":
11
+ main()
mongo_mcp/tools.py ADDED
@@ -0,0 +1,203 @@
1
+
2
+ from fastmcp import FastMCP
3
+ from pymongo import MongoClient
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # MongoDB Connection
11
+ client = MongoClient(os.getenv("MONGODB_URI"))
12
+ db = client[os.getenv("DATABASE_NAME")]
13
+
14
+ # MCP Instance
15
+ mcp = FastMCP("MongoDB MCP")
16
+
17
+
18
+ # =========================
19
+ # COLLECTION TOOLS
20
+ # =========================
21
+
22
+ @mcp.tool()
23
+ def all_collections():
24
+ """
25
+ Returns all collections in the database.
26
+ """
27
+ try:
28
+ return db.list_collection_names()
29
+
30
+ except Exception as e:
31
+ return {"error": str(e)}
32
+
33
+
34
+ @mcp.tool()
35
+ def fetch_collection_schema(collection_name: str):
36
+ """
37
+ Returns inferred schema from a sample document.
38
+ """
39
+
40
+ try:
41
+ doc = db[collection_name].find_one()
42
+
43
+ if not doc:
44
+ return {"error": "Collection is empty"}
45
+
46
+ return {
47
+ key: type(value).__name__
48
+ for key, value in doc.items()
49
+ }
50
+
51
+ except Exception as e:
52
+ return {"error": str(e)}
53
+
54
+
55
+ # =========================
56
+ # READ TOOLS
57
+ # =========================
58
+
59
+ @mcp.tool()
60
+ def fetch_collection_data(
61
+ collection_name: str,
62
+ limit: int = 100
63
+ ):
64
+ """
65
+ Fetch documents from a collection.
66
+ """
67
+
68
+ try:
69
+ documents = list(
70
+ db[collection_name]
71
+ .find()
72
+ .limit(limit)
73
+ )
74
+
75
+ for doc in documents:
76
+ doc["_id"] = str(doc["_id"])
77
+
78
+ return documents
79
+
80
+ except Exception as e:
81
+ return {"error": str(e)}
82
+
83
+
84
+ @mcp.tool()
85
+ def find_document(
86
+ collection_name: str,
87
+ query: dict
88
+ ):
89
+ """
90
+ Find a single document.
91
+ """
92
+
93
+ try:
94
+ document = db[collection_name].find_one(query)
95
+
96
+ if not document:
97
+ return {"message": "Document not found"}
98
+
99
+ document["_id"] = str(document["_id"])
100
+
101
+ return document
102
+
103
+ except Exception as e:
104
+ return {"error": str(e)}
105
+
106
+
107
+ @mcp.tool()
108
+ def count_documents(
109
+ collection_name: str,
110
+ query: dict = {}
111
+ ):
112
+ """
113
+ Count matching documents.
114
+ """
115
+
116
+ try:
117
+ count = db[collection_name].count_documents(query)
118
+
119
+ return {"count": count}
120
+
121
+ except Exception as e:
122
+ return {"error": str(e)}
123
+
124
+
125
+ # =========================
126
+ # INSERT TOOL
127
+ # =========================
128
+
129
+ @mcp.tool()
130
+ def insert_document(
131
+ collection_name: str,
132
+ document: dict
133
+ ):
134
+ """
135
+ Insert a document.
136
+ """
137
+
138
+ try:
139
+ result = db[collection_name].insert_one(document)
140
+
141
+ return {
142
+ "success": True,
143
+ "inserted_id": str(result.inserted_id)
144
+ }
145
+
146
+ except Exception as e:
147
+ return {"error": str(e)}
148
+
149
+
150
+ # =========================
151
+ # UPDATE TOOL
152
+ # =========================
153
+
154
+ @mcp.tool()
155
+ def update_document(
156
+ collection_name: str,
157
+ filter_query: dict,
158
+ update_data: dict
159
+ ):
160
+ """
161
+ Update matching document.
162
+ """
163
+
164
+ try:
165
+ result = db[collection_name].update_one(
166
+ filter_query,
167
+ {"$set": update_data}
168
+ )
169
+
170
+ return {
171
+ "matched_count": result.matched_count,
172
+ "modified_count": result.modified_count
173
+ }
174
+
175
+ except Exception as e:
176
+ return {"error": str(e)}
177
+
178
+
179
+ # =========================
180
+ # DELETE TOOL
181
+ # =========================
182
+
183
+ @mcp.tool()
184
+ def delete_document(
185
+ collection_name: str,
186
+ filter_query: dict
187
+ ):
188
+ """
189
+ Delete matching document.
190
+ """
191
+
192
+ try:
193
+ result = db[collection_name].delete_one(
194
+ filter_query
195
+ )
196
+
197
+ return {
198
+ "deleted_count": result.deleted_count
199
+ }
200
+
201
+ except Exception as e:
202
+ return {"error": str(e)}
203
+