Skip to main content
Glama
ricleedo

MCP Server Boilerplate

by ricleedo

mongo-find-documents

Query documents from a MongoDB collection by specifying database, collection, and optional filters to retrieve specific data.

Instructions

Query documents from a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
filterNoQuery filter as JSON object (optional)
limitNoMaximum number of documents to return (optional)

Implementation Reference

  • Handler function that ensures MongoDB connection, queries the collection with optional filter and limit, fetches documents, formats output, and returns formatted results as text content.
      async ({ database: dbName, collection: collectionName, filter = {}, limit }) => {
        try {
          const db = await ensureConnection(dbName);
          const collection: Collection = db.collection(collectionName);
          
          let cursor = collection.find(filter);
          if (limit) {
            cursor = cursor.limit(limit);
          }
          
          const documents = await cursor.toArray();
          
          const formattedOutput = formatJsonOutput(documents);
          
          return {
            content: [
              {
                type: "text",
                text: `Found ${documents.length} document(s):\n\n${formattedOutput}`,
              },
            ],
          };
        } catch (error) {
          throw new Error(`Failed to find documents: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    );
  • Zod input schema defining parameters for the tool: database name, collection name, optional query filter, and optional limit.
    {
      database: z.string().describe("Database name"),
      collection: z.string().describe("Collection name"),
      filter: z.record(z.any()).optional().describe("Query filter as JSON object (optional)"),
      limit: z.number().optional().describe("Maximum number of documents to return (optional)"),
    },
  • src/index.ts:131-133 (registration)
    Registration of the mongo-find-documents tool using server.tool() with name and description.
    server.tool(
      "mongo-find-documents",
      "Query documents from a MongoDB collection",
  • Helper function to format JSON output, truncating large data and cleaning up placeholder strings for better readability.
    function formatJsonOutput(data: unknown): string {
      const truncatedData = truncateForOutput(data);
      let outputText = JSON.stringify(truncatedData, null, 2);
      
      outputText = outputText.replace(
        /"\.\.\.(\d+) more items"/g,
        "...$1 more items"
      );
      outputText = outputText.replace(
        /"\.\.\.(\d+) more properties": "\.\.\.?"/g,
        "...$1 more properties"
      );
      
      return outputText;
    }
  • Helper function to establish and reuse MongoDB connection and database instances.
    async function ensureConnection(dbName: string): Promise<Db> {
      if (!mongoClient) {
        const uri = getMongoUri();
        mongoClient = new MongoClient(uri);
        await mongoClient.connect();
      }
      
      if (!databases.has(dbName)) {
        databases.set(dbName, mongoClient.db(dbName));
      }
      
      return databases.get(dbName)!;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden but only states the basic action. It doesn't disclose behavioral traits such as read-only nature (implied but not explicit), potential performance impacts, error handling, or return format details, leaving significant gaps for agent understanding.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence with zero wasted words. It's front-loaded and efficiently conveys the core purpose without unnecessary elaboration, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a database query tool with no annotations and no output schema, the description is insufficient. It lacks details on return values, error conditions, or behavioral constraints, leaving the agent with incomplete information for proper tool invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so parameters are well-documented in the schema. The description adds no additional meaning beyond implying querying with a filter, which aligns with the schema but doesn't provide extra context like query syntax examples or usage tips.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Query') and resource ('documents from a MongoDB collection'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'mongo-aggregate' or 'mongo-count-documents' which also involve querying, so it lacks sibling distinction.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like 'mongo-aggregate' for complex queries or 'mongo-count-documents' for counting. The description only states what it does without context for selection among siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/ricleedo/mongo-boilerplate-mcp'

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