Skip to main content
Glama
jghidalgo

Lambda Performance MCP Server

by jghidalgo

analyze_memory_utilization

Analyze AWS Lambda function memory usage to identify optimization opportunities and provide right-sizing recommendations for improved performance.

Instructions

Analyze memory utilization and provide right-sizing recommendations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
functionNameYesName of the Lambda function
timeRangeNoTime range for memory analysis (default: 24h)

Implementation Reference

  • Core handler implementing the memory utilization analysis, fetching config, logs, calculating stats, and generating recommendations.
    async analyzeMemoryUtilization(functionName, timeRange) {
      const timeRangeMs = this.parseTimeRange(timeRange);
      const endTime = new Date();
      const startTime = new Date(endTime.getTime() - timeRangeMs);
    
      // Get function configuration
      const config = await this.getFunctionConfig(functionName);
      const allocatedMemory = config.MemorySize;
    
      // Get memory usage from logs
      const memoryUsage = await this.getMemoryUsageFromLogs(functionName, startTime, endTime);
      
      const avgUsed = memoryUsage.reduce((sum, usage) => sum + usage, 0) / memoryUsage.length || 0;
      const peakUsed = Math.max(...memoryUsage, 0);
      const minUsed = Math.min(...memoryUsage, allocatedMemory);
      const utilizationPercent = Math.round((avgUsed / allocatedMemory) * 100);
    
      // Generate recommendation
      const recommendation = this.generateMemoryRecommendation(
        allocatedMemory, 
        avgUsed, 
        peakUsed, 
        utilizationPercent
      );
    
      return {
        allocated: allocatedMemory,
        avgUsed: Math.round(avgUsed),
        peakUsed: Math.round(peakUsed),
        minUsed: Math.round(minUsed),
        utilizationPercent,
        recommended: recommendation.memory,
        reasoning: recommendation.reasoning,
        performanceImpact: recommendation.performanceImpact,
        costImpact: recommendation.costImpact,
        patterns: this.analyzeMemoryPatterns(memoryUsage)
      };
    }
  • MCP server handler wrapper that invokes the analyzer and formats the response for the MCP protocol.
    async analyzeMemoryUtilization(args) {
      const { functionName, timeRange = '24h' } = args;
      
      const memoryAnalysis = await this.lambdaAnalyzer.analyzeMemoryUtilization(
        functionName, 
        timeRange
      );
    
      return {
        content: [
          {
            type: 'text',
            text: `# Memory Utilization Analysis: ${functionName}\n\n` +
                  `## Current Configuration\n` +
                  `- **Allocated Memory**: ${memoryAnalysis.allocated}MB\n` +
                  `- **Average Used**: ${memoryAnalysis.avgUsed}MB (${memoryAnalysis.utilizationPercent}%)\n` +
                  `- **Peak Usage**: ${memoryAnalysis.peakUsed}MB\n` +
                  `- **Minimum Usage**: ${memoryAnalysis.minUsed}MB\n\n` +
                  `## Right-sizing Recommendation\n` +
                  `- **Recommended Memory**: ${memoryAnalysis.recommended}MB\n` +
                  `- **Reasoning**: ${memoryAnalysis.reasoning}\n` +
                  `- **Expected Performance Impact**: ${memoryAnalysis.performanceImpact}\n` +
                  `- **Cost Impact**: ${memoryAnalysis.costImpact}\n\n` +
                  `## Memory Usage Patterns\n` +
                  `${memoryAnalysis.patterns.map(pattern => `- ${pattern}`).join('\n')}`
          }
        ]
      };
    }
  • index.js:148-164 (registration)
    Tool registration in the MCP server's listTools response, including name, description, and input schema.
    name: 'analyze_memory_utilization',
    description: 'Analyze memory utilization and provide right-sizing recommendations',
    inputSchema: {
      type: 'object',
      properties: {
        functionName: {
          type: 'string',
          description: 'Name of the Lambda function'
        },
        timeRange: {
          type: 'string',
          enum: ['1h', '6h', '24h', '7d'],
          description: 'Time range for memory analysis (default: 24h)'
        }
      },
      required: ['functionName']
    }
  • Helper method that generates memory right-sizing recommendations based on utilization stats.
    generateMemoryRecommendation(allocated, avgUsed, peakUsed, utilizationPercent) {
      let recommendedMemory = allocated;
      let reasoning = '';
      let performanceImpact = 'No change expected';
      let costImpact = 'No change';
    
      if (utilizationPercent < 50) {
        // Over-provisioned
        recommendedMemory = Math.max(128, Math.ceil(peakUsed * 1.2 / 64) * 64);
        reasoning = 'Memory is over-provisioned. Reducing memory will lower costs.';
        costImpact = `Reduce costs by ~${Math.round((1 - recommendedMemory/allocated) * 100)}%`;
        performanceImpact = 'Minimal performance impact expected';
      } else if (utilizationPercent > 85) {
        // Under-provisioned
        recommendedMemory = Math.ceil(peakUsed * 1.3 / 64) * 64;
        reasoning = 'Memory utilization is high. Increasing memory may improve performance.';
        costImpact = `Increase costs by ~${Math.round((recommendedMemory/allocated - 1) * 100)}%`;
        performanceImpact = 'Improved performance and reduced duration expected';
      } else {
        reasoning = 'Memory allocation appears optimal for current usage patterns.';
      }
    
      return {
        memory: recommendedMemory,
        reasoning,
        performanceImpact,
        costImpact
      };
  • Helper to fetch memory usage data from CloudWatch logs (placeholder implementation).
    async getMemoryUsageFromLogs(functionName, startTime, endTime) {
      // Placeholder implementation - would parse CloudWatch logs for memory usage
      return Array.from({ length: 100 }, () => Math.floor(Math.random() * 200) + 100);
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jghidalgo/lambda-performance-mcp-nodejs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server