Skip to main content
Glama

optimize_readme

Restructures and condenses README files by extracting detailed sections into separate documentation, focusing on clarity and conciseness for better project understanding.

Instructions

Optimize README content by restructuring, condensing, and extracting detailed sections to separate documentation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
readme_pathYesPath to the README file to optimize
strategyNoOptimization strategycommunity_focused
max_lengthNoTarget maximum length in lines
include_tldrNoGenerate and include TL;DR section
preserve_existingNoPreserve existing content structure where possible
output_pathNoPath to write optimized README (if not specified, returns content only)
create_docs_directoryNoCreate docs/ directory for extracted content

Implementation Reference

  • The main handler function `optimizeReadme` that implements the core logic of the `optimize_readme` MCP tool. It handles input validation, README parsing, TL;DR generation, section extraction, documentation structure creation, and returns structured results in MCPToolResponse format.
    export async function optimizeReadme(
      input: Partial<OptimizeReadmeInput>,
    ): Promise<
      MCPToolResponse<{ optimization: OptimizationResult; nextSteps: string[] }>
    > {
      const startTime = Date.now();
    
      try {
        // Validate input
        const validatedInput = OptimizeReadmeInputSchema.parse(input);
        const {
          readme_path,
          strategy,
          max_length,
          include_tldr,
          output_path,
          create_docs_directory,
        } = validatedInput;
    
        // Read original README
        const originalContent = await fs.readFile(readme_path, "utf-8");
        const originalLength = originalContent.split("\n").length;
    
        // Parse README structure
        const sections = parseReadmeStructure(originalContent);
    
        // Generate TL;DR if requested
        const tldrGenerated = include_tldr
          ? generateTldr(originalContent, sections)
          : null;
    
        // Identify sections to extract
        const extractedSections = identifySectionsToExtract(
          sections,
          strategy,
          max_length,
        );
    
        // Create basic optimization result
        const optimizedContent =
          originalContent +
          "\n\n## TL;DR\n\n" +
          (tldrGenerated || "Quick overview of the project.");
        const restructuringChanges = [
          {
            type: "added" as const,
            section: "TL;DR",
            description: "Added concise project overview",
            impact: "Helps users quickly understand project value",
          },
        ];
    
        const optimizedLength = optimizedContent.split("\n").length;
        const reductionPercentage = Math.round(
          ((originalLength - optimizedLength) / originalLength) * 100,
        );
    
        // Create docs directory and extract detailed content if requested
        if (create_docs_directory && extractedSections.length > 0) {
          await createDocsStructure(path.dirname(readme_path), extractedSections);
        }
    
        // Write optimized README if output path specified
        if (output_path) {
          await fs.writeFile(output_path, optimizedContent, "utf-8");
        }
    
        const recommendations = generateOptimizationRecommendations(
          originalLength,
          optimizedLength,
          extractedSections,
          strategy,
        );
    
        const optimization: OptimizationResult = {
          originalLength,
          optimizedLength,
          reductionPercentage,
          optimizedContent,
          extractedSections,
          tldrGenerated,
          restructuringChanges,
          recommendations,
        };
    
        const nextSteps = generateOptimizationNextSteps(
          optimization,
          validatedInput,
        );
    
        return {
          success: true,
          data: {
            optimization,
            nextSteps,
          },
          metadata: {
            toolVersion: "1.0.0",
            executionTime: Date.now() - startTime,
            timestamp: new Date().toISOString(),
          },
        };
      } catch (error) {
        return {
          success: false,
          error: {
            code: "OPTIMIZATION_FAILED",
            message: "Failed to optimize README",
            details: error instanceof Error ? error.message : "Unknown error",
            resolution: "Check README file path and permissions",
          },
          metadata: {
            toolVersion: "1.0.0",
            executionTime: Date.now() - startTime,
            timestamp: new Date().toISOString(),
          },
        };
      }
    }
  • Zod input schema `OptimizeReadmeInputSchema` and TypeScript type `OptimizeReadmeInput` defining the parameters for the `optimize_readme` tool, including README path, optimization strategy, length limits, and output options.
    const OptimizeReadmeInputSchema = z.object({
      readme_path: z.string().min(1, "README path is required"),
      strategy: z
        .enum([
          "community_focused",
          "enterprise_focused",
          "developer_focused",
          "general",
        ])
        .optional()
        .default("community_focused"),
      max_length: z.number().min(50).max(1000).optional().default(300),
      include_tldr: z.boolean().optional().default(true),
      preserve_existing: z.boolean().optional().default(false),
      output_path: z.string().optional(),
      create_docs_directory: z.boolean().optional().default(true),
    });
  • Helper function `parseReadmeStructure` that parses the README markdown into structured sections (title, content, level, lines, word count, essential flag) used for analysis and optimization decisions.
    function parseReadmeStructure(content: string): ReadmeSection[] {
      const lines = content.split("\n");
      const sections: ReadmeSection[] = [];
      let currentTitle = "";
      let currentLevel = 0;
      let currentStartLine = 0;
    
      lines.forEach((line, index) => {
        const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
    
        if (headingMatch) {
          // Save previous section
          if (currentTitle) {
            const endLine = index - 1;
            const sectionContent = lines
              .slice(currentStartLine, endLine + 1)
              .join("\n");
            const wordCount = sectionContent.split(/\s+/).length;
            const isEssential = isEssentialSection(currentTitle);
    
            sections.push({
              title: currentTitle,
              content: sectionContent,
              level: currentLevel,
              startLine: currentStartLine,
              endLine: endLine,
              wordCount: wordCount,
              isEssential: isEssential,
            });
          }
    
          // Start new section
          currentTitle = headingMatch[2].trim();
          currentLevel = headingMatch[1].length;
          currentStartLine = index;
        }
      });
    
      // Add final section
      if (currentTitle) {
        const endLine = lines.length - 1;
        const sectionContent = lines
          .slice(currentStartLine, endLine + 1)
          .join("\n");
        const wordCount = sectionContent.split(/\s+/).length;
        const isEssential = isEssentialSection(currentTitle);
    
        sections.push({
          title: currentTitle,
          content: sectionContent,
          level: currentLevel,
          startLine: currentStartLine,
          endLine: endLine,
          wordCount: wordCount,
          isEssential: isEssential,
        });
      }
    
      return sections;
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions actions like 'restructuring, condensing, and extracting,' but doesn't specify whether this is a read-only or destructive operation, what permissions are needed, or how outputs are handled (e.g., file writes vs. content return). For a tool with 7 parameters and no annotations, this is a significant gap in transparency.

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

Conciseness5/5

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

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It is front-loaded with the core action and avoids redundancy, making it easy for an agent to parse quickly. Every part of the sentence earns its place by specifying key operations.

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 the tool's complexity (7 parameters, no annotations, no output schema), the description is insufficient. It lacks details on behavioral traits (e.g., whether it modifies files or returns content), output handling, and how it differs from sibling tools. Without this context, an agent may struggle to use it correctly in a server with many documentation-related tools.

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?

The schema description coverage is 100%, meaning all parameters are documented in the schema itself. The description doesn't add any parameter-specific details beyond what the schema provides (e.g., it doesn't explain the 'strategy' enum options or how 'max_length' interacts with restructuring). With high schema coverage, the baseline score of 3 is appropriate, as the description doesn't compensate with extra semantic value.

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 tool's purpose: 'Optimize README content by restructuring, condensing, and extracting detailed sections to separate documentation.' This specifies the verb ('optimize') and resource ('README content') with concrete actions. However, it doesn't explicitly differentiate from sibling tools like 'analyze_readme' or 'evaluate_readme_health,' which prevents a score of 5.

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?

The description provides no guidance on when to use this tool versus alternatives. With many sibling tools related to READMEs and documentation (e.g., 'analyze_readme,' 'evaluate_readme_health,' 'generate_readme_template'), there is no indication of context, prerequisites, or exclusions. This leaves the agent without usage direction.

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

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/tosin2013/documcp'

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