Skip to main content
Glama
bpamiri

SQL Server MCP

by bpamiri

delete_row

Remove specific database records by primary key value to maintain data integrity and manage SQL Server table content.

Instructions

Delete a row by primary key.

Args:
    table: Table name (can include schema: 'dbo.Users' or 'Users')
    id: Primary key value of the row to delete

Returns:
    Dictionary with:
    - status: 'deleted' or error
    - table: Full table name
    - id: The deleted row's ID
    - rows_affected: Number of rows deleted (should be 1)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableYes
idYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Handler for the 'delete_row' MCP tool. Deletes a single row from the specified table using its primary key value. Includes input validation via type hints and docstring, decorated with @mcp.tool() for automatic registration.
    @mcp.tool()
    def delete_row(table: str, id: Any) -> dict[str, Any]:
        """Delete a row by primary key.
    
        Args:
            table: Table name (can include schema: 'dbo.Users' or 'Users')
            id: Primary key value of the row to delete
    
        Returns:
            Dictionary with:
            - status: 'deleted' or error
            - table: Full table name
            - id: The deleted row's ID
            - rows_affected: Number of rows deleted (should be 1)
        """
        try:
            manager = get_connection_manager()
            config = manager.config
    
            # Check read-only mode
            if config.read_only:
                return {"error": "Delete operations disabled in read-only mode"}
    
            schema, table_name = parse_table_name(table)
    
            # Get primary key column
            pk_cols = _get_primary_key_columns(schema, table_name)
            if not pk_cols:
                return {"error": f"No primary key found for table {schema}.{table_name}"}
    
            # Build DELETE statement
            query = f"DELETE FROM [{schema}].[{table_name}] WHERE [{pk_cols[0]}] = %s"
    
            affected = manager.execute_non_query(query, (id,))
    
            if affected == 0:
                return {
                    "error": f"No row found with {pk_cols[0]} = {id}",
                    "table": f"{schema}.{table_name}",
                }
    
            return {
                "status": "deleted",
                "table": f"{schema}.{table_name}",
                "id": id,
                "rows_affected": affected,
            }
    
        except QueryError as e:
            logger.error(f"Error deleting row from {table}: {e}")
            return {"error": str(e)}
        except Exception as e:
            logger.error(f"Unexpected error deleting row from {table}: {e}")
            return {"error": str(e)}
  • Helper function used by delete_row (and other CRUD tools) to dynamically retrieve the primary key column(s) for the target table.
    def _get_primary_key_columns(schema: str, table: str) -> list[str]:
        """Get primary key column(s) for a table.
    
        Args:
            schema: Schema name
            table: Table name
    
        Returns:
            List of primary key column names
        """
        manager = get_connection_manager()
    
        query = """
            SELECT c.COLUMN_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
            JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
                ON tc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
                AND tc.TABLE_SCHEMA = c.TABLE_SCHEMA
                AND tc.TABLE_NAME = c.TABLE_NAME
            WHERE tc.TABLE_SCHEMA = %s
                AND tc.TABLE_NAME = %s
                AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
            ORDER BY c.ORDINAL_POSITION
        """
        rows = manager.execute_query(query, (schema, table))
        return [row["COLUMN_NAME"] for row in rows]
  • The @mcp.tool() decorator registers the delete_row function as an MCP tool.
    @mcp.tool()
  • Input schema defined by function parameters (table: str, id: Any) and return type (dict[str, Any]), with detailed documentation in docstring.
    def delete_row(table: str, id: Any) -> dict[str, Any]:
        """Delete a row by primary key.
    
        Args:
            table: Table name (can include schema: 'dbo.Users' or 'Users')
            id: Primary key value of the row to delete
    
        Returns:
            Dictionary with:
            - status: 'deleted' or error
            - table: Full table name
            - id: The deleted row's ID
            - rows_affected: Number of rows deleted (should be 1)
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 of behavioral disclosure. It clearly indicates this is a destructive operation ('Delete') and specifies the return format, including error handling and rows_affected. However, it lacks details on permissions required, transaction behavior, or potential side effects like cascading deletions.

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 with clear sections (Args, Returns) and uses bullet points for readability. It's front-loaded with the core purpose. Minor verbosity in the Returns section could be tightened, but overall it's efficient and earns its place.

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 (destructive operation with 2 params), no annotations, but with an output schema (implied by Returns section), the description is reasonably complete. It covers purpose, parameters, and return values adequately, though it could benefit from more usage context or error examples.

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?

Schema description coverage is 0%, so the description must compensate. It effectively explains both parameters: 'table' (including schema notation examples) and 'id' (as the primary key value). This adds meaningful context beyond the bare schema, though it doesn't detail data types or constraints for 'id'.

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 specific action ('Delete a row by primary key') and identifies the resource ('row'), making the purpose immediately understandable. It distinguishes itself from sibling tools like 'insert_row' and 'update_row' by specifying deletion rather than creation or modification.

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 like 'update_row' for modifications or 'execute_query' for custom deletion logic. It mentions primary key deletion but doesn't clarify prerequisites such as needing an existing connection or database context, which are implied by sibling tools like 'connect' and 'switch_database'.

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/bpamiri/mssql-mcp'

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