Skip to main content
Glama

filter_rows

Filter data rows using flexible conditions with support for null values, text matching, and logical combinations to extract specific subsets from datasets.

Instructions

Filter rows using flexible conditions: comprehensive null value and text matching support.

Provides powerful filtering capabilities optimized for AI-driven data analysis. Supports multiple operators, logical combinations, and comprehensive null value handling.

Examples: # Numeric filtering filter_rows(ctx, [{"column": "age", "operator": ">", "value": 25}])

# Text filtering with null handling
filter_rows(ctx, [
    {"column": "name", "operator": "contains", "value": "Smith"},
    {"column": "email", "operator": "is_not_null"}
], mode="and")

# Multiple conditions with OR logic
filter_rows(ctx, [
    {"column": "status", "operator": "==", "value": "active"},
    {"column": "priority", "operator": "==", "value": "high"}
], mode="or")

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
conditionsYesList of filter conditions with column, operator, and value
modeNoLogic for combining conditions (and/or)and

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
successNoWhether operation completed successfully
rows_afterYesRow count after filtering
rows_beforeYesRow count before filtering
rows_filteredYesNumber of rows removed by filter
conditions_appliedYesNumber of filter conditions applied

Implementation Reference

  • The main filter_rows tool handler function that applies row filtering logic using pandas masks based on multiple conditions with AND/OR modes, supporting numeric, text, list, and null operators.
    def filter_rows(
        ctx: Annotated[Context, Field(description="FastMCP context for session access")],
        conditions: Annotated[
            list[FilterCondition],
            Field(description="List of filter conditions with column, operator, and value"),
        ],
        mode: Annotated[
            Literal["and", "or"],
            Field(description="Logic for combining conditions (and/or)"),
        ] = "and",
    ) -> FilterOperationResult:
        """Filter rows using flexible conditions: comprehensive null value and text matching support.
    
        Provides powerful filtering capabilities optimized for AI-driven data analysis. Supports
        multiple operators, logical combinations, and comprehensive null value handling.
    
        Examples:
            # Numeric filtering
            filter_rows(ctx, [{"column": "age", "operator": ">", "value": 25}])
    
            # Text filtering with null handling
            filter_rows(ctx, [
                {"column": "name", "operator": "contains", "value": "Smith"},
                {"column": "email", "operator": "is_not_null"}
            ], mode="and")
    
            # Multiple conditions with OR logic
            filter_rows(ctx, [
                {"column": "status", "operator": "==", "value": "active"},
                {"column": "priority", "operator": "==", "value": "high"}
            ], mode="or")
    
        """
        session_id = ctx.session_id
        session, df = get_session_data(session_id)
        rows_before = len(df)
    
        # Initialize mask based on mode: AND starts True, OR starts False
        mask = pd.Series([mode == "and"] * len(df))
    
        # Convert dict conditions to FilterCondition objects if needed
        typed_conditions: list[FilterCondition] = []
        for cond in conditions:
            if isinstance(cond, dict):
                # Normalize operator: convert == to = for compatibility
                normalized_cond = dict(cond)
                if "operator" in normalized_cond and normalized_cond["operator"] == "==":
                    normalized_cond["operator"] = "="
                typed_conditions.append(FilterCondition(**normalized_cond))
            else:
                typed_conditions.append(cond)
    
        # Process conditions
        for condition in typed_conditions:
            column = condition.column
            operator = (
                condition.operator.value if hasattr(condition.operator, "value") else condition.operator
            )
            value = condition.value
    
            if column is None or column not in df.columns:
                raise ColumnNotFoundError(column, df.columns.tolist())
    
            col_data = df[column]
    
            if operator in {"=", "=="}:
                condition_mask = col_data == value
            elif operator in {"!=", "not_equals"}:
                condition_mask = col_data != value
            elif operator == ">":
                condition_mask = col_data > value
            elif operator == "<":
                condition_mask = col_data < value
            elif operator == ">=":
                condition_mask = col_data >= value
            elif operator == "<=":
                condition_mask = col_data <= value
            elif operator == "contains":
                condition_mask = col_data.astype(str).str.contains(str(value), na=False)
            elif operator == "not_contains":
                condition_mask = ~col_data.astype(str).str.contains(str(value), na=False)
            elif operator == "starts_with":
                condition_mask = col_data.astype(str).str.startswith(str(value), na=False)
            elif operator == "ends_with":
                condition_mask = col_data.astype(str).str.endswith(str(value), na=False)
            elif operator == "in":
                condition_mask = col_data.isin(value if isinstance(value, list) else [value])
            elif operator == "not_in":
                condition_mask = ~col_data.isin(value if isinstance(value, list) else [value])
            elif operator == "is_null":
                condition_mask = col_data.isna()
            elif operator == "is_not_null":
                condition_mask = col_data.notna()
            else:
                msg = (
                    f"Invalid operator '{operator}'. Valid operators: "
                    "==, !=, >, <, >=, <=, contains, not_contains, starts_with, ends_with, "
                    "in, not_in, is_null, is_not_null"
                )
                raise ToolError(
                    msg,
                )
    
            mask = mask & condition_mask if mode == "and" else mask | condition_mask
    
        # Apply filter
        session.df = df[mask].reset_index(drop=True)
        rows_after = len(session.df)
    
        # No longer needed - conditions are already FilterCondition objects
    
        # No longer recording operations (simplified MCP architecture)
    
        return FilterOperationResult(
            rows_before=rows_before,
            rows_after=rows_after,
            rows_filtered=rows_before - rows_after,
            conditions_applied=len(conditions),
        )
  • Registration of the filter_rows handler as an MCP tool on the transformation_server instance.
    transformation_server.tool(name="filter_rows")(filter_rows)
  • Input schema: Pydantic model for FilterCondition used in filter_rows parameters, defining column, ComparisonOperator enum, and value with special validation for null/list operators.
    class FilterCondition(BaseModel):
        """A single filter condition."""
    
        column: str = Field(..., description="Column name to filter on")
        operator: ComparisonOperator = Field(..., description="Comparison operator")
        value: FilterValue = Field(default=None, description="Value to compare against")
    
        @field_validator("value", mode="before")
        @classmethod
        def validate_value(cls, v: FilterValue, info: Any) -> FilterValue:
            """Validate value based on operator."""
            operator = info.data.get("operator") if hasattr(info, "data") else None
            if operator in [ComparisonOperator.IS_NULL, ComparisonOperator.IS_NOT_NULL]:
                return None
            if operator in [
                ComparisonOperator.IN,
                ComparisonOperator.NOT_IN,
            ] and not isinstance(v, list):
                return [v]
            return v
  • Output schema: Pydantic model FilterOperationResult returned by filter_rows, providing statistics on rows before/after filtering and conditions applied.
    class FilterOperationResult(BaseToolResponse):
        """Response model for row filtering operations."""
    
        rows_before: int = Field(description="Row count before filtering")
        rows_after: int = Field(description="Row count after filtering")
        rows_filtered: int = Field(description="Number of rows removed by filter")
        conditions_applied: int = Field(description="Number of filter conditions applied")
Behavior3/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 discloses behavioral traits like 'flexible conditions,' 'multiple operators,' 'logical combinations,' and 'comprehensive null value handling,' which are useful beyond basic filtering. However, it doesn't mention performance implications, error handling, or what happens with invalid conditions. The examples add practical context but leave gaps in full behavioral understanding.

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, starting with a clear purpose statement, followed by supporting details and practical examples. It's appropriately sized for a complex tool, with each sentence adding value—no redundant information. However, the phrase 'optimized for AI-driven data analysis' is somewhat vague and could be trimmed without loss of clarity.

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

Completeness4/5

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

Given the tool's complexity (flexible filtering with multiple parameters), the description is reasonably complete. It covers key capabilities like operators and logic modes, supported by examples. With an output schema present (as indicated by context signals), the description doesn't need to explain return values. However, it could better address error cases or limitations to be fully comprehensive.

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 input schema has 100% description coverage, thoroughly documenting 'conditions' and 'mode' with enums and examples. The description adds minimal value beyond this, as it doesn't explain parameter semantics like the structure of 'conditions' beyond what's in the schema. The examples illustrate usage but don't provide new semantic insights. With high schema coverage, the baseline score of 3 is appropriate.

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 as 'Filter rows using flexible conditions' with specific mention of 'null value and text matching support.' It distinguishes itself from siblings like 'select_columns' or 'get_row_data' by emphasizing conditional filtering rather than simple selection or retrieval. However, it doesn't explicitly differentiate from tools like 'find_cells_with_value' or 'detect_outliers' that might also involve filtering logic.

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 through examples showing numeric filtering, text filtering with null handling, and logical combinations, suggesting it's for data analysis tasks. However, it lacks explicit guidance on when to use this tool versus alternatives like 'find_cells_with_value' or 'detect_outliers,' and doesn't mention prerequisites such as needing loaded data. The examples provide context but no clear 'when-not' scenarios.

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/jonpspri/databeak'

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