Skip to main content
Glama

analyze_resources

Audit website resources (images, JS, CSS, fonts) to identify optimization opportunities, improve performance, and streamline content delivery for specified devices.

Instructions

Analyze website resources (images, JS, CSS, fonts) for optimization opportunities

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
deviceNoDevice to emulate (default: desktop)desktop
minSizeNoMinimum resource size in KB to include
resourceTypesNoTypes of resources to analyze
urlYesURL to audit

Implementation Reference

  • Core handler function that runs Lighthouse audit, extracts network requests, categorizes resources by type, filters by size/types, computes summary statistics
    export async function analyzeResources(
      url: string,
      device: "desktop" | "mobile" = "desktop",
      resourceTypes?: string[],
      minSize = DEFAULTS.MIN_RESOURCE_SIZE_KB,
    ) {
      const runnerResult = await runRawLighthouseAudit(url, ["performance"], device);
      const { lhr } = runnerResult;
    
      // Get resource summary from network-requests audit
      const networkAudit = lhr.audits["network-requests"];
    
      if (!networkAudit || !networkAudit.details) {
        return {
          url: lhr.finalDisplayedUrl,
          device,
          resources: [],
          summary: {},
          fetchTime: lhr.fetchTime,
        };
      }
    
      const resources = (networkAudit.details.items || [])
        .map((item: Record<string, unknown>) => {
          const sizeKB = ((item.transferSize as number) || 0) / 1024;
          const resourceType = categorizeResourceType(item);
    
          return {
            url: item.url as string,
            resourceType,
            transferSize: (item.transferSize as number) || 0,
            resourceSize: (item.resourceSize as number) || 0,
            sizeKB: Math.round(sizeKB * 100) / 100,
            mimeType: item.mimeType as string,
          };
        })
        .filter((resource: { sizeKB: number; resourceType: string }) => {
          if (minSize && resource.sizeKB < minSize) return false;
          if (resourceTypes && !resourceTypes.includes(resource.resourceType)) return false;
          return true;
        });
    
      // Create summary by resource type
      const summary = resources.reduce(
        (
          acc: Record<string, { count: number; totalSize: number }>,
          resource: { resourceType: string; transferSize: number },
        ) => {
          if (!acc[resource.resourceType]) {
            acc[resource.resourceType] = { count: 0, totalSize: 0 };
          }
          acc[resource.resourceType].count++;
          acc[resource.resourceType].totalSize += resource.transferSize;
          return acc;
        },
        {},
      );
    
      return {
        url: lhr.finalDisplayedUrl,
        device,
        resources,
        summary,
        fetchTime: lhr.fetchTime,
      };
    }
  • Input schema (Zod) for the analyze_resources tool defining parameters: url, device, resourceTypes, minSize
    export const resourceAnalysisSchema = {
      url: baseSchemas.url,
      device: baseSchemas.device,
      resourceTypes: z
        .array(z.enum(["images", "javascript", "css", "fonts", "other"]))
        .optional()
        .describe("Types of resources to analyze"),
      minSize: z.number().min(0).optional().describe("Minimum resource size in KB to include"),
    };
  • MCP tool registration for 'analyze_resources': sets name, description, schema, and inline handler that calls core analysis and returns formatted content with optimizations/recommendations
    server.tool(
      "analyze_resources",
      "Analyze website resources (images, JS, CSS, fonts) for optimization opportunities",
      resourceAnalysisSchema,
      async ({ url, device, resourceTypes, minSize }) => {
        try {
          const result = await analyzeResources(url, device, resourceTypes, minSize);
    
          // Create structured, AI-friendly response
          const analysisData = {
            url: result.url,
            device: result.device,
            timestamp: result.fetchTime,
            filters: {
              resourceTypes: resourceTypes || ["all"],
              minSizeKB: minSize || 0,
            },
            summary: {
              totalResources: result.resources.length,
              totalSizeKB:
                Math.round((Object.values(result.summary).reduce((sum, data) => sum + data.totalSize, 0) / 1024) * 100) /
                100,
              resourceCounts: Object.fromEntries(
                Object.entries(result.summary).map(([type, data]) => [
                  type,
                  {
                    count: data.count,
                    sizeKB: Math.round((data.totalSize / 1024) * 100) / 100,
                  },
                ]),
              ),
            },
            resources: result.resources.slice(0, 50).map((resource) => ({
              filename: resource.url.split("/").pop() || resource.url,
              type: resource.resourceType,
              sizeKB: Math.round(resource.sizeKB * 100) / 100,
              mimeType: resource.mimeType || "unknown",
              url: resource.url,
            })),
            optimization: {
              recommendations: [] as string[],
              priorities: [] as string[],
            },
          };
    
          // Add type-specific recommendations
          if (result.summary.images) {
            analysisData.optimization.recommendations.push("Convert images to WebP/AVIF formats");
            analysisData.optimization.recommendations.push("Implement lazy loading for images");
            analysisData.optimization.priorities.push("images");
          }
          if (result.summary.javascript) {
            analysisData.optimization.recommendations.push("Minify and compress JavaScript files");
            analysisData.optimization.recommendations.push("Remove unused JavaScript code");
            analysisData.optimization.priorities.push("javascript");
          }
          if (result.summary.css) {
            analysisData.optimization.recommendations.push("Minify CSS and remove unused styles");
            analysisData.optimization.priorities.push("css");
          }
          if (result.summary.fonts) {
            analysisData.optimization.recommendations.push("Use font-display: swap for better loading");
            analysisData.optimization.priorities.push("fonts");
          }
    
          if (analysisData.optimization.recommendations.length === 0) {
            analysisData.optimization.recommendations.push("Resource usage appears optimized");
          }
    
          // Add truncation info if needed
          if (result.resources.length > 50) {
            analysisData.resources.push({
              filename: `... and ${result.resources.length - 50} more resources`,
              type: "truncated",
              sizeKB: 0,
              mimeType: "info",
              url: "",
            });
          }
    
          const summary = `Analyzed ${result.resources.length} resources (${analysisData.summary.totalSizeKB}KB total) - ${analysisData.optimization.recommendations.length} optimization opportunities found`;
    
          return {
            content: createStructuredAnalysis("Resource Analysis", analysisData, summary),
          };
        } catch (error: unknown) {
          const errorMessage = error instanceof Error ? error.message : String(error);
          return {
            content: [
              {
                type: "text" as const,
                text: `Analysis failed: ${errorMessage}`,
              },
              {
                type: "text" as const,
                text: JSON.stringify(
                  {
                    error: true,
                    url,
                    device,
                    resourceTypes,
                    minSize,
                    message: errorMessage,
                    timestamp: new Date().toISOString(),
                  },
                  null,
                  2,
                ),
              },
            ],
            isError: true,
          };
        }
      },
    );
  • Helper function to categorize Lighthouse network resources into types: images, javascript, css, fonts, other based on resourceType or mimeType
    function categorizeResourceType(item: Record<string, unknown>): string {
      if (item.resourceType) {
        return (item.resourceType as string).toLowerCase();
      }
    
      if (item.mimeType) {
        const mimeType = item.mimeType as string;
        if (mimeType.startsWith("image/")) return "images";
        if (mimeType.includes("javascript")) return "javascript";
        if (mimeType.includes("css")) return "css";
        if (mimeType.includes("font")) return "fonts";
      }
    
      return "other";
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It mentions 'analyze' and 'optimization opportunities' but lacks behavioral details: it doesn't specify if this is a read-only operation, whether it makes network requests, what the output format is (e.g., report, list), or any rate limits/authentication needs. The description is vague on actual behavior beyond the high-level goal.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose. It avoids unnecessary words, though it could be slightly more structured (e.g., by explicitly mentioning parameters like URL). Every part earns its place by specifying what is analyzed and why.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations and no output schema, the description is incomplete for a tool with 4 parameters and behavioral complexity. It lacks details on what the analysis entails (e.g., performance metrics, recommendations), output format, error handling, or dependencies. For a resource analysis tool, this leaves significant gaps for an AI agent to understand how to use it effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters (url, device, minSize, resourceTypes) with descriptions and constraints. The description adds no additional parameter semantics beyond implying resource analysis, which is already covered by the schema's resourceTypes enum. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('analyze') and resource ('website resources') with specific examples (images, JS, CSS, fonts) and the goal ('for optimization opportunities'). It distinguishes from siblings like 'get_security_audit' or 'get_accessibility_score' by focusing on resource optimization, but doesn't explicitly differentiate from 'run_audit' or 'check_performance_budget' which might overlap.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance on when to use this tool versus alternatives. The description implies usage for optimization analysis, but doesn't specify prerequisites (e.g., requires a valid URL), exclusions (e.g., not for real-time monitoring), or direct comparisons to siblings like 'find_unused_javascript' or 'run_audit'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/danielsogl/lighthouse-mcp-server'

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