#
tokens: 1899/50000 6/6 files
lines: off (toggle) GitHub
raw markdown copy
# Directory Structure

```
├── .gitignore
├── package-lock.json
├── package.json
├── README.md
├── src
│   ├── .prettierignore
│   └── index.ts
└── tsconfig.json
```

# Files

--------------------------------------------------------------------------------
/src/.prettierignore:
--------------------------------------------------------------------------------

```
build
pocketbase

```

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
node_modules/
build/
pocketbase/
*.log
.env*
```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
# pocketbase-mcp-server MCP Server

Model Context Protocol Server for PocketBase

This is a TypeScript-based MCP server that provides:

- Tools for listing PocketBase collections

## Features

### Tools

- `pocketbase_list_collections` - List all collections from a PocketBase instance
  - Requires server to be started with PocketBase configuration
  - Returns JSON representation of all collections

## Development

Install dependencies:

```bash
npm install
```

Build the server:

```bash
npm run build
```

For development with auto-rebuild:

```bash
npm run watch
```

## Installation

To use with Claude Desktop, add the server config:

On MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
On Windows: `%APPDATA%/Claude/claude_desktop_config.json`

```json
{
  "mcpServers": {
    "pocketbase-mcp-server": {
      "command": "/path/to/pocketbase-mcp-server/build/index.js --pb-url=http://localhost:8090 [email protected] --pb-admin-password=your-secure-password"
    }
  }
}
```

### PocketBase Configuration

To enable the PocketBase collections tool, you must provide the following configuration either as command line arguments or environment variables:

- `--pb-url=<url>` or `PB_URL` - The URL of your PocketBase instance (e.g., http://localhost:8090)
- `--pb-admin-email=<email>` or `PB_ADMIN_EMAIL` - Admin email for authentication
- `--pb-admin-password=<password>` or `PB_ADMIN_PASSWORD` - Admin password for authentication

If using environment variables, you can set them like this:

```bash
export PB_URL=http://localhost:8090
export [email protected]
export PB_ADMIN_PASSWORD=your-secure-password
```

Example using command line arguments:

```bash
node build/index.js --pb-url=http://localhost:8090 [email protected] --pb-admin-password=your-secure-password
```

### Debugging

Since MCP servers communicate over stdio, debugging can be challenging. We recommend using the [MCP Inspector](https://github.com/modelcontextprotocol/inspector), which is available as a package script:

```bash
npm run inspector
```

The Inspector will provide a URL to access debugging tools in your browser.

```

--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

```

--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------

```json
{
  "name": "pocketbase-mcp-server",
  "version": "0.1.0",
  "description": "Model Context Protocol Server for PocketBase",
  "private": true,
  "type": "module",
  "bin": {
    "pocketbase-mcp-server": "./build/index.js"
  },
  "files": [
    "build"
  ],
  "scripts": {
    "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
    "prepare": "npm run build",
    "watch": "tsc --watch",
    "inspector": "npx @modelcontextprotocol/inspector build/index.js"
  },
  "dependencies": {
    "@modelcontextprotocol/sdk": "0.6.0",
    "pocketbase": "^0.25.2"
  },
  "devDependencies": {
    "@types/node": "^20.11.24",
    "prettier": "3.5.2",
    "typescript": "^5.3.3"
  },
  "prettier": {
    "printWidth": 120,
    "singleQuote": true
  }
}

```

--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------

```typescript
#!/usr/bin/env node

/**
 * This is an MCP server to manage PocketBase via LLMs.
 */

import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import PocketBase from 'pocketbase';

/**
 * PocketBase configuration options obtained from command line args.
 */
interface PocketBaseConfig {
  url: string;
  adminEmail: string;
  adminPassword: string;
}

/**
 * Parse configuration from command line arguments and environment variables.
 * Command line arguments take precedence over environment variables.
 */
function getPocketBaseConfig(): PocketBaseConfig {
  const args = process.argv.slice(2);
  const url = args.find((arg) => arg.startsWith('--pb-url='))?.split('=')[1] || process.env.PB_URL;
  const adminEmail =
    args.find((arg) => arg.startsWith('--pb-admin-email='))?.split('=')[1] || process.env.PB_ADMIN_EMAIL;
  const adminPassword =
    args.find((arg) => arg.startsWith('--pb-admin-password='))?.split('=')[1] || process.env.PB_ADMIN_PASSWORD;

  if (!url || !adminEmail || !adminPassword) {
    console.error(
      'PocketBase configuration is required. Please provide --pb-url, --pb-admin-email, and --pb-admin-password as command line arguments or set PB_URL, PB_ADMIN_EMAIL, and PB_ADMIN_PASSWORD as environment variables.',
    );
    process.exit(1);
  }

  return { url, adminEmail, adminPassword };
}

// Load PocketBase configuration from command line args or environment variables
const pocketBaseConfig = getPocketBaseConfig();

/**
 * Create an MCP server with capabilities for resources,
 * tools, and prompts.
 */
const server = new Server(
  {
    name: 'pocketbase-mcp-server',
    version: '0.1.0',
  },
  {
    capabilities: {
      resources: {},
      tools: {},
      prompts: {},
    },
  },
);

/**
 * Handler that lists available tools.
 * Exposes tools for creating notes and listing PocketBase collections (if configured).
 */
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: 'pocketbase_list_collections',
        description: 'List PocketBase Collections',
        inputSchema: {
          type: 'object',
          properties: {},
          required: [],
        },
      },
    ],
  };
});

/**
 * Handler for tool calls.
 * Handles:
 * - pocketbase_list_collections: Connects to PocketBase and lists all collections
 */
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  switch (request.params.name) {
    case 'pocketbase_list_collections': {
      if (!pocketBaseConfig) {
        throw new Error(
          'PocketBase configuration is not available. Please start the server with --pb-url, --pb-admin-email, and --pb-admin-password arguments.',
        );
      }

      try {
        // Connect to PocketBase
        const pb = new PocketBase(pocketBaseConfig.url);

        // Authenticate as admin
        await pb
          .collection('_superusers')
          .authWithPassword(pocketBaseConfig.adminEmail, pocketBaseConfig.adminPassword);

        // Fetch all collections
        const collections = await pb.collections.getFullList();

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(collections, null, 2),
            },
          ],
        };
      } catch (error) {
        throw new Error(
          `Failed to list PocketBase collections: ${error instanceof Error ? error.message : String(error)}`,
        );
      }
    }

    default:
      throw new Error('Unknown tool');
  }
});

/**
 * Start the server using stdio transport.
 * This allows the server to communicate via standard input/output streams.
 */
async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
}

main().catch((error) => {
  console.error('Server error:', error);
  process.exit(1);
});

```