skills/mlops/agent-architect/agent-tool-routing/SKILL.md
Use this skill when implementing tool selection for AI agents. Activate when the user needs agents to choose the right tools, implement dynamic tool routing, integrate MCP servers, design tool selection logic, or build agents that can use external services effectively.
npx skillsauth add latestaiagents/agent-skills agent-tool-routingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Design intelligent systems for agents to select and use the right tools at the right time.
┌─────────────────────────────────────────────────────────────┐
│ AGENT │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ TOOL ROUTER │
│ ┌─────────────┐ ┌──────────────┐ ┌────────────────────┐ │
│ │ Classifier │ │ Capabilities │ │ Cost/Latency │ │
│ │ │ │ Matcher │ │ Optimizer │ │
│ └─────────────┘ └──────────────┘ └────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Tool A │ │ Tool B │ │ Tool C │
│ (Local) │ │ (API) │ │ (MCP) │
└─────────┘ └─────────┘ └─────────┘
interface Tool {
name: string;
description: string;
category: string;
// Schema
inputSchema: JSONSchema;
outputSchema: JSONSchema;
// Capabilities
capabilities: string[];
limitations: string[];
// Execution
execute: (input: unknown) => Promise<ToolResult>;
// Metadata
metadata: {
costPerCall?: number;
avgLatencyMs?: number;
rateLimit?: RateLimit;
requiresAuth?: boolean;
supportsBatching?: boolean;
};
}
interface ToolResult {
success: boolean;
data?: unknown;
error?: {
code: string;
message: string;
retryable: boolean;
};
metadata: {
durationMs: number;
tokensUsed?: number;
};
}
class ToolRegistry {
private tools = new Map<string, Tool>();
private capabilityIndex = new Map<string, Set<string>>();
register(tool: Tool): void {
this.tools.set(tool.name, tool);
// Index by capability
for (const cap of tool.capabilities) {
if (!this.capabilityIndex.has(cap)) {
this.capabilityIndex.set(cap, new Set());
}
this.capabilityIndex.get(cap)!.add(tool.name);
}
}
findByCapability(capability: string): Tool[] {
const toolNames = this.capabilityIndex.get(capability) || new Set();
return Array.from(toolNames).map(name => this.tools.get(name)!);
}
getAll(): Tool[] {
return Array.from(this.tools.values());
}
// Generate tool descriptions for LLM
getToolDescriptions(): string {
return this.getAll()
.map(t => `- ${t.name}: ${t.description}`)
.join('\n');
}
}
Let the model choose based on descriptions.
class LLMToolRouter {
async route(
task: string,
availableTools: Tool[]
): Promise<RoutingDecision> {
const response = await this.llm.complete({
system: `You are a tool routing assistant.
Given a task and available tools, select the best tool to use.
Available tools:
${availableTools.map(t => `
- ${t.name}
Description: ${t.description}
Capabilities: ${t.capabilities.join(', ')}
Cost: ${t.metadata.costPerCall || 'free'}
Latency: ${t.metadata.avgLatencyMs || 'unknown'}ms
`).join('\n')}
Respond with JSON: { "tool": "tool_name", "reasoning": "why", "input": {...} }`,
user: `Task: ${task}`
});
return JSON.parse(response);
}
}
Deterministic routing based on patterns.
class RuleBasedRouter {
private rules: RoutingRule[] = [];
addRule(rule: RoutingRule): void {
this.rules.push(rule);
this.rules.sort((a, b) => b.priority - a.priority);
}
route(task: string, context: Context): RoutingDecision {
for (const rule of this.rules) {
if (rule.matches(task, context)) {
return {
tool: rule.targetTool,
reasoning: rule.description
};
}
}
return { tool: 'default', reasoning: 'No specific rule matched' };
}
}
// Example rules
const rules: RoutingRule[] = [
{
priority: 100,
description: 'Use web search for current information',
matches: (task) => /current|latest|today|news|2024|2025|2026/.test(task),
targetTool: 'web_search'
},
{
priority: 90,
description: 'Use code execution for calculations',
matches: (task) => /calculate|compute|sum|average|math/.test(task),
targetTool: 'code_interpreter'
},
{
priority: 80,
description: 'Use file reader for document analysis',
matches: (task, ctx) => ctx.hasAttachments && /read|analyze|extract/.test(task),
targetTool: 'file_reader'
}
];
Choose based on cost/performance trade-offs.
class CostOptimizedRouter {
async route(
task: string,
capableTools: Tool[],
budget: Budget
): Promise<RoutingDecision> {
// Score each tool
const scored = capableTools.map(tool => ({
tool,
score: this.calculateScore(tool, budget)
}));
// Sort by score (higher is better)
scored.sort((a, b) => b.score - a.score);
return {
tool: scored[0].tool.name,
reasoning: `Best cost/performance ratio within budget`
};
}
private calculateScore(tool: Tool, budget: Budget): number {
const cost = tool.metadata.costPerCall || 0;
const latency = tool.metadata.avgLatencyMs || 1000;
// Can't use if over budget
if (cost > budget.remaining) return -Infinity;
// Score: lower cost and latency = higher score
const costScore = 1 - (cost / budget.max);
const latencyScore = 1 - Math.min(latency / 5000, 1);
return costScore * budget.costWeight + latencyScore * budget.latencyWeight;
}
}
interface MCPServer {
name: string;
transport: 'stdio' | 'http' | 'websocket';
config: MCPConfig;
}
class MCPToolProvider {
private clients = new Map<string, MCPClient>();
async connect(server: MCPServer): Promise<void> {
const client = new MCPClient(server.transport, server.config);
await client.connect();
// Discover tools
const tools = await client.listTools();
// Register each tool
for (const tool of tools) {
registry.register({
name: `${server.name}:${tool.name}`,
description: tool.description,
inputSchema: tool.inputSchema,
execute: (input) => client.callTool(tool.name, input),
metadata: {
source: 'mcp',
server: server.name
}
});
}
this.clients.set(server.name, client);
}
async disconnect(serverName: string): Promise<void> {
const client = this.clients.get(serverName);
if (client) {
await client.close();
this.clients.delete(serverName);
}
}
}
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@anthropic/mcp-github"],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
},
"filesystem": {
"command": "npx",
"args": ["-y", "@anthropic/mcp-filesystem"],
"env": {
"ALLOWED_PATHS": "/Users/dev/projects"
}
},
"database": {
"transport": "http",
"url": "http://localhost:3001/mcp",
"auth": {
"type": "bearer",
"token": "${DB_MCP_TOKEN}"
}
}
}
}
async function executeWithRetry(
tool: Tool,
input: unknown,
options: ExecutionOptions = {}
): Promise<ToolResult> {
const maxRetries = options.maxRetries || 3;
const backoff = options.backoffMs || 1000;
let lastError: Error;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
// Check rate limit
await rateLimiter.acquire(tool.name);
// Execute
const result = await tool.execute(input);
if (result.success) {
return result;
}
if (!result.error?.retryable) {
return result;
}
lastError = new Error(result.error.message);
} catch (error) {
lastError = error as Error;
}
// Wait before retry
if (attempt < maxRetries) {
await sleep(backoff * Math.pow(2, attempt - 1));
}
}
return {
success: false,
error: {
code: 'MAX_RETRIES_EXCEEDED',
message: `Failed after ${maxRetries} attempts: ${lastError.message}`,
retryable: false
},
metadata: { durationMs: 0 }
};
}
async function executeWithFallback(
task: string,
tools: Tool[]
): Promise<ToolResult> {
for (const tool of tools) {
try {
const result = await tool.execute({ task });
if (result.success) {
return result;
}
console.log(`Tool ${tool.name} failed, trying next...`);
} catch (error) {
console.log(`Tool ${tool.name} threw error, trying next...`);
}
}
return {
success: false,
error: {
code: 'ALL_TOOLS_FAILED',
message: 'All fallback tools failed',
retryable: false
},
metadata: { durationMs: 0 }
};
}
interface ToolUsageMetrics {
toolName: string;
invocations: number;
successRate: number;
avgLatencyMs: number;
totalCost: number;
errorCounts: Map<string, number>;
}
class ToolMetricsCollector {
private metrics = new Map<string, ToolUsageMetrics>();
record(toolName: string, result: ToolResult): void {
const m = this.getOrCreate(toolName);
m.invocations++;
m.avgLatencyMs = (m.avgLatencyMs * (m.invocations - 1) + result.metadata.durationMs) / m.invocations;
if (result.success) {
m.successRate = (m.successRate * (m.invocations - 1) + 1) / m.invocations;
} else {
m.successRate = (m.successRate * (m.invocations - 1)) / m.invocations;
const errorCode = result.error?.code || 'UNKNOWN';
m.errorCounts.set(errorCode, (m.errorCounts.get(errorCode) || 0) + 1);
}
}
getReport(): ToolUsageMetrics[] {
return Array.from(this.metrics.values());
}
}
development
Test skills for correct activation, content quality, and regression — both automated checks (frontmatter validity, lint) and manual verification (query-suite activation testing). Covers CI integration and how to catch skill regressions before users do. Use this skill when adding skills to a repo, setting up CI for a skill library, or debugging "the skill exists but doesn't work". Activate when: test skills, validate skills, skill CI, skill linting, skill activation test, skill regression.
documentation
Write the YAML frontmatter for a SKILL.md file so it activates reliably — name, description, and activation keywords that the model matches against. Covers length, tone, and the most common frontmatter mistakes. Use this skill when authoring a new skill, fixing a skill that isn't auto-activating, or reviewing skills for publication. Activate when: SKILL.md frontmatter, skill description, skill activation, skill YAML, write a skill, author a skill.
development
Design skills that fire at the right moment — neither over-eager (noise) nor under-eager (silent). Covers activation specificity, trigger phrases, disambiguation between overlapping skills, and debugging activation. Use this skill when multiple skills could fire on the same query, a skill never fires, or a skill fires too often. Activate when: skill won't activate, skill over-activates, overlapping skills, skill triggers, skill selection, skill disambiguation.
development
Structure SKILL.md content so the model reads just enough — concise summary up front, progressively deeper detail, examples on demand. Covers section ordering, length budgets, when to split into multiple skills. Use this skill when writing or refactoring a skill body, one skill has grown too long, or a skill is wordy but not useful. Activate when: SKILL.md structure, skill content, skill too long, split skill, progressive disclosure, skill body.