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

README
๐Ÿš€ 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_tools table
  • 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
  • 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 curl command
  • ๐Ÿง  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_roles in mcp_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