BEST_PRACTICES.md•8.24 kB
# Best Practices
This guide provides performance optimization tips, security best practices, and operational guidance for professional implementation of the MSSQL MCP Server.
## ⚡ Performance Optimization
### Query Optimization
**✅ Good Practices:**
```
✅ Good: "Show customers from New York with orders this month"
✅ Good: "Update orders where status is pending"
✅ Good: "Find products with low inventory (less than 10 units)"
```
**❌ Avoid:**
```
❌ Avoid: "Show me everything in the database"
❌ Avoid: "Update all orders" (requires WHERE clause)
❌ Avoid: "SELECT * FROM large_table" without filters
```
### Batch Operations
**✅ Efficient:**
```
✅ Efficient: "Insert these 10 products in a single transaction"
✅ Efficient: "Update multiple order statuses where conditions match"
✅ Efficient: "Create indexes for frequently queried columns"
```
**❌ Inefficient:**
```
❌ Inefficient: "Insert product A, then product B, then product C..."
❌ Inefficient: Multiple individual updates instead of batch operations
❌ Inefficient: Creating indexes on every column
```
### Index Usage
**Optimization Strategies:**
```
✅ Optimized: "Create index on frequently queried columns"
✅ Monitor: "Show me query execution plans for slow queries"
✅ Analyze: "Identify missing indexes for common queries"
```
**Index Best Practices:**
- Create indexes on columns used in WHERE clauses
- Use composite indexes for multi-column queries
- Monitor index usage and remove unused indexes
- Consider covering indexes for frequently accessed data
### Connection Management
**Connection Pooling:**
- The server automatically manages connection pooling
- Default pool size: 10 connections
- Pool timeout: 30 seconds
- Idle timeout: 30 minutes
**Optimization Tips:**
```bash
# Optimize connection timeouts
CONNECTION_TIMEOUT=30 # Reasonable for most scenarios
REQUEST_TIMEOUT=15 # Prevent long-running queries
CANCEL_TIMEOUT=5 # Quick cancellation
```
## 🔒 Security Best Practices
### Authentication Security
**SQL Server Authentication:**
```bash
# Use strong passwords
SQL_PASSWORD="ComplexP@ssw0rd123!"
# Rotate passwords regularly
# Use different passwords for different environments
```
**Azure AD Authentication:**
```bash
# Preferred for Azure SQL Database
# No credentials stored in configuration
# Leverages Azure AD security features
```
**Windows Authentication:**
```bash
# Most secure for local SQL Server
# Uses Windows security context
# No password management required
```
### Connection Security
**Encryption:**
```bash
# Always encrypt connections
ENCRYPT=true
# Use proper certificates in production
TRUST_SERVER_CERTIFICATE=false # Production
TRUST_SERVER_CERTIFICATE=true # Development only
```
**Network Security:**
- Use VPN for remote connections
- Implement firewall rules
- Restrict database access by IP
- Use private networks when possible
### Credential Management
**✅ Secure Practices:**
```bash
# Use environment variables
export SQL_PASSWORD="SecurePassword"
# Use Azure Key Vault
az keyvault secret set --vault-name MyVault --name sql-password --value "SecurePassword"
# Use .env files (not committed to git)
echo "SQL_PASSWORD=SecurePassword" >> .env.local
```
**❌ Insecure Practices:**
```bash
# Never hardcode credentials
# Never commit credentials to version control
# Never use weak passwords
# Never share credentials in plain text
```
### Production Security
**Read-Only Mode:**
```bash
# Use read-only mode for production queries
READONLY=true
# Allows: SELECT, DESCRIBE, LIST operations
# Blocks: INSERT, UPDATE, DELETE, CREATE, DROP operations
```
**Access Control:**
- Use least privilege principle
- Create dedicated database users for MCP server
- Grant only necessary permissions
- Monitor database access logs
## 🛠️ Error Handling Patterns
### Connection Issues
**Graceful Handling:**
```
You: "Show me all customers"
AI: I'm having trouble connecting to the database. Let me help troubleshoot:
1. Check if SQL Server is running
2. Verify connection string in configuration
3. Confirm network connectivity
4. Validate credentials
Error: Login failed for user 'sa'
Solution: Check SQL_PASSWORD environment variable
```
### Query Validation
**Security Validation:**
```
You: "Delete all customers"
AI: I cannot execute this query for security reasons:
❌ DELETE operations are not allowed through the read_data tool
✅ Use specific WHERE clauses: "Delete customers where status = 'inactive'"
✅ Or use update_data tool with proper conditions
```
### Permission Issues
**Clear Guidance:**
```
You: "Create a new database"
AI: This operation requires elevated permissions:
❌ CREATE DATABASE requires sysadmin rights
✅ Alternative: "Create a new table in the current database"
✅ Contact your DBA for database creation
```
## 📊 Monitoring and Logging
### Performance Monitoring
**Key Metrics to Track:**
- Query execution time
- Connection pool usage
- Error rates
- Database response times
**Monitoring Setup:**
```bash
# Enable debug logging
DEBUG=mcp:*
NODE_ENV=development
# Log to file
node dist/index.js 2>&1 | tee mcp-server.log
```
### Health Checks
**Connection Health:**
```javascript
// Regular health check query
SELECT 1 as health_check
```
**Database Monitoring:**
```sql
-- Monitor active connections
SELECT COUNT(*) as active_connections
FROM sys.dm_exec_sessions
WHERE is_user_process = 1
-- Check database size
SELECT
DB_NAME() as database_name,
SUM(size * 8.0 / 1024) as size_mb
FROM sys.database_files
```
## 🏢 Production Deployment
### Environment Configuration
**Development:**
```bash
SERVER_NAME=localhost
READONLY=false
TRUST_SERVER_CERTIFICATE=true
DEBUG=mcp:*
```
**Staging:**
```bash
SERVER_NAME=staging-sql.company.com
READONLY=true
TRUST_SERVER_CERTIFICATE=false
CONNECTION_TIMEOUT=30
```
**Production:**
```bash
SERVER_NAME=prod-sql.company.com
READONLY=true
TRUST_SERVER_CERTIFICATE=false
CONNECTION_TIMEOUT=30
ENCRYPT=true
```
### Deployment Checklist
**Pre-Deployment:**
- [ ] Test all database connections
- [ ] Verify environment variables
- [ ] Check firewall rules
- [ ] Validate SSL certificates
- [ ] Test read-only mode functionality
**Post-Deployment:**
- [ ] Monitor connection health
- [ ] Check error logs
- [ ] Verify query performance
- [ ] Test failover scenarios
- [ ] Document configuration
### Backup and Recovery
**Database Backups:**
- Ensure regular database backups
- Test backup restoration procedures
- Document recovery time objectives (RTO)
- Implement point-in-time recovery
**Configuration Backups:**
- Backup MCP server configuration
- Document environment variables
- Version control configuration files
- Maintain deployment documentation
## 🔧 Operational Considerations
### Capacity Planning
**Resource Requirements:**
- CPU: Minimal (Node.js application)
- Memory: 256MB - 512MB typical
- Network: Depends on query volume
- Storage: Minimal (logs only)
**Scaling Considerations:**
- Each MCP server instance handles one database
- Multiple instances for multiple databases
- Load balancing not typically required
- Consider connection limits on database server
### Maintenance
**Regular Tasks:**
- Update Node.js dependencies
- Monitor security vulnerabilities
- Review and rotate credentials
- Update SSL certificates
- Check database performance
**Troubleshooting:**
- Monitor error logs regularly
- Set up alerting for connection failures
- Document common issues and solutions
- Maintain troubleshooting runbooks
### Disaster Recovery
**Backup Strategy:**
- Regular configuration backups
- Document all environment variables
- Maintain deployment procedures
- Test recovery scenarios
**Failover Planning:**
- Document failover procedures
- Test with backup database instances
- Maintain emergency contact information
- Plan for extended outages
---
**Related Documentation:**
- [Advanced Configuration](ADVANCED_CONFIGURATION.md) - Complex setup scenarios
- [Usage Examples](USAGE_EXAMPLES.md) - Practical examples and patterns
- [Troubleshooting](TROUBLESHOOTING.md) - Common issues and solutions
- [Platform Notes](PLATFORM_NOTES.md) - Platform-specific considerations