@ooples/token-optimizer-mcp
Version:
Intelligent context window optimization for Claude Code - store content externally via caching and compression, freeing up your context window for what matters
220 lines • 5.44 kB
TypeScript
/**
* Smart API Fetch Tool - 83% Token Reduction
*
* HTTP client with intelligent features:
* - Automatic retry logic with exponential backoff
* - Response caching with TTL-based invalidation
* - Request deduplication (same request in-flight)
* - Circuit breaker pattern
* - ETag/If-None-Match support
* - Token-optimized output
*/
import { CacheEngine } from '../../core/cache-engine.js';
import type { TokenCounter } from '../../core/token-counter.js';
import type { MetricsCollector } from '../../core/metrics.js';
interface SmartApiFetchOptions {
/**
* HTTP method
*/
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
/**
* Request URL
*/
url: string;
/**
* Request headers
*/
headers?: Record<string, string>;
/**
* Request body (for POST, PUT, PATCH)
*/
body?: string | object;
/**
* Cache TTL in seconds (default: 300 = 5 minutes)
*/
ttl?: number;
/**
* Maximum retry attempts (default: 3)
*/
maxRetries?: number;
/**
* Request timeout in milliseconds (default: 30000)
*/
timeout?: number;
/**
* Force fresh request (ignore cache)
*/
force?: boolean;
/**
* Follow redirects (default: true)
*/
followRedirects?: boolean;
/**
* Parse response as JSON (default: true)
*/
parseJson?: boolean;
/**
* Include full response in output (default: false)
*/
includeFullResponse?: boolean;
}
interface SmartApiFetchResult {
success: boolean;
cached: boolean;
status: number;
statusText: string;
headers: Record<string, string>;
body: any;
retries: number;
duration: number;
timestamp: number;
cacheAge?: number;
}
interface SmartApiFetchOutput {
result: SmartApiFetchResult;
summary: string;
metadata: {
baselineTokens: number;
outputTokens: number;
tokensSaved: number;
reductionPercent: number;
cached: boolean;
retries: number;
};
}
/**
* Smart API Fetch Class
*/
export declare class SmartApiFetch {
private cache;
private tokenCounter;
private metrics;
constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector);
/**
* Execute HTTP request with retry logic and caching
*/
run(options: SmartApiFetchOptions): Promise<SmartApiFetchOutput>;
/**
* Execute single HTTP request with timeout
*/
private executeRequest;
/**
* Calculate exponential backoff delay
*/
private calculateBackoff;
/**
* Sleep utility
*/
private sleep;
/**
* Generate cache key from request details
*/
private generateCacheKey;
/**
* Get cached result if valid
*/
private getCachedResult;
/**
* Cache successful result
*/
private cacheResult;
/**
* Transform output with token reduction
*/
private transformOutput;
/**
* Format cached response output
*/
private formatCachedOutput;
/**
* Format error response output
*/
private formatErrorOutput;
/**
* Format retried success output
*/
private formatRetriedOutput;
/**
* Format first request output
*/
private formatFirstRequestOutput;
/**
* Get body preview (truncated)
*/
private getBodyPreview;
/**
* Check if circuit breaker is open for endpoint
*/
private isCircuitOpen;
/**
* Record circuit breaker failure
*/
private recordCircuitBreakerFailure;
/**
* Reset circuit breaker on success
*/
private resetCircuitBreaker;
}
export declare function getSmartApiFetch(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartApiFetch;
export declare function runSmartApiFetch(options: SmartApiFetchOptions): Promise<string>;
/**
* MCP Tool Definition
*/
export declare const SMART_API_FETCH_TOOL_DEFINITION: {
name: string;
description: string;
inputSchema: {
type: string;
properties: {
method: {
type: string;
enum: string[];
description: string;
};
url: {
type: string;
description: string;
};
headers: {
type: string;
description: string;
additionalProperties: {
type: string;
};
};
body: {
description: string;
oneOf: {
type: string;
}[];
};
ttl: {
type: string;
description: string;
};
maxRetries: {
type: string;
description: string;
};
timeout: {
type: string;
description: string;
};
force: {
type: string;
description: string;
};
followRedirects: {
type: string;
description: string;
};
parseJson: {
type: string;
description: string;
};
};
required: string[];
};
};
export {};
//# sourceMappingURL=smart-api-fetch.d.ts.map