# SACL Usage Examples
This document provides comprehensive examples of using the SACL MCP Server for bias-aware code analysis and retrieval.
## Table of Contents
- [Basic Usage Examples](#basic-usage-examples)
- [Relationship Analysis Examples](#relationship-analysis-examples)
- [Context-Aware Query Examples](#context-aware-query-examples)
- [File Update Examples](#file-update-examples)
- [Bias Analysis Examples](#bias-analysis-examples)
- [Advanced Workflows](#advanced-workflows)
## Basic Usage Examples
### Repository Analysis
**Scenario**: Initial setup of SACL for a new project
```json
{
"tool": "analyze_repository",
"arguments": {
"repositoryPath": "/workspace/my-react-app",
"incremental": false
}
}
```
**Expected Response**:
```
π Repository Analysis Complete
**Processing Statistics:**
β’ Files processed: 147/150
β’ Average bias score: 0.425
β’ High bias files detected: 12
β’ Processing time: 23.4s
π SACL Framework Applied:
β’ Textual bias detection completed
β’ Semantic augmentation applied
β’ Knowledge graph populated
β’ Ready for bias-aware code retrieval
Use 'query_code' to search with reduced textual bias.
```
### Basic Code Query
**Scenario**: Finding authentication-related code
```json
{
"tool": "query_code",
"arguments": {
"query": "user authentication and validation",
"repositoryPath": "/workspace/my-react-app",
"maxResults": 3
}
}
```
**Expected Response**:
```
π SACL Query Results for: "user authentication and validation"
Found 3 bias-aware matches:
**1. src/auth/AuthService.js**
β’ Bias Score: 45.2% π‘
β’ SACL Score: 87.3%
β’ Pattern: Service layer authentication with validation pipeline
```javascript
class AuthService {
async validateUser(credentials) {
// Validate user credentials
const user = await this.userRepository.findByEmail(credentials.email);
if (!user || !await this.comparePasswords(credentials.password, user.hashedPassword)) {
throw new AuthenticationError('Invalid credentials');
}
return this.generateToken(user);
}
}
```
π SACL Ranking Analysis:
β’ Query: "user authentication and validation"
β’ Textual Similarity: 78.5%
β’ Semantic Similarity: 91.2%
β’ Functional Relevance: 89.7%
β’ Bias Level: Medium (45.2%)
β’ Final Score: 87.3%
```
## Relationship Analysis Examples
### File Relationship Exploration
**Scenario**: Understanding dependencies of a controller
```json
{
"tool": "get_relationships",
"arguments": {
"filePath": "src/controllers/UserController.js",
"maxDepth": 2,
"relationshipTypes": ["imports", "calls"]
}
}
```
**Expected Response**:
```
π Code Relationships Analysis
**File:** src/controllers/UserController.js
**Traversal Depth:** 2
**Related Components:** 8
**Key Relationships:**
1. **UserService** (imports)
β’ File: src/services/UserService.js
β’ Relationship: Service imported by controller
β’ Relevance: 95.0%
β’ Distance: 1 hop
2. **validateRequest** (calls)
β’ File: src/middleware/validation.js
β’ Relationship: Validation middleware called in routes
β’ Relevance: 88.0%
β’ Distance: 1 hop
3. **DatabaseConnection** (calls)
β’ File: src/database/connection.js
β’ Relationship: Database operations through service
β’ Relevance: 75.0%
β’ Distance: 2 hops
**Traversal Statistics:**
β’ Nodes Visited: 12
β’ Edges Traversed: 8
β’ Max Depth Reached: 2
β’ Processing Time: 145ms
```
### File Context Analysis
**Scenario**: Understanding a component's role in the system
```json
{
"tool": "get_file_context",
"arguments": {
"filePath": "src/models/User.js",
"includeSnippets": true
}
}
```
**Expected Response**:
```
π― File Context Analysis
**File:** src/models/User.js
**Total Related:** 12
**Context Summary:**
β’ imports: 3 components
β’ exports: 1 components
β’ calls: 6 components
β’ extends: 1 components
**Relationship Breakdown:**
**IMPORTS (3):**
β’ mongoose
ββ Database modeling framework imported
ββ Code: const mongoose = require('mongoose');
β’ bcrypt
ββ Password hashing utility imported
ββ Code: const bcrypt = require('bcrypt');
**EXTENDS (1):**
β’ BaseModel
ββ Inherits from base model class
ββ Code: class User extends BaseModel { constructor(data) { super(data);
**CALLS (6):**
β’ hashPassword
ββ Password hashing method called in pre-save hook
β’ validateEmail
ββ Email validation called in schema validation
β’ generateToken
ββ JWT token generation for authentication
**Usage Recommendations:**
β’ Use 'query_code_with_context' to find similar patterns
β’ Check imports/exports when modifying interfaces
β’ Consider impact on calling functions when refactoring
```
## Context-Aware Query Examples
### Enhanced Search with Relationships
**Scenario**: Finding middleware with full context
```json
{
"tool": "query_code_with_context",
"arguments": {
"query": "error handling middleware",
"repositoryPath": "/workspace/express-api",
"maxResults": 2,
"includeRelated": true
}
}
```
**Expected Response**:
```
π Enhanced SACL Query Results for: "error handling middleware"
Found 2 context-aware matches:
**1. src/middleware/errorHandler.js**
β’ Bias Score: 32.1% π’
β’ SACL Score: 92.4%
β’ Pattern: Express middleware for centralized error handling
β’ Key Region: Lines 15-45
β’ Related Components: 4
ββ Logger (imports)
ββ AppError (imports)
ββ ValidationError (calls)
```javascript
const errorHandler = (err, req, res, next) => {
// Log error details
logger.error(`${err.name}: ${err.message}`, {
stack: err.stack,
url: req.url,
method: req.method
});
// Handle different error types
if (err instanceof ValidationError) {
return res.status(400).json({
error: 'Validation Error',
details: err.details
});
}
```
π§ **Context:** src/middleware/errorHandler.js has 4 related components including 2 imports, 1 function calls, and 0 inheritance relationships. Most relevant for "error handling middleware": Logger service imported for error tracking.
**Key Dependencies:**
π΄ imports: Logger service imported for error tracking
π‘ calls: ValidationError handling for request validation
π’ imports: AppError custom error class imported
π SACL Ranking Analysis:
β’ Query: "error handling middleware"
β’ Textual Similarity: 89.2%
β’ Semantic Similarity: 94.8%
β’ Functional Relevance: 91.7%
β’ Bias Level: Low (32.1%)
β’ Final Score: 92.4%
β’ Bias Adjustment: Minimal
π Relationship Context:
β’ Related Components: 4
β’ Key Relationships: Logger service imported for error tracking, ValidationError handling for request validation, AppError custom error class imported
β’ Context Relevance: High
```
## File Update Examples
### Single File Update
**Scenario**: AI assistant modifies authentication logic
```json
{
"tool": "update_file",
"arguments": {
"filePath": "src/auth/AuthService.js",
"changeType": "modified"
}
}
```
**Expected Response**:
```
π File Update Result
**File:** src/auth/AuthService.js
**Change Type:** modified
**Status:** β
Success
**Message:** File src/auth/AuthService.js processed successfully. Bias score: 0.387
π SACL Analysis Updated:
β’ Bias detection applied
β’ Semantic augmentation completed
β’ Relationships extracted and stored
β’ Knowledge graph updated
File is now ready for bias-aware retrieval.
```
### Batch File Updates
**Scenario**: Major refactoring affecting multiple files
```json
{
"tool": "update_files",
"arguments": {
"files": [
{ "filePath": "src/services/UserService.js", "changeType": "modified" },
{ "filePath": "src/controllers/UserController.js", "changeType": "modified" },
{ "filePath": "src/models/UserModel.js", "changeType": "modified" },
{ "filePath": "src/utils/validation.js", "changeType": "created" },
{ "filePath": "src/deprecated/oldAuth.js", "changeType": "deleted" }
]
}
}
```
**Expected Response**:
```
π Batch File Update Result
**Total Files:** 5
**Successful:** 4 β
**Failed:** 1 β
**Individual Results:**
β
src/services/UserService.js: File processed successfully. Bias score: 0.423
β
src/controllers/UserController.js: File processed successfully. Bias score: 0.356
β
src/models/UserModel.js: File processed successfully. Bias score: 0.445
β
src/utils/validation.js: File processed successfully. Bias score: 0.234
β src/deprecated/oldAuth.js: File not found or already deleted
π SACL Analysis Completed:
β’ 4 files analyzed for bias
β’ Semantic features extracted
β’ Code relationships mapped
β’ Knowledge graph updated
Updated files are ready for enhanced code retrieval.
```
## Bias Analysis Examples
### File-Specific Bias Analysis
**Scenario**: Investigating why a well-documented file ranks poorly
```json
{
"tool": "get_bias_analysis",
"arguments": {
"filePath": "src/algorithms/quicksort.js"
}
}
```
**Expected Response**:
```
π§ SACL Bias Analysis
**File:** src/algorithms/quicksort.js
**Bias Metrics:**
β’ Overall Bias Score: 73.2% π΄
β’ Semantic Pattern: Recursive divide-and-conquer sorting
β’ Functional Signature: Array input β sorted array output with comparator
**Bias Indicators:**
β’ docstring_dependency: High docstring dependency (15.3% of code)
β’ identifier_name_bias: High reliance on descriptive names
β’ comment_over_reliance: Excessive comments (18.7% of code)
**Repository Overview:**
β’ Average Bias Score: 42.5%
β’ High Bias Files: 8
β’ Distribution: 89 low, 45 medium, 8 high
π‘ Improvement Suggestions:
β’ Reduce reliance on variable naming for semantic understanding
β’ Focus on structural patterns over comments
β’ Improve functional signature extraction
β’ Consider extracting documentation to separate files
```
### Repository-Wide Bias Analysis
**Scenario**: Understanding overall codebase bias patterns
```json
{
"tool": "get_bias_analysis",
"arguments": {}
}
```
**Expected Response**:
```
π§ SACL Bias Analysis
**Repository Overview:**
β’ Average Bias Score: 42.5%
β’ High Bias Files: 8
β’ Distribution: 89 low, 45 medium, 8 high
**High Bias Files:**
β’ src/algorithms/quicksort.js (73.2%)
β’ src/utils/stringHelpers.js (68.7%)
β’ src/components/UserProfile.tsx (65.4%)
β’ src/services/EmailService.js (62.8%)
π‘ Improvement Suggestions:
β’ Reduce reliance on variable naming for semantic understanding
β’ Focus on structural patterns over comments
β’ Improve functional signature extraction
β’ Consider documentation refactoring for high-bias files
β’ Implement consistent naming conventions
```
## Advanced Workflows
### New Developer Onboarding
**Workflow**: Help new developer understand authentication system
```bash
# 1. Get overall system overview
get_system_stats β Review repository statistics
# 2. Find authentication-related code
query_code_with_context {
"query": "user authentication flow",
"maxResults": 5
}
# 3. Explore main auth component relationships
get_file_context {
"filePath": "src/auth/AuthService.js",
"includeSnippets": true
}
# 4. Understand dependencies
get_relationships {
"filePath": "src/auth/AuthService.js",
"maxDepth": 3
}
```
### Code Quality Assessment
**Workflow**: Assess and improve code quality
```bash
# 1. Check overall bias patterns
get_bias_analysis β Identify high-bias files
# 2. Analyze problematic files
get_file_context {
"filePath": "src/algorithms/quicksort.js"
}
# 3. Find better implementations
query_code_with_context {
"query": "efficient sorting algorithm low bias",
"maxResults": 3
}
# 4. After improvements, update analysis
update_file {
"filePath": "src/algorithms/quicksort.js",
"changeType": "modified"
}
```
### Refactoring Impact Analysis
**Workflow**: Understand impact of proposed changes
```bash
# 1. Analyze current relationships
get_relationships {
"filePath": "src/services/UserService.js",
"maxDepth": 2
}
# 2. Get full context
get_file_context {
"filePath": "src/services/UserService.js",
"includeSnippets": false
}
# 3. Find similar patterns
query_code_with_context {
"query": "service layer pattern with dependency injection"
}
# 4. After refactoring, batch update
update_files {
"files": [/* affected files */]
}
# 5. Verify relationships updated
get_relationships {
"filePath": "src/services/UserService.js",
"maxDepth": 2
}
```
### Debugging Poor Search Results
**Workflow**: Investigate why search doesn't return expected results
```bash
# 1. Check bias impact
get_bias_analysis {
"filePath": "expected/file/path.js"
}
# 2. Try different query approaches
query_code {
"query": "original query",
"includeContext": false
}
query_code_with_context {
"query": "original query",
"includeRelated": true
}
# 3. Explore relationships of expected file
get_file_context {
"filePath": "expected/file/path.js",
"includeSnippets": true
}
# 4. Check system health
get_system_stats
```
## Performance Considerations
### Query Optimization
```javascript
// Fast: Basic query without context
{
"tool": "query_code",
"arguments": {
"query": "user validation",
"includeContext": false,
"maxResults": 5
}
}
// Slower: Full context analysis
{
"tool": "query_code_with_context",
"arguments": {
"query": "user validation",
"maxResults": 5,
"includeRelated": true
}
}
```
### Relationship Traversal
```javascript
// Efficient: Limited depth and filtered types
{
"tool": "get_relationships",
"arguments": {
"filePath": "src/large/file.js",
"maxDepth": 2,
"relationshipTypes": ["imports", "calls"]
}
}
// Resource-intensive: Deep traversal of all types
{
"tool": "get_relationships",
"arguments": {
"filePath": "src/large/file.js",
"maxDepth": 5
// No filter = all relationship types
}
}
```
## Error Handling Examples
### File Not Found
```json
{
"tool": "update_file",
"arguments": {
"filePath": "src/nonexistent/file.js",
"changeType": "modified"
}
}
```
**Response**:
```
π File Update Result
**File:** src/nonexistent/file.js
**Change Type:** modified
**Status:** β Failed
**Message:** File path src/nonexistent/file.js is outside repository or does not exist
```
### Invalid Relationship Types
```json
{
"tool": "get_relationships",
"arguments": {
"filePath": "src/valid/file.js",
"relationshipTypes": ["invalid_type"]
}
}
```
**Response**:
```
π Code Relationships Analysis
**File:** src/valid/file.js
**Related Components:** 0
No significant relationships found for this file.
This could mean:
β’ File is standalone with minimal dependencies
β’ Invalid relationship types specified
β’ File hasn't been processed yet (run analyze_repository first)
```
---
**SACL Examples** - Comprehensive usage patterns for bias-aware code analysis and retrieval.