Semantic D1 MCP
Enables AI-assisted analysis and optimization of Cloudflare D1 databases through comprehensive schema introspection, relationship mapping, validation, and optimization recommendations. Demonstrates semantic intent patterns with hexagonal architecture for maintainable database development workflows.
README
Semantic D1 MCP
Reference implementation of Semantic Intent as Single Source of Truth patterns
A Model Context Protocol (MCP) server for Cloudflare D1 database introspection, demonstrating semantic anchoring, observable properties, and domain-driven design for AI-assisted database development.
📚 Table of Contents
🎯 What Makes This Different
This isn't just another database introspection tool—it's a reference implementation of proven semantic intent patterns:
- ✅ Semantic Anchoring: Schema analysis based on meaning (table purpose, relationships), not technical metrics (row counts, sizes)
- ✅ Observable Properties: Decisions anchored to directly observable schema markers (foreign keys, indexes, constraints)
- ✅ Intent Preservation: Database semantics maintained through all transformations (development → staging → production)
- ✅ Domain Boundaries: Clear semantic ownership (Schema Domain ≠ Query Optimization Domain ≠ MCP Protocol Domain)
Built on research from Semantic Intent as Single Source of Truth, this implementation demonstrates how to build maintainable, AI-friendly database tools that preserve intent.
🚀 Quick Start
Prerequisites
- Node.js 20.x or higher
- Cloudflare account with D1 databases
- Cloudflare API token with D1 access
Installation
-
Clone the repository
git clone https://github.com/semanticintent/semantic-d1-mcp.git cd semantic-d1-mcp -
Install dependencies
npm install -
Configure environment
Copy the example configuration:
cp .env.example .envUpdate
.envwith your Cloudflare credentials:# Cloudflare Configuration CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id CLOUDFLARE_API_TOKEN=your_cloudflare_api_token # D1 Database Configuration - Development D1_DEV_DATABASE_ID=your_dev_database_id D1_DEV_DATABASE_NAME=your_dev_database_name # D1 Database Configuration - Staging (Optional) D1_STAGING_DATABASE_ID=your_staging_database_id D1_STAGING_DATABASE_NAME=your_staging_database_name # D1 Database Configuration - Production (Optional) D1_PROD_DATABASE_ID=your_prod_database_id D1_PROD_DATABASE_NAME=your_prod_database_nameNote: At least one database environment must be configured.
-
Build the server
npm run build -
Start the MCP server
npm startOr use the provided shell script:
./start-d1-mcp.sh
Get Cloudflare API Token
- Go to Cloudflare Dashboard
- Navigate to My Profile → API Tokens
- Click Create Token
- Use the Edit Cloudflare Workers template
- Add D1 permissions:
D1:Read - Copy the token to your
.envfile
Get D1 Database IDs
# List all your D1 databases
wrangler d1 list
# Get specific database info
wrangler d1 info <database-name>
Copy the database IDs to your .env file.
🛠️ MCP Tools
This server provides 4 comprehensive MCP tools for D1 database introspection:
1. analyze_database_schema
Analyze complete database schema structure with metadata and optional sample data.
Parameters:
environment(required):"development"|"staging"|"production"includeSamples(optional, default:true): Include sample data from tablesmaxSampleRows(optional, default:5): Maximum rows per table sample
Returns:
- Complete schema analysis
- Table structures with columns, types, constraints
- Indexes and foreign keys
- Sample data from each table (if enabled)
- Schema metadata and statistics
Example:
{
"name": "analyze_database_schema",
"arguments": {
"environment": "development",
"includeSamples": true,
"maxSampleRows": 5
}
}
2. get_table_relationships
Extract and analyze foreign key relationships between tables.
Parameters:
environment(required): Database environmenttableName(optional): Filter relationships for specific table
Returns:
- Foreign key relationships with cardinality (one-to-many, many-to-one)
- Referential integrity rules (CASCADE, SET NULL, etc.)
- Relationship metadata and statistics
Example:
{
"name": "get_table_relationships",
"arguments": {
"environment": "production",
"tableName": "users"
}
}
3. validate_database_schema
Validate database schema for common issues and anti-patterns.
Parameters:
environment(required): Database environment
Returns:
- Schema validation results
- Missing primary keys
- Foreign keys without indexes
- Naming convention violations
- Tables without relationships
Example:
{
"name": "validate_database_schema",
"arguments": {
"environment": "production"
}
}
4. suggest_database_optimizations
Generate schema optimization recommendations based on structure analysis.
Parameters:
environment(required): Database environment
Returns:
- Prioritized optimization suggestions (high/medium/low)
- Missing index recommendations
- Primary key suggestions
- Schema improvement opportunities
- Performance optimization tips
Example:
{
"name": "suggest_database_optimizations",
"arguments": {
"environment": "production"
}
}
🔌 Connect to Claude Desktop
Connect this MCP server to Claude Desktop for AI-assisted database development.
Configuration
-
Edit Claude Desktop config - Go to Settings → Developer → Edit Config
-
Add MCP server configuration:
{
"mcpServers": {
"semantic-d1": {
"command": "node",
"args": [
"/absolute/path/to/semantic-d1-mcp/dist/index.js"
],
"env": {
"CLOUDFLARE_ACCOUNT_ID": "your_account_id",
"CLOUDFLARE_API_TOKEN": "your_api_token",
"D1_DEV_DATABASE_ID": "your_dev_db_id",
"D1_DEV_DATABASE_NAME": "your_dev_db_name",
"D1_STAGING_DATABASE_ID": "your_staging_db_id",
"D1_STAGING_DATABASE_NAME": "your_staging_db_name",
"D1_PROD_DATABASE_ID": "your_prod_db_id",
"D1_PROD_DATABASE_NAME": "your_prod_db_name"
}
}
}
}
-
Restart Claude Desktop
-
Verify tools are available - You should see 4 D1 tools in Claude's tool list
Usage Example
In Claude Desktop:
"Analyze my production database schema and suggest optimizations for tables with foreign keys"
Claude will use the analyze_database_schema and suggest_database_optimizations tools automatically.
🏗️ Architecture
This project demonstrates Domain-Driven Hexagonal Architecture with clean separation of concerns:
┌─────────────────────────────────────────────────────────┐
│ Presentation Layer │
│ (MCP Server - Protocol Handling) │
└────────────────────┬────────────────────────────────────┘
│
┌────────────────────▼────────────────────────────────────┐
│ Application Layer │
│ (Use Cases - Schema Analysis Orchestration) │
└────────────────────┬────────────────────────────────────┘
│
┌────────────────────▼────────────────────────────────────┐
│ Domain Layer │
│ (Schema Entities, Relationship Logic, Services) │
│ Pure Business Logic │
└────────────────────┬────────────────────────────────────┘
│
┌────────────────────▼────────────────────────────────────┐
│ Infrastructure Layer │
│ (Cloudflare D1 REST API, HTTP Client) │
│ Technical Adapters │
└─────────────────────────────────────────────────────────┘
Implementation Status
Status: ✅ Hexagonal architecture refactoring complete
Current Structure:
src/
├── domain/ # Business logic (entities, services)
│ ├── entities/ # DatabaseSchema, TableInfo, Column, etc.
│ ├── services/ # SchemaAnalyzer, RelationshipAnalyzer, etc.
│ ├── repositories/ # Port interfaces
│ └── value-objects/ # Environment enum
├── application/ # Use cases and orchestration
│ ├── use-cases/ # AnalyzeSchema, GetRelationships, etc.
│ └── ports/ # Cache provider interface
├── infrastructure/ # External adapters
│ ├── adapters/ # CloudflareD1Repository, Cache
│ ├── config/ # CloudflareConfig, DatabaseConfig
│ └── http/ # CloudflareAPIClient
├── presentation/ # MCP protocol layer
│ └── mcp/ # D1DatabaseMCPServer
└── index.ts # Composition root (DI)
See ARCHITECTURE.md for detailed design documentation.
Layer Responsibilities
Domain Layer:
- Database schema entities (Schema, Table, Relationship, Index)
- Schema analysis business logic
- Relationship extraction logic
- Optimization recommendation rules
Application Layer:
- Orchestrate domain services
- Execute use cases (AnalyzeSchema, GetRelationships, etc.)
- Coordinate infrastructure adapters
Infrastructure Layer:
- Cloudflare D1 REST API integration
- HTTP client for API calls
- Cache provider (in-memory)
Presentation Layer:
- MCP server initialization
- Tool registration and routing
- Request/response formatting
Semantic Intent Principles
This codebase follows strict semantic anchoring rules:
-
Semantic Over Structural
// ✅ SEMANTIC: Based on observable schema properties const needsIndex = table.hasForeignKey() && !table.hasIndexOnForeignKey() // ❌ STRUCTURAL: Based on technical metrics const needsIndex = table.rowCount > 10000 && table.queryCount > 100 -
Intent Preservation
// ✅ Environment semantics preserved through transformations const schema = await fetchSchema(Environment.PRODUCTION) // Schema analysis preserves "production" intent - no overrides -
Observable Anchoring
// ✅ Based on directly observable properties const relationships = extractForeignKeys(sqliteMaster) // ❌ Based on inferred behavior const relationships = inferFromQueryPatterns(logs)
See SEMANTIC_ANCHORING_GOVERNANCE.md for complete governance rules.
🧪 Testing
Status: ✅ Comprehensive test suite with 398 tests passing
Test Coverage
- ✅ Domain Layer: 212 tests (entities, services, validation)
- ✅ Infrastructure Layer: 64 tests (D1 adapter, API client, config)
- ✅ Application Layer: 35 tests (use cases, orchestration)
- ✅ Presentation Layer: 13 tests (MCP server, tool routing)
- ✅ Integration: 15 tests (end-to-end flows)
- ✅ Value Objects: 59 tests (Environment, immutability)
Total: 398 tests (all passing ✅)
Running Tests
# Run all tests
npm test
# Watch mode
npm run test:watch
# With UI
npm run test:ui
# Coverage report
npm run test:coverage
Test Framework
- Vitest: Fast unit testing framework
- @vitest/coverage-v8: Code coverage reports
- Mock Strategy: Mock Cloudflare D1 API responses via interface implementations
📖 Learning from This Implementation
This codebase serves as a reference implementation for semantic intent patterns in database tooling.
Key Files to Study
Hexagonal Architecture Implementation:
- src/index.ts - Composition root with dependency injection
- src/domain/entities/ - Domain entities with semantic validation
- src/domain/services/ - Pure business logic services
- src/application/use-cases/ - Orchestration layer
- src/infrastructure/adapters/ - External adapters
- src/presentation/mcp/ - MCP protocol layer
Reference Documentation:
- D1_MCP_REFACTORING_PLAN.md - Complete refactoring plan
- SEMANTIC_ANCHORING_GOVERNANCE.md - Governance rules
- ARCHITECTURE.md - Architecture details
Related Projects
- semantic-context-mcp - Sibling reference implementation for context management
🤝 Contributing
We welcome contributions! This is a reference implementation, so contributions should maintain semantic intent principles.
How to Contribute
- Read the guidelines: CONTRIBUTING.md
- Check refactoring plan: D1_MCP_REFACTORING_PLAN.md
- Follow the architecture: Maintain layer boundaries and semantic anchoring
- Add tests: All changes need comprehensive test coverage
- Document intent: Explain WHY, not just WHAT
Contribution Standards
- ✅ Follow semantic intent patterns
- ✅ Maintain hexagonal architecture (post-refactoring)
- ✅ Add comprehensive tests (90%+ coverage target)
- ✅ Include semantic documentation
- ✅ Pass all CI checks
Quick Links:
- Contributing Guide - Detailed guidelines
- Code of Conduct - Community standards
- Architecture Guide - Design principles
- Security Policy - Report vulnerabilities
Community
- 💬 Discussions - Ask questions
- 🐛 Issues - Report bugs
- 🔒 Security - Report vulnerabilities privately
🔒 Security
Security is a top priority. Please review our Security Policy for:
- API token management best practices
- What to commit / what to exclude
- Reporting security vulnerabilities
- Security checklist for deployment
Found a vulnerability? Email: security@semanticintent.dev
🔬 Research Foundation
This implementation is based on the research paper "Semantic Intent as Single Source of Truth: Immutable Governance for AI-Assisted Development".
Core Principles Applied
- Semantic Over Structural - Schema analysis based on meaning, not metrics
- Intent Preservation - Environment semantics maintained through transformations
- Observable Anchoring - Decisions based on directly observable schema properties
- Immutable Governance - Protect semantic integrity at runtime
Related Resources
- Research Paper (coming soon)
- Semantic Anchoring Governance
- semanticintent.dev (coming soon)
📊 Project Roadmap
✅ Phase 0: Initial Implementation (Complete)
- Monolithic MCP server with 6 tools
- D1 REST API integration
- Basic schema analysis
✅ Phase 1: Domain Layer (Complete)
- 10 domain entities with semantic validation
- 3 domain services (SchemaAnalyzer, RelationshipAnalyzer, OptimizationService)
- 212 passing tests
✅ Phase 2: Infrastructure Layer (Complete)
- CloudflareD1Repository adapter
- CloudflareAPIClient HTTP client
- InMemoryCacheProvider
- 64 passing tests
✅ Phase 3: Application Layer (Complete)
- 4 use cases (AnalyzeSchema, GetRelationships, ValidateSchema, SuggestOptimizations)
- Port interfaces (ICloudflareD1Repository, ICacheProvider)
- 35 passing tests
✅ Phase 4: Presentation Layer (Complete)
- D1DatabaseMCPServer with 4 MCP tools
- Request/response DTOs
- 13 passing tests
✅ Phase 5: Integration & Composition Root (Complete)
- Dependency injection in index.ts
- Environment configuration
- 15 integration tests
✅ Phase 6: CI/CD & Documentation (Complete)
- TypeScript build verification
- README updated
- 398 total tests passing
🎯 Phase 7: Production Readiness (Planned)
- GitHub Actions CI/CD workflow
- Dependabot automation
- Security scanning
- GitHub repository setup
See D1_MCP_REFACTORING_PLAN.md for detailed roadmap.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built on Model Context Protocol by Anthropic
- Inspired by Hexagonal Architecture (Alistair Cockburn)
- Based on Domain-Driven Design principles (Eric Evans)
- Part of the Semantic Intent research initiative
This is a reference implementation demonstrating semantic intent patterns for database introspection. Study the code, learn the patterns, and apply them to your own projects. 🏗️
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。
