pgmcp
PostgreSQL extension to define AI tools compatible with Anthropic's Model Context Protocol (MCP). Fully customizable, schema-agnostic, secure, and integrated with PostgREST.
GitHub Stars
0
User Rating
Not Rated
Favorites
0
Views
68
Forks
0
Issues
0
๐ MCP PostgREST Extension
The mcp_postgrest PostgreSQL extension transforms your database into an AI-powered tool interface compliant with Anthropicโs Model Context Protocol (MCP).
โ Secure, extensible, PostgREST-compatible, and now supports AI-assisted function generation using OpenAI or Anthropic models.
๐ง Features
๐ง MCP-Compatible Tool Interface
- Define tools directly via the
mcp_toolstable - Each tool maps to a PostgreSQL function, with JSON Schema for inputs/outputs
- Callable with:
SELECT call_tool('tool_name', '{"arg1": "value"}'::jsonb);
โ๏ธ Automatic CRUD Tool Generation
- On
CREATE TABLE, generates:- A
tool_create_<table>function - A corresponding entry in
mcp_tools
- A
- Globally toggleable using:
SET mcp_postgrest.crud_autogen_enabled = 'on'; -- or 'off'
๐ค AI-Based Tool Generation (NEW)
Use OpenAI or Anthropic to generate PostgreSQL tool functions using your existing schema!
SELECT generate_ai_tool(
provider := 'openai', -- or 'anthropic'
api_key := '<your-api-key>',
tool_name := 'summarize_customers',
description := 'Summarize customer behavior for marketing',
table_names := ARRAY['customers', 'orders']
);
This returns:
- โ
A ready-to-run
curlcommand - ๐ง Prompt includes inferred schema details
- ๐ฏ Output (when pasted into shell and run) will generate SQL you can paste back into the DB
๐ก๏ธ Security & Access Control
- PostgreSQL RLS-compatible
- Tools can be role-restricted via
allowed_rolesinmcp_tools - Tool behavior is customizable per-function
๐ฆ Installation
1. Copy Extension Files
cp mcp_postgrest.control /usr/share/postgresql/extension/
cp sql/mcp_postgrest--0.1.1.sql /usr/share/postgresql/extension/
Check your location with
pg_config --sharedir
2. Create the Extension
CREATE EXTENSION mcp_postgrest;
๐ Configuration
Global Auto-CRUD Toggle
SET mcp_postgrest.crud_autogen_enabled = 'on'; -- or 'off'
In postgresql.conf for permanent config:
mcp_postgrest.crud_autogen_enabled = 'on'
๐ Tables & Functions
mcp_tools
| Column | Description |
|---|---|
name |
Unique tool name |
description |
Tool purpose |
function_name |
Underlying PostgreSQL function |
input_schema |
JSON Schema for input validation |
output_schema |
JSON Schema for output validation |
allowed_roles |
Allowed PostgreSQL roles |
is_enabled |
Boolean toggle for tool availability |
config |
Optional JSONB config |
call_tool(tool_name, args JSONB) โ JSONB
Dispatches a request to the corresponding function with proper access control and input.
generate_ai_tool(provider, api_key, tool_name, description, table_names[])
Returns a curl command using the selected LLM provider (openai or anthropic) with schema-aware context to generate SQL functions.
๐งช Example
CREATE TABLE products(name TEXT, price INT);
-- Generates:
-- - tool_create_products(JSONB)
-- - mcp_tools entry: create_products
SELECT call_tool('create_products', '{"name": "Shoe", "price": 50}');
๐ PostgREST Integration
PostgREST automatically exposes all call_tool RPCs:
POST /rpc/call_tool
Content-Type: application/json
{
"tool_name": "get_weather",
"args": { "location": "New York" }
}
๐ชช License
MIT โ Open Source. Build responsibly.
๐ฌ Coming Soon
- โ
Streamed response support for
HTTPStreamable - ๐ง Tool chaining + tool logs
- ๐ AI loop completion from within the DB
Half Developer, Half Curioso. Using technology to Understand the world around me. Berkeley CS 2024
12
Followers
89
Repositories
1
Gists
0
Total Contributions