# MCP Server Logging Examples
This directory contains practical examples demonstrating how to integrate dc_logger into the ComfyUI MCP Server architecture.
## Examples Overview
### [01_simple_tool_logging.py](01_simple_tool_logging.py)
**Decorator-based logging for MCP tools (Interface Layer)**
Demonstrates:
- Basic `@log_call` decorator usage
- Parameter logging with `include_params=True`
- Sensitive parameter sanitization (`sensitive_params`)
- Custom logger instances
- Automatic error logging with stack traces
**When to use:** For all MCP tool functions (`@mcp.tool()` decorated functions)
### [02_manager_logging.py](02_manager_logging.py)
**Manual structured logging for business logic (Logic Layer)**
Demonstrates:
- Manager class initialization with logger
- Cache hit/miss logging
- Parameter validation logging
- Resource cleanup logging
- Entity tracking with `LogEntity`
- Structured metadata with `extra` fields
**When to use:** For managers, clients, and business logic classes
### [03_http_logging.py](03_http_logging.py)
**External API call logging with HTTPDetails**
Demonstrates:
- HTTP request/response logging
- Timing and duration tracking
- Status code and response size logging
- Error handling with HTTP context
- Progress logging for downloads
- Entity correlation (job IDs, asset names)
**When to use:** For ComfyUI client or any external API interactions
### [04_parameter_sanitization.py](04_parameter_sanitization.py) ✨ NEW
**Parameter logging with sensitive data redaction**
Demonstrates:
- Logging function parameters for debugging and auditing
- Automatic sanitization of sensitive parameters (passwords, tokens, API keys, secrets)
- Using `log_params=True` and `sensitive_params` in `@log_call` decorator
- Environment-based parameter logging control (DEBUG_LOGGING env var)
- Multiple real-world examples: webhooks, uploads, API calls, database connections
- Security best practices for parameter logging
**When to use:**
- When debugging tools that accept sensitive data
- For audit trails that need parameter tracking
- During development (disable in production)
- When integrating with external APIs that require credentials
**Run**:
```bash
# Standard run (some examples disable param logging)
python 04_parameter_sanitization.py
# With debug logging enabled for all examples
DEBUG_LOGGING=true python 04_parameter_sanitization.py
```
**Example output**:
```
[demo-123] [tool] configure_webhook - ENTER params={'url': 'https://example.com/webhook', 'webhook_secret': '[REDACTED]', 'api_key': '[REDACTED]', 'timeout': 60}
[demo-123] [tool] configure_webhook - SUCCESS (0.100s)
```
## Running the Examples
Each example is self-contained and can be run independently:
```bash
# Install dependencies first
pip install dc_logger mcp-server-fastmcp
# Run individual examples
python 01_simple_tool_logging.py
python 02_manager_logging.py
python 03_http_logging.py
```
## Integration Pattern
```
┌─────────────────────────────────────────────────┐
│ MCP Server Layers │
├─────────────────────────────────────────────────┤
│ │
│ Protocol Layer (server.py) │
│ └─ Manual logging: startup, health checks │
│ │
├─────────────────────────────────────────────────┤
│ │
│ Interface Layer (tools/*.py) │
│ └─ @log_call decorators: tool invocations │
│ Example: 01_simple_tool_logging.py │
│ │
├─────────────────────────────────────────────────┤
│ │
│ Logic Layer (managers/, clients/) │
│ └─ Manual logging: business operations │
│ Example: 02_manager_logging.py │
│ Example: 03_http_logging.py │
│ │
└─────────────────────────────────────────────────┘
```
## Key Patterns Demonstrated
### 1. Separation of Concerns
- **Interface Layer**: Use `@log_call` decorator (Example 01)
- **Logic Layer**: Use manual `logger.info/error/debug` (Examples 02, 03)
### 2. Structured Logging
- Use `entity` for resource tracking
- Use `action` for operation names
- Use `extra` for metadata
- Use `http_details` for API calls
### 3. Async-First
- All logging methods are async (`await logger.info()`)
- Integrates seamlessly with FastMCP's async execution
### 4. Error Handling
- Automatic stack traces for errors
- Contextual error metadata
- Duration tracking even on failure
## Next Steps
After reviewing these examples:
1. **Read the main skill**: [../SKILL.md](../SKILL.md)
2. **Review architecture**: [../../../docs/ARCHITECTURE.md](../../../docs/ARCHITECTURE.md)
3. **Start integration**: Add logging to your MCP tools and managers
4. **Configure for production**: Set up LangSmith or Datadog integration
## Questions or Issues?
See the [main skill documentation](../SKILL.md) for:
- Configuration options
- Migration strategy
- Best practices
- Troubleshooting