/**
* MCP Tools for Claudio Integration
* Provides learning tools that can be registered in Claudio's MCP server
*/
import { ClaudioLearningAgent } from './claudio-integration.js';
export class ClaudioMCPLearningTools {
constructor(options = {}) {
this.learningAgent = new ClaudioLearningAgent(options);
this.isInitialized = false;
}
async initialize() {
if (!this.isInitialized) {
await this.learningAgent.initialize();
this.isInitialized = true;
}
}
getToolDefinitions() {
return [
{
name: 'learn_from_interaction',
description: 'Learn from a Claudio agent interaction to improve future performance',
inputSchema: {
type: 'object',
properties: {
agent_id: {
type: 'string',
description: 'ID of the agent that performed the interaction'
},
workflow_id: {
type: 'string',
description: 'ID of the workflow this interaction belongs to'
},
input: {
type: 'string',
description: 'Input to the agent'
},
output: {
type: 'string',
description: 'Output from the agent'
},
success: {
type: 'boolean',
description: 'Whether the interaction was successful',
default: true
},
duration: {
type: 'number',
description: 'Duration of the interaction in milliseconds'
},
metadata: {
type: 'object',
description: 'Additional metadata about the interaction'
}
},
required: ['agent_id', 'input', 'output']
}
},
{
name: 'get_learning_insights',
description: 'Get learning insights and recommendations for Claudio workflows',
inputSchema: {
type: 'object',
properties: {
workflow_id: {
type: 'string',
description: 'Optional specific workflow to get insights for'
},
agent_id: {
type: 'string',
description: 'Optional specific agent to get insights for'
}
}
}
},
{
name: 'optimize_workflow',
description: 'Get optimization suggestions for a Claudio workflow',
inputSchema: {
type: 'object',
properties: {
workflow_id: {
type: 'string',
description: 'ID of the workflow to optimize'
},
involved_agents: {
type: 'array',
items: { type: 'string' },
description: 'List of agent IDs involved in the workflow'
},
current_step: {
type: 'string',
description: 'Current step in the workflow'
},
performance: {
type: 'object',
description: 'Current performance metrics'
}
},
required: ['workflow_id']
}
},
{
name: 'predict_next_action',
description: 'Predict the next best action in a Claudio workflow',
inputSchema: {
type: 'object',
properties: {
current_agent: {
type: 'string',
description: 'Currently active agent'
},
workflow_state: {
type: 'object',
description: 'Current state of the workflow'
},
user_intent: {
type: 'string',
description: 'Detected user intent'
},
available_agents: {
type: 'array',
items: { type: 'object' },
description: 'Available agents that can be called'
}
},
required: ['current_agent', 'workflow_state']
}
},
{
name: 'learn_from_workflow_outcome',
description: 'Learn from the complete outcome of a Claudio workflow',
inputSchema: {
type: 'object',
properties: {
workflow_id: {
type: 'string',
description: 'ID of the completed workflow'
},
initial_request: {
type: 'object',
description: 'Original request that started the workflow'
},
final_result: {
type: 'object',
description: 'Final result of the workflow'
},
success: {
type: 'boolean',
description: 'Whether the workflow was successful'
},
involved_agents: {
type: 'array',
items: { type: 'string' },
description: 'All agents that participated in the workflow'
},
total_duration: {
type: 'number',
description: 'Total duration of the workflow in milliseconds'
},
user_satisfaction: {
type: 'number',
description: 'User satisfaction rating (0-1)',
minimum: 0,
maximum: 1
}
},
required: ['workflow_id', 'initial_request', 'final_result', 'success']
}
},
{
name: 'get_agent_performance_metrics',
description: 'Get performance metrics for specific Claudio agents',
inputSchema: {
type: 'object',
properties: {
agent_id: {
type: 'string',
description: 'Optional specific agent to get metrics for'
},
time_range: {
type: 'string',
description: 'Time range for metrics (1h, 24h, 7d, 30d)',
enum: ['1h', '24h', '7d', '30d'],
default: '24h'
}
}
}
}
];
}
async handleToolCall(toolName, args) {
await this.initialize();
switch (toolName) {
case 'learn_from_interaction':
return await this.handleLearnFromInteraction(args);
case 'get_learning_insights':
return await this.handleGetLearningInsights(args);
case 'optimize_workflow':
return await this.handleOptimizeWorkflow(args);
case 'predict_next_action':
return await this.handlePredictNextAction(args);
case 'learn_from_workflow_outcome':
return await this.handleLearnFromWorkflowOutcome(args);
case 'get_agent_performance_metrics':
return await this.handleGetAgentPerformanceMetrics(args);
default:
throw new Error(`Unknown learning tool: ${toolName}`);
}
}
async handleLearnFromInteraction(args) {
const request = {
action: 'analyze-interaction',
data: {
input: args.input,
output: args.output,
success: args.success,
duration: args.duration
},
context: {
sourceAgent: args.agent_id,
workflowId: args.workflow_id,
metadata: args.metadata || {}
}
};
const result = await this.learningAgent.handleRequest(request);
return {
success: true,
pattern_id: result.patternId,
learning_impact: result.learningImpact,
recommendations: result.recommendations,
message: `Learned from ${args.agent_id} interaction`,
timestamp: new Date().toISOString()
};
}
async handleGetLearningInsights(args) {
const request = {
action: 'get-insights',
data: {},
context: {
workflowId: args.workflow_id,
agentId: args.agent_id
}
};
const result = await this.learningAgent.handleRequest(request);
return {
success: true,
insights: result.insights,
recommendations: result.recommendations,
metadata: result.metadata,
summary: {
total_patterns: result.insights.topPatterns?.length || 0,
claudio_interactions: result.insights.claudioSpecific?.totalClaudioInteractions || 0,
most_used_agents: result.insights.claudioSpecific?.mostUsedAgents || {},
workflow_efficiency: result.insights.claudioSpecific?.workflowEfficiency || 0
}
};
}
async handleOptimizeWorkflow(args) {
const request = {
action: 'optimize-workflow',
data: {
workflowId: args.workflow_id,
involvedAgents: args.involved_agents || [],
currentStep: args.current_step,
performance: args.performance || {}
},
context: {
workflowId: args.workflow_id
}
};
const result = await this.learningAgent.handleRequest(request);
return {
success: true,
workflow_id: args.workflow_id,
optimizations: result.optimizations,
next_step_suggestions: result.nextStepSuggestions,
performance_improvements: result.performanceImprovements,
confidence: result.confidence,
implementation_guide: {
priority: this.prioritizeOptimizations(result.optimizations),
estimated_impact: this.summarizeImpact(result.performanceImprovements),
next_steps: this.generateNextSteps(result.optimizations)
}
};
}
async handlePredictNextAction(args) {
const request = {
action: 'predict-next-action',
data: {
workflowState: args.workflow_state,
userIntent: args.user_intent,
availableAgents: args.available_agents || []
},
context: {
sourceAgent: args.current_agent
}
};
const result = await this.learningAgent.handleRequest(request);
return {
success: true,
predictions: result.predictions,
confidence: result.confidence,
recommended_action: result.predictions[0] || null,
reasoning: result.predictions[0]?.reasoning || 'No predictions available',
suggested_agent: result.predictions[0]?.suggestedAgent || null
};
}
async handleLearnFromWorkflowOutcome(args) {
const request = {
action: 'learn-from-outcome',
data: {
workflowId: args.workflow_id,
initialRequest: args.initial_request,
finalResult: args.final_result,
success: args.success,
involvedAgents: args.involved_agents || [],
totalDuration: args.total_duration,
userSatisfaction: args.user_satisfaction
},
context: {
workflowId: args.workflow_id
}
};
const result = await this.learningAgent.handleRequest(request);
return {
success: true,
workflow_id: args.workflow_id,
learned: result.learned,
pattern_id: result.patternId,
learning_impact: result.impact,
recommendations: result.nextRecommendations,
message: `Learned from workflow ${args.workflow_id} outcome`
};
}
async handleGetAgentPerformanceMetrics(args) {
const metrics = await this.learningAgent.client.getPerformanceMetrics(args.agent_id);
// Filter and format metrics for Claudio context
const claudioMetrics = this.formatMetricsForClaudio(metrics, args);
return {
success: true,
agent_id: args.agent_id || 'all',
time_range: args.time_range || '24h',
metrics: claudioMetrics,
summary: this.summarizeAgentMetrics(claudioMetrics)
};
}
prioritizeOptimizations(optimizations) {
if (!optimizations || !optimizations.length) return [];
return optimizations
.map(opt => ({
...opt,
priority: this.calculateOptimizationPriority(opt)
}))
.sort((a, b) => b.priority - a.priority);
}
calculateOptimizationPriority(optimization) {
let priority = 0;
if (optimization.estimatedImpact > 0.2) priority += 3;
else if (optimization.estimatedImpact > 0.1) priority += 2;
else priority += 1;
if (optimization.implementation?.estimatedEffort === 'low') priority += 2;
else if (optimization.implementation?.estimatedEffort === 'medium') priority += 1;
return priority;
}
summarizeImpact(improvements) {
if (!improvements || !Object.keys(improvements).length) {
return 'No significant improvements identified';
}
const summaries = Object.entries(improvements).map(([metric, data]) => {
return `${metric}: ${data.improvement} improvement potential`;
});
return summaries.join(', ');
}
generateNextSteps(optimizations) {
if (!optimizations || !optimizations.length) {
return ['No optimizations available at this time'];
}
const steps = [];
const highPriorityOptimizations = optimizations.filter(opt =>
(opt.priority || 0) > 3
);
if (highPriorityOptimizations.length > 0) {
steps.push(`Implement high-priority optimization: ${highPriorityOptimizations[0].description}`);
}
steps.push('Monitor performance after implementation');
steps.push('Collect feedback on changes');
steps.push('Schedule follow-up optimization review');
return steps;
}
formatMetricsForClaudio(metrics, args) {
const formatted = {
response_times: metrics.responseTime || {},
success_rates: metrics.successRate || {},
resource_usage: metrics.resourceUsage || {},
error_patterns: metrics.errorPatterns || {}
};
// Add Claudio-specific formatting
if (args.agent_id) {
formatted.agent_specific = {
agent_id: args.agent_id,
specialization: this.detectAgentSpecialization(metrics),
efficiency_score: this.calculateAgentEfficiency(metrics)
};
}
return formatted;
}
detectAgentSpecialization(metrics) {
// Simplified specialization detection based on tool usage patterns
const toolUsage = metrics.toolUsage || {};
const mostUsedTool = Object.entries(toolUsage)
.sort(([,a], [,b]) => b - a)[0];
if (mostUsedTool) {
return `Specialized in ${mostUsedTool[0]} operations`;
}
return 'General purpose agent';
}
calculateAgentEfficiency(metrics) {
const successRate = metrics.successRate?.overall || 0;
const responseTime = metrics.responseTime?.average || 0;
// Simplified efficiency calculation
let efficiency = successRate;
if (responseTime < 1000) efficiency += 0.2; // Fast response bonus
if (responseTime > 5000) efficiency -= 0.1; // Slow response penalty
return Math.min(1.0, Math.max(0.0, efficiency));
}
summarizeAgentMetrics(metrics) {
const summary = {
overall_health: 'good',
key_insights: [],
recommendations: []
};
// Analyze response times
const avgResponseTime = metrics.response_times?.average || 0;
if (avgResponseTime > 5000) {
summary.overall_health = 'needs_attention';
summary.key_insights.push(`High average response time: ${avgResponseTime}ms`);
summary.recommendations.push('Consider optimizing agent processing logic');
}
// Analyze success rates
const successRate = metrics.success_rates?.overall || 0;
if (successRate < 0.8) {
summary.overall_health = 'needs_attention';
summary.key_insights.push(`Low success rate: ${(successRate * 100).toFixed(1)}%`);
summary.recommendations.push('Review error patterns and improve error handling');
}
// Agent efficiency
if (metrics.agent_specific?.efficiency_score > 0.8) {
summary.key_insights.push('High efficiency agent');
} else if (metrics.agent_specific?.efficiency_score < 0.6) {
summary.key_insights.push('Below average efficiency');
summary.recommendations.push('Consider agent optimization or retraining');
}
return summary;
}
async cleanup() {
if (this.learningAgent) {
await this.learningAgent.cleanup();
}
}
}
export default ClaudioMCPLearningTools;