Skip to main content
Glama

Detect Knowledge Communities

graph_communities
Read-only

Find clusters of densely interconnected entities in a knowledge graph. Each entity belongs to one community, helping you explore related groups without specifying a starting point.

Instructions

Find clusters of densely-interconnected entities in the graph. Uses greedy seed-based BFS through edges above the weight threshold — works without GDS or APOC. Each entity is assigned to at most one community (the first that reaches it from a high-degree seed). Useful for understanding knowledge neighbourhoods (e.g. "everything related to infrastructure"). Returns at most max_communities clusters, each shaped {community_id, seed: {id, name, type}, size, members: [{id, name, type}]}, sorted by size desc; communities below min_size are filtered out. Use graph_query or graph_search instead when you have a specific entity to start from.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
weight_thresholdNoOnly traverse edges with weight strictly greater than this (default 0.4).
max_communitiesNoMaximum number of communities to return (default 10).
max_hopsNoBFS depth from each seed (default 3, capped at 4).
min_sizeNoMinimum members for a community to be returned (default 2).

Implementation Reference

  • The handler function for graph_communities tool - calls client.findCommunities() with the parsed args and returns the result.
    }, async (args) => {
      try {
        const result = await client.findCommunities(currentTenant(), {
          weight_threshold: args.weight_threshold,
          max_communities: args.max_communities,
          max_hops: args.max_hops,
          min_size: args.min_size,
        });
        return toolResult(result);
      } catch (err) {
        const e = err instanceof Error ? err : new Error(String(err));
        return toolError(`graph_communities failed: ${e.message}`);
      }
    });
  • The tool registration with input schema (weight_threshold, max_communities, max_hops, min_size) and description for graph_communities.
    server.registerTool("graph_communities", {
      title: "Detect Knowledge Communities",
      description:
        "Find clusters of densely-interconnected entities in the graph. Uses greedy seed-based BFS through " +
        "edges above the weight threshold — works without GDS or APOC. Each entity is assigned to at most " +
        "one community (the first that reaches it from a high-degree seed). Useful for understanding " +
        "knowledge neighbourhoods (e.g. \"everything related to infrastructure\"). " +
        "Returns at most `max_communities` clusters, each shaped `{community_id, seed: {id, name, type}, size, members: [{id, name, type}]}`, sorted by size desc; communities below `min_size` are filtered out. " +
        "Use graph_query or graph_search instead when you have a specific entity to start from.",
      inputSchema: {
        weight_threshold: z
          .number()
          .min(0)
          .max(1)
          .optional()
          .default(0.4)
          .describe("Only traverse edges with weight strictly greater than this (default 0.4)."),
        max_communities: z
          .number()
          .int()
          .min(1)
          .max(30)
          .optional()
          .default(10)
          .describe("Maximum number of communities to return (default 10)."),
        max_hops: z
          .number()
          .int()
          .min(1)
          .max(4)
          .optional()
          .default(3)
          .describe("BFS depth from each seed (default 3, capped at 4)."),
        min_size: z
          .number()
          .int()
          .min(2)
          .optional()
          .default(2)
          .describe("Minimum members for a community to be returned (default 2)."),
      },
      annotations: { readOnlyHint: true },
  • Registration of graph_communities tool via server.registerTool() with the name 'graph_communities'.
    server.registerTool("graph_communities", {
      title: "Detect Knowledge Communities",
      description:
        "Find clusters of densely-interconnected entities in the graph. Uses greedy seed-based BFS through " +
        "edges above the weight threshold — works without GDS or APOC. Each entity is assigned to at most " +
        "one community (the first that reaches it from a high-degree seed). Useful for understanding " +
        "knowledge neighbourhoods (e.g. \"everything related to infrastructure\"). " +
        "Returns at most `max_communities` clusters, each shaped `{community_id, seed: {id, name, type}, size, members: [{id, name, type}]}`, sorted by size desc; communities below `min_size` are filtered out. " +
        "Use graph_query or graph_search instead when you have a specific entity to start from.",
      inputSchema: {
        weight_threshold: z
          .number()
          .min(0)
          .max(1)
          .optional()
          .default(0.4)
          .describe("Only traverse edges with weight strictly greater than this (default 0.4)."),
        max_communities: z
          .number()
          .int()
          .min(1)
          .max(30)
          .optional()
          .default(10)
          .describe("Maximum number of communities to return (default 10)."),
        max_hops: z
          .number()
          .int()
          .min(1)
          .max(4)
          .optional()
          .default(3)
          .describe("BFS depth from each seed (default 3, capped at 4)."),
        min_size: z
          .number()
          .int()
          .min(2)
          .optional()
          .default(2)
          .describe("Minimum members for a community to be returned (default 2)."),
      },
      annotations: { readOnlyHint: true },
    }, async (args) => {
      try {
        const result = await client.findCommunities(currentTenant(), {
          weight_threshold: args.weight_threshold,
          max_communities: args.max_communities,
          max_hops: args.max_hops,
          min_size: args.min_size,
        });
        return toolResult(result);
      } catch (err) {
        const e = err instanceof Error ? err : new Error(String(err));
        return toolError(`graph_communities failed: ${e.message}`);
      }
    });
  • The findCommunities() method on Neo4jClient which implements greedy seed-based BFS clustering using pure Cypher (no GDS/APOC).
    // ─── Communities ───
    // Greedy seed-based BFS clustering. No GDS/APOC required — works on Aura Free.
    // Algorithm:
    //   1. Rank entities by strong-edge degree
    //   2. Take the highest-degree unassigned entity as a seed
    //   3. BFS through edges with weight > threshold up to max_hops
    //   4. Assign all reached entities to this community
    //   5. Repeat until max_communities reached or no more high-degree seeds
    
    async findCommunities(tenantId: string, options: {
      weight_threshold?: number;
      max_communities?: number;
      max_hops?: number;
      min_size?: number;
    } = {}): Promise<{
      communities: Array<{
        id: number;
        seed_name: string;
        seed_id: string;
        member_count: number;
        dominant_type: string;
        members: Array<{ id: string; name: string; type: string }>;
      }>;
      coverage: {
        total_entities: number;
        assigned: number;
        unassigned: number;
      };
    }> {
      const threshold = options.weight_threshold ?? 0.4;
      const maxCommunities = options.max_communities ?? 10;
      const maxHops = Math.max(1, Math.min(options.max_hops ?? 3, 4));
      const minSize = options.min_size ?? 2;
    
      // Step 1: rank nodes by strong-edge degree (tenant-scoped)
      const hubRows = await this.run(
        `
        MATCH (n:Entity {tenant_id: $tenantId})-[r]-(other:Entity {tenant_id: $tenantId})
        WHERE r.weight > $threshold
        WITH n, count(r) AS degree
        WHERE degree >= 2
        RETURN n.id AS id,
               n.name AS name,
               [l IN labels(n) WHERE l <> 'Entity'][0] AS type,
               degree
        ORDER BY degree DESC
        LIMIT 100
        `,
        { tenantId, threshold },
      );
    
      // Total entity count for coverage stats (tenant-scoped)
      const totalRows = await this.run(
        `MATCH (n:Entity {tenant_id: $tenantId}) RETURN count(n) AS total`,
        { tenantId },
      );
      const totalEntities = Number(totalRows[0]?.["total"] ?? 0);
    
      const assigned = new Set<string>();
      const communities: Array<{
        id: number;
        seed_name: string;
        seed_id: string;
        member_count: number;
        dominant_type: string;
        members: Array<{ id: string; name: string; type: string }>;
      }> = [];
    
      for (const hub of hubRows) {
        if (communities.length >= maxCommunities) break;
        const hubId = String(hub["id"]);
        if (assigned.has(hubId)) continue;
    
        // BFS: variable-length path from seed, all relationships above threshold,
        // confined to this tenant's subgraph. Path is bound to a variable so we
        // can pass it to nodes() — passing the pattern directly is List<Path>.
        const memberRows = await this.run(
          `
          MATCH (seed:Entity {tenant_id: $tenantId, id: $seedId})
          MATCH path = (seed)-[r*1..${maxHops}]-(m:Entity)
          WHERE ALL(node IN nodes(path) WHERE node.tenant_id = $tenantId)
            AND ALL(rel IN r WHERE rel.weight > $threshold)
          RETURN DISTINCT m.id AS id,
                 m.name AS name,
                 [l IN labels(m) WHERE l <> 'Entity'][0] AS type
          `,
          { tenantId, seedId: hubId, threshold },
        );
    
        const seedRow = {
          id: hubId,
          name: String(hub["name"] ?? hubId),
          type: String(hub["type"] ?? "?"),
        };
    
        const members = [seedRow];
        const seenInCluster = new Set<string>([hubId]);
        for (const row of memberRows) {
          const id = String(row["id"]);
          if (assigned.has(id) || seenInCluster.has(id)) continue;
          seenInCluster.add(id);
          members.push({
            id,
            name: String(row["name"] ?? id),
            type: String(row["type"] ?? "?"),
          });
        }
    
        if (members.length < minSize) continue;
    
        // Compute dominant type
        const typeCounts: Record<string, number> = {};
        for (const m of members) typeCounts[m.type] = (typeCounts[m.type] ?? 0) + 1;
        const dominantType = Object.entries(typeCounts).sort((a, b) => b[1] - a[1])[0]?.[0] ?? "?";
    
        // Mark these as assigned (greedy: each entity belongs to the first community that grabs it)
        for (const m of members) assigned.add(m.id);
    
        communities.push({
          id: communities.length + 1,
          seed_name: seedRow.name,
          seed_id: seedRow.id,
          member_count: members.length,
          dominant_type: dominantType,
          members: members.slice(0, 30),
        });
      }
    
      return {
        communities,
        coverage: {
          total_entities: totalEntities,
          assigned: assigned.size,
          unassigned: totalEntities - assigned.size,
        },
      };
    }
Behavior5/5

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

Annotations indicate read-only; description adds that no GDS/APOC is needed, explains seed selection and BFS behavior, and notes community assignment logic, providing rich context beyond annotations.

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

Conciseness5/5

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

Four sentences covering purpose, algorithm, use case, output format, and alternatives—no wasted words, front-loaded with key information.

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

Completeness4/5

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

Covers algorithm details, return structure, filtering, and alternative tools. Minor gap: no mention of behavior when no communities are found, but otherwise thorough.

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 coverage is 100% with clear descriptions for each parameter. The description reaffirms parameter roles but does not add significant new meaning beyond the schema.

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

Purpose5/5

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

Clearly states the tool finds clusters of densely-interconnected entities, describes the algorithm and output format, and distinguishes from siblings by advising when to use alternatives.

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

Usage Guidelines5/5

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

Explicitly advises to use graph_query or graph_search when starting from a specific entity, and explains the algorithmic constraints (greedy seed-based BFS, weight threshold, unique community assignment).

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/stevepridemore/graph-memory'

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