Skip to main content
Glama
biocontext-ai

BioContextAI Knowledgebase MCP

Official

bc_count_drugs_by_field

Count unique values in a specified drug field (e.g., dosage form) across FDA-approved drugs for statistical analysis. Optionally filter by search criteria.

Instructions

Count unique values in a field across FDA-approved drugs. Useful for statistical analysis.

Returns: dict: Results array with term and count for each unique value or error message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fieldYesField to count (e.g., 'sponsor_name', 'products.dosage_form', 'products.route', 'openfda.pharm_class_epc')
search_filterNoOptional search filter to apply before counting
limitNoMaximum number of count results to return

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function decorated with @core_mcp.tool() that executes the bc_count_drugs_by_field tool logic. It calls the FDA Drugs@FDA API with count parameter to get unique value counts for a given field, with support for search filtering and limit control.
    @core_mcp.tool()
    def count_drugs_by_field(
        field: Annotated[
            str,
            Field(
                description="Field to count (e.g., 'sponsor_name', 'products.dosage_form', 'products.route', 'openfda.pharm_class_epc')"
            ),
        ],
        search_filter: Annotated[
            Optional[str], Field(description="Optional search filter to apply before counting")
        ] = None,
        limit: Annotated[int, Field(description="Maximum number of count results to return", ge=1, le=1000)] = 100,
    ) -> dict:
        """Count unique values in a field across FDA-approved drugs. Useful for statistical analysis.
    
        Returns:
            dict: Results array with term and count for each unique value or error message.
        """
        # If field is an array, use .exact for correct counting
        array_fields = [
            "openfda.brand_name",
            "openfda.generic_name",
            "openfda.manufacturer_name",
            "openfda.pharm_class_epc",
            "openfda.pharm_class_moa",
            "openfda.pharm_class_pe",
            "openfda.pharm_class_cs",
            "products.brand_name",
        ]
        count_field = field + ".exact" if field in array_fields and not field.endswith(".exact") else field
        url_params = {"count": count_field, "limit": limit}
    
        # Add search filter if provided
        if search_filter:
            url_params["search"] = search_filter
    
        # Build the complete URL
        base_url = "https://api.fda.gov/drug/drugsfda.json"
    
        try:
            response = requests.get(base_url, params=url_params)  # type: ignore
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            return {"error": f"Failed to fetch FDA drug count data: {e!s}"}
  • Input schema/type definitions for the count_drugs_by_field tool: field (str, required), search_filter (Optional[str], optional), limit (int, optional, 1-1000, default 100).
    field: Annotated[
        str,
        Field(
            description="Field to count (e.g., 'sponsor_name', 'products.dosage_form', 'products.route', 'openfda.pharm_class_epc')"
        ),
    ],
    search_filter: Annotated[
        Optional[str], Field(description="Optional search filter to apply before counting")
    ] = None,
    limit: Annotated[int, Field(description="Maximum number of count results to return", ge=1, le=1000)] = 100,
  • Re-export of count_drugs_by_field from the _count_drugs module in the openfda package.
    from ._count_drugs import count_drugs_by_field, get_drug_statistics
  • The FastMCP server instance (core_mcp) that the tool is registered on via @core_mcp.tool() decorator.
    core_mcp = FastMCP(  # type: ignore
        "BC",
        instructions="Provides access to biomedical knowledge bases.",
    )
  • Helper logic for handling array fields by appending '.exact' suffix to ensure correct counting for array-type fields in the FDA API.
    array_fields = [
Behavior4/5

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

With no annotations provided, the description carries the full burden. It clearly states the output format (dict with term and count) and indicates it operates on FDA-approved drugs. Though it doesn't discuss side effects, the operation is inherently non-destructive.

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 extremely concise with two sentences plus a return note. Every sentence adds value: purpose and utility upfront, then output format. No wasted words.

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

Completeness5/5

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

Given the tool's simplicity, the description covers the essential behavior and output. The presence of an output schema (indicated by context) reduces the need for additional detail. The description is complete for an agent to understand and invoke the tool.

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 description adds little beyond what the schema already provides. The description does not elaborate on parameter behavior or constraints beyond the schema examples.

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 ('Count unique values') and resource ('in a field across FDA-approved drugs'), with a hint of utility ('Useful for statistical analysis'). It does not explicitly distinguish from sibling tools, but the purpose is unambiguous.

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

Usage Guidelines3/5

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

The description implies usage for statistical analysis but lacks explicit guidance on when to use this tool versus alternatives like bc_get_drug_statistics or bc_search_drugs_fda. No exclusions or context are provided.

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/biocontext-ai/knowledgebase-mcp'

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