Skip to main content
Glama
FosterG4

Code Reference Optimizer MCP Server

by FosterG4

get_cached_context

Retrieve cached code context for a specified file path to optimize AI assistant token usage. Supports TypeScript, JavaScript, Python, Go, and Rust with minimal, relevant context extraction.

Instructions

Retrieve cached code context for a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cacheKeyNoOptional cache key for specific context
filePathYesPath to the source file

Implementation Reference

  • The MCP tool handler for 'get_cached_context'. Extracts parameters from args, logs the call, delegates to CodeReferenceOptimizer.getCachedContext, and returns the result as JSON-formatted text content.
    private async handleGetCachedContext(args: any) {
      const { filePath, cacheKey } = args;
      
      this.logger.info(`get_cached_context: filePath=${filePath} cacheKey=${cacheKey ?? ''}`);
      const result = await this.optimizer.getCachedContext(filePath, cacheKey);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2),
          },
        ],
      };
    }
  • src/index.ts:125-142 (registration)
    Tool registration in ListToolsRequestSchema handler, including name, description, and input schema definition.
    {
      name: 'get_cached_context',
      description: 'Retrieve previously extracted and cached code context for a file. Provides fast access to analyzed code structures without re-parsing. Useful for repeated queries on the same file or when working with large codebases where re-analysis would be expensive.',
      inputSchema: {
        type: 'object',
        properties: {
          filePath: {
            type: 'string',
            description: 'Path to the source file for which to retrieve cached context. Must match the path used in previous extract_code_context calls.',
          },
          cacheKey: {
            type: 'string',
            description: 'Optional specific cache key to retrieve a particular cached analysis. If omitted, returns the most recent cached context for the file.',
          },
        },
        required: ['filePath'],
      },
    },
  • Core implementation of getCachedContext in CodeReferenceOptimizer class. Retrieves specific cached context by key or the most recent/relevant one for the filePath from cacheManager.
    async getCachedContext(filePath: string, cacheKey?: string): Promise<CodeContext | null> {
      if (cacheKey) {
        return await this.cacheManager.get(cacheKey);
      }
      
      // Find most relevant cached context for the file
      const allCached = await this.cacheManager.getByFilePath(filePath);
      if (allCached.length === 0) {
        return null;
      }
      
      // Return the most recent and relevant cached context
      const sortedCached = (allCached || []).sort((a, b) => {
        const scoreA = a.relevanceScore * (1 - this.getAgeWeight(a.timestamp));
        const scoreB = b.relevanceScore * (1 - this.getAgeWeight(b.timestamp));
        return scoreB - scoreA;
      });
      return sortedCached.length > 0 ? (sortedCached[0] as CodeContext) : null;
    }
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/FosterG4/mcpsaver'

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