Skip to content

Claude Code Complete Guide

Serena MCP Security Best Practices — Tool Permission Management & Gradual Authorization Strategy

This is a follow-up to the morning article

For basic Serena MCP setup procedures, see Serena MCP Implementation Guide.

Goals

  • Master concrete procedures for gradually enabling tools from Serena's read_only mode
  • Understand recommended security settings by project type (OSS/Enterprise/PoC)
  • Implement execution log auditing methods and anomaly detection patterns

Why Security Management Matters

Serena MCP provides a powerful toolset, but features like execute_shell_command can execute arbitrary shell commands. Without proper permission management, the following risks exist:

  • Unintended file deletion/overwriting
  • Leakage of confidential information (environment variables, authentication tokens)
  • Destructive changes to system configuration

In production deployment, you need to design a "safely operational" state from the start, not just "make it work."

Architecture: 3-Layer Security Model

┌─────────────────────────────────────┐
│  Layer 1: Project-level Settings    │
│  (.serena/project.yml)              │
│  - read_only flag                   │
│  - Allowed tools list               │
└─────────────────────────────────────┘
           ↓
┌─────────────────────────────────────┐
│  Layer 2: User-level Settings       │
│  (~/.serena/serena_config.yml)      │
│  - Usage statistics recording       │
│  - Global exclusion settings        │
└─────────────────────────────────────┘
           ↓
┌─────────────────────────────────────┐
│  Layer 3: Log Auditing              │
│  (mcp.log, dashboard)               │
│  - Executed command recording       │
│  - Error tracking                   │
└─────────────────────────────────────┘

Implementation Steps

Step 1: Initial Setup (Fully Read-Only)

When first introducing Serena to a project, always start with read_only: true.

# <project>/.serena/project.yml
read_only: true
project_name: my_secure_project

In this state, Serena can only perform the following operations:

  • Reading file contents
  • Analyzing code structure
  • Semantic search
  • Creating project indices

No write operations are executed.

Step 2: Verify Execution on Dashboard

First, operate with read_only: true for one day and observe what Serena is attempting on the dashboard (http://localhost:24282/dashboard/index.html).

Check points:

  • Which files are being accessed
  • Which tools are being invoked
  • Whether errors are occurring

Step 3: Gradual Tool Authorization

Enable necessary tools one at a time. Opening everything at once is dangerous.

# <project>/.serena/project.yml
read_only: false  # Enable write mode
project_name: my_secure_project

# Explicitly specify tools to enable
included_optional_tools:
  - edit_file          # File editing (needed first)
  - create_directory   # Directory creation (needed next)
  # execute_shell_command should be reserved until last

OSS Development Projects (Public Repositories)

# .serena/project.yml
read_only: false
project_name: oss_project
included_optional_tools:
  - edit_file
  - create_directory
  - delete_file
  # execute_shell_command: Do not allow (run in CI)
SettingRecommendedReason
read_onlyfalsePrioritize development efficiency
execute_shell_commandDisabledDelegate to CI/CD, no need locally
Log recordingEnabledFor issue tracking

Enterprise Development (Contains Confidential Information)

# .serena/project.yml
read_only: true  # Default read-only
project_name: enterprise_app

# Whitelist approach: Allow only minimal necessary
included_optional_tools:
  - edit_file  # Enable only after review
SettingRecommendedReason
read_onlytrue (default)Confidentiality protection first
execute_shell_commandNever enableRisk insufficient even with audit logs
Environment variable accessRestricted.env files in .gitignore + Serena exclusion

PoC/Experimental Projects

# .serena/project.yml
read_only: false
project_name: poc_experiment
included_optional_tools:
  - edit_file
  - create_directory
  - delete_file
  - execute_shell_command  # Remove after PoC completion
SettingRecommendedReason
read_onlyfalsePrioritize experiment speed
execute_shell_commandTemporarily allowedDisable after PoC completion
BackupRequiredCheck every git commit

Execution Log Auditing in Practice

Log File Locations

Log storage locations by client:

ClientLog Path
Claude Desktop~/Library/Application Support/Claude/mcp.log (macOS)
Claude Desktop%APPDATA%\Claude\mcp.log (Windows)
Claude Code.claude/mcp-*.log in project directory

Items to Audit

Regularly check the following:

# Extract executed commands
grep "execute_shell_command" mcp.log

# Extract only errors
grep "ERROR" mcp.log

# File modification history
grep "edit_file\|delete_file" mcp.log | head -20

Anomaly Detection Patterns

If the following signs appear, immediately revert to read_only: true:

SymptomPossible CauseAction
Unexpected file deletiondelete_file misexecutionDisable tool, restore with git
Massive shell command executionInfinite loop, configuration errorDisable execute_shell_command
Environment variable access logsAuthentication credential read attemptsAdd .env to exclusion settings

Failure Patterns and Mitigation Strategies

Failure 1: Starting with read_only: false from the beginning

SymptomCauseMitigation
Unintended file overwritingLack of understanding of Serena's behaviorAlways operate with read_only: true for 1 day

Failure 2: Carelessly enabling execute_shell_command

SymptomCauseMitigation
Dangerous command execution like rm -rfExcessive tool permissionsExecute in CI/CD, disable locally

Failure 3: Neglecting log auditing

SymptomCauseMitigation
Unable to identify root cause when problems occurLogs not checkedReview mcp.log weekly

Failure 4: Using identical settings across all projects

SymptomCauseMitigation
Excessive permissions in confidential projectsIgnoring project typeApply recommended settings table above

Automation & Extension Ideas

Methods to automate practical security operations:

  1. Log Rotation: Archive logs weekly and automatically detect anomaly patterns
  2. pre-commit hook: Warn of dangerous settings when .serena/project.yml changes
  3. Dashboard Extension: Collect metrics with Prometheus, visualize with Grafana
  4. Periodic Audit Reports: Share tool usage statistics with team monthly
  5. Permission Request Flow: Introduce review process for tool enablement in Enterprise environments

Next Steps

Once the security foundation is established, proceed to advanced utilization:

Summary

Key points for Serena MCP security practices:

  • Always start with read_only: true and observe on dashboard for 1 day
  • Apply settings according to project type (different for OSS/Enterprise/PoC)
  • execute_shell_command is last resort, basically delegate to CI/CD
  • Conduct weekly log audits and detect anomaly patterns early
  • Enable tools one at a time, absolutely avoid full authorization

Only with a safe operational foundation can you maximize the powerful features of Serena.