Skip to main content
Glama

chroma_get_collection_count

Retrieve the total number of documents in a specified Chroma collection using this tool, enabling efficient data management and analysis for AI-driven applications.

Instructions

Get the number of documents in a Chroma collection.

Args:
    collection_name: Name of the collection to count

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collection_nameYes

Implementation Reference

  • The handler function decorated with @mcp.tool(), which registers the tool and defines its input schema via type hints (collection_name: str) and output (int). It fetches the Chroma client, retrieves the collection, and returns the document count.
    @mcp.tool()
    async def chroma_get_collection_count(collection_name: str) -> int:
        """Get the number of documents in a Chroma collection.
        
        Args:
            collection_name: Name of the collection to count
        """
        client = get_chroma_client()
        try:
            collection = client.get_collection(collection_name)
            return collection.count()
        except Exception as e:
            raise Exception(f"Failed to get collection count for '{collection_name}': {str(e)}") from e
  • The @mcp.tool() decorator registers the chroma_get_collection_count function as an MCP tool.
    @mcp.tool()
  • The function signature provides the input schema (collection_name: str) and output type (int) for the tool, used by FastMCP for validation.
    async def chroma_get_collection_count(collection_name: str) -> int:
  • Helper function to initialize and return the Chroma client instance, called within the tool handler.
    def get_chroma_client(args=None):
        """Get or create the global Chroma client instance."""
        global _chroma_client
        if _chroma_client is None:
            if args is None:
                # Create parser and parse args if not provided
                parser = create_parser()
                args = parser.parse_args()
            
            # Load environment variables from .env file if it exists
            load_dotenv(dotenv_path=args.dotenv_path)
            if args.client_type == 'http':
                if not args.host:
                    raise ValueError("Host must be provided via --host flag or CHROMA_HOST environment variable when using HTTP client")
                
                settings = Settings()
                if args.custom_auth_credentials:
                    settings = Settings(
                        chroma_client_auth_provider="chromadb.auth.basic_authn.BasicAuthClientProvider",
                        chroma_client_auth_credentials=args.custom_auth_credentials
                    )
                
                # Handle SSL configuration
                try:
                    _chroma_client = chromadb.HttpClient(
                        host=args.host,
                        port=args.port if args.port else None,
                        ssl=args.ssl,
                        settings=settings
                    )
                except ssl.SSLError as e:
                    print(f"SSL connection failed: {str(e)}")
                    raise
                except Exception as e:
                    print(f"Error connecting to HTTP client: {str(e)}")
                    raise
                
            elif args.client_type == 'cloud':
                if not args.tenant:
                    raise ValueError("Tenant must be provided via --tenant flag or CHROMA_TENANT environment variable when using cloud client")
                if not args.database:
                    raise ValueError("Database must be provided via --database flag or CHROMA_DATABASE environment variable when using cloud client")
                if not args.api_key:
                    raise ValueError("API key must be provided via --api-key flag or CHROMA_API_KEY environment variable when using cloud client")
                
                try:
                    _chroma_client = chromadb.HttpClient(
                        host="api.trychroma.com",
                        ssl=True,  # Always use SSL for cloud
                        tenant=args.tenant,
                        database=args.database,
                        headers={
                            'x-chroma-token': args.api_key
                        }
                    )
                except ssl.SSLError as e:
                    print(f"SSL connection failed: {str(e)}")
                    raise
                except Exception as e:
                    print(f"Error connecting to cloud client: {str(e)}")
                    raise
                    
            elif args.client_type == 'persistent':
                if not args.data_dir:
                    raise ValueError("Data directory must be provided via --data-dir flag when using persistent client")
                _chroma_client = chromadb.PersistentClient(path=args.data_dir)
            else:  # ephemeral
                _chroma_client = chromadb.EphemeralClient()
                
        return _chroma_client
Behavior2/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 of behavioral disclosure. It states the tool is a read operation ('Get'), but doesn't cover error handling (e.g., if collection doesn't exist), performance aspects, or return format. For a tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded: the first sentence states the purpose clearly, followed by a brief parameter explanation. There's no wasted text, but the structure could be slightly improved by integrating parameter details more seamlessly rather than a separate 'Args:' section.

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 low complexity (single parameter, no output schema, no annotations), the description is minimally adequate. It covers the basic purpose and parameter meaning, but lacks details on usage context, behavioral traits, or output expectations. For a simple read tool, this is acceptable but not comprehensive.

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

Parameters4/5

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

The description adds meaningful context for the single parameter: 'collection_name: Name of the collection to count.' Since schema description coverage is 0%, this compensates by explaining what the parameter represents. However, it doesn't provide additional details like format constraints or examples, keeping it from a perfect score.

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 tool's purpose: 'Get the number of documents in a Chroma collection.' It specifies the verb ('Get') and resource ('number of documents'), making it easy to understand. However, it doesn't explicitly differentiate from sibling tools like 'chroma_get_collection_info' or 'chroma_peek_collection', which might also provide collection metadata.

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?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., collection must exist), exclusions, or compare it to siblings like 'chroma_get_collection_info' that might offer similar or overlapping functionality. Usage is implied but not explicitly stated.

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

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/chroma-core/chroma-mcp'

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