Applicability
When to Use
✓When multiple tools could handle a request
✓When you want to optimize tool selection over time
✓When different tools have different reliability profiles
Overview
How It Works
This pattern scores available MCP server tools against the current task and selects the one with the highest confidence. Scores are based on tool descriptions, past success rates, latency, and cost. Over time, the system learns which tools work best for which queries.
The agent maintains a performance log for each tool, tracking success rate, average latency, and error types. When a new request comes in, it evaluates all capable tools, applies the confidence model, and selects the best option.
Implementation
Code Example
typescript
async function selectBestTool(task, availableTools) {
const scores = await Promise.all(availableTools.map(async (tool) => {
const stats = await redis.get(`tool-stats:${tool.name}`);
const parsed = stats ? JSON.parse(stats) : { successRate: 0.5, avgLatency: 1000 };
const relevanceScore = calculateRelevance(task, tool.description);
const reliabilityScore = parsed.successRate;
const speedScore = 1 - (parsed.avgLatency / 10000);
return {
tool,
score: relevanceScore * 0.5 + reliabilityScore * 0.3 + speedScore * 0.2
};
}));
return scores.sort((a, b) => b.score - a.score)[0].tool;
}
async function recordToolPerformance(toolName, success, latency) {
const key = `tool-stats:${toolName}`;
const stats = JSON.parse(await redis.get(key) || "{}") ;
stats.calls = (stats.calls || 0) + 1;
stats.successes = (stats.successes || 0) + (success ? 1 : 0);
stats.successRate = stats.successes / stats.calls;
stats.avgLatency = ((stats.avgLatency || 0) * (stats.calls - 1) + latency) / stats.calls;
await redis.set(key, JSON.stringify(stats));
}Quick Info
Categoryai-agent
ComplexityMedium