Firestore访问控制服务器

一个为Firebase Firestore提供安全、权限控制的MCP服务器,通过标准化接口实现AI助手和其他MCP客户端与Firestore数据库的交互。

  • by devlimelabs
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 devlimelabs/firestore-mcp

使用说明

Firestore MCP服务器

一种模型上下文协议(MCP)服务器,提供对Firebase Firestore的安全、权限控制的访问。该服务器允许AI助手和其他MCP客户端通过标准化接口与Firestore数据库进行交互。

特性

核心功能

  • 🔐 精细权限:在收集和操作层面控制访问
  • 📄 完整的CRUD操作:创建、读取、更新和删除文档
  • 🔍 高级查询:支持过滤、排序和限制结果
  • 📁 子集合支持:使用嵌套集合和文档
  • 🔄 批量操作:以原子方式执行多个操作
  • 💾 交易:确保数据与事务操作的一致性
  • 🎯 现场价值操作:原子增量、数组操作和服务器时间戳

安全与控制

  • ✅ 收集级访问控制
  • 🛡️ 操作特定权限(读、写、删除、查询)
  • 🔒 默认拒绝,带有明确的允许规则
  • 📋 有条件许可(即将推出)

安装

使用npm/yarn/pnpm

npm install mcp-firestore
# or
yarn add mcp-firestore
# or
pnpm add mcp-firestore

来源

git clone https://github.com/yourusername/mcp-firestore.git
cd mcp-firestore
pnpm install
pnpm build

配置

环境变量

创建一个 .env 使用您的Firestore配置文件:

# Required
FIRESTORE_PROJECT_ID=your-project-id

# Optional - for authentication
GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account.json

权限配置

创建一个 permissions.json 用于控制访问的文件:

{
  "collections": [
    {
      "collectionId": "users",
      "operations": ["read", "write", "query"]
    },
    {
      "collectionId": "posts",
      "operations": ["read", "query"]
    }
  ],
  "defaultAllow": false
}

用法

启动服务器

# With default permissions
mcp-firestore

# With custom permissions file
mcp-firestore --config permissions.json

# With full access (development only)
mcp-firestore --full-access

# With read-only access to specific collections
mcp-firestore --read-only --collections users,posts

Claude桌面集成

添加到您的Claude Desktop配置中:

{
  "servers": {
    "firestore": {
      "command": "mcp-firestore",
      "args": ["--config", "path/to/permissions.json"],
      "env": {
        "FIRESTORE_PROJECT_ID": "your-project-id"
      }
    }
  }
}

可用工具

基本操作

  1. 消防站列表集合

    • 列出所有可访问的收藏
    {}
    
  2. 消防站收集

    • 从集合中获取所有文档
    {
      "collectionId": "users"
    }
    
  3. firestore获取文档

    • 获取特定文档
    {
      "collectionId": "users",
      "documentId": "user123"
    }
    
  4. firestore创建文档

    • 创建新文档
    {
      "collectionId": "users",
      "documentId": "user123",
      "data": {
        "name": "John Doe",
        "email": "john@example.com"
      }
    }
    
  5. firestore更新文档

    • 更新现有文档
    {
      "collectionId": "users",
      "documentId": "user123",
      "data": {
        "name": "Jane Doe"
      }
    }
    
  6. firestore删除文档

    • 删除文档
    {
      "collectionId": "users",
      "documentId": "user123"
    }
    

查询操作

  1. firestore查询集合
    • 使用筛选器查询文档
    {
      "collectionId": "users",
      "filters": [
        {
          "field": "age",
          "operator": ">",
          "value": 18
        }
      ],
      "orderBy": {
        "field": "createdAt",
        "direction": "desc"
      },
      "limit": 10
    }
    

子集合操作

  1. firestore列表子集合

    • 列出文档的子集合
    {
      "documentPath": "users/user123"
    }
    
  2. firestore按路径获取集合

    • 从子集合中获取文档
    {
      "collectionPath": "users/user123/orders"
    }
    
  3. firestore按路径创建文档

    • 在子集合中创建文档
    {
      "collectionPath": "users/user123/orders",
      "data": {
        "item": "Widget",
        "quantity": 2
      }
    }
    

批量操作

  1. firestore批量写入

    • 以原子方式执行多个写入操作
    {
      "operations": [
        {
          "type": "create",
          "collectionPath": "products",
          "documentId": "product1",
          "data": { "name": "Widget" }
        },
        {
          "type": "update",
          "documentPath": "inventory/product1",
          "data": { "count": 100 }
        }
      ]
    }
    
  2. firestore批读取

    • 在一次操作中读取多个文档
    {
      "documentPaths": [
        "users/user1",
        "users/user2",
        "products/product1"
      ]
    }
    
  3. 火场交易

    • 通过读取和条件写入执行事务
    {
      "reads": ["products/product1"],
      "operations": [
        {
          "type": "update",
          "documentPath": "products/product1",
          "data": { "stock": 99 }
        }
      ],
      "conditionScript": "return readResults['products/product1'].data.stock > 0;"
    }
    

现场价值操作

  1. firestore 增量字段

    • 原子递增数值字段
    {
      "documentPath": "stats/daily",
      "field": "visitCount",
      "incrementBy": 1
    }
    
  2. 火力库阵列联合体

    • 向数组中添加不重复的元素
    {
      "documentPath": "users/user123",
      "field": "tags",
      "elements": ["premium", "verified"]
    }
    
  3. firestore服务器时间戳

    • 将字段设置为服务器时间戳
    {
      "documentPath": "users/user123",
      "fields": ["lastLogin", "modifiedAt"]
    }
    

资源

服务器还提供MCP资源,用于直接访问Firestore数据:

  • firestore://collections -列出所有收藏
  • firestore://collection/{collectionId} -访问收集数据
  • firestore://collection/{collectionId}/document/{documentId} -访问文档数据
  • firestore://path/{path} -访问任何路径(集合或文档)

例子

基本CRUD操作

// Create a new user
await client.callTool("firestore-create-document", {
  collectionId: "users",
  documentId: "user123",
  data: {
    name: "John Doe",
    email: "john@example.com",
    createdAt: new Date().toISOString()
  }
});

// Update user data
await client.callTool("firestore-update-document", {
  collectionId: "users",
  documentId: "user123",
  data: {
    lastLogin: new Date().toISOString()
  }
});

// Query active users
await client.callTool("firestore-query-collection", {
  collectionId: "users",
  filters: [
    { field: "status", operator: "==", value: "active" }
  ],
  orderBy: { field: "createdAt", direction: "desc" },
  limit: 10
});

使用子收藏

// Create an order for a user
await client.callTool("firestore-create-document-by-path", {
  collectionPath: "users/user123/orders",
  data: {
    items: ["widget1", "widget2"],
    total: 99.99,
    status: "pending"
  }
});

// Get all orders for a user
await client.callTool("firestore-get-collection-by-path", {
  collectionPath: "users/user123/orders"
});

批量操作

// Atomic updates across multiple documents
await client.callTool("firestore-batch-write", {
  operations: [
    {
      type: "update",
      documentPath: "products/widget1",
      data: { stock: 95 }
    },
    {
      type: "create",
      collectionPath: "orders",
      data: {
        product: "widget1",
        quantity: 5,
        userId: "user123"
      }
    },
    {
      type: "update",
      documentPath: "users/user123",
      data: { orderCount: 1 }
    }
  ]
});

现场价值操作

// Increment a counter
await client.callTool("firestore-increment-field", {
  documentPath: "stats/global",
  field: "totalOrders",
  incrementBy: 1
});

// Add tags without duplicates
await client.callTool("firestore-array-union", {
  documentPath: "products/widget1",
  field: "tags",
  elements: ["bestseller", "featured"]
});

// Set server timestamp
await client.callTool("firestore-server-timestamp", {
  documentPath: "logs/access",
  fields: ["timestamp", "lastModified"]
});

安全最佳实践

  1. 使用最小权限:仅授予对必要集合和操作的访问权限
  2. 默认拒绝:设置 defaultAllow: false 在生产环境中
  3. 服务帐户安全:保护您的服务帐户凭据
  4. 环境变量:从不将凭据提交到版本控制
  5. 审核访问权限:定期审查权限配置

发展

运行测试

pnpm test
pnpm test:coverage

建筑

pnpm build

码头工人

# Build image
docker build -t mcp-firestore .

# Run container
docker run -e FIRESTORE_PROJECT_ID=your-project mcp-firestore

故障排除

常见问题

  1. 身份验证错误

    • 确保 GOOGLE_APPLICATION_CREDENTIALS 指向有效的服务帐户
    • 检查服务帐户是否具有必要的Firestore权限
  2. 权限不足

    • 验证权限配置中是否列出了集合
    • 检查是否允许该操作进行收集
  3. 连接问题

    • 确认项目ID正确
    • 检查与Firestore的网络连接

贡献

欢迎投稿!请阅读我们的 贡献指南 了解详情。

许可证

MIT许可证-请参阅 许可证 了解详情。

致谢

建立在 模型上下文协议 通过Anthropic。