Skip to content

Commit 26c0a1b

Browse files
committed
Merge branch 'release/7.2.1'
2 parents d750b53 + d0e6689 commit 26c0a1b

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
node_modules/
33
repomix*
4+
DEMO.md
45
CLAUDE.md
56
ROADMAP.md
67
.mongodb-lens*.json*

mongodb-lens.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ const extractDbNameFromConnectionString = (uri) => {
202202
}
203203

204204
const resolveMongoUri = (uriOrAlias) => {
205-
if (uriOrAlias.includes('://') || uriOrAlias.includes('/') || uriOrAlias.includes('@')) {
205+
if (uriOrAlias.includes('://') || uriOrAlias.includes('@')) {
206206
return uriOrAlias
207207
}
208208

@@ -213,7 +213,7 @@ const resolveMongoUri = (uriOrAlias) => {
213213
return uri
214214
}
215215

216-
return uriOrAlias
216+
throw new Error(`"${uriOrAlias}" is not a valid MongoDB URI or connection alias.`)
217217
}
218218

219219
const obfuscateMongoUri = (uri) => {
@@ -1293,6 +1293,17 @@ const registerTools = (server) => {
12931293
async ({ uri, validateConnection }) => {
12941294
return withErrorHandling(async () => {
12951295
try {
1296+
if (!uri.includes('://') && !uri.includes('@') && !mongoUriMap.has(uri.toLowerCase())) {
1297+
if (await isDatabaseName(uri)) {
1298+
return {
1299+
content: [{
1300+
type: 'text',
1301+
text: `It seems you're trying to switch to database "${uri}" rather than connect to a new MongoDB instance. Please use the "use-database" tool instead.`
1302+
}]
1303+
}
1304+
}
1305+
}
1306+
12961307
const resolvedUri = resolveMongoUri(uri)
12971308
await changeConnection(resolvedUri, validateConnection === 'true')
12981309
return {
@@ -3160,6 +3171,29 @@ const withErrorHandling = async (operation, errorMessage, defaultValue = null) =
31603171
}
31613172
}
31623173

3174+
const isDatabaseName = async (name) => {
3175+
try {
3176+
const dbs = await listDatabases()
3177+
if (dbs.some(db => db.name === name)) return true
3178+
3179+
if (name.length > 0 && name.length < 64 &&
3180+
!name.includes('/') && !name.includes(':')) {
3181+
const dbPatterns = [
3182+
/^sample_/,
3183+
/^test/,
3184+
/^db/,
3185+
/^admin$|^local$|^config$/
3186+
]
3187+
3188+
if (dbPatterns.some(pattern => pattern.test(name))) return true
3189+
}
3190+
3191+
return false
3192+
} catch (error) {
3193+
return false
3194+
}
3195+
}
3196+
31633197
const listDatabases = async () => {
31643198
log('DB Operation: Listing databases…')
31653199
const adminDb = mongoClient.db('admin')

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongodb-lens",
3-
"version": "7.2.0",
3+
"version": "7.2.1",
44
"author": "James Furey (https://about.me/jamesfurey)",
55
"description": "MongoDB Lens: Full Featured MCP Server for MongoDB Databases",
66
"license": "MIT",

0 commit comments

Comments
 (0)