Claude Code MCP Server SOC 2 Compliance Guide
Building MCP (Model Context Protocol) servers that meet SOC 2 compliance requirements is essential for enterprises deploying AI assistants in regulated environments. This guide walks you through the technical implementation of security controls, audit trails, and access management patterns that satisfy SOC 2 Trust Service Criteria.
Understanding SOC 2 Requirements for MCP Servers
SOC 2 compliance centers on five trust service criteria: security, availability, processing integrity, confidentiality, and privacy. When your MCP server handles sensitive data or interacts with protected systems, you need controls addressing all five areas.
The security criterion is your primary concern. MCP servers must implement authentication, authorization, encryption, and logging These controls prevent unauthorized access and provide evidence for audits.
Your MCP server likely processes data that falls under confidentiality requirements. Customer data, business logic, and API credentials demand protection through proper secrets management both in transit and at rest.
Implementing Authentication and Authorization
Every MCP server needs strong authentication. For production deployments, implement token-based authentication using JWTs or API keys with appropriate expiration policies.
# Example: Token validation for MCP server endpoints
from functools import wraps
import jwt
from datetime import datetime, timedelta
SECRET_KEY = "your-secure-secret-key" # Load from environment variable
def validate_token(token: str) -> dict:
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
# Check token expiration
if datetime.fromtimestamp(payload['exp']) < datetime.now():
raise jwt.ExpiredSignatureError("Token has expired")
# Validate required claims
required_claims = ['sub', 'iat', 'exp', 'scope']
for claim in required_claims:
if claim not in payload:
raise jwt.InvalidTokenError(f"Missing required claim: {claim}")
return payload
except jwt.InvalidTokenError as e:
raise PermissionError(f"Invalid token: {str(e)}")
def require_auth(f):
@wraps(f)
async def wrapper(request, *args, **kwargs):
auth_header = request.headers.get('Authorization', '')
if not auth_header.startswith('Bearer '):
raise PermissionError("Missing or invalid Authorization header")
token = auth_header[7:] # Remove 'Bearer ' prefix
user_context = validate_token(token)
return await f(request, user_context, *args, **kwargs)
return wrapper
Authorization determines what authenticated users can access. Implement role-based access control (RBAC) with scopes that map to MCP tool permissions.
// Example: Authorization middleware for MCP server
const authorizationMiddleware = (requiredScope) => {
return (req, res, next) => {
const token = extractToken(req);
const scopes = token?.scope?.split(' ') || [];
if (!scopes.includes(requiredScope)) {
return res.status(403).json({
error: 'Insufficient permissions',
required: requiredScope
});
}
next();
};
};
// Apply to MCP tool endpoints
mcpServer.registerTool('read_customer_data', {
middleware: authorizationMiddleware('read:customers')
});
Audit Logging for SOC 2 Compliance
SOC 2 requires detailed audit trails. Your MCP server must log all security-relevant events: authentication attempts, authorization decisions, data access, and configuration changes.
Structure your logs for easy analysis and long-term retention:
import json
import hashlib
from datetime import datetime
from typing import Optional
class AuditLogger:
def __init__(self, output_path: str):
self.output_path = output_path
def log_event(self, event_type: str, user_id: Optional[str],
resource: str, action: str, outcome: str,
metadata: dict = None):
event = {
"timestamp": datetime.utcnow().isoformat() + "Z",
"event_type": event_type,
"user_id": user_id,
"resource": resource,
"action": action,
"outcome": outcome,
"metadata": metadata or {},
# Hash for integrity verification
"event_hash": ""
}
# Calculate hash of event data (excluding hash field)
event_json = json.dumps(event, sort_keys=True, default=str)
event['event_hash'] = hashlib.sha256(event_json.encode()).hexdigest()
# Write to audit log (append to file or send to SIEM)
with open(self.output_path, 'a') as f:
f.write(json.dumps(event) + '\n')
# Usage in your MCP server
audit = AuditLogger('/var/log/mcp-audit.jsonl')
# Log authentication events
audit.log_event(
event_type="authentication",
user_id="user123",
resource="/api/login",
action="login_attempt",
outcome="success",
metadata={"ip_address": "192.168.1.100", "mfa_used": True}
)
# Log data access
audit.log_event(
event_type="data_access",
user_id="user123",
resource="customer_db.orders",
action="read",
outcome="success",
metadata={"record_count": 50, "query_hash": "abc123..."}
)
Your logs should capture who did what, when, and the result. Include sufficient context for reconstructing events during audits.
Data Encryption Requirements
Encrypt all sensitive data in transit using TLS 1.2 or higher. For data at rest, use AES-256 encryption for stored credentials, API keys, and sensitive payloads.
# Example: Encrypting sensitive configuration
from cryptography.fernet import Fernet
import base64
import os
class SecureConfig:
def __init__(self, encryption_key: bytes):
self.cipher = Fernet(encryption_key)
def encrypt_value(self, value: str) -> str:
encrypted = self.cipher.encrypt(value.encode())
return base64.urlsafe_b64encode(encrypted).decode()
def decrypt_value(self, encrypted_value: str) -> str:
decoded = base64.urlsafe_b64decode(encrypted_value.encode())
decrypted = self.cipher.decrypt(decoded)
return decrypted.decode()
# Generate key: Fernet.generate_key()
# Store key in secure vault (HashiCorp Vault, AWS Secrets Manager, etc.)
Never hardcode secrets. Use environment variables or secrets management services. Your MCP server configuration should load credentials at runtime from secure storage, following least privilege configuration principles.
Integrating with Claude Code Skills
When building MCP servers for Claude Code environments, consider how they interact with existing skills. The frontend-design skill can validate your server’s API responses against expected schemas. Use the pdf skill to generate compliance documentation automatically. The tdd skill helps you write tests for security controls before implementation.
For knowledge management, the supermemory skill can index your compliance documentation, making it searchable through natural language queries. This accelerates incident response and audit preparation.
Monitoring and Incident Response
SOC 2 requires monitoring for security events and documented incident response procedures. Implement health checks that verify:
- Authentication service availability
- Audit log write success
- Token validation latency
- Failed login attempt thresholds
# Example: Health check endpoint
@app.get("/health")
def health_check():
return {
"status": "healthy",
"checks": {
"auth_service": check_auth_service(),
"audit_logging": check_audit_log_writable(),
"encryption": check_encryption_keys_valid()
},
"timestamp": datetime.utcnow().isoformat()
}
Set up alerts for security-relevant events: multiple failed authentication attempts, unusual data access patterns, or audit log failures.
Deployment Considerations
Deploy your SOC 2-compliant MCP server in isolated network segments. Use containers with minimal base images to reduce attack surface. Implement network policies that restrict communication to necessary paths only.
Regularly rotate credentials and keys. Automate this process to avoid manual errors. Your deployment pipeline should support secret rotation without service interruption.
Conclusion
Building a SOC 2-compliant MCP server requires attention to authentication, authorization, encryption, and audit logging. Implement these controls from the start rather than retrofitting them later. Use the patterns shown here as a foundation, then adapt them to your specific compliance scope.
Document your implementation clearly. This documentation serves both auditors and future maintainers. Combined with operational controls and regular assessments, your MCP server will meet SOC 2 requirements for production AI deployments.
Related Reading
- MCP OAuth 2.1 Authentication Implementation Guide
- MCP Server Logging Audit Trail Security Guide
- Securing MCP Servers in Production Environments
- Advanced Hub
Built by theluckystrike — More at zovo.one