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

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)

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