PocketBase Model Context Protocol Server

repository·main·Indexed 41 days ago

https://github.com/mrwyndham/pocketbase-mcp

An MCP server providing tools for interacting with PocketBase databases via AI assistants like Claude, Cursor, and VS Code. It enables advanced database operations, schema management, and data manipulation, including collection management (create_collection, get_collection), record CRUD operations (create_record, list_records), user authentication, and administrative tasks like backup_database and import_data. Supports local installation, Docker, and Docker Compose.

Tokens
2.9K
Snippets
5
Records
12
Agent score
50%

What's inside PocketBase MCP

  1. Setup PocketBase MCP Server with Docker

    main

    You can run the server as a containerized service.

    1. Build the image: docker build -t pocketbase-mcp .

    2. Run the container: Use docker run with environment variables. If your PocketBase instance is running on your host machine, use http://host.docker.internal:8090 as the POCKETBASE_URL to allow the container to reach the host.

    3. Configure AI Assistant: Update your MCP settings to use docker run -i --rm as the command. The -i flag is required for stdio communication.

    # Build the image
    docker build -t pocketbase-mcp .
    
    # Run the container
    docker run -d \
      --name pocketbase-mcp \
      -e POCKETBASE_URL=http://host.docker.internal:8090 \
      -e POCKETBASE_ADMIN_EMAIL=your_admin@example.com \
      -e POCKETBASE_ADMIN_PASSWORD=your_admin_password \
      pocketbase-mcp
  2. Setup PocketBase MCP Server locally

    main

    To run the server locally, you must configure your AI assistant's MCP settings file (e.g., cline_mcp_settings.json for Cursor/Cline or .vscode/mcp.json for VS Code).

    Prerequisites:

    • You must have the repository cloned locally.
    • You must run npm run build to compile the TypeScript source into the build/index.js file.

    Configuration for Cline/Cursor: Add an entry to the mcpServers object in your cline_mcp_settings.json. Ensure the args path points to your local build/index.js file.

    Configuration for VS Code: Create or edit .vscode/mcp.json. This method allows using inputs to securely prompt for admin credentials at runtime.

    {
      "mcpServers": {
        "pocketbase-server": {
          "command": "node",
          "args": ["/path/to/your/pocketbase-mcp/build/index.js"],
          "env": {
            "POCKETBASE_URL": "http://127.0.0.1:8090",
            "POCKETBASE_ADMIN_EMAIL": "admin@example.com",
            "POCKETBASE_ADMIN_PASSWORD": "admin_password"
          },
          "disabled": false,
          "autoApprove": ["create_record", "create_collection"]
        }
      }
    }
  3. Configure PocketBase MCP via Docker Compose

    main

    For easier management, you can use a docker-compose.yml file.

    docker-compose.yml example:

    version: "3.8"
    
    services:
      pocketbase-mcp:
        build: .
        environment:
          - POCKETBASE_URL=http://host.docker.internal:8090
          - POCKETBASE_ADMIN_EMAIL=your_admin@example.com
          - POCKETBASE_ADMIN_PASSWORD=your_admin_password
        stdin_open: true
        tty: true

    MCP Settings for Docker Compose: When using Compose, configure your MCP settings to use docker-compose as the command:

    {
      "mcpServers": {
        "pocketbase-docker": {
          "command": "docker-compose",
          "args": ["run", "--rm", "pocketbase-mcp"],
          "disabled": false
        }
      }
    }
  4. Configure the PocketBase MCP Server environment

    main

    The PocketBase MCP server requires specific environment variables to connect to your PocketBase instance and perform administrative tasks.

    Required Variables:

    • POCKETBASE_URL: The full URL of your PocketBase instance (e.g., https://your-instance.pockethost.io).

    Administrative Variables (used for collection management and backups):

    • POCKETBASE_ADMIN_EMAIL: The email address of the superuser/admin.
    • POCKETBASE_ADMIN_PASSWORD: The password for the superuser/admin.
    # Example environment setup
    export POCKETBASE_URL="https://pb.example.com"
    export POCKETBASE_ADMIN_EMAIL="admin@example.com"
    export POCKETBASE_ADMIN_PASSWORD="secure_password"
  5. Usage examples for PocketBase MCP tools

    main

    Examples of how to call the MCP tools using a programmatic interface (e.g., via an MCP client implementation).

    // Create a new collection
    await mcp.use_tool("pocketbase", "create_collection", {
      name: "posts",
      schema: [
        {
          name: "title",
          type: "text",
          required: true,
        },
        {
          name: "content",
          type: "text",
          required: true,
        },
      ],
    });
    
    // Authenticate with password
    await mcp.use_tool("pocketbase", "authenticate_user", {
      email: "user@example.com",
      password: "securepassword",
      collection: "users",
    });
  6. Available Tools in PocketBase MCP

    main

    The server provides the following tools for interacting with PocketBase:

    Collection Management

    • create_collection: Create a new collection with a custom schema.
    • get_collection: Retrieve schema details and metadata for a collection.

    Record Operations

    • create_record: Create a new record in a specific collection.
    • list_records: List records with support for filters and pagination.
    • update_record: Update an existing record.
    • delete_record: Delete a record.

    User Management

    • authenticate_user: Authenticate a user and retrieve an auth token.
    • create_user: Create a new user account.

    Database Operations

    • backup_database: Create a backup of the PocketBase database with specified format options.
  7. PocketBase MCP Environment Variables

    main

    The server uses the following environment variables for configuration:

    VariableRequiredDescription
    POCKETBASE_URLYesThe URL of your PocketBase instance (e.g., http://127.0.0.1:8090)
    POCKETBASE_ADMIN_EMAILNoAdmin email for administrative operations
    POCKETBASE_ADMIN_PASSWORDNoAdmin password for administrative operations
    POCKETBASE_DATA_DIRNoCustom data directory path
  8. Manage PocketBase records

    main

    Use these tools to perform CRUD operations on data within existing collections.

    create_record

    • Arguments:
      • collection (string, required): Collection name.
      • data (object, required): The record data.

    list_records

    • Arguments:
      • collection (string, required): Collection name.
      • filter (string): Filter query.
      • sort (string): Sort field and direction.
      • page (number): Page number.
      • perPage (number): Items per page.

    update_record

    • Arguments:
      • collection (string, required): Collection name.
      • id (string, required): Record ID.
      • data (object, required): Updated record data.

    delete_record

    • Arguments:
      • collection (string, required): Collection name.
      • id (string, required): Record ID.
  9. Authenticate users and manage sessions

    main

    The server supports various authentication flows for both standard users and administrators.

    authenticate_user

    Authenticates a user via email/password.

    • Arguments:
      • email (string, required): User email.
      • password (string, required): User password.
      • collection (string): Collection name (default: users).
      • isAdmin (boolean): If true, uses the _superusers collection and environment credentials if email/password are omitted.

    authenticate_with_oauth2

    • Arguments:
      • provider (string, required): e.g., google, github.
      • code (string, required): Authorization code.
      • codeVerifier (string, required): PKCE verifier.
      • redirectUrl (string, required): Redirect URL.
      • collection (string): Collection name (default: users).

    authenticate_with_otp

    • Arguments:
      • email (string, required): User email.
      • collection (string): Collection name (default: users).

    auth_refresh

    Refreshes the current authentication token.

    • Arguments:
      • collection (string): Collection name (default: users).

    impersonate_user (Admin only)

    • Arguments:
      • id (string, required): User ID to impersonate.
      • collectionIdOrName (string): Collection name/ID (default: users).
      • duration (number): Token expiry in seconds (default: 3600).
  10. Format PocketBase error messages

    main

    The server exports utility functions to handle and flatten complex PocketBase error objects into human-readable strings.

    • pocketbaseErrorMessage(errors: unknown): Returns a joined string of all error messages found in the error object. If no errors are found, returns "No errors found".
    • flattenErrors(errors: unknown): Recursively traverses error objects (including nested data properties) to extract all error message strings into a flat array.
  11. Manage PocketBase collections

    main

    The server provides tools to manage the schema of your PocketBase instance. Note that some operations require administrative credentials via environment variables.

    create_collection

    Creates a new collection. Automatically adds created and updated system fields.

    • Arguments:
      • name (string, required): Unique collection name.
      • type (string): base, view, or auth (default: base).
      • fields (array): List of field objects containing name, type (e.g., text, number, relation), required, values (for select), and collectionId (for relation).
      • createRule, updateRule, deleteRule, listRule, viewRule (strings): API rules.
      • viewQuery (string): SQL query for view type collections.
      • passwordAuth (object): { enabled: boolean, identityFields: string[] }.

    update_collection

    Updates an existing collection (Admin only).

    • Arguments:
      • collectionIdOrName (string, required): ID or name of the collection.
      • name, type, fields, etc.: Same as create_collection.

    get_collection

    Retrieves details for a specific collection.

    • Arguments:
      • collectionIdOrName (string, required): ID or name.
      • fields (string): Comma-separated list of fields to return.

    list_collections

    Lists all collections.

    • Arguments:
      • filter (string): Filter query.
      • sort (string): Sort order.

    delete_collection

    Deletes a collection (Admin only).

    • Arguments:
      • collectionIdOrName (string, required): ID or name.
  12. Perform administrative and maintenance tasks

    main

    Tools for system-level operations.

    backup_database

    Creates a database backup.

    • Arguments:
      • name (string): The name of the backup.

    import_data

    Imports an array of records into a collection.

    • Arguments:
      • collection (string, required): Target collection.
      • data (array, required): Array of record objects.
      • mode (string): create, update, or upsert (default: create).

    create_user

    Creates a new user account.

    • Arguments:
      • email (string, required): User email.
      • password (string, required): User password.
      • passwordConfirm (string, required): Confirmation.
      • name (string): User name.
      • collection (string): Collection name (default: users).