Skip to main content
Glama

find_similar_elements

Identify semantically similar elements in DollhouseMCP using NLP scoring to discover related personas, skills, templates, agents, memories, or ensembles based on Jaccard similarity and Shannon entropy analysis.

Instructions

Find elements that are semantically similar to a given element using NLP scoring (Jaccard similarity and Shannon entropy). Returns elements with similarity scores and relationships.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
element_nameYesName of the element to find similar items for
element_typeNoType of the element. If not specified, searches all types.
limitNoMaximum number of similar elements to return. Defaults to 10.
thresholdNoMinimum similarity score (0-1) to include. Defaults to 0.5.

Implementation Reference

  • Core handler function implementing the tool logic: validates inputs with security checks (Unicode normalization), retrieves similar elements from EnhancedIndexManager using relationship strength, sorts and limits results, formats Markdown response with scores and relationships.
    async findSimilarElements(options: {
      elementName: string;
      elementType?: string;
      limit: number;
      threshold: number;
    }) {
      try {
        // Validate inputs
        if (!options.elementName || typeof options.elementName !== 'string') {
          throw new Error('Element name is required and must be a string');
        }
    
        // FIX: DMCP-SEC-004 - Normalize Unicode in user input to prevent homograph attacks
        const normalized = UnicodeValidator.normalize(options.elementName);
        if (!normalized.isValid) {
          throw new Error(`Invalid element name: ${normalized.detectedIssues?.join(', ')}`);
        }
        options.elementName = normalized.normalizedContent;
    
        // Also normalize element type if provided
        if (options.elementType) {
          const normalizedType = UnicodeValidator.normalize(options.elementType);
          if (!normalizedType.isValid) {
            throw new Error(`Invalid element type: ${normalizedType.detectedIssues?.join(', ')}`);
          }
          options.elementType = normalizedType.normalizedContent;
        }
        if (options.limit <= 0 || options.limit > 100) {
          options.limit = 5; // Default to reasonable limit
        }
        if (options.threshold < 0 || options.threshold > 1) {
          options.threshold = 0.3; // Default to reasonable threshold
        }
        // Ensure the enhanced index is available with error handling
        try {
          await this.enhancedIndexManager.getIndex();
        } catch (indexError) {
          logger.error('Failed to get Enhanced Index', indexError);
          // Try to recover by forcing rebuild
          try {
            await this.enhancedIndexManager.getIndex({ forceRebuild: true });
          } catch (rebuildError) {
            throw new Error('Enhanced Index is unavailable. Please try again later.');
          }
        }
    
        // FIX: DMCP-SEC-006 - Add security audit logging for index operations
        SecurityMonitor.logSecurityEvent({
          type: 'ELEMENT_CREATED',
          severity: 'LOW',
          source: 'EnhancedIndexHandler.findSimilarElements',
          details: `Similarity search performed for element: ${options.elementName}`,
          additionalData: {
            elementType: options.elementType,
            limit: options.limit,
            threshold: options.threshold
          }
        });
    
        // Find the element
        const elementId = options.elementType ?
          `${options.elementType}/${options.elementName}` :
          options.elementName;
    
        // Get connected elements (similar/related)
        const connectedMap = await this.enhancedIndexManager.getConnectedElements(
          elementId,
          {
            maxDepth: 1,  // Direct relationships only
            minStrength: options.threshold
          }
        );
    
        // Convert to array and sort by relationship strength
        const similarElements = Array.from(connectedMap.entries())
          .map(([id, path]) => {
            const [type, name] = id.split('/');
            return {
              type,
              name,
              score: path.totalStrength || 0,
              relationships: path.relationships || []  // relationships is already an array of strings
            };
          })
          .sort((a, b) => b.score - a.score)
          .slice(0, options.limit);
    
        // Format results
        let text = `${this.personaIndicator}🔍 **Similar Elements**\n\n`;
        text += `**Reference**: ${options.elementName}\n`;
        if (options.elementType) {
          text += `**Type**: ${options.elementType}\n`;
        }
        text += `**Found**: ${similarElements.length} similar elements\n\n`;
    
        if (similarElements.length === 0) {
          text += `No similar elements found with similarity score >= ${options.threshold}\n`;
        } else {
          for (const element of similarElements) {
            const icon = this.getElementIcon(element.type);
            text += `${icon} **${element.name}** (${element.type})\n`;
            text += `   📊 Similarity: ${(element.score * 100).toFixed(1)}%\n`;
            if (element.relationships && element.relationships.length > 0) {
              text += `   🔗 Relationships: ${element.relationships.join(', ')}\n`;
            }
            text += '\n';
          }
        }
    
        return {
          content: [{
            type: "text",
            text
          }]
        };
      } catch (error: any) {
        ErrorHandler.logError('EnhancedIndexHandler.findSimilarElements', error, options);
        return {
          content: [{
            type: "text",
            text: `${this.personaIndicator}❌ Failed to find similar elements: ${SecureErrorHandler.sanitizeError(error).message}`
          }]
        };
      }
    }
  • Tool registration defining the 'find_similar_elements' MCP tool with description, input schema, and handler that maps args to server.findSimilarElements call.
      tool: {
        name: "find_similar_elements",
        description: "Find elements that are semantically similar to a given element using NLP scoring (Jaccard similarity and Shannon entropy). Returns elements with similarity scores and relationships.",
        inputSchema: {
          type: "object",
          properties: {
            element_name: {
              type: "string",
              description: "Name of the element to find similar items for",
            },
            element_type: {
              type: "string",
              enum: ["personas", "skills", "templates", "agents", "memories", "ensembles"],
              description: "Type of the element. If not specified, searches all types.",
            },
            limit: {
              type: "number",
              description: `Maximum number of similar elements to return. Defaults to ${config.performance.defaultSimilarLimit}.`,
            },
            threshold: {
              type: "number",
              description: `Minimum similarity score (0-1) to include. Defaults to ${config.performance.defaultSimilarityThreshold}.`,
            },
          },
          required: ["element_name"],
        },
      },
      handler: (args: FindSimilarElementsArgs) => server.findSimilarElements({
        elementName: args.element_name,
        elementType: args.element_type,
        limit: args.limit || config.performance.defaultSimilarLimit,
        threshold: args.threshold || config.performance.defaultSimilarityThreshold
      })
    },
  • TypeScript interface defining the input arguments for the find_similar_elements tool handler.
    interface FindSimilarElementsArgs {
      element_name: string;
      element_type?: string;
      limit?: number;
      threshold?: number;
    }
  • Interface definition in IToolHandler for the server's findSimilarElements method.
    findSimilarElements(options: {elementName: string; elementType?: string; limit: number; threshold: number}): Promise<any>;
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions the tool 'returns elements with similarity scores and relationships,' which gives some behavioral context, but lacks details on permissions, rate limits, error handling, or whether it's a read-only operation. For a tool with no annotations, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded, with two sentences that efficiently cover the tool's purpose and return values. There is no wasted verbiage, though it could be slightly more structured by separating usage guidelines.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations and no output schema, the description is moderately complete. It explains the tool's purpose and return format, but lacks details on behavioral traits, error cases, and usage context. For a tool with 4 parameters and no structured safety hints, it should do more to compensate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all parameters. The description adds no additional meaning beyond what the schema provides, such as explaining how 'element_name' is used in similarity calculations or the impact of 'threshold'. Baseline 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Find elements that are semantically similar to a given element using NLP scoring (Jaccard similarity and Shannon entropy).' It specifies the verb ('find'), resource ('elements'), and methodology ('NLP scoring'), but does not explicitly differentiate from sibling tools like 'search_all' or 'search_collection' that might also find elements.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It does not mention any prerequisites, exclusions, or compare it to sibling tools such as 'search_all' or 'get_element_relationships', leaving the agent with no context for tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/DollhouseMCP/DollhouseMCP'

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