pg-semantic-mcp
pg-semantic-mcp is a read-only MCP server for AI coding agents that exposes database schema information for PostgreSQL or SQL Server without executing SQL. Core capabilities: list tables with comments, describe columns (names, types, nullability), fetch sample rows, and perform semantic schema search using natural language via any OpenAI-compatible LLM (optionally enhanced by a user-authored semantic layer document). The server requires no vector database and operates entirely read-only.
Can use local LLM models served by Ollama for semantic schema search.
Uses OpenAI-compatible LLM APIs, including GPT models, to power semantic search across the database schema.
Exposes PostgreSQL database schema (tables, columns, comments) and sample data as MCP tools, with semantic search over the schema.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pg-semantic-mcp Show me the schema for the orders table"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
db-semantic-mcp
English | 简体中文
A multi-backend MCP server for AI coding agents — supports both PostgreSQL and SQL Server.
Exposes your database schema — table names, column types, comments, and bounded sample data — as MCP tools. Includes semantic search powered by any OpenAI-compatible LLM, enriched by a user-authored semantic layer document.
No SQL execution. Read-only. No vector database required.
Backends
Backend | Scheme | Driver | Required Extras |
PostgreSQL |
| asyncpg | (built-in) |
SQL Server |
| pymssql |
|
The backend is auto-detected from DATABASE_URL. Everything else works the same.
Related MCP server: dbecho
Features
list_tables — discover all tables with comments
describe_table — inspect column names, types, nullability, and comments
sample_data — fetch example rows from any table
search_schema — semantic keyword search across tables and columns using LLM
Install
# PostgreSQL only
pip install db-semantic-mcp
# With SQL Server support
pip install "db-semantic-mcp[sqlserver]"Requires Python 3.11+.
Quick Start
# PostgreSQL
export DATABASE_URL="postgresql://user:pass@localhost:5432/mydb"
# SQL Server (Kingdee ERP or any MSSQL instance)
export DATABASE_URL="sqlserver://user:pass@host:1433?database=mydb&encrypt=disable"
export LLM_API_KEY="sk-..." # required only for search_schema
pg-semantic-mcpConfiguration
Variable | Required | Default | Description |
| yes | — | PostgreSQL or SQL Server connection string |
| no | — | Path to your semantic layer markdown |
| no |
| OpenAI-compatible endpoint |
| no | — | Required for |
| no |
| LLM model name |
| no |
| Background cache refresh interval |
| no | all | Comma-separated schema names to cache |
| no | — | Comma-separated table name prefixes to cache |
| no |
| Default row count for |
| no |
| Hard maximum rows returned by |
| no |
| Approximate hard maximum serialized response bytes for |
| no | all | Comma-separated case-insensitive glob patterns for columns that may be returned |
| no | — | Comma-separated case-insensitive glob patterns for columns that must be omitted |
| no | built-in sensitive patterns | Comma-separated case-insensitive glob patterns for columns whose values are replaced with |
You can also use a .env file in the working directory.
sample_data Security Boundary
sample_data is read-only, but it is not metadata-only: it can expose actual
business data from the connected database. Treat it as a small data-plane tool.
The tool applies these response controls before returning rows to the MCP client:
limitmust be greater than 0 and is hard-capped bySAMPLE_DATA_MAX_ROWS.The response is reduced until its serialized size is within
SAMPLE_DATA_MAX_BYTES.SAMPLE_DATA_ALLOW_COLUMNSlimits returned columns when set.SAMPLE_DATA_DENY_COLUMNSomits matching columns and takes precedence over allow rules.SAMPLE_DATA_REDACT_COLUMNSmasks matching values with[REDACTED].
Column policies use case-insensitive glob patterns. For example:
SAMPLE_DATA_ALLOW_COLUMNS="id,name,email,created_at"
SAMPLE_DATA_DENY_COLUMNS="*password*,*token*"
SAMPLE_DATA_REDACT_COLUMNS="*email*,*phone*,*secret*"The response shape is intentionally short to save model context:
{
"rows": [
{"id": 1, "email": "[REDACTED]"}
],
"_meta": {
"table": "public.customers",
"returned": 1,
"truncated": false
}
}Register with OpenCode
Add to your opencode.jsonc:
{
"mcp": {
"pg-data": {
"type": "local",
"command": "pg-semantic-mcp",
"environment": {
"DATABASE_URL": "postgresql://user:pass@host:5432/dbname",
"SEMANTIC_FILE": "/path/to/SCHEMA.md",
"LLM_API_KEY": "sk-..."
}
}
}
}Same config format works for Claude Code, Cursor, and any MCP-compatible agent.
Semantic Layer
Create a SCHEMA.md file describing your database — naming conventions,
business term mappings, design decisions. See
SCHEMA.md.example for a template.
This document is loaded at startup and included in the search_schema LLM
prompt. It is the main way to teach the agent about your specific domain.
Compatible LLMs
search_schema calls any OpenAI-compatible endpoint:
OpenAI (
gpt-4o-mini,gpt-4o, …)DeepSeek (
deepseek-v4, setLLM_BASE_URL=https://api.deepseek.com/v1)Anthropic via proxy
Local models via Ollama or LM Studio
License
MIT
Available Tools
4 toolsdescribe_tableB
Return column metadata for a table.
Inputs: table (required). Returns: result
list_tablesA
List all tables in the database with their comments.
Inputs: schema. Returns: result
sample_dataB
Return sample rows from a table.
Inputs: table (required), limit. Returns: object
search_schemaA
Semantically search tables and columns by keyword using LLM.
Combines the in-memory schema cache and the semantic layer markdown (if configured) into a prompt, then calls the configured LLM to find relevant tables and columns.
Inputs: keyword (required), type. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: list_tables for enumeration, describe_table for schema details, sample_data for row previews, and search_schema for semantic discovery. There is no ambiguity or overlap between them.
All tool names follow a consistent verb_noun pattern using snake_case (list_tables, describe_table, sample_data, search_schema). The naming is predictable and follows standard database exploration terminology.
With 4 tools, the server is well-scoped for its purpose of schema exploration and semantic search. Each tool adds unique value without unnecessary bloat, fitting comfortably in the ideal 3-15 tool range.
The core schema exploration lifecycle (list, describe, sample, search) is covered completely. Minor gaps exist such as no direct schema listing or database-level information, but these are not essential for the server's apparent purpose.
Maintenance
Related MCP Connectors
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Text-to-SQL MCP server: read-only queries on PostgreSQL, MySQL and SQL Server with your real schema
Hosted PostgreSQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA read-only PostgreSQL MCP server that enables AI agents to perform schema introspection and execute SELECT-only queries. It supports secure database connections through SSL and SSH tunnels while offering a structure-only mode to restrict query access.9 npmMIT
- AlicenseAqualityCmaintenanceAn MCP server that gives AI agents direct read-only access to PostgreSQL databases, enabling natural language analytics through tools for schema exploration, querying, trend analysis, and data quality checks.115MIT
- FlicenseNot gradedqualityDmaintenanceA read-only MCP server that enables AI agents to explore database schemas and execute safe queries on PostgreSQL and MySQL.-
- AlicenseNot gradedqualityDmaintenanceA zero-config, read-only PostgreSQL MCP server that enforces read-only access at the database level using READ ONLY transactions, allowing AI agents to safely explore schemas and run SELECT queries without risk of mutation.11 npmMIT