Spaces:
Sleeping
Sleeping
File size: 10,919 Bytes
63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 9458a3a 63e44c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | /**
* MCP Server
*
* The server is the dispatch layer between the agent and the tools.
* It validates tool calls, enforces rate limits, logs every invocation,
* and streams progress updates to the UI via Socket.IO.
*
* Features:
* - Semantic caching: results are cached by tool+args so identical
* GitHub API calls within a session are served instantly.
* - Budget enforcement: hard cap on tool calls.
* - Socket.IO streaming: every invocation is broadcast in real time.
*
* The server does NOT contain any LLM logic β that belongs to the agent.
*/
import { Octokit } from "@octokit/rest";
import { TOOL_MAP, getToolSchemas } from "./tools";
import { TTLCache } from "@/lib/cache";
import type {
MCPToolCall,
MCPToolResult,
MCPToolDefinition,
MCPServerConfig,
} from "./types";
import { getIO, emitStageUpdate } from "@/lib/socket";
// ββ Cache Configuration ββββββββββββββββββββββββββββββββββββββββββββββ
/** 10-minute TTL β generous because PR data rarely changes mid-analysis */
const CACHE_TTL_MS = 10 * 60 * 1000;
/** Tools whose results are deterministic for a given set of args */
const CACHEABLE_TOOLS = new Set([
"get_pr_details",
"get_pr_diff",
"get_pr_files",
"get_pr_commits",
"get_pr_reviews",
"get_pr_comments",
"get_linked_issue",
"fetch_repo_file",
]);
/** PR-related tools whose cache keys should include head_sha */
const PR_TOOLS = new Set([
"get_pr_details",
"get_pr_diff",
"get_pr_files",
"get_pr_commits",
"get_pr_reviews",
"get_pr_comments",
]);
/**
* Build a deterministic cache key from tool name + arguments.
*
* For PR-related tools the key includes `head_sha` (when available) so
* that force-pushes / new commits automatically invalidate the cache.
*
* Keys look like:
* `get_pr_diff:owner/repo#42@abc1234`
* `fetch_repo_file:owner/repo:src/main.ts@main`
*/
function buildCacheKey(
toolName: string,
args: Record<string, unknown>,
headSha?: string
): string {
const owner = args.owner as string | undefined;
const repo = args.repo as string | undefined;
const base = owner && repo ? `${toolName}:${owner}/${repo}` : toolName;
if (args.pr_number != null) {
const sha = headSha ? `@${(headSha as string).slice(0, 8)}` : "";
return `${base}#${args.pr_number}${sha}`;
}
if (args.issue_number != null) return `${base}#issue-${args.issue_number}`;
if (args.path != null) return `${base}:${args.path}@${args.ref ?? "main"}`;
return `${base}:${JSON.stringify(args)}`;
}
export class MCPServer {
private octokit: Octokit;
private config: MCPServerConfig;
private callCount = 0;
private sessionId: string | null = null;
private toolCache = new TTLCache<MCPToolResult>(CACHE_TTL_MS);
/** Cached head SHA for PR-scoped cache keys */
private headShaCache = new Map<string, string>();
constructor(
githubAccessToken: string,
config?: Partial<MCPServerConfig>,
sessionId?: string
) {
this.octokit = new Octokit({ auth: githubAccessToken });
this.config = {
maxToolCalls: config?.maxToolCalls ?? 15,
timeoutMs: config?.timeoutMs ?? 120_000,
enableStreaming: config?.enableStreaming ?? true,
};
this.sessionId = sessionId ?? null;
}
// ββ Tool Discovery βββββββββββββββββββββββββββββββββββββββββββββββ
/** Return all available tool schemas (for the agent's system prompt) */
listTools(): MCPToolDefinition[] {
return getToolSchemas();
}
/** Format tool schemas as a string block for prompt injection */
describeTools(): string {
return this.listTools()
.map((t) => {
const params = t.parameters
.map(
(p) =>
` - ${p.name} (${p.type}${p.required ? ", required" : ""}): ${p.description}`
)
.join("\n");
return `[${t.name}] ${t.description}\n Parameters:\n${params}`;
})
.join("\n\n");
}
// ββ Tool Execution βββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Execute a single tool call.
*
* - Validates the tool exists
* - Checks the semantic cache (deterministic GitHub tools only)
* - Enforces the max-call budget
* - Times the execution
* - Caches successful results
* - Streams status to Socket.IO
*/
async executeTool(call: MCPToolCall): Promise<MCPToolResult> {
const tool = TOOL_MAP.get(call.toolName);
if (!tool) {
return {
toolName: call.toolName,
success: false,
error: `Unknown tool: ${call.toolName}. Available: ${Array.from(TOOL_MAP.keys()).join(", ")}`,
durationMs: 0,
};
}
// ββ Semantic cache check ββ
const isCacheable = CACHEABLE_TOOLS.has(call.toolName);
const prKey = call.arguments.pr_number != null
? `${call.arguments.owner}/${call.arguments.repo}#${call.arguments.pr_number}`
: null;
const headSha = prKey ? this.headShaCache.get(prKey) : undefined;
const cacheKey = isCacheable
? buildCacheKey(call.toolName, call.arguments, headSha)
: "";
if (isCacheable) {
const cached = this.toolCache.get(cacheKey);
if (cached) {
console.log(
`[MCP Cache] HIT ${call.toolName} key=${cacheKey}`
);
this.emitProgress({
stage: 1,
label: `Cache HIT for ${call.toolName} (skipped API call)`,
meta: {
toolName: call.toolName,
cacheHit: true,
cacheKey,
},
});
return { ...cached, durationMs: 0 };
}
console.log(
`[MCP Cache] MISS ${call.toolName} key=${cacheKey}`
);
}
if (this.callCount >= this.config.maxToolCalls) {
return {
toolName: call.toolName,
success: false,
error: `Tool call budget exhausted (${this.config.maxToolCalls} max). Provide your best answer with the information gathered so far.`,
durationMs: 0,
};
}
this.callCount++;
// Stream to UI
this.emitProgress({
stage: 1,
label: `Calling tool: ${call.toolName}`,
meta: {
toolName: call.toolName,
args: call.arguments,
callNumber: this.callCount,
budget: this.config.maxToolCalls,
},
});
const t0 = Date.now();
try {
const data = await tool.execute(this.octokit, call.arguments);
const durationMs = Date.now() - t0;
this.emitProgress({
stage: 1,
label: `Tool ${call.toolName} completed (${durationMs}ms)`,
meta: { toolName: call.toolName, durationMs, success: true },
});
const result: MCPToolResult = {
toolName: call.toolName,
success: true,
data,
durationMs,
};
// ββ Cache successful results for deterministic tools ββ
if (isCacheable) {
// If this was get_pr_details, extract head SHA for future cache keys
if (
call.toolName === "get_pr_details" &&
prKey &&
data &&
typeof data === "object"
) {
const sha = (data as Record<string, unknown>).headSha;
if (typeof sha === "string" && sha.length >= 7) {
this.headShaCache.set(prKey, sha);
}
}
this.toolCache.set(cacheKey, result);
console.log(
`[MCP Cache] SET ${call.toolName} key=${cacheKey}`
);
}
return result;
} catch (err: unknown) {
const durationMs = Date.now() - t0;
const message = err instanceof Error ? err.message : String(err);
this.emitProgress({
stage: -1,
label: `Tool ${call.toolName} failed: ${message.slice(0, 100)}`,
meta: { toolName: call.toolName, durationMs, error: message },
});
return {
toolName: call.toolName,
success: false,
error: message,
durationMs,
};
}
}
/**
* Execute multiple tool calls in parallel (when the agent decides
* to gather data from several sources at once).
*/
async executeToolBatch(calls: MCPToolCall[]): Promise<MCPToolResult[]> {
return Promise.all(calls.map((c) => this.executeTool(c)));
}
// ββ Socket.IO Streaming ββββββββββββββββββββββββββββββββββββββββββ
private emitProgress(payload: {
stage: number;
label: string;
progress?: number;
meta?: Record<string, unknown>;
}) {
if (!this.config.enableStreaming) return;
if (this.sessionId) {
emitStageUpdate(this.sessionId, payload);
} else {
// Broadcast if no session
const io = getIO();
if (io) {
io.emit("agent_progress", {
timestamp: new Date().toISOString(),
...payload,
});
}
}
}
// ββ Status βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
get toolCallsUsed(): number {
return this.callCount;
}
get toolCallsRemaining(): number {
return Math.max(0, this.config.maxToolCalls - this.callCount);
}
resetBudget(): void {
this.callCount = 0;
}
/** Get semantic cache statistics */
get cacheStats(): { size: number; keys: string[] } {
return this.toolCache.stats();
}
/** Clear the semantic cache */
clearCache(): void {
this.toolCache.clear();
}
}
|