Skip to main content
Glama
fadlee

PocketBase MCP Server

by fadlee

analyze_collection_data

Analyze data patterns and generate insights from a PocketBase collection to identify trends, distributions, and relationships within your database records.

Instructions

Analyze data patterns and provide insights about a collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name to analyze
optionsNoAnalysis options

Implementation Reference

  • The core handler function that implements the logic for the 'analyze_collection_data' tool. It fetches collection data, analyzes fields for null rates, uniqueness, min/max for numbers, and generates insights.
    export function createAnalyzeCollectionDataHandler(pb: PocketBase): ToolHandler {
      return async (args: AnalyzeCollectionDataArgs) => {
        try {
          const { collection, options = {} } = args;
          const sampleSize = options.sampleSize || 100;
          
          // Get collection info and records
          const collectionInfo = await pb.collections.getOne(collection);
          const records = await pb
            .collection(collection)
            .getList(1, sampleSize);
          
          // Initialize analysis structure
          const analysis = {
            collectionName: collection,
            recordCount: records.totalItems,
            fields: [] as any[],
            insights: [] as string[],
          };
          
          if (records.items.length === 0) {
            analysis.insights.push("No records available for analysis");
            return createJsonResponse(analysis);
          }
          
          // Analyze each field
          const fields = collectionInfo.fields || [];
          
          for (const field of fields) {
            if (options.fields && !options.fields.includes(field.name)) {
              continue;
            }
            
            const fieldAnalysis = {
              name: field.name,
              type: field.type,
              nonNullValues: 0,
              uniqueValues: new Set(),
              min: null as any,
              max: null as any,
            };
            
            // Analyze field values
            for (const record of records.items) {
              const value = record[field.name];
              
              if (value !== null && value !== undefined) {
                fieldAnalysis.nonNullValues++;
                fieldAnalysis.uniqueValues.add(JSON.stringify(value));
                
                // For numeric fields, track min/max
                if (field.type === "number") {
                  if (fieldAnalysis.min === null || value < fieldAnalysis.min) {
                    fieldAnalysis.min = value;
                  }
                  if (fieldAnalysis.max === null || value > fieldAnalysis.max) {
                    fieldAnalysis.max = value;
                  }
                }
              }
            }
            
            // Process analysis results
            const processedAnalysis = {
              ...fieldAnalysis,
              uniqueValueCount: fieldAnalysis.uniqueValues.size,
              fillRate: `${(
                (fieldAnalysis.nonNullValues / records.items.length) * 100
              ).toFixed(2)}%`,
              uniqueValues: undefined, // Remove the Set before serializing
            };
            
            analysis.fields.push(processedAnalysis);
            
            // Generate insights
            if (
              processedAnalysis.uniqueValueCount === records.items.length &&
              records.items.length > 5
            ) {
              analysis.insights.push(
                `Field '${field.name}' contains all unique values, consider using it as an identifier.`
              );
            }
            
            if (processedAnalysis.nonNullValues === 0) {
              analysis.insights.push(
                `Field '${field.name}' has no values. Consider removing it or ensuring it's populated.`
              );
            }
          }
          
          return createJsonResponse(analysis);
        } catch (error: unknown) {
          throw handlePocketBaseError("analyze collection data", error);
        }
      };
    }
  • The JSON schema defining the input parameters for the 'analyze_collection_data' tool, including collection name and optional analysis options.
    export const analyzeCollectionDataSchema = {
      type: "object",
      properties: {
        collection: {
          type: "string",
          description: "Collection name to analyze",
        },
        options: {
          type: "object",
          description: "Analysis options",
          properties: {
            sampleSize: {
              type: "number",
              description: "Number of records to sample for analysis (default: 100)",
            },
            fields: {
              type: "array",
              items: { type: "string" },
              description: "Specific fields to analyze (if not provided, all fields will be analyzed)",
            },
          },
        },
      },
      required: ["collection"],
    };
  • src/server.ts:166-170 (registration)
    The registration of the 'analyze_collection_data' tool in the MCP server array, linking the name, description, input schema, and handler function.
      name: "analyze_collection_data",
      description: "Analyze data patterns and provide insights about a collection",
      inputSchema: analyzeCollectionDataSchema,
      handler: createAnalyzeCollectionDataHandler(pb),
    },
  • TypeScript interface defining the argument types for the analyze_collection_data handler, used for type safety.
    export interface AnalyzeCollectionDataArgs {
      collection: string;
      options?: {
        sampleSize?: number;
        fields?: string[];
      };
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure but offers minimal information. It mentions 'analyze data patterns and provide insights' but doesn't specify what types of patterns or insights, whether it's read-only, if it requires specific permissions, or how results are returned. This leaves significant gaps for a tool that presumably performs data analysis.

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 a single, efficient sentence that gets straight to the point without unnecessary words. While it could be more informative, it doesn't waste space and is appropriately sized for a basic description.

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

Completeness2/5

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

For a data analysis tool with no annotations and no output schema, the description is insufficient. It doesn't explain what 'insights' means, what format results come in, whether analysis is statistical or visual, or any limitations. Given the complexity implied by 'analyze' and the lack of structured metadata, more context is needed.

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?

The description adds no parameter-specific information beyond what's already in the schema, which has 100% coverage. It doesn't explain what 'analyze' means in terms of the collection parameter or what 'insights' might relate to the options. Since the schema does the heavy lifting, the baseline score of 3 is appropriate.

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 action ('analyze data patterns and provide insights') and resource ('about a collection'), making the tool's purpose understandable. However, it doesn't differentiate from sibling tools like 'query_collection' or 'get_collection_schema' that might also involve collection analysis, so it doesn't reach the highest clarity level.

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. With siblings like 'query_collection' and 'list_records' that might overlap in data examination, there's no indication of when this analysis tool is preferred or what specific insights it provides that others don't.

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/fadlee/dynamic-pocketbase-mcp'

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