sample_data
Retrieve sample rows from PostgreSQL tables to understand data structure without writing queries. Specify table name, schema, and row limit for quick data inspection.
Instructions
Get sample rows from a table.
Useful for understanding what data looks like without
writing a full query.
Args:
table_name: Name of the table
schema_name: Schema name (default: public)
limit: Number of rows to return (default: 10, max: 100)
Returns:
JSON array of sample rows
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes | ||
| schema_name | No | public | |
| limit | No |
Implementation Reference
- mcp_postgres/server.py:174-202 (handler)The handler function for the 'sample_data' MCP tool. It is decorated with @mcp.tool() for registration and implements fetching up to 100 sample rows from a PostgreSQL table, serializing them to JSON.@mcp.tool() async def sample_data(table_name: str, schema_name: str = "public", limit: int = 10) -> str: """Get sample rows from a table. Useful for understanding what data looks like without writing a full query. Args: table_name: Name of the table schema_name: Schema name (default: public) limit: Number of rows to return (default: 10, max: 100) Returns: JSON array of sample rows """ limit = min(limit, 100) # Cap at 100 rows # Use identifier quoting to prevent SQL injection sql = f'SELECT * FROM "{schema_name}"."{table_name}" LIMIT {limit}' try: async with get_connection() as conn: rows = await conn.fetch(sql) results = [] for row in rows: results.append({k: _serialize_value(v) for k, v in dict(row).items()}) return json.dumps(results, indent=2, default=str) except Exception as e: return json.dumps({"error": str(e)})