Skip to main content
Glama

context_gc

Trigger context garbage collection to free up context window space by clearing unused file skeletons. Ideal after completing tasks or when context is full.

Instructions

Trigger context garbage collection. This clears cached file skeletons that are no longer needed, freeing context window space. Use this when you notice context is getting full or after completing a task branch.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
strategyNoGC strategy: 'lru' evicts least-recently-used entries, 'all' clears everything, 'older-than' clears entries older than TTL. Default: 'lru'.
ttlMinutesNoOnly used with 'older-than' strategy. Entries older than this many minutes will be evicted. Default: 30.

Implementation Reference

  • Handler function for the context_gc tool. Takes optional 'strategy' (lru/all/older-than) and 'ttlMinutes' params, calls cache.gc(), and returns a result message.
    server.tool(
      "context_gc",
      "Trigger context garbage collection. This clears cached file skeletons that are no longer needed, freeing context window space. Use this when you notice context is getting full or after completing a task branch.",
      {
        strategy: z.enum(["lru", "all", "older-than"]).optional().describe("GC strategy: 'lru' evicts least-recently-used entries, 'all' clears everything, 'older-than' clears entries older than TTL. Default: 'lru'."),
        ttlMinutes: z.number().optional().describe("Only used with 'older-than' strategy. Entries older than this many minutes will be evicted. Default: 30."),
      },
      async (args): Promise<ToolResult> => {
        const evicted = cache.gc(args.strategy ?? "lru", args.ttlMinutes);
        return {
          content: [{
            type: "text",
            text: `[ContextGC] GC complete. Evicted ${evicted} cache entries. Cache now has ${cache.size} entries.`,
          }],
        };
      }
    );
  • CacheManager.gc() method that implements the actual garbage collection logic. Supports three strategies: 'all' (clear everything), 'older-than' (evict entries older than cutoff), and 'lru' (evicts entries last accessed before the cutoff time).
    gc(strategy: "lru" | "all" | "older-than", ttlMinutes?: number): number {
      let evicted = 0;
      if (strategy === "all") {
        evicted = this.cache.size;
        this.cache.clear();
        this.totalBytes = 0;
      } else if (strategy === "older-than" || strategy === "lru") {
        const cutoff = Date.now() - (ttlMinutes ?? 30) * 60 * 1000;
        for (const [key, entry] of this.cache) {
          if (entry.lastAccessedAt < cutoff) {
            this.evict(key);
            evicted++;
          }
        }
      }
      return evicted;
    }
  • Registration of the context_gc tool via server.tool() in registerAllTools().
    // ─────────────────────────────────────────────────────
    // Tool 4: context_gc
    // ─────────────────────────────────────────────────────
    server.tool(
      "context_gc",
      "Trigger context garbage collection. This clears cached file skeletons that are no longer needed, freeing context window space. Use this when you notice context is getting full or after completing a task branch.",
      {
        strategy: z.enum(["lru", "all", "older-than"]).optional().describe("GC strategy: 'lru' evicts least-recently-used entries, 'all' clears everything, 'older-than' clears entries older than TTL. Default: 'lru'."),
        ttlMinutes: z.number().optional().describe("Only used with 'older-than' strategy. Entries older than this many minutes will be evicted. Default: 30."),
      },
      async (args): Promise<ToolResult> => {
        const evicted = cache.gc(args.strategy ?? "lru", args.ttlMinutes);
        return {
          content: [{
            type: "text",
            text: `[ContextGC] GC complete. Evicted ${evicted} cache entries. Cache now has ${cache.size} entries.`,
          }],
        };
      }
    );
  • Configuration schema including cache settings (maxEntries, maxTotalBytes, ttlMs) which define the constraints within which the GC operates.
    export interface ContextGCConfig {
      parser: {
        maxFileSizeBytes: number;
        parseTimeoutMs: number;
      };
      skeleton: {
        preserveComments: "none" | "doc" | "all";
        preserveTypes: boolean;
        preserveImports: boolean;
        preserveExports: boolean;
        maxOutputLines: number;
      };
      cache: {
        maxEntries: number;
        maxTotalBytes: number;
        ttlMs: number;
      };
      logTrimmer: {
        maxFrames: number;
        filterPatterns: string[];
      };
      enabled: boolean;
      logLevel: string;
    }
Behavior4/5

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

No annotations are provided, so the description bears full responsibility. It discloses the tool clears cached file skeletons and frees context window space, and mentions the effect of different strategies. It does not warn of any destructive side effects, but the described action is a non-destructive cleanup of 'no longer needed' items.

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 two sentences long: the first states what it does, the second gives usage guidance. It is front-loaded and concise, with no superfluous information.

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

Completeness3/5

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

The tool has two well-documented parameters and no output schema. The description covers the purpose and usage, but does not mention what the tool returns (e.g., success status). Given the complexity, this is a minor gap.

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 input schema has 100% coverage with detailed descriptions for both parameters, including enum values and defaults. The description does not add significant new information beyond what is already in the schema, so a score of 3 is appropriate.

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

Purpose5/5

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

The description clearly states 'Trigger context garbage collection' with the specific verb 'trigger' and resource 'context garbage collection'. It further explains it clears cached file skeletons to free context window space, which is distinct from sibling tools like parse_error_log or read_code_skeleton.

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

Usage Guidelines4/5

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

The description explicitly tells when to use: 'when you notice context is getting full or after completing a task branch.' It doesn't cover when not to use or alternatives, but the context is clear and sufficient give the tool's specific purpose.

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/superZavier/contextgc-mcp'

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