Skip to main content
Glama

Detect Knowledge Communities

graph_communities
Read-only

Find clusters of densely connected entities in your knowledge graph by traversing high-weight edges from high-degree seeds. Groups related concepts like 'infrastructure' into communities without requiring plugins.

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

  • Registration of the graph_communities tool with the MCP server. Defines title, description, input schema (weight_threshold, max_communities, max_hops, min_size), and calls client.findCommunities().
    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 actual implementation of community detection. Uses greedy seed-based BFS: ranks entities by strong-edge degree, picks highest-degree unassigned seed, BFS through edges above weight threshold up to max_hops, assigns all reached entities to that community. Returns communities sorted by size with coverage stats.
    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,
        },
      };
    }
Behavior4/5

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

Annotations declare readOnlyHint=true, and the description adds significant behavioral insights: the greedy seed-based BFS, assignment of each entity to at most one community, and filtering thresholds. This goes well beyond the annotation alone.

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 the main purpose, followed by algorithm details and usage alternatives. Every sentence adds value, though it could be slightly more streamlined.

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

Completeness5/5

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

The description fully explains the tool's functionality, algorithm, constraints, and return format (with example structure). No output schema exists, but the description provides sufficient detail about the returned communities and their sorting.

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

Parameters4/5

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

With 100% schema coverage, the baseline is 3. The description adds context on how parameters interact (e.g., weight_threshold controls traversal, max_communities limits output, min_size filters communities), enhancing understanding 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?

The description clearly states 'Find clusters of densely-interconnected entities in the graph' using a specific verb and resource. It distinguishes itself from siblings like graph_query and graph_search by noting the alternative use case for specific entities.

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?

The description explicitly states when to use this tool (for understanding knowledge neighborhoods) and when to use alternatives ('Use graph_query or graph_search instead when you have a specific entity to start from'). It also explains the algorithm's constraints.

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