Skip to content

ðŸ’ū Database Persistence & Envelope Encryption Pipeline ​

This document details the multi-engine persistence architecture, relational data model, AES-256-GCM authenticated envelope encryption pipeline, and dynamic secret provider resolution in the Model Context Gateway (MCG).


📑 Table of Contents ​

  1. Database & Persistence Architecture
  2. Secret Provider & Envelope Encryption Pipeline

1. Database & Persistence Architecture ​

The gateway abstracts persistent storage behind IDbConnectionFactory and high-performance Dapper repositories. It natively supports SQLite, Microsoft SQL Server, and MySQL / MariaDB.


Unified Entity-Relationship Diagram (Mermaid ERD) ​

For the standalone schema reference and column constraints catalog, see Canonical Data Model & Database ERD and Database Provider Support & Deployment Matrix.


Engine Dialect Strategies (SQLite, MS SQL Server, MySQL) ​

Persistence DimensionðŸŠķ SQLite (Default)ðŸĒ Microsoft SQL Server🐎 MySQL / MariaDB
Provider Keysqlitemssqlmysql
Concurrency ModeWAL (Write-Ahead Logging)Row-Level Locking / Always OnInnoDB MVCC Transactions
Execution ParadigmDirect SQL & Parameterized DapperT-SQL Stored Procedures (sp_*)Stored Procedures (sp_*)
Upsert SyntaxON CONFLICT(Id) DO UPDATEIF EXISTS ... UPDATE ELSE INSERTON DUPLICATE KEY UPDATE
Parameter Prefix@Param@ParamStrict p_Param
Timestamp GenerationCURRENT_TIMESTAMP (ISO-8601)SYSUTCDATETIME() (DATETIME2)CURRENT_TIMESTAMP / NOW()
DDL MigrationsIn-Process Automatic (DatabaseSeederService)Scripted DDL (scripts/db/mssql/)Scripted DDL (scripts/db/mysql/)

2. Secret Provider & Envelope Encryption Pipeline ​

AES-256-GCM Envelope Encryption Specification ​

Sensitive database columns (e.g. AppKeys.EncryptedKey, SecretProviders.EncryptedConfigJson, AuthProviderConfigs.EncryptedConfigJson, Servers.ApiKey) are protected using AES-256-GCM authenticated envelope encryption via SymmetricEncryptionHelper:

  1. Nonce Generation: A cryptographically secure 12-byte random nonce is generated for every encryption operation using RandomNumberGenerator.GetBytes(12).
  2. Authenticated Tagging: AesGcm computes a 16-byte authentication tag over the ciphertext, guaranteeing tamper detection and payload integrity.
  3. Storage Encoding: The concatenated packet [ Nonce (12B) || Tag (16B) || Ciphertext (NB) ] is Base64 encoded before persistence into relational database columns.

Master Key Derivation (PBKDF2) ​

  • Derives a 256-bit symmetric encryption key from the environment variables MCG_SECRET, MCG_MASTER_KEY, or DB_ENCRYPTION_KEY.
  • Uses Rfc2898DeriveBytes.Pbkdf2 configured with 600,000 iterations of SHA-256 and a deployment-specific salt (_McpRouter_Salt_v2).

Pluggable Retrievers & Dynamic Reload Without Restart ​

When an administrator updates a Secret Provider in the Settings UI:

  1. The frontend submits the updated credentials to POST /api/providers/secret.
  2. ProviderConfigSecurityHelper encrypts the payload with AES-256-GCM and persists it to the database.
  3. The in-memory cache in CompositeSecretRetriever is immediately invalidated.
  4. Subsequent downstream requests fetch fresh tokens from HashiCorp Vault or the updated provider without requiring a container or service restart.

Secret Resolution & Encryption Pipeline Flowchart ​

The following flowchart illustrates how secrets are dynamically resolved, decrypted, and injected for downstream MCP requests:


Related Specifications:

Released under the Apache 2.0 License.