Wisdom Layer MCP
by PV-Bhat
Verified
- wisdom-layer-mcp
- src
- tools
import { addMistake, getPrioritizedMistakes, MistakeEntry } from '../utils/storage.js';
// Log tool interfaces
export interface LogInput {
mistake: string;
category: string;
solution: string;
sessionId?: string;
}
export interface LogOutput {
added: boolean;
currentTally: number;
topMistakes: Array<{
category: string;
count: number;
examples: MistakeEntry[];
}>;
}
/**
* The wisdom_log tool records lessons learned and provides a persistent
* memory of mistakes for future improvement
*/
export async function logTool(input: LogInput): Promise<LogOutput> {
try {
// Validate input
if (!input.mistake) {
throw new Error('Mistake description is required');
}
if (!input.category) {
throw new Error('Mistake category is required');
}
if (!input.solution) {
throw new Error('Solution is required');
}
// Enforce single-sentence constraints
const mistake = enforceOneSentence(input.mistake);
const solution = enforceOneSentence(input.solution);
// Add mistake to log
const entry = addMistake(mistake, input.category, solution);
// Get top mistakes for response
const prioritizedMistakes = getPrioritizedMistakes();
// Find current tally for this category
const categoryData = prioritizedMistakes.find(m => m.category === input.category);
const currentTally = categoryData?.count || 1;
// Format top mistakes (limit to top 3)
const topMistakes = prioritizedMistakes
.slice(0, 3)
.map(m => ({
category: m.category,
count: m.count,
examples: m.examples
}));
return {
added: true,
currentTally,
topMistakes
};
} catch (error) {
console.error('Error in wisdom_log tool:', error);
return {
added: false,
currentTally: 0,
topMistakes: []
};
}
}
/**
* Ensure text is a single sentence
*/
function enforceOneSentence(text: string): string {
// Remove newlines
let sentence = text.replace(/\r?\n/g, ' ');
// Split by sentence-ending punctuation
const sentences = sentence.split(/([.!?])\s+/);
// Take just the first sentence
if (sentences.length > 0) {
// If there's punctuation, include it
const firstSentence = sentences[0] + (sentences[1] || '');
sentence = firstSentence.trim();
}
// Ensure it ends with sentence-ending punctuation
if (!/[.!?]$/.test(sentence)) {
sentence += '.';
}
return sentence;
}