Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

text_search

Enables full-text search on MongoDB collections with features like phrase support, word stemming, stop words removal, and text score ranking. Requires a text index on the collection.

Instructions

Perform a full-text search on a collection.

Requirements:

  • Collection must have a text index

  • Only one text index per collection is allowed

Features:

  • Supports phrases and keywords

  • Word stemming

  • Stop words removal

  • Text score ranking

Example: use_mcp_tool with server_name: "mongodb", tool_name: "text_search", arguments: { "collection": "articles", "searchText": "mongodb database", "filter": { "published": true }, "limit": 10, "includeScore": true }

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseNoDatabase name (optional if default database is configured)
filterNoAdditional MongoDB query filter (optional)
includeScoreNoInclude text search score in results (optional)
limitNoMaximum number of results to return (optional)
searchTextYesText to search for

Implementation Reference

  • Handler for the 'text_search' tool that executes MongoDB $text search queries with optional filters, score inclusion, and limit. Includes error handling for missing text indexes.
    case 'text_search': { const { database, collection, searchText, filter, limit, includeScore } = request.params .arguments as { database?: string; collection: string; searchText: string; filter?: object; limit?: number; includeScore?: boolean; }; const dbName = database || this.defaultDatabase; if (!dbName) { throw new McpError( ErrorCode.InvalidRequest, 'Database name is required when no default database is configured' ); } const db = client.db(dbName); try { const searchQuery = { $text: { $search: searchText }, ...(filter || {}), }; const projection = includeScore ? { score: { $meta: 'textScore' } } : undefined; let query = db.collection(collection).find(searchQuery); if (projection) { query = query.project(projection); } if (includeScore) { query = query.sort({ score: { $meta: 'textScore' } }); } if (limit) { query = query.limit(limit); } const results = await query.toArray(); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } catch (error) { // Check if error is due to missing text index if (error instanceof Error && error.message.includes('text index')) { throw new McpError( ErrorCode.InvalidRequest, 'No text index found on this collection. Create a text index first using db.collection.createIndex({ "field": "text" })' ); } throw error; } }
  • Input schema for the text_search tool defining parameters: database (optional), collection (required), searchText (required), filter (optional), limit (1-1000), includeScore (boolean).
    inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, searchText: { type: 'string', description: 'Text to search for', }, filter: { type: 'object', description: 'Additional MongoDB query filter (optional)', }, limit: { type: 'number', description: 'Maximum number of results to return (optional)', minimum: 1, maximum: 1000, }, includeScore: { type: 'boolean', description: 'Include text search score in results (optional)', }, }, required: ['collection', 'searchText'], },
  • src/index.ts:946-1002 (registration)
    Registration of the 'text_search' tool in the ListTools response, including name, detailed description, usage example, and input schema.
    name: 'text_search', description: `Perform a full-text search on a collection. Requirements: - Collection must have a text index - Only one text index per collection is allowed Features: - Supports phrases and keywords - Word stemming - Stop words removal - Text score ranking Example: use_mcp_tool with server_name: "mongodb", tool_name: "text_search", arguments: { "collection": "articles", "searchText": "mongodb database", "filter": { "published": true }, "limit": 10, "includeScore": true }`, inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, searchText: { type: 'string', description: 'Text to search for', }, filter: { type: 'object', description: 'Additional MongoDB query filter (optional)', }, limit: { type: 'number', description: 'Maximum number of results to return (optional)', minimum: 1, maximum: 1000, }, includeScore: { type: 'boolean', description: 'Include text search score in results (optional)', }, }, required: ['collection', 'searchText'], }, },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jonfreeland/mongodb-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server