A Model Context Protocol (MCP) server that enables persistent memory capabilities for Claude through a knowledge graph stored in PostgreSQL. This server allows AI assistants to create, manage, and query a structured knowledge base of entities, observations, and relationships.
- PostgreSQL Backend: Robust, scalable database storage for knowledge graphs
- Entity Management: Create and manage entities with types and observations
- Relationship Mapping: Define and query relationships between entities
- Full-text Search: Advanced search capabilities across entities and observations
- Multiple Transports: Support for both stdio and HTTP transports
- Auto-migration: Automatic database schema setup and migration
- Docker Support: Containerized deployment with multi-stage builds
The server implements a knowledge graph structure with three main components:
- Entities: Named objects with types and associated observations
- Observations: Timestamped content associated with entities
- Relations: Typed connections between entities
- Node.js 22 or higher
- PostgreSQL database
- npm or compatible package manager
# Clone the repository
git clone <repository-url>
cd mcp-memory-postgres
# Install dependencies
npm install
# Set up environment variables
export DATABASE_URL="postgresql://postgres:password@localhost:5432/memory_graph"
# Build the project
npm run build
# Run the server
npm start
# Build the Docker image
docker build -t mcp-memory-postgres .
# Run with environment variables
docker run -e DATABASE_URL="postgresql://postgres:password@host:5432/memory_graph" \
-p 3001:3001 \
mcp-memory-postgres
DATABASE_URL
: PostgreSQL connection string (default:postgresql://postgres:password@localhost:5432/memory_graph
)NODE_ENV
: Environment mode (development/production)
The server automatically creates the required database schema on first run, including:
entities
table for storing named entitiesobservations
table for timestamped contentrelations
table for entity relationships- Indexes for performance optimization
- Full-text search capabilities
node dist/index.js [options]
Options:
--transport <type> Transport type (stdio, http) [default: stdio]
--host <host> Host to bind to (http mode) [default: 0.0.0.0]
--port <port> Port to bind to (http mode) [default: 3001]
Default mode for integration with MCP clients:
node dist/index.js --transport stdio
REST API mode with CORS support:
node dist/index.js --transport http --port 3001
The server provides the following MCP tools:
Create multiple new entities in the knowledge graph.
Parameters:
{
"entities": [
{
"name": "string",
"entityType": "string",
"observations": ["string"]
}
]
}
Delete entities and their associated relations.
Parameters:
{
"entityNames": ["string"]
}
Add new observations to existing entities.
Parameters:
{
"observations": [
{
"entityName": "string",
"contents": ["string"]
}
]
}
Delete specific observations from entities.
Parameters:
{
"deletions": [
{
"entityName": "string",
"observations": ["string"]
}
]
}
Create relationships between entities (use active voice).
Parameters:
{
"relations": [
{
"from": "string",
"to": "string",
"relationType": "string"
}
]
}
Delete specific relationships.
Parameters:
{
"relations": [
{
"from": "string",
"to": "string",
"relationType": "string"
}
]
}
Retrieve the entire knowledge graph.
Search for nodes based on a query string.
Parameters:
{
"query": "string"
}
Retrieve specific nodes by name.
Parameters:
{
"names": ["string"]
}
id
(SERIAL PRIMARY KEY)name
(TEXT UNIQUE NOT NULL)entity_type
(TEXT NOT NULL)created_at
(TIMESTAMP)updated_at
(TIMESTAMP)
id
(SERIAL PRIMARY KEY)entity_id
(INTEGER REFERENCES entities)content
(TEXT NOT NULL)created_at
(TIMESTAMP)
id
(SERIAL PRIMARY KEY)from_entity_id
(INTEGER REFERENCES entities)to_entity_id
(INTEGER REFERENCES entities)relation_type
(TEXT NOT NULL)created_at
(TIMESTAMP)- UNIQUE constraint on (from_entity_id, to_entity_id, relation_type)
# Development build with watch mode
npm run dev
npm run watch
# Production build
npm run build
# Prepare for distribution
npm run prepare
# Run database migrations
npm run migrate
# Migrate from JSON format
npm run migrate-from-json
version: '3.8'
services:
postgres:
image: postgres:15
environment:
POSTGRES_DB: memory_graph
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
mcp-memory:
build: .
environment:
DATABASE_URL: postgresql://postgres:password@postgres:5432/memory_graph
NODE_ENV: production
ports:
- "3001:3001"
depends_on:
- postgres
command: ["--transport", "http"]
volumes:
postgres_data:
The HTTP transport mode provides a health endpoint:
curl http://localhost:3001/health
The server includes comprehensive error handling:
- Database connection retry logic with exponential backoff
- Graceful shutdown on SIGINT/SIGTERM
- Transaction rollback on errors
- Detailed error logging
- Connection pooling for PostgreSQL
- Indexed searches for fast queries
- Full-text search using PostgreSQL's built-in capabilities
- Efficient batch operations for bulk data operations
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see package.json for details
For issues and questions, please refer to the project's issue tracker.
Version: 0.6.3
Author: Anthropic, PBC
Homepage: https://modelcontextprotocol.io