Skip to main content
Glama
angrysky56

Narrative Graph MCP

findOptimalDepth.ts2.76 kB
// Tool implementation for finding optimal depth import { createNarrative } from "../core/rtm-builder.js"; import { RTMTreeBuilder } from "../core/rtm-builder.js"; import { createTraversal } from "../core/rtm-traversal.js"; import { createDefaultParameters } from "../core/rtm-ensemble.js"; interface FindOptimalDepthParams { text: string; title: string; targetRecallLength: number; maxBranchingFactor?: number; maxRecallDepth?: number; } // Tool implementation export default async function findOptimalDepth(params: FindOptimalDepthParams) { try { // Create narrative and build tree const narrative = createNarrative(params.text, params.title); const parameters = { ...createDefaultParameters(), maxBranchingFactor: params.maxBranchingFactor || 4, maxRecallDepth: params.maxRecallDepth || 6 }; const builder = new RTMTreeBuilder(parameters); const tree = builder.buildTree(narrative); // Create clause map for traversal const clauseMap = new Map( narrative.clauses.map(c => [c.id, c]) ); // Find optimal depth const traversal = createTraversal(tree, clauseMap); const optimalDepth = traversal.findOptimalDepth(params.targetRecallLength); // Get results at optimal depth const optimalResult = traversal.traverseToDepth(optimalDepth); // Also get results at neighboring depths for comparison const depthComparison = []; for (let d = Math.max(1, optimalDepth - 1); d <= Math.min(optimalDepth + 1, parameters.maxRecallDepth); d++) { const result = traversal.traverseToDepth(d); depthComparison.push({ depth: d, recallLength: result.totalClauses, compressionRatio: result.compressionRatio, nodeCount: result.nodes.length }); } const accuracy = (1 - Math.abs(optimalResult.totalClauses - params.targetRecallLength) / params.targetRecallLength) * 100; return { content: [{ type: "text", text: JSON.stringify({ success: true, treeId: tree.id, targetRecallLength: params.targetRecallLength, optimalDepth: optimalDepth, actualRecallLength: optimalResult.totalClauses, accuracy: accuracy, depthComparison: depthComparison, message: `Found optimal depth ${optimalDepth} yielding ${optimalResult.totalClauses} clauses (${accuracy.toFixed(1)}% accuracy)` }, null, 2) }], }; } catch (error) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : "Unknown error occurred" }, null, 2) }], }; } }

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/angrysky56/narrative-graph-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server