HTTP Server Setup
This server supports the Streamable HTTP transport from MCP SDK 1.26.0, which is a modern HTTP-based protocol that supports:
- Bi-directional streaming with Server-Sent Events (SSE)
- Session management with automatic reconnection
- Direct HTTP responses for simple requests
- Better performance than traditional SSE-only approaches
Quick Start
# Development mode
npm run dev:http
# Production mode
npm run build
npm run start:http
The server will start on http://localhost:3000 by default.
Configuration
Set these environment variables in your .env file:
# PostgreSQL connection
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password_here
DB_NAME=postgres
DB_SSL=true
# HTTP server (optional)
PORT=3000
ALLOWED_HOSTS=localhost,127.0.0.1
Docker Setup
To run the HTTP server in Docker, use the MODE environment variable:
# Using docker run
docker run -p 3000:3000 \
-e MODE=http \
-e DB_HOST=your_host \
-e DB_PORT=5432 \
-e DB_USER=postgres \
-e DB_PASSWORD=your_password \
-e DB_NAME=postgres \
-e PORT=3000 \
-e ALLOWED_HOSTS=localhost,127.0.0.1 \
postgres-mcp-server
# Using docker-compose
services:
postgres-mcp-http:
image: postgres-mcp-server:latest
environment:
MODE: http
PORT: 3000
ALLOWED_HOSTS: localhost,127.0.0.1
# ... database config
ports:
- "3000:3000"
See Docker Setup for more details.
Endpoints
POST /mcp
Main MCP endpoint for sending requests. Requires mcp-session-id header after initialization.
Initialization Request:
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "test-client",
"version": "1.0.0"
}
},
"id": 1
}'
The response will include an mcp-session-id header. Use this for all subsequent requests.
Tool Call Request:
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "mcp-session-id: YOUR_SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "list_tables",
"arguments": {
"schema": "public"
}
},
"id": 2
}'
GET /mcp
Establishes an SSE stream for receiving server notifications. Requires mcp-session-id header.
curl -N -H "mcp-session-id: YOUR_SESSION_ID" \
http://localhost:3000/mcp
DELETE /mcp
Terminates a session. Requires mcp-session-id header.
curl -X DELETE \
-H "mcp-session-id: YOUR_SESSION_ID" \
http://localhost:3000/mcp
GET /health
Health check endpoint.
curl http://localhost:3000/health
Features
Streamable HTTP Transport
The MCP SDK's StreamableHTTPServerTransport provides:
- Stateful sessions: Each client gets a unique session ID
- SSE streaming: Long-lived connections for server-to-client messages
- HTTP requests: Client-to-server messages via POST
- Automatic reconnection: Clients can reconnect with
Last-Event-ID - Session cleanup: Automatic cleanup on disconnect
Available Tools
All the standard PostgreSQL tools are available:
query- Execute SQL queries with paginationdescribe_table- Get table structurelist_tables- List tables in a schemalist_schemas- List all schemasget_constraints- Get table constraintslist_indexes- List indexesexplain_query- Get query execution planget_table_stats- Get table statisticslist_views- List viewslist_functions- List functions and procedures
Using with MCP Clients
Most MCP clients will handle the Streamable HTTP protocol automatically. Just configure your client with the endpoint URL:
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
const transport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
const client = new Client({
name: 'my-client',
version: '1.0.0',
}, {
capabilities: {}
});
await client.connect(transport);
// Use the client...
const tools = await client.listTools();
Advantages Over SSE-Only
The new Streamable HTTP transport offers several advantages:
- Better for Claude Desktop/Web: Modern MCP protocol support
- Stateful sessions: Proper session management with reconnection
- Bi-directional: Both client and server can initiate messages
- Built-in reconnection: Handles network issues gracefully
- Industry standard: Uses standard HTTP + SSE, works with proxies/load balancers
Troubleshooting
Session ID errors: Make sure you're using the session ID from the initialize response in all subsequent requests.
Connection closed: The server will close SSE streams periodically. Clients should reconnect automatically using the Last-Event-ID header.
Port in use: Change the PORT environment variable if 3000 is already in use.