MCP-DB Konfiguration
Zentrale Konfiguration über Environment Variables, Config-Klasse und Claude Code Hooks.
Übersicht
| Bereich | Datei | Beschreibung |
|---|---|---|
| MCP-DB Server | /opt/mcp-servers/mcp-db/.env | DB-Credentials |
| MCP-DB Config | /opt/mcp-servers/mcp-db/config.py | Allowlists, Limits |
| Claude Code Hooks | /var/www/.claude/settings.local.json | Blocking-Hook |
| Hook-Skript | /var/www/tools/ki-protokoll/claude-hook/block_direct_db.py | DB-Zugriff blockieren |
Claude Code Hook-Konfiguration
Wichtig: Der Blocking-Hook verhindert direkte mysql/mariadb-Befehle!
settings.local.json
Pfad: /var/www/.claude/settings.local.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "/var/www/tools/ki-protokoll/claude-hook/block_direct_db.py",
"timeout": 5
}
]
}
]
},
"permissions": {
"allow": [
"mcp__mcp-db__db_select",
"mcp__mcp-db__db_schema",
"mcp__mcp-db__db_stats",
// ... weitere Permissions
]
}
}
Hook-Konfiguration erklärt
| Feld | Wert | Beschreibung |
|---|---|---|
| matcher | "Bash" | Hook wird nur bei Bash-Tool-Aufrufen getriggert |
| type | "command" | Externes Skript ausführen |
| command | Pfad zum Python-Skript | Blocking-Hook |
| timeout | 5 | Timeout in Sekunden |
Exit-Codes
| Exit Code | Bedeutung | Aktion |
|---|---|---|
| 0 | Erlauben | Befehl wird ausgeführt |
| 2 | Blockieren | Befehl wird abgelehnt, stderr wird angezeigt |
config.py
"""Zentrale Konfiguration"""
import os
from typing import List
class Config:
"""Konfiguration aus Environment Variables"""
# Datenbank für Queries (mcp_readonly)
DB_HOST: str = os.getenv("DB_HOST", "localhost")
DB_USER: str = os.getenv("DB_USER", "mcp_readonly")
DB_PASSWORD: str = os.getenv("DB_PASSWORD", "")
# Datenbank für Logging (mcp_logger)
LOG_DB_HOST: str = os.getenv("LOG_DB_HOST", "localhost")
LOG_DB_NAME: str = os.getenv("LOG_DB_NAME", "ki_protokoll")
LOG_DB_USER: str = os.getenv("LOG_DB_USER", "mcp_logger")
LOG_DB_PASSWORD: str = os.getenv("LOG_DB_PASSWORD", "")
# Database Allowlist
ALLOWED_DATABASES: List[str] = ["ki_protokoll", "ki_system"]
# Dangerous Keyword Blocklist
BLOCKED_KEYWORDS: List[str] = [
"DROP", "DELETE", "INSERT", "UPDATE", "TRUNCATE",
"ALTER", "CREATE", "RENAME", "GRANT", "REVOKE",
"LOAD_FILE", "INTO OUTFILE", "INTO DUMPFILE",
"BENCHMARK", "SLEEP"
]
# Query Limits
MAX_QUERY_LENGTH: int = 2000
MAX_ROWS: int = 100
QUERY_TIMEOUT_SEC: int = 30
# Table Allowlist (29 Tabellen)
ALLOWED_TABLES: List[str] = [
# ki_protokoll
"mcp_log", "protokoll", "file_backup_history",
# ki_system - Content Studio
"content_orders", "content_versions", "content_contracts",
"content_critiques", "content_sources", "content_structures",
"author_profiles", "critics",
# ki_system - RAG/Documents
"documents", "chunks", "document_entities", "document_taxonomy",
# ki_system - Chat
"chat_sessions", "chat_messages",
# ki_system - Pipeline
"pipeline_log", "pipeline_queue", "prompts", "prompt_tests", "llm_requests",
# ki_system - Entities/Taxonomy
"entities", "entity_relations", "entity_synonyms",
"entity_classifications", "entity_corrections",
"taxonomy_terms", "ontology_classes",
# ki_system - Search
"search_history",
]
Environment Variables
| Variable | Beschreibung | Default |
|---|---|---|
| DB_HOST | Datenbank-Host für Queries | localhost |
| DB_USER | User für Queries (nur SELECT) | mcp_readonly |
| DB_PASSWORD | Passwort für Query-User | - |
| LOG_DB_HOST | Datenbank-Host für Logging | localhost |
| LOG_DB_NAME | Datenbank für Logging | ki_protokoll |
| LOG_DB_USER | User für Logging (nur INSERT) | mcp_logger |
| LOG_DB_PASSWORD | Passwort für Logger-User | - |
.env.example
# Datenbank für Queries (mcp_readonly)
DB_HOST=localhost
DB_USER=mcp_readonly
DB_PASSWORD=
# Datenbank für Logging (mcp_logger)
LOG_DB_HOST=localhost
LOG_DB_NAME=ki_protokoll
LOG_DB_USER=mcp_logger
LOG_DB_PASSWORD=
Allowlists
Database Allowlist
Nur diese Datenbanken sind erlaubt:
ki_protokoll- Protokoll und Loggingki_system- KI-System Daten
Table Allowlist (30 Tabellen)
Nur diese Tabellen sind für SELECT erlaubt:
ki_protokoll (3 Tabellen)
mcp_log- MCP Loggingprotokoll- Claude Protokollfile_backup_history- File Backup Verlauf
ki_system - Content Studio (8 Tabellen)
content_orders- Content-Aufträgecontent_versions- Content-Versionencontent_contracts- Content-Contractscontent_critiques- Kritikencontent_sources- Quellencontent_structures- Strukturenauthor_profiles- Autoren-Profilecritics- Kritiker-Definitionen
ki_system - RAG/Documents (4 Tabellen)
documents- Dokumentechunks- Dokument-Chunks für RAGdocument_entities- Dokument-Entitätendocument_taxonomy- Dokument-Taxonomie
ki_system - Chat (2 Tabellen)
chat_sessions- Chat-Sessionschat_messages- Chat-Nachrichten
ki_system - Pipeline (5 Tabellen)
pipeline_log- Pipeline-Logpipeline_queue- Pipeline-Queueprompts- Promptsprompt_tests- Prompt-Testsllm_requests- LLM-Requests
ki_system - Entities/Taxonomy (7 Tabellen)
entities- Entitätenentity_relations- Entitäts-Relationenentity_synonyms- Synonymeentity_classifications- Klassifikationenentity_corrections- Korrekturentaxonomy_terms- Taxonomie-Begriffeontology_classes- Ontologie-Klassen
ki_system - Search (1 Tabelle)
search_history- Suchverlauf
Zusätzlich erlaubt: information_schema.TABLES für Schema-Abfragen.
Keyword Blocklist
Diese Keywords werden in Queries blockiert (mit Word Boundaries):
| Kategorie | Keywords |
|---|---|
| DML | DELETE, INSERT, UPDATE |
| DDL | DROP, TRUNCATE, ALTER, CREATE, RENAME |
| DCL | GRANT, REVOKE |
| File-Operationen | LOAD_FILE, INTO OUTFILE, INTO DUMPFILE |
| DoS-Funktionen | BENCHMARK, SLEEP |
Limits
| Limit | Wert | Beschreibung |
|---|---|---|
| MAX_QUERY_LENGTH | 2000 | Maximale Query-Länge in Zeichen |
| MAX_ROWS | 100 | Maximale Ergebniszeilen |
| QUERY_TIMEOUT_SEC | 30 | Query-Timeout in Sekunden |
Hook-Konfiguration ändern
Hook aktivieren/deaktivieren
# Hook deaktivieren (NICHT EMPFOHLEN!)
# Entferne den PreToolUse Block aus settings.local.json
# Hook aktivieren
# Füge den Block wieder hinzu (siehe oben)
Nach Änderungen
# Claude Code muss neu gestartet werden
# damit Änderungen an settings.local.json wirksam werden
Erweiterung der Allowlists
# In config.py erweitern:
ALLOWED_TABLES: List[str] = [
"mcp_log",
"ki_eintraege",
# Neue Tabellen hinzufügen:
"neue_tabelle",
]
ALLOWED_DATABASES: List[str] = [
"ki_protokoll",
"ki_system",
# Neue Datenbank hinzufügen:
"neue_db",
]
Verwandte Kapitel
- Sicherheit - DB-User Hardening, Blocking-Hook Details
- Installation - .env Setup
- Claude Hooks - Allgemeine Hook-Dokumentation