Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

get_distinct_values

Retrieve unique values from a specific field in a MongoDB collection to analyze data distribution, identify categories, or perform quality checks.

Instructions

Get distinct values for a field in a collection.

Useful for:

  • Understanding data distribution

  • Finding unique categories

  • Data quality checks

  • Identifying outliers

Example: use_mcp_tool with server_name: "mongodb", tool_name: "get_distinct_values", arguments: { "collection": "users", "field": "role", "filter": { "active": true } }

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseNoDatabase name (optional if default database is configured)
collectionYesCollection name
fieldYesField name to get distinct values for
filterNoMongoDB query filter to apply before getting distinct values (optional)

Implementation Reference

  • The handler for the 'get_distinct_values' tool. Extracts parameters, connects to the MongoDB database, executes the distinct operation on the specified field with optional filter, and returns the unique values as JSON.
    case 'get_distinct_values': {
      const { database, collection, field, filter } = request.params.arguments as {
        database?: string;
        collection: string;
        field: string;
        filter?: object;
      };
      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);
      const values = await db.collection(collection).distinct(field, filter || {});
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(values, null, 2),
          },
        ],
      };
    }
  • src/index.ts:639-679 (registration)
    Tool registration in the list_tools response, including the name, description, and input schema definition for 'get_distinct_values'.
              name: 'get_distinct_values',
              description: `Get distinct values for a field in a collection.
    
    Useful for:
    - Understanding data distribution
    - Finding unique categories
    - Data quality checks
    - Identifying outliers
    
    Example:
    use_mcp_tool with
      server_name: "mongodb",
      tool_name: "get_distinct_values",
      arguments: {
        "collection": "users",
        "field": "role",
        "filter": { "active": true }
      }`,
              inputSchema: {
                type: 'object',
                properties: {
                  database: {
                    type: 'string',
                    description: 'Database name (optional if default database is configured)',
                  },
                  collection: {
                    type: 'string',
                    description: 'Collection name',
                  },
                  field: {
                    type: 'string',
                    description: 'Field name to get distinct values for',
                  },
                  filter: {
                    type: 'object',
                    description: 'MongoDB query filter to apply before getting distinct values (optional)',
                  },
                },
                required: ['collection', 'field'],
              },
            },
  • Input schema definition for the 'get_distinct_values' tool, specifying parameters like database, collection, field, and optional filter.
      inputSchema: {
        type: 'object',
        properties: {
          database: {
            type: 'string',
            description: 'Database name (optional if default database is configured)',
          },
          collection: {
            type: 'string',
            description: 'Collection name',
          },
          field: {
            type: 'string',
            description: 'Field name to get distinct values for',
          },
          filter: {
            type: 'object',
            description: 'MongoDB query filter to apply before getting distinct values (optional)',
          },
        },
        required: ['collection', 'field'],
      },
    },
Behavior3/5

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

No annotations are provided, so the description carries the full burden. It describes the tool's function and includes an example, but lacks details on behavioral traits such as performance considerations (e.g., impact on large collections), error handling, or output format. The example clarifies usage but doesn't fully compensate for the absence of annotations.

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 appropriately sized and front-loaded, starting with a clear purpose statement followed by a bulleted list of use cases and a practical example. Every sentence earns its place by adding value without redundancy, making it efficient and well-structured for quick understanding.

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

Completeness3/5

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

Given the tool's moderate complexity (4 parameters, no output schema, no annotations), the description is somewhat complete but has gaps. It explains the tool's purpose and usage but lacks details on behavioral aspects like performance or error handling. Without an output schema, it doesn't describe return values, leaving the agent to infer results from the example.

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?

Schema description coverage is 100%, so the schema already documents all four parameters thoroughly. The description does not add meaning beyond what the schema provides, such as explaining parameter interactions or constraints. The example illustrates usage but doesn't enhance parameter semantics, resulting in a baseline score of 3.

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

Purpose5/5

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

The description clearly states the verb 'Get' and resource 'distinct values for a field in a collection', making the purpose specific and unambiguous. It distinguishes this tool from siblings like 'count_documents', 'query', or 'sample_data' by focusing on unique value extraction rather than counting, filtering, or sampling.

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

Usage Guidelines4/5

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

The description provides clear context with a 'Useful for' section listing scenarios like understanding data distribution and data quality checks. However, it does not explicitly state when to use this tool versus alternatives like 'query' for filtered results or 'sample_data' for sampling, nor does it specify exclusions or prerequisites for usage.

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/jonfreeland/mongodb-mcp'

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