Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import { LinearClient } from '@linear/sdk';
import { runMCPServer } from './mcp-server.js';

import { LinearService } from './services/linear-service.js';
import { CachedLinearService } from './services/cached-linear-service.js';
import { allToolDefinitions } from './tools/definitions/index.js';
import { cacheToolDefinitions } from './tools/definitions/cache-tools.js';
import { registerToolHandlers } from './tools/handlers/index.js';
import { registerCacheHandlers } from './tools/handlers/cache-handlers.js';
import { getLinearApiToken, logInfo, logError } from './utils/config.js';
import pkg from '../package.json' with { type: 'json' }; // Import package.json to access version

Expand All @@ -26,22 +28,37 @@ async function runServer() {
);
}

logInfo(`Starting MCP Linear...`);
// Cache config from env (opt-out: enabled by default)
const cacheEnabled = process.env.LINEAR_CACHE_ENABLED !== 'false';
const cacheMaxSize = parseInt(process.env.LINEAR_CACHE_MAX_SIZE || '500', 10);

// Initialize Linear client and service
logInfo(`Starting MCP Linear... (cache: ${cacheEnabled ? 'on' : 'off'}, max: ${cacheMaxSize})`);

// Initialize Linear client and cached service
const linearClient = new LinearClient({ apiKey: linearApiToken });
const linearService = new LinearService(linearClient);
const linearService = new CachedLinearService(linearClient, cacheEnabled, cacheMaxSize);

// Merge tool definitions: original + cache management tools
const allTools = [...allToolDefinitions, ...cacheToolDefinitions];

// Start the MCP server
const server = await runMCPServer({
tools: allToolDefinitions,
tools: allTools,
handleInitialize: async () => {
logInfo('MCP Linear initialized successfully.');
return {
tools: allToolDefinitions,
tools: allTools,
};
},
handleRequest: async (req: { name: string; args: unknown }) => {
// Check cache handlers first
const cacheHandlers = registerCacheHandlers(linearService);
if (req.name in cacheHandlers) {
const handler = cacheHandlers[req.name as keyof typeof cacheHandlers];
return await handler(req.args);
}

// Then check regular handlers
const handlers = registerToolHandlers(linearService);
const toolName = req.name;

Expand Down
168 changes: 168 additions & 0 deletions src/services/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/**
* Simple in-memory TTL cache with LRU eviction.
* Zero external dependencies.
*
* Reduces Linear API calls by 50-90% for repeated reads within TTL windows.
* Each resource type has an appropriate TTL based on how frequently it changes.
*/

interface CacheEntry<T> {
value: T;
expiresAt: number;
createdAt: number;
}

export interface CacheStats {
hits: number;
misses: number;
size: number;
maxSize: number;
hitRate: string;
entries: { key: string; ttl: number; age: number }[];
}

/** TTL values in milliseconds by resource type */
export const TTL = {
/** Teams rarely change */
TEAMS: 60 * 60 * 1000, // 1 hour
/** Users stable, roles change occasionally */
USERS: 30 * 60 * 1000, // 30 min
/** Workflow states very stable */
WORKFLOW_STATES: 60 * 60 * 1000, // 1 hour
/** Labels moderately stable */
LABELS: 30 * 60 * 1000, // 30 min
/** Org info nearly static */
ORGANIZATION: 60 * 60 * 1000, // 1 hour
/** Viewer info stable within session */
VIEWER: 30 * 60 * 1000, // 30 min
/** Projects metadata somewhat stable */
PROJECTS: 5 * 60 * 1000, // 5 min
/** Cycles change during planning */
CYCLES: 5 * 60 * 1000, // 5 min
/** Issue lists change frequently */
ISSUE_LIST: 2 * 60 * 1000, // 2 min
/** Single issue with comments */
ISSUE_SINGLE: 60 * 1000, // 1 min
/** Search results query-dependent */
SEARCH: 60 * 1000, // 1 min
/** Initiatives somewhat stable */
INITIATIVES: 5 * 60 * 1000, // 5 min
} as const;

export class CacheManager {
private cache = new Map<string, CacheEntry<unknown>>();
private hits = 0;
private misses = 0;
private maxSize: number;
private enabled: boolean;

constructor(maxSize = 500, enabled = true) {
this.maxSize = maxSize;
this.enabled = enabled;
}

/** Get a cached value, or undefined if expired/missing */
get<T>(key: string): T | undefined {
if (!this.enabled) {
this.misses++;
return undefined;
}

const entry = this.cache.get(key);
if (!entry) {
this.misses++;
return undefined;
}

if (Date.now() > entry.expiresAt) {
this.cache.delete(key);
this.misses++;
return undefined;
}

this.hits++;
return entry.value as T;
}

/** Set a value with TTL in milliseconds */
set<T>(key: string, value: T, ttlMs: number): void {
if (!this.enabled) return;

// Evict oldest entries if at capacity
if (this.cache.size >= this.maxSize) {
this.evictExpired();
// If still at capacity, remove oldest 10%
if (this.cache.size >= this.maxSize) {
const toRemove = Math.ceil(this.maxSize * 0.1);
const keys = this.cache.keys();
for (let i = 0; i < toRemove; i++) {
const next = keys.next();
if (!next.done) this.cache.delete(next.value);
}
}
}

this.cache.set(key, {
value,
expiresAt: Date.now() + ttlMs,
createdAt: Date.now(),
});
}

/** Invalidate entries matching a prefix */
invalidate(prefix: string): number {
let count = 0;
for (const key of this.cache.keys()) {
if (key.startsWith(prefix)) {
this.cache.delete(key);
count++;
}
}
return count;
}

/** Clear the entire cache */
clear(): void {
this.cache.clear();
this.hits = 0;
this.misses = 0;
}

/** Remove expired entries */
private evictExpired(): void {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now > entry.expiresAt) {
this.cache.delete(key);
}
}
}

/** Get cache statistics */
getStats(): CacheStats {
this.evictExpired();
const total = this.hits + this.misses;
const now = Date.now();

const entries = Array.from(this.cache.entries()).map(([key, entry]) => ({
key,
ttl: Math.round((entry.expiresAt - now) / 1000),
age: Math.round((now - entry.createdAt) / 1000),
}));

return {
hits: this.hits,
misses: this.misses,
size: this.cache.size,
maxSize: this.maxSize,
hitRate: total > 0 ? `${((this.hits / total) * 100).toFixed(1)}%` : '0%',
entries,
};
}

/** Generate a cache key from method name and arguments */
static key(method: string, ...args: unknown[]): string {
const argsStr = args.length > 0 ? ':' + JSON.stringify(args) : '';
return `${method}${argsStr}`;
}
}
Loading