MCP gateway security patterns, token management, request validation, and audit logging for MCP communications
license
Apache-2.0
MCP Gateway Security Skill
🔴 AI FIRST Quality Principle
Apply the AI FIRST principle: never accept first-pass quality. Minimum 2 iterations. Read all output, improve every section. No shortcuts.
Purpose
This skill provides security patterns and best practices for securing MCP (Model Context Protocol) gateway communications in the Riksdagsmonitor platform. It covers token management, request validation, audit logging, and threat mitigation for AI-assisted development workflows.
When to Use This Skill
Apply this skill when:
✅ Hardening MCP gateway configurations
✅ Managing tokens and credentials for MCP servers
✅ Implementing request validation for MCP tool calls
✅ Setting up audit logging for MCP communications
✅ Conducting security reviews of MCP configurations
✅ DO: Use environment variable references (${VAR_NAME} in MCP JSON config files; $VAR_NAME in shell scripts; ${{ secrets.VAR_NAME }} in GitHub Actions)
✅ DO: Use GitHub Actions secrets for CI/CD tokens
✅ DO: Rotate tokens at least quarterly
✅ DO: Use fine-grained PATs with minimal scopes
✅ DO: Revoke tokens immediately when compromised
❌ DON'T: Hardcode tokens in configuration files
❌ DON'T: Commit tokens to version control
❌ DON'T: Share tokens between environments
❌ DON'T: Use classic PATs with broad scopes
❌ DON'T: Log token values in any log output
GitHub Token Scopes (Principle of Least Privilege)
MCP Operation
Required Scope
Justification
Read code
contents:read
Code search and file reading
Create PRs
pull_requests:write
PR creation and updates
Manage issues
issues:write
Issue creation and updates
Read workflows
actions:read
CI/CD status checking
Security alerts
security_events:read
CodeQL and Dependabot
Request Validation
Input Validation for MCP Tools
File Operations:
Validation Rules:
1. Path must be within allowed directories
2. Path must not contain traversal sequences (../)
3. File extension must be in allowed list
4. File size must not exceed limits
5. Content must not contain known malicious patterns
Code Operations:
Validation Rules:
1. Branch names must match allowed pattern
2. Commit messages must not contain secrets
3. File content must pass security scanning
4. PR descriptions must not leak sensitive data
Output Sanitization
Before Returning MCP Tool Output:
1. Strip any credential-like patterns
2. Remove internal IP addresses/hostnames
3. Truncate excessively large outputs
4. Validate JSON/structured output format
5. Log sanitization actions for audit
Dangerous Tool Patterns
Tool
Risk
Mitigation
filesystem.write_file
Overwrite critical files
Restrict to project directories
filesystem.delete
Data loss
Require confirmation, backup
github.push_files
Inject malicious code
Code review before merge
playwright.evaluate
Execute arbitrary JS
Sandbox, restrict domains
bash.execute
System command execution
Allowlist commands, sandbox
Audit Logging
What to Log
Event
Log Level
Data to Capture
MCP server start/stop
INFO
Server name, timestamp
Tool invocation
INFO
Tool name, parameters (sanitized)
Authentication success
INFO
Server name, token type (not value)
Authentication failure
WARN
Server name, failure reason
Access denied
WARN
Tool, resource, reason
Configuration change
INFO
What changed, who changed it
Error/exception
ERROR
Error details, stack trace
What NOT to Log
❌ Token values or API keys
❌ File contents containing secrets
❌ User passwords or credentials
❌ Full request/response bodies with PII
❌ Internal network topology details
□ All tokens use environment variables (never hardcoded)
□ Token scopes follow least privilege
□ Filesystem access restricted to project directory only
□ MCP package versions pinned to specific releases
□ Configuration file committed (without secrets)
□ No sensitive data in MCP server arguments
□ SSE server URLs use HTTPS only
□ Certificate validation enabled for remote servers
Periodic Review (Monthly)
□ Review token scopes — remove unnecessary permissions
□ Rotate tokens per schedule
□ Check for new MCP package versions and CVEs
□ Review audit logs for anomalies
□ Verify directory restrictions still appropriate
□ Test authentication failure handling
□ Review and update threat model
□ Check for deprecated MCP server versions
Incident Response
MCP Security Incident
│
├─→ Token Compromise
│ ├─→ Revoke token immediately
│ ├─→ Rotate all related tokens
│ ├─→ Review audit logs for unauthorized access
│ └─→ Update token storage mechanism
│
├─→ Unauthorized File Access
│ ├─→ Review filesystem server configuration
│ ├─→ Check for directory traversal attempts
│ ├─→ Restrict filesystem paths
│ └─→ Review accessed files for data exposure
│
├─→ Supply Chain Attack
│ ├─→ Pin to known-good version
│ ├─→ Verify package integrity
│ ├─→ Check for malicious tool behavior
│ └─→ Report to MCP package maintainers
│
└─→ Prompt Injection
├─→ Review tool invocation logs
├─→ Identify injected content
├─→ Assess data exposure
└─→ Update input validation rules