Audit¶
audit ¶
Audit logging utilities for Linux MCP Server.
This module provides helper functions for consistent audit logging across the entire MCP server. All functions add structured context to log records that can be output in both human-readable and JSON formats.
AuditContext ¶
Context manager for adding extra fields to all log records.
Usage
with AuditContext(tool="list_services", host="server1.com") as logger: logger.info("Starting operation")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**extra_fields
|
Any
|
Additional fields to add to log records. |
{}
|
Yields:
| Type | Description |
|---|---|
LoggerAdapter
|
logging.LoggerAdapter: Logger adapter with extra fields attached. |
Source code in src/linux_mcp_server/audit.py
log_ssh_command ¶
Log SSH command execution.
Verbosity is tiered based on log level: - INFO: Command and exit code - DEBUG: Also includes execution duration
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command that was executed |
required |
host
|
str
|
Remote host |
required |
exit_code
|
int
|
Command exit code |
required |
duration
|
float | None
|
Optional execution duration in seconds (shown at DEBUG level) |
None
|
Source code in src/linux_mcp_server/audit.py
log_ssh_connect ¶
log_ssh_connect(
host: str,
status: str,
username: str = "",
reused: bool = False,
key_path: str | None = None,
error: str | None = None,
)
Log SSH connection event.
Verbosity is tiered based on log level: - INFO: Basic connection success/failure - DEBUG: Detailed information including key path, reuse status
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Remote host |
required |
username
|
str
|
SSH username |
''
|
status
|
str
|
Connection status ("success" or "failed") |
required |
reused
|
bool
|
Whether connection was reused (shown at DEBUG level) |
False
|
key_path
|
str | None
|
Path to SSH key used (shown at DEBUG level) |
None
|
error
|
str | None
|
Optional error message |
None
|
Source code in src/linux_mcp_server/audit.py
log_tool_call ¶
Decorator to log tool calls
Works with sync or async functions.
Source code in src/linux_mcp_server/audit.py
sanitize_parameters ¶
Sanitize parameters by redacting sensitive fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
Dictionary of parameters to sanitize |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with sensitive fields redacted |