Back to MCP directory
publicPublicdnsLocal runtime

pocketbase-mcp

PocketBase MCP Server是一个与PocketBase实例交互的MCP服务器,提供记录管理、集合管理、日志管理、定时任务管理和迁移管理等功能。

article

README

🚀 PocketBase MCP 服务器

PocketBase MCP 服务器提供了一套强大的数据库迁移系统,可高效管理 PocketBase 数据库的模式变更,为数据库模式管理提供了灵活且强大的工具。

🚀 快速开始

要在 PocketBase 中使用 MCP 服务器,请按照以下步骤配置:

  1. pocketbase.migrations 配置中启用 MCP:
{
  "mcp": {
    "enabled": true,
    "host": "localhost",
    "port": 9001,
    "tls": false,
    "cert": "",
    "key": ""
  }
}
  1. 启动 MCP 服务器:

    在控制台运行:

    node mcp-server.js
    

✨ 主要特性

PocketBase MCP 服务器的数据库迁移系统支持以下功能:

  1. 创建带有时间戳前缀和描述性名称的迁移文件
  2. 自动生成常见操作(创建集合、添加字段)的迁移文件
  3. 单个或批量应用和回滚迁移
  4. 跟踪已应用的迁移

📦 安装指南

依赖项

  • @pocketbase/migration-engine - PocketBase 迁移引擎
  • @pocketbase/core - PocketBase 核心模块
  • node >=14.0.0 - Node.js 版本要求

💻 使用示例

基础用法

功能命令

以下是在 MCP 服务器中使用 PocketBase 迁移系统所需的配置命令:

  • setMigrationsDirectory(directory: string) - 设置自定义迁移目录
  • createNewMigration(name: string) - 创建新的空白迁移文件
  • createCollectionMigration(collectionSchema: CollectionSchema) - 根据集合架构生成创建新集合的迁移文件
  • createAddFieldMigration(collectionId: string, field: Field) - 为指定集合添加字段的迁移文件
  • applyMigration(filePath: string, pocketbase: PocketBase) - 应用特定迁移文件
  • applyAllMigrations(pocketbase: PocketBase) - 应用所有未应用的迁移文件
  • revertMigration(filePath: string, pocketbase: PocketBase) - 回滚指定的迁移文件
  • revertToMigration(targetPathOrId: string | number, pocketbase: PocketBase) - 回滚到特定点(不包含该点)
  • runCronJob(id: string) - 运行指定 ID 的计划任务
  • listCronJobs() - 列出所有计划任务

示例代码

设置自定义迁移目录:

await setMigrationsDirectory("./my_migrations");

创建基本迁移:

await createNewMigration("add_user_email_index");

创建集合迁移:

await createCollectionMigration({
  id: "users",
  name: "users", 
  fields: [
    { name: "email", type: "email", required: true }
  ]
});

为集合添加字段:

await createAddFieldMigration("users", {
  name: "address",
  type: "text"
});

应用迁移:

// 应用特定迁移
await applyMigration("1744005374_update_transactions_add_debt_link.js", pocketbaseInstance);

// 应用所有待处理迁移
await applyAllMigrations(pocketbaseInstance);

回滚迁移:

// 回滚特定迁移
await revertMigration("1744005374_update_transactions_add_debt_link.js", pocketbaseInstance);

// 回滚到特定点(不包含该点)
await revertToMigration("1743958155_update_transactions_add_relation_to_itself.js", pocketbaseInstance);

// 回滚所有迁移
await revertToMigration("", pocketbaseInstance);

高级用法

迁移文件格式

迁移文件是 JavaScript 文件,带有时间戳前缀和描述性名称:

// 1744005374_update_transactions_add_debt_link.js
/// <reference path="../pb_data/types.d.ts" />
migrate((app) => {
  // 上行迁移代码在这里
  return app.save();
}, (app) => {
  // 下行迁移代码在这里
  return app.save();
});
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client