Skip to main content
Glama

search_substance

Search the OpenFoodTox database for chemical substances by name, E-number, or description to retrieve toxicity data, safety assessments, and genotoxicity studies.

Instructions

MCP tool to search the OpenFoodTox database for substances by name, E-number, or description.

Searches the database using a two-step approach:
1. First searches SYNONYM table (E-numbers, common names, trade names, alternative names)
2. If no results, searches COMPONENT table (SUB_NAME and COM_NAME fields)

The search is case-insensitive and supports partial matches. E-numbers are automatically
normalized (e.g., "E 951" or "E-951" becomes "E951").

Returns a list of unique substances (one dictionary per SUB_COM_ID) with all study data
aggregated into arrays. Each substance may have multiple studies, opinions, and assessments,
which are grouped together by SUB_COM_ID.

Args:
    description_search: Search term (substance or component name e.g. "aspartame", OR E-number e.g. "E 951") or any of the following CAS name, Council of Europe number, E number, E.C enzyme number, EC name, EU Flavour Information System number, EUgroup-no, Flavour and Extract Manufacturers Association number, Joint FAO/WHO Expert Committee on Food Additives number, Name, OECD Toolbox Classification, Pharmalogical class, Swiss Prot no., Trade name

Returns:
    List of dictionaries, where each dictionary represents a unique substance with:
    - Basic component information (name, type, formula, description)
    - Aggregated study identifiers (arrays of IDs linking to related tables)
    - Study classifications and remarks (arrays of unique values from all studies)

    Returns None if no matches are found.

<dictionary_descriptions>
<name>SUB_COM_ID</name>
<description>Unique identifier for the substance-component link. Primary key for grouping results. Multiple studies, opinions, and assessments may reference the same SUB_COM_ID.</description>
<name>COM_NAME</name>
<description>Component name as derived in the opinions. If more than one name is reported in the opinion (excluding IUPAC name), then the most common/most specific name is reported as component name.</description>
<name>COM_TYPE</name>
<description>High level classification of component type (e.g., single, mixture, botanical, synthetic).</description>
<name>MOLECULARFORMULA</name>
<description>Molecular formula of the chemical component.</description>
<name>SUB_DESCRIPTION</name>
<description>Summary of the substance description as derived from opinions. This includes also the group description.</description>
<name>SUB_OP_CLASS</name>
<description>Array of unique values indicating the class of the substance and the corresponding opinion as provided by EFSA (e.g., "food additive", "pesticide", "flavoring"). Aggregated from all studies for this substance.</description>
<name>REMARKS</name>
<description>Array of unique remarks from all studies. Indicates the objective of the opinion and reports any general remarks as retrieved from the opinion. Aggregated from REMARKS_STUDY field.</description>
<name>GENOTOX_ID</name>
<description>Array of unique identifiers linking to the GENOTOX table. Each ID represents a genotoxicity study associated with this substance. May be None if no genotoxicity studies exist.</description>
<name>TOX_ID</name>
<description>Array of unique identifiers linking to the ENDPOINT_STUDY table. Each ID represents a toxicity endpoint study (e.g., NOAEL, LD50). May be None if no endpoint studies exist.</description>
<name>HAZARD_ID</name>
<description>Array of unique identifiers linking to the CHEM_ASSESS table. Each ID represents a chemical risk assessment (e.g., ADI, TDI values). May be None if no assessments exist.</description>
<name>OP_ID</name>
<description>Array of unique identifiers linking to the OPINION table. Each ID represents an EFSA published opinion/document associated with this substance. May be None if no opinions exist.</description>
</dictionary_descriptions>

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
description_searchYes

Implementation Reference

  • The main handler function for the 'search_substance' MCP tool. It takes a search term, normalizes E-numbers implicitly via the query, calls the database query helper, and returns the list of matching substances with aggregated study data. The docstring provides detailed input/output schema descriptions.
    def search_substance(description_search):
        """
        MCP tool to search the OpenFoodTox database for substances by name, E-number, or description.
    
        Searches the database using a two-step approach:
        1. First searches SYNONYM table (E-numbers, common names, trade names, alternative names)
        2. If no results, searches COMPONENT table (SUB_NAME and COM_NAME fields)
    
        The search is case-insensitive and supports partial matches. E-numbers are automatically
        normalized (e.g., "E 951" or "E-951" becomes "E951").
    
        Returns a list of unique substances (one dictionary per SUB_COM_ID) with all study data
        aggregated into arrays. Each substance may have multiple studies, opinions, and assessments,
        which are grouped together by SUB_COM_ID.
    
        Args:
            description_search: Search term (substance or component name e.g. "aspartame", OR E-number e.g. "E 951") or any of the following CAS name, Council of Europe number, E number, E.C enzyme number, EC name, EU Flavour Information System number, EUgroup-no, Flavour and Extract Manufacturers Association number, Joint FAO/WHO Expert Committee on Food Additives number, Name, OECD Toolbox Classification, Pharmalogical class, Swiss Prot no., Trade name
    
        Returns:
            List of dictionaries, where each dictionary represents a unique substance with:
            - Basic component information (name, type, formula, description)
            - Aggregated study identifiers (arrays of IDs linking to related tables)
            - Study classifications and remarks (arrays of unique values from all studies)
    
            Returns None if no matches are found.
    
        <dictionary_descriptions>
        <name>SUB_COM_ID</name>
        <description>Unique identifier for the substance-component link. Primary key for grouping results. Multiple studies, opinions, and assessments may reference the same SUB_COM_ID.</description>
        <name>COM_NAME</name>
        <description>Component name as derived in the opinions. If more than one name is reported in the opinion (excluding IUPAC name), then the most common/most specific name is reported as component name.</description>
        <name>COM_TYPE</name>
        <description>High level classification of component type (e.g., single, mixture, botanical, synthetic).</description>
        <name>MOLECULARFORMULA</name>
        <description>Molecular formula of the chemical component.</description>
        <name>SUB_DESCRIPTION</name>
        <description>Summary of the substance description as derived from opinions. This includes also the group description.</description>
        <name>SUB_OP_CLASS</name>
        <description>Array of unique values indicating the class of the substance and the corresponding opinion as provided by EFSA (e.g., "food additive", "pesticide", "flavoring"). Aggregated from all studies for this substance.</description>
        <name>REMARKS</name>
        <description>Array of unique remarks from all studies. Indicates the objective of the opinion and reports any general remarks as retrieved from the opinion. Aggregated from REMARKS_STUDY field.</description>
        <name>GENOTOX_ID</name>
        <description>Array of unique identifiers linking to the GENOTOX table. Each ID represents a genotoxicity study associated with this substance. May be None if no genotoxicity studies exist.</description>
        <name>TOX_ID</name>
        <description>Array of unique identifiers linking to the ENDPOINT_STUDY table. Each ID represents a toxicity endpoint study (e.g., NOAEL, LD50). May be None if no endpoint studies exist.</description>
        <name>HAZARD_ID</name>
        <description>Array of unique identifiers linking to the CHEM_ASSESS table. Each ID represents a chemical risk assessment (e.g., ADI, TDI values). May be None if no assessments exist.</description>
        <name>OP_ID</name>
        <description>Array of unique identifiers linking to the OPINION table. Each ID represents an EFSA published opinion/document associated with this substance. May be None if no opinions exist.</description>
        </dictionary_descriptions>
        """
        results = query_search_substance(description_search)
        return results
  • main.py:18-18 (registration)
    Registers the search_substance tool with the FastMCP server instance.
    mcp.add_tool(search_substance)
  • Supporting database query function that implements the core search logic: searches synonyms then components by SUB_COM_ID, fetches related studies, and aggregates data into structured dictionaries with arrays of IDs and classifications.
    def query_search_substance(description_search) -> Optional[list[dict]]:
        """
        Atomic query function.
        Database-agnostic search function.
    
        Returns unique substance/es (by SUB_COM_ID) with all study data aggregated into arrays.
        """
        normalized_search = normalize_e_number(description_search)
    
        with get_connection() as db_connection:
            # Step 1: Find SUB_COM_IDs (database-agnostic via pandas)
            synonyms = pd.read_sql_query(
                "SELECT DISTINCT SUB_COM_ID FROM synonym WHERE DESCRIPTION LIKE ?",
                db_connection,
                params=[f"%{normalized_search}%"],
            )
    
            sub_com_ids = synonyms["SUB_COM_ID"].unique().tolist()
    
            if not sub_com_ids:
                # Try component search...
                components = pd.read_sql_query(
                    "SELECT DISTINCT SUB_COM_ID FROM component WHERE SUB_NAME LIKE ? OR COM_NAME LIKE ?",
                    db_connection,
                    params=[f"%{normalized_search}%", f"%{normalized_search}%"],
                )
                if components.empty:
                    return None
                sub_com_ids = components["SUB_COM_ID"].unique().tolist()
    
            # Step 2: Get unique component info (one row per SUB_COM_ID)
            placeholders = ",".join("?" * len(sub_com_ids))
            component_query = f"""
                SELECT DISTINCT
                    SUB_COM_ID,
                    COM_NAME,
                    COM_TYPE,
                    MOLECULARFORMULA,
                    SUB_DESCRIPTION
                FROM component
                WHERE SUB_COM_ID IN ({placeholders})
            """
            components_df = pd.read_sql_query(component_query, db_connection, params=sub_com_ids)
    
            # Step 3: Get all studies for these SUB_COM_IDs
            study_query = f"""
                SELECT 
                    SUB_COM_ID,
                    SUB_OP_CLASS,
                    REMARKS_STUDY,
                    GENOTOX_ID,
                    TOX_ID,
                    HAZARD_ID,
                    OP_ID
                FROM study
                WHERE SUB_COM_ID IN ({placeholders})
            """
            studies_df = pd.read_sql_query(study_query, db_connection, params=sub_com_ids)
    
            # Step 4: Group studies by SUB_COM_ID and aggregate into arrays
            result = []
    
            for _, component_row in components_df.iterrows():
                sub_com_id = component_row["SUB_COM_ID"]
    
                # Get all studies for this component
                component_studies = studies_df[studies_df["SUB_COM_ID"] == sub_com_id]
    
                # Helper function to convert to array, filtering out None/NaN
                def to_array_or_none(series):
                    """Convert pandas series to list of non-null values, or None if empty."""
                    values = series.dropna().unique().tolist()
                    # Convert numpy types to native Python types
                    cleaned = []
                    for val in values:
                        if pd.notna(val):
                            if hasattr(val, "item"):
                                cleaned.append(val.item())
                            else:
                                cleaned.append(val)
                    return cleaned if cleaned else None
    
                # Helper function for string arrays (like SUB_OP_CLASS, REMARKS)
                def to_string_array_or_none(series):
                    """Convert pandas series to list of non-null strings, or None if empty."""
                    values = series.dropna().unique().tolist()
                    cleaned = [str(v) for v in values if pd.notna(v) and str(v).strip()]
                    return cleaned if cleaned else None
    
                # Helper to safely get value or None
                def safe_get(val):
                    """Get value if not null, else None."""
                    try:
                        if pd.isna(val):
                            return None
                        if hasattr(val, "item"):
                            return val.item()
                        return val
                    except (TypeError, ValueError):
                        return None
    
                # Build result entry
                entry = {
                    "SUB_COM_ID": int(sub_com_id),
                    "COM_NAME": safe_get(component_row["COM_NAME"]),
                    "COM_TYPE": safe_get(component_row["COM_TYPE"]),
                    "MOLECULARFORMULA": safe_get(component_row["MOLECULARFORMULA"]),
                    "SUB_DESCRIPTION": safe_get(component_row["SUB_DESCRIPTION"]),
                }
    
                if not component_studies.empty:
                    # Aggregate study fields into arrays
                    entry["SUB_OP_CLASS"] = to_string_array_or_none(component_studies["SUB_OP_CLASS"])
                    entry["REMARKS"] = to_string_array_or_none(component_studies["REMARKS_STUDY"])
                    entry["GENOTOX_ID"] = to_array_or_none(component_studies["GENOTOX_ID"])
                    entry["TOX_ID"] = to_array_or_none(component_studies["TOX_ID"])
                    entry["HAZARD_ID"] = to_array_or_none(component_studies["HAZARD_ID"])
                    entry["OP_ID"] = to_array_or_none(component_studies["OP_ID"])
                else:
                    # No studies found for this component
                    entry["SUB_OP_CLASS"] = None
                    entry["REMARKS"] = None
                    entry["GENOTOX_ID"] = None
                    entry["TOX_ID"] = None
                    entry["HAZARD_ID"] = None
                    entry["OP_ID"] = None
    
                result.append(entry)
    
            return result
  • Structured output schema description in the tool's docstring, detailing each field in the returned dictionaries.
    <dictionary_descriptions>
    <name>SUB_COM_ID</name>
    <description>Unique identifier for the substance-component link. Primary key for grouping results. Multiple studies, opinions, and assessments may reference the same SUB_COM_ID.</description>
    <name>COM_NAME</name>
    <description>Component name as derived in the opinions. If more than one name is reported in the opinion (excluding IUPAC name), then the most common/most specific name is reported as component name.</description>
    <name>COM_TYPE</name>
    <description>High level classification of component type (e.g., single, mixture, botanical, synthetic).</description>
    <name>MOLECULARFORMULA</name>
    <description>Molecular formula of the chemical component.</description>
    <name>SUB_DESCRIPTION</name>
    <description>Summary of the substance description as derived from opinions. This includes also the group description.</description>
    <name>SUB_OP_CLASS</name>
    <description>Array of unique values indicating the class of the substance and the corresponding opinion as provided by EFSA (e.g., "food additive", "pesticide", "flavoring"). Aggregated from all studies for this substance.</description>
    <name>REMARKS</name>
    <description>Array of unique remarks from all studies. Indicates the objective of the opinion and reports any general remarks as retrieved from the opinion. Aggregated from REMARKS_STUDY field.</description>
    <name>GENOTOX_ID</name>
    <description>Array of unique identifiers linking to the GENOTOX table. Each ID represents a genotoxicity study associated with this substance. May be None if no genotoxicity studies exist.</description>
    <name>TOX_ID</name>
    <description>Array of unique identifiers linking to the ENDPOINT_STUDY table. Each ID represents a toxicity endpoint study (e.g., NOAEL, LD50). May be None if no endpoint studies exist.</description>
    <name>HAZARD_ID</name>
    <description>Array of unique identifiers linking to the CHEM_ASSESS table. Each ID represents a chemical risk assessment (e.g., ADI, TDI values). May be None if no assessments exist.</description>
    <name>OP_ID</name>
    <description>Array of unique identifiers linking to the OPINION table. Each ID represents an EFSA published opinion/document associated with this substance. May be None if no opinions exist.</description>
    </dictionary_descriptions>
Behavior5/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It thoroughly explains the search behavior: case-insensitive, partial matches, E-number normalization, and the two-step approach (SYNONYM then COMPONENT tables). It also details the return structure, including what happens when no matches are found (returns None) and how data is aggregated (by SUB_COM_ID). This provides comprehensive behavioral context beyond basic functionality.

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 well-structured and appropriately sized, with clear sections for purpose, search behavior, parameters, returns, and dictionary descriptions. It is front-loaded with the core functionality. However, the detailed dictionary descriptions, while useful, are lengthy and could be condensed or moved to an output schema if available, slightly affecting conciseness.

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 complexity of the tool (search with data aggregation), no annotations, and no output schema, the description is highly complete. It covers purpose, search behavior, parameter details, return structure, and comprehensive field descriptions for the output. This provides all necessary context for an AI agent to understand and use the tool effectively, compensating for the lack of structured data.

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

Parameters5/5

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

The schema description coverage is 0%, so the description must compensate. It provides extensive parameter semantics: 'description_search' is explained as a search term for substance/component names or E-numbers, with examples ('aspartame', 'E 951') and a detailed list of what can be searched (e.g., CAS name, trade name). This adds significant meaning beyond the minimal schema, fully documenting the parameter's purpose and usage.

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 tool's purpose: 'search the OpenFoodTox database for substances by name, E-number, or description.' It specifies the exact resource (OpenFoodTox database) and the search criteria (name, E-number, description), distinguishing it from sibling tools that retrieve specific data types like genotoxicity details or risk assessments.

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 for when to use this tool: for searching substances in the OpenFoodTox database. It explains the two-step search approach and what types of terms can be used (e.g., 'aspartame', 'E 951'). However, it does not explicitly state when to use this tool versus alternatives like 'list_substances_by_class_and_safety' or 'list_substances_by_assessment', which are sibling tools that might retrieve substances based on different criteria.

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/spyrosze/mcp-openfoodtox'

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