Skip to main content
Glama
fadlee

PocketBase MCP Server

by fadlee

generate_pb_schema

Convert TypeScript interfaces or database diagrams into PocketBase schema definitions to streamline database setup and ensure structural consistency.

Instructions

Generate a PocketBase schema based on TypeScript interfaces or database diagram

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
optionsNoGeneration options
sourceCodeYesTypeScript interface or database diagram to convert to PocketBase schema

Implementation Reference

  • The main handler function createGeneratePbSchemaHandler that analyzes TypeScript code, generates PocketBase collections from interfaces, adds auth and timestamps as needed, and returns the schema as JSON.
    export function createGeneratePbSchemaHandler(pb: PocketBase): ToolHandler {
      return async (args: GeneratePbSchemaArgs) => {
        try {
          const { sourceCode, options = {} } = args;
          const includeAuth = options.includeAuthentication ?? true;
          const includeTimestamps = options.includeTimestamps ?? true;
          
          // Analyze TypeScript source code
          const interfaces = analyzeTypeScriptForSchema(sourceCode, options);
          
          if (interfaces.length === 0) {
            return createTextResponse("No TypeScript interfaces found in the provided source code.");
          }
          
          const collections = [];
          
          // Generate collections from interfaces
          for (const iface of interfaces) {
            const fields = [];
            
            // Add standard fields if requested
            if (includeTimestamps) {
              fields.push(
                {
                  name: "created",
                  type: "autodate",
                  required: false,
                  system: true,
                  onCreate: true,
                  onUpdate: false,
                },
                {
                  name: "updated",
                  type: "autodate",
                  required: false,
                  system: true,
                  onCreate: true,
                  onUpdate: true,
                }
              );
            }
            
            // Convert interface properties to PocketBase fields
            for (const prop of iface.properties) {
              const field: any = {
                name: prop.name,
                type: mapTypeScriptToPocketBase(prop.type),
                required: !prop.optional,
              };
              
              // Add type-specific options
              if (field.type === "text" && prop.type.includes("email")) {
                field.type = "email";
              } else if (field.type === "text" && prop.type.includes("url")) {
                field.type = "url";
              } else if (field.type === "text" && prop.type.includes("Date")) {
                field.type = "date";
              }
              
              fields.push(field);
            }
            
            const collection = {
              name: iface.name.toLowerCase(),
              type: "base" as const,
              fields,
              listRule: "",
              viewRule: "",
              createRule: "",
              updateRule: "",
              deleteRule: "",
            };
            
            collections.push(collection);
          }
          
          // Add authentication collection if requested
          if (includeAuth) {
            const authCollection = {
              name: "users",
              type: "auth" as const,
              fields: [
                {
                  name: "name",
                  type: "text",
                  required: false,
                },
                {
                  name: "avatar",
                  type: "file",
                  required: false,
                  options: {
                    maxSelect: 1,
                    maxSize: 5242880,
                    mimeTypes: ["image/jpeg", "image/png", "image/svg+xml", "image/gif"],
                  },
                },
              ],
              listRule: "id = @request.auth.id",
              viewRule: "id = @request.auth.id",
              createRule: "",
              updateRule: "id = @request.auth.id",
              deleteRule: "id = @request.auth.id",
            };
            
            collections.unshift(authCollection);
          }
          
          const schema = {
            collections,
            generatedAt: new Date().toISOString(),
            source: "TypeScript interfaces",
          };
          
          return createJsonResponse(schema);
        } catch (error: unknown) {
          throw handlePocketBaseError("generate PocketBase schema", error);
        }
      };
    }
  • Input schema validation for the generate_pb_schema tool, defining sourceCode as required and options for authentication and timestamps.
    export const generatePbSchemaSchema = {
      type: "object",
      properties: {
        sourceCode: {
          type: "string",
          description: "TypeScript interface or database diagram to convert to PocketBase schema",
        },
        options: {
          type: "object",
          description: "Generation options",
          properties: {
            includeAuthentication: {
              type: "boolean",
              description: "Whether to include authentication related collections",
            },
            includeTimestamps: {
              type: "boolean",
              description: "Whether to include created/updated timestamps",
            },
          },
        },
      },
      required: ["sourceCode"],
    };
  • src/server.ts:206-210 (registration)
    Tool registration in the MCP server, specifying name, description, input schema, and handler.
      name: "generate_pb_schema",
      description: "Generate a PocketBase schema based on TypeScript interfaces or database diagram",
      inputSchema: generatePbSchemaSchema,
      handler: createGeneratePbSchemaHandler(pb),
    },
  • TypeScript interface defining the arguments for the generate_pb_schema handler.
    export interface GeneratePbSchemaArgs {
      sourceCode: string;
      options?: {
        includeAuthentication?: boolean;
        includeTimestamps?: boolean;
      };
    }
  • Helper function to map TypeScript types to corresponding PocketBase field types.
    function mapTypeScriptToPocketBase(tsType: string): string {
      const type = tsType.toLowerCase();
      
      if (type.includes("string")) return "text";
      if (type.includes("number")) return "number";
      if (type.includes("boolean")) return "bool";
      if (type.includes("date")) return "date";
      if (type.includes("email")) return "email";
      if (type.includes("url")) return "url";
      if (type.includes("[]")) return "json";
      if (type.includes("object") || type.includes("{")) return "json";
      
      return "text"; // Default fallback
    }
Behavior2/5

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

With no annotations provided, the description carries full burden but lacks behavioral details. It doesn't disclose whether this is a read-only generation or if it modifies the database, what permissions are needed, error handling, or output format. The description only states what it does, not how it behaves.

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?

The description is a single, efficient sentence with zero waste. It's front-loaded with the core purpose and includes essential qualifiers about input sources. Every word earns its place without redundancy.

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 code generation tool with no annotations and no output schema, the description is incomplete. It doesn't explain what the generated schema looks like, whether it's returned as text or applied directly, error conditions, or dependencies. This leaves significant gaps for an AI agent to understand the tool's behavior.

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 both parameters. The description adds no additional parameter semantics beyond implying 'sourceCode' is the input to convert, which is already clear from the schema. Baseline 3 is appropriate when schema does the heavy lifting.

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 the specific action ('generate') and resource ('PocketBase schema'), and distinguishes from siblings by specifying the input source ('TypeScript interfaces or database diagram'). This differentiates it from tools like 'get_collection_schema' (retrieval) or 'create_collection' (manual creation).

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

Usage Guidelines3/5

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

The description implies usage by mentioning the input source, but doesn't explicitly state when to use this tool versus alternatives like 'create_collection' (for manual schema creation) or 'generate_typescript_interfaces' (reverse process). No guidance on prerequisites or exclusions is provided.

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