mcp-mongo-server 1.1.2 → 1.3.0

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,281 @@
1
+ export async function handleListToolsRequest({ request, client, db, isReadOnlyMode, }) {
2
+ return {
3
+ tools: [
4
+ {
5
+ name: "query",
6
+ description: "Execute a MongoDB query with optional execution plan analysis",
7
+ inputSchema: {
8
+ type: "object",
9
+ properties: {
10
+ collection: {
11
+ type: "string",
12
+ description: "Name of the collection to query",
13
+ },
14
+ filter: {
15
+ type: "object",
16
+ description: "MongoDB query filter. Supports date strings in ISO format ('2025-01-01T00:00:00Z') and ISODate('2025-01-01T00:00:00Z') notation"
17
+ },
18
+ projection: {
19
+ type: "object",
20
+ description: "Fields to include/exclude",
21
+ },
22
+ limit: {
23
+ type: "number",
24
+ description: "Maximum number of documents to return",
25
+ default: 10,
26
+ },
27
+ explain: {
28
+ type: "string",
29
+ description: "Optional: Get query execution information",
30
+ enum: ["queryPlanner", "executionStats", "allPlansExecution"],
31
+ },
32
+ objectIdMode: {
33
+ type: "string",
34
+ description: "Control how 24-character hex strings are handled",
35
+ enum: ["auto", "none", "force"],
36
+ default: "auto",
37
+ },
38
+ },
39
+ required: ["collection"],
40
+ },
41
+ },
42
+ {
43
+ name: "aggregate",
44
+ description: "Execute a MongoDB aggregation pipeline with optional execution plan analysis",
45
+ inputSchema: {
46
+ type: "object",
47
+ properties: {
48
+ collection: {
49
+ type: "string",
50
+ description: "Name of the collection to aggregate",
51
+ },
52
+ pipeline: {
53
+ type: "array",
54
+ description: "Aggregation pipeline stages. Supports date strings in ISO format ('2025-01-01T00:00:00Z') and ISODate('2025-01-01T00:00:00Z') notation",
55
+ items: {
56
+ type: "object",
57
+ },
58
+ },
59
+ explain: {
60
+ type: "string",
61
+ description: "Optional: Get aggregation execution information (queryPlanner, executionStats, or allPlansExecution)",
62
+ enum: ["queryPlanner", "executionStats", "allPlansExecution"],
63
+ },
64
+ objectIdMode: {
65
+ type: "string",
66
+ description: "Control how 24-character hex strings are handled",
67
+ enum: ["auto", "none", "force"],
68
+ default: "auto",
69
+ },
70
+ },
71
+ required: ["collection", "pipeline"],
72
+ },
73
+ },
74
+ {
75
+ name: "update",
76
+ description: "Update documents in a MongoDB collection",
77
+ inputSchema: {
78
+ type: "object",
79
+ properties: {
80
+ collection: {
81
+ type: "string",
82
+ description: "Name of the collection to update",
83
+ },
84
+ filter: {
85
+ type: "object",
86
+ description: "Filter to select documents to update. Supports date strings in ISO format ('2025-01-01T00:00:00Z') and ISODate('2025-01-01T00:00:00Z') notation",
87
+ },
88
+ update: {
89
+ type: "object",
90
+ description: "Update operations to apply ($set, $unset, $inc, etc.)",
91
+ },
92
+ upsert: {
93
+ type: "boolean",
94
+ description: "Create a new document if no documents match the filter",
95
+ },
96
+ multi: {
97
+ type: "boolean",
98
+ description: "Update multiple documents that match the filter",
99
+ },
100
+ objectIdMode: {
101
+ type: "string",
102
+ description: "Control how 24-character hex strings are handled",
103
+ enum: ["auto", "none", "force"],
104
+ default: "auto",
105
+ },
106
+ },
107
+ required: ["collection", "filter", "update"],
108
+ },
109
+ },
110
+ {
111
+ name: "serverInfo",
112
+ description: "Get MongoDB server information including version, storage engine, and other details",
113
+ inputSchema: {
114
+ type: "object",
115
+ properties: {
116
+ includeDebugInfo: {
117
+ type: "boolean",
118
+ description: "Include additional debug information about the server",
119
+ },
120
+ },
121
+ },
122
+ },
123
+ {
124
+ name: "insert",
125
+ description: "Insert one or more documents into a MongoDB collection",
126
+ inputSchema: {
127
+ type: "object",
128
+ properties: {
129
+ collection: {
130
+ type: "string",
131
+ description: "Name of the collection to insert into",
132
+ },
133
+ documents: {
134
+ type: "array",
135
+ description: "Array of documents to insert",
136
+ items: { type: "object" },
137
+ },
138
+ ordered: {
139
+ type: "boolean",
140
+ description: "If true, perform ordered insert. If false, insert unordered",
141
+ },
142
+ writeConcern: {
143
+ type: "object",
144
+ description: "Write concern for the insert operation",
145
+ },
146
+ bypassDocumentValidation: {
147
+ type: "boolean",
148
+ description: "Allow insert to bypass schema validation",
149
+ },
150
+ objectIdMode: {
151
+ type: "string",
152
+ description: "Control how 24-character hex strings are handled",
153
+ enum: ["auto", "none", "force"],
154
+ default: "auto",
155
+ },
156
+ },
157
+ required: ["collection", "documents"],
158
+ },
159
+ },
160
+ {
161
+ name: "createIndex",
162
+ description: "Create one or more indexes on a MongoDB collection",
163
+ inputSchema: {
164
+ type: "object",
165
+ properties: {
166
+ collection: {
167
+ type: "string",
168
+ description: "Name of the collection to create indexes on",
169
+ },
170
+ indexes: {
171
+ type: "array",
172
+ description: "Array of index specifications",
173
+ items: {
174
+ type: "object",
175
+ properties: {
176
+ key: {
177
+ type: "object",
178
+ description: "Index key pattern, e.g. { field: 1 }",
179
+ },
180
+ name: {
181
+ type: "string",
182
+ description: "Optional: Name of the index",
183
+ },
184
+ unique: {
185
+ type: "boolean",
186
+ description: "Optional: Creates a unique index",
187
+ },
188
+ sparse: {
189
+ type: "boolean",
190
+ description: "Optional: Creates a sparse index",
191
+ },
192
+ background: {
193
+ type: "boolean",
194
+ description: "Optional: Builds index in background",
195
+ },
196
+ expireAfterSeconds: {
197
+ type: "number",
198
+ description: "TTL in seconds for documents",
199
+ },
200
+ partialFilterExpression: {
201
+ type: "object",
202
+ description: "Filter expression for partial indexes",
203
+ },
204
+ },
205
+ required: ["key"],
206
+ },
207
+ },
208
+ writeConcern: {
209
+ type: "object",
210
+ description: "Write concern for index creation",
211
+ },
212
+ commitQuorum: {
213
+ type: "string",
214
+ description: "Number of members required to create the index",
215
+ },
216
+ objectIdMode: {
217
+ type: "string",
218
+ description: "Control how 24-character hex strings are handled",
219
+ enum: ["auto", "none", "force"],
220
+ default: "auto",
221
+ },
222
+ },
223
+ required: ["collection", "indexes"],
224
+ },
225
+ },
226
+ {
227
+ name: "count",
228
+ description: "Count documents in a collection matching a query",
229
+ inputSchema: {
230
+ type: "object",
231
+ properties: {
232
+ collection: { type: "string", description: "Collection name" },
233
+ query: { type: "object", description: "Query filter to count" },
234
+ limit: { type: "integer", description: "Max documents to count" },
235
+ skip: {
236
+ type: "integer",
237
+ description: "Docs to skip before counting",
238
+ },
239
+ hint: { type: "object", description: "Index hint" },
240
+ readConcern: { type: "object", description: "Read concern option" },
241
+ maxTimeMS: { type: "integer", description: "Max execution time" },
242
+ collation: {
243
+ type: "object",
244
+ description: "Collation rules for comparison",
245
+ },
246
+ objectIdMode: {
247
+ type: "string",
248
+ description: "Control how 24-character hex strings are handled",
249
+ enum: ["auto", "none", "force"],
250
+ default: "auto",
251
+ },
252
+ },
253
+ required: ["collection"],
254
+ },
255
+ },
256
+ {
257
+ name: "listCollections",
258
+ description: "List all collections in the MongoDB database",
259
+ inputSchema: {
260
+ type: "object",
261
+ properties: {
262
+ nameOnly: {
263
+ type: "boolean",
264
+ description: "If true, return only collection names",
265
+ },
266
+ filter: {
267
+ type: "object",
268
+ description: "Filter for collections",
269
+ },
270
+ objectIdMode: {
271
+ type: "string",
272
+ description: "Control how 24-character hex strings are handled",
273
+ enum: ["auto", "none", "force"],
274
+ default: "auto",
275
+ },
276
+ },
277
+ },
278
+ },
279
+ ],
280
+ };
281
+ }
@@ -0,0 +1,65 @@
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ import { CallToolRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, ListPromptsRequestSchema, GetPromptRequestSchema, ListResourceTemplatesRequestSchema, PingRequestSchema, CompleteRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
3
+ import { handleReadResourceRequest, handleListResourcesRequest, } from "./schemas/resource.js";
4
+ import { handlePingRequest } from "./schemas/ping.js";
5
+ import { handleListToolsRequest } from "./schemas/tools.js";
6
+ import { handleListPromptsRequest } from "./schemas/prompts.js";
7
+ import { handleCallToolRequest } from "./schemas/call.js";
8
+ import { handleListResourceTemplatesRequest } from "./schemas/templates.js";
9
+ import { handleGetPromptRequest } from "./schemas/prompts.js";
10
+ import { handleCompletionRequest } from "./schemas/completion.js";
11
+ /**
12
+ * Create an MCP server with capabilities for resources (to list/read collections),
13
+ * tools (to query data), and prompts (to analyze collections).
14
+ */
15
+ export function createServer(client, db, isReadOnlyMode = false, options = {}) {
16
+ const server = new Server({
17
+ name: "mongodb",
18
+ version: "1.3.0",
19
+ ...options,
20
+ }, {
21
+ capabilities: {
22
+ resources: {},
23
+ tools: {},
24
+ prompts: {},
25
+ },
26
+ ...options,
27
+ });
28
+ /**
29
+ * Handler for ping requests to check server health
30
+ */
31
+ server.setRequestHandler(PingRequestSchema, (request) => handlePingRequest({ request, client, db, isReadOnlyMode }));
32
+ /**
33
+ * Handler for listing available collections as resources.
34
+ */
35
+ server.setRequestHandler(ListResourcesRequestSchema, (request) => handleListResourcesRequest({ request, client, db, isReadOnlyMode }));
36
+ /**
37
+ * Handler for reading a collection's schema or contents.
38
+ */
39
+ server.setRequestHandler(ReadResourceRequestSchema, (request) => handleReadResourceRequest({ request, client, db, isReadOnlyMode }));
40
+ /**
41
+ * Handler that lists available tools.
42
+ */
43
+ server.setRequestHandler(ListToolsRequestSchema, (request) => handleListToolsRequest({ request, client, db, isReadOnlyMode }));
44
+ /**
45
+ * Handler for MongoDB tools.
46
+ */
47
+ server.setRequestHandler(CallToolRequestSchema, (request) => handleCallToolRequest({ request, client, db, isReadOnlyMode }));
48
+ /**
49
+ * Handler that lists available prompts.
50
+ */
51
+ server.setRequestHandler(ListPromptsRequestSchema, (request) => handleListPromptsRequest({ request, client, db, isReadOnlyMode }));
52
+ /**
53
+ * Handler for collection analysis prompt.
54
+ */
55
+ server.setRequestHandler(GetPromptRequestSchema, (request) => handleGetPromptRequest({ request, client, db, isReadOnlyMode }));
56
+ /**
57
+ * Handler for listing templates.
58
+ */
59
+ server.setRequestHandler(ListResourceTemplatesRequestSchema, (request) => handleListResourceTemplatesRequest({ request, client, db, isReadOnlyMode }));
60
+ /**
61
+ * Handler for completion requests.
62
+ */
63
+ server.setRequestHandler(CompleteRequestSchema, (request) => handleCompletionRequest({ request, client, db, isReadOnlyMode }));
64
+ return server;
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-mongo-server",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "description": "A Model Context Protocol server for MongoDB connections",
5
5
  "private": false,
6
6
  "type": "module",
package/build/types.js DELETED
@@ -1,169 +0,0 @@
1
- import { z } from "zod";
2
- import { CallToolRequestSchema, ToolSchema, ResourceSchema, TextResourceContentsSchema, } from "@modelcontextprotocol/sdk/types.js";
3
- // MongoDB Collection Schema
4
- export const MongoCollectionSchema = ResourceSchema.extend({
5
- collectionName: z.string(),
6
- databaseName: z.string(),
7
- indexes: z
8
- .array(z.object({
9
- name: z.string(),
10
- keys: z.record(z.union([z.number(), z.string()])),
11
- unique: z.optional(z.boolean()),
12
- }))
13
- .optional(),
14
- });
15
- // MongoDB Document Schema
16
- export const MongoDocumentSchema = TextResourceContentsSchema.extend({
17
- document: z.record(z.unknown()),
18
- });
19
- // MongoDB Query Operators
20
- export const MongoQueryOperatorSchema = z.object({
21
- $eq: z.unknown().optional(),
22
- $gt: z.unknown().optional(),
23
- $gte: z.unknown().optional(),
24
- $in: z.array(z.unknown()).optional(),
25
- $lt: z.unknown().optional(),
26
- $lte: z.unknown().optional(),
27
- $ne: z.unknown().optional(),
28
- $nin: z.array(z.unknown()).optional(),
29
- $and: z.array(z.unknown()).optional(),
30
- $not: z.unknown().optional(),
31
- $nor: z.array(z.unknown()).optional(),
32
- $or: z.array(z.unknown()).optional(),
33
- $exists: z.boolean().optional(),
34
- $type: z.union([z.string(), z.number()]).optional(),
35
- $expr: z.unknown().optional(),
36
- $regex: z.string().optional(),
37
- $options: z.string().optional(),
38
- });
39
- // MongoDB Sort Options
40
- export const MongoSortSchema = z.record(z.union([z.literal(1), z.literal(-1)]));
41
- // MongoDB Query Tool Schema
42
- export const MongoQueryToolSchema = ToolSchema.extend({
43
- inputSchema: z.object({
44
- type: z.literal("object"),
45
- properties: z
46
- .object({
47
- collection: z.string(),
48
- filter: z.record(z.union([z.unknown(), MongoQueryOperatorSchema])),
49
- projection: z.record(z.union([z.literal(0), z.literal(1)])).optional(),
50
- sort: MongoSortSchema.optional(),
51
- limit: z.number().int().positive().optional(),
52
- skip: z.number().int().nonnegative().optional(),
53
- })
54
- .passthrough(),
55
- required: z.array(z.literal("collection")),
56
- }),
57
- });
58
- // MongoDB Aggregate Tool Schema
59
- export const MongoAggregateToolSchema = ToolSchema.extend({
60
- inputSchema: z.object({
61
- type: z.literal("object"),
62
- properties: z
63
- .object({
64
- collection: z.string(),
65
- pipeline: z.array(z.record(z.unknown())),
66
- })
67
- .passthrough(),
68
- required: z.array(z.literal("collection")),
69
- }),
70
- });
71
- // MongoDB Count Tool Schema
72
- export const MongoCountToolSchema = ToolSchema.extend({
73
- inputSchema: z.object({
74
- type: z.literal("object"),
75
- properties: z
76
- .object({
77
- collection: z.string(),
78
- filter: z
79
- .record(z.union([z.unknown(), MongoQueryOperatorSchema]))
80
- .optional(),
81
- })
82
- .passthrough(),
83
- required: z.array(z.literal("collection")),
84
- }),
85
- });
86
- // MongoDB Distinct Tool Schema
87
- export const MongoDistinctToolSchema = ToolSchema.extend({
88
- inputSchema: z.object({
89
- type: z.literal("object"),
90
- properties: z
91
- .object({
92
- collection: z.string(),
93
- field: z.string(),
94
- filter: z
95
- .record(z.union([z.unknown(), MongoQueryOperatorSchema]))
96
- .optional(),
97
- })
98
- .passthrough(),
99
- required: z.array(z.literal("collection")),
100
- }),
101
- });
102
- // MongoDB Call Tool Request Schema
103
- export const MongoCallToolRequestSchema = CallToolRequestSchema.extend({
104
- params: z.object({
105
- name: z.enum([
106
- "query",
107
- "aggregate",
108
- "update",
109
- "serverInfo",
110
- "insert",
111
- "createIndex",
112
- "count",
113
- "distinct",
114
- "listCollections",
115
- ]),
116
- arguments: z.union([
117
- MongoQueryToolSchema.shape.inputSchema,
118
- MongoAggregateToolSchema.shape.inputSchema,
119
- MongoCountToolSchema.shape.inputSchema,
120
- MongoDistinctToolSchema.shape.inputSchema,
121
- ]),
122
- }),
123
- });
124
- // MongoDB Error Types
125
- export const MongoErrorCodeSchema = z.enum([
126
- "INVALID_QUERY",
127
- "COLLECTION_NOT_FOUND",
128
- "DATABASE_NOT_FOUND",
129
- "INVALID_PIPELINE",
130
- "INVALID_PROJECTION",
131
- "INVALID_SORT",
132
- "CONNECTION_ERROR",
133
- "TIMEOUT",
134
- "UNAUTHORIZED",
135
- ]);
136
- export const MongoErrorSchema = z.object({
137
- code: MongoErrorCodeSchema,
138
- message: z.string(),
139
- details: z.unknown().optional(),
140
- });
141
- // MongoDB Schema Inference Types
142
- export const MongoFieldSchemaSchema = z.object({
143
- type: z.union([
144
- z.literal("string"),
145
- z.literal("number"),
146
- z.literal("boolean"),
147
- z.literal("date"),
148
- z.literal("objectId"),
149
- z.literal("array"),
150
- z.literal("object"),
151
- z.literal("null"),
152
- z.literal("mixed"),
153
- ]),
154
- required: z.boolean().optional(),
155
- unique: z.boolean().optional(),
156
- indexed: z.boolean().optional(),
157
- items: z.lazy(() => MongoFieldSchemaSchema).optional(),
158
- properties: z.record(z.lazy(() => MongoFieldSchemaSchema)).optional(),
159
- });
160
- export const MongoCollectionSchemaSchema = z.object({
161
- name: z.string(),
162
- fields: z.record(MongoFieldSchemaSchema),
163
- options: z
164
- .object({
165
- timestamps: z.boolean().optional(),
166
- strict: z.boolean().optional(),
167
- })
168
- .optional(),
169
- });