Firestore数据库查询工具

一个MCP服务器,允许通过Gemini CLI直接查询Firebase Firestore数据库,提供文档获取、查询、搜索和计数等功能。

  • by salmasaleem01
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 salmasaleem01/MCP-Configuration-with-Firebase

使用说明

使用Firebase进行MCP配置

此项目提供了一个MCP(模型上下文协议)服务器,允许您直接从Gemini CLI查询Firebase Firestore数据库。

特性

MCP服务器提供6种不同的工具用于与Firebase数据库交互:

  1. get_document -从集合中按ID获取单个文档
  2. 查询集合 -使用筛选器、排序和限制查询集合
  3. 获取所有文档 -从集合中获取所有文档(有可选限制)
  4. 搜索文档 -搜索包含特定搜索词的文档
  5. count_文档 -统计集合中的文档(使用可选筛选器)
  6. get_collection_list -列出Firestore数据库中的所有收藏

先决条件

  • Node.js(v18或更高版本)
  • npm或纱线
  • 启用Firestore的Firebase项目
  • Firebase服务帐户JSON密钥
  • 已安装并配置Gemini CLI

安装说明

1.安装依赖项

npm install

2.建设项目

npm run build

3.配置Firebase

选项A:使用服务帐户文件(推荐)

  1. 从Firebase控制台下载您的Firebase服务帐户JSON密钥:

    • 转到项目设置→ 服务帐户
    • 点击“生成新私钥”
    • 将文件另存为 firebase-service-account.json 在项目根中
  2. 配置已设置为在中使用此文件 gemini-mcp-config.json

选项B:使用环境变量

您还可以通过环境变量设置Firebase配置:

export FIREBASE_PROJECT_ID="your-project-id"

或者通过运行以下命令使用应用程序默认凭据:

gcloud auth application-default login

4.使用游标配置MCP(建议直接集成)

Cursor内置了对MCP服务器的支持。要将此MCP服务器与Cursor连接,请执行以下操作:

选项A:使用光标设置UI

  1. 打开光标设置:

    • 按 Cmd+, (macOS)或 Ctrl+, (Windows/Linux)
    • 或前往 Cursor → Settings → Features → MCP Servers
  2. 添加Firebase MCP服务器:

    • 点击“添加MCP服务器”或使用“+”按钮
    • 配置如下:
      • 名字: firebase
      • 命令: node
      • 参数:
        /Users/salmasaleem/Desktop/MCP Configuration with Firebase/dist/index.js
        
      • 环境变量:
        FIREBASE_SERVICE_ACCOUNT=/Users/salmasaleem/Desktop/MCP Configuration with Firebase/firebase-service-account.json
        

选项B:使用配置文件

  1. 更新路径 cursor-mcp-config.json 以匹配您的系统

  2. 将配置复制到Cursor的MCP设置位置:

    • macOS: ~/Library/Application Support/Cursor/User/globalStorage/mcp.json
    • 视窗: %APPDATA%\Cursor\User\globalStorage\mcp.json
    • Linux: ~/.config/Cursor/User/globalStorage/mcp.json
  3. 或者将配置合并到Cursor现有的MCP设置文件中

选项C:工作空间设置(特定于项目)

创建一个 .cursor/mcp.json 在项目根目录中使用以下文件:

{
  "mcpServers": {
    "firebase": {
      "command": "node",
      "args": [
        "/Users/salmasaleem/Desktop/MCP Configuration with Firebase/dist/index.js"
      ],
      "env": {
        "FIREBASE_SERVICE_ACCOUNT": "/Users/salmasaleem/Desktop/MCP Configuration with Firebase/firebase-service-account.json"
      }
    }
  }
}

5.配置Gemini CLI(备选)

如果您更喜欢使用Gemini CLI而不是Cursor:

Gemini CLI配置文件(gemini-mcp-config.json)应放置在适当的位置:

对于macOS/Linux:

~/.config/gemini/mcp.json

更新中的路径 gemini-mcp-config.json 要匹配您的实际项目路径:

{
  "mcpServers": {
    "firebase": {
      "command": "node",
      "args": [
        "/absolute/path/to/MCP Configuration with Firebase/dist/index.js"
      ],
      "env": {
        "FIREBASE_SERVICE_ACCOUNT": "/absolute/path/to/firebase-service-account.json"
      }
    }
  }
}

6.使用光标开始使用

配置后:

  1. 重新启动Cursor以加载MCP配置
  2. 打开光标聊天/编辑器
  3. 现在,您可以直接从Cursor查询Firebase数据库!

当您与Cursor的AI助手聊天时,MCP服务器将自动可用。

查询示例

以下是您可以尝试的5个示例查询:

查询1:获取特定文档

Use the get_document tool to get the document with ID "user123" from the "users" collection

查询2:使用过滤器进行查询

Use query_collection to find all documents in the "posts" collection where the "status" field equals "published" and order them by "createdAt" in descending order, limit to 10 results

查询3:获取所有文档

Use get_all_documents to get all documents from the "products" collection, limit to 50 results

查询4:搜索文档

Use search_documents to search for any documents in the "articles" collection that contain the term "javascript" in any field

查询5:盘点单据

Use count_documents to count how many documents are in the "orders" collection where the "status" field equals "completed"

奖金查询:列表收藏

Use get_collection_list to see all available collections in the database

项目结构

.
├── src/
│   └── index.ts          # MCP server implementation
├── dist/                 # Compiled JavaScript (generated)
├── package.json          # Dependencies and scripts
├── tsconfig.json         # TypeScript configuration
├── cursor-mcp-config.json  # Cursor IDE MCP configuration
├── gemini-mcp-config.json  # Gemini CLI MCP configuration
├── firebase-service-account.example.json  # Example service account template
└── README.md            # This file

发展

开发和测试:

# Watch mode for development
npm run dev

# Build once
npm run build

# Run the server (for testing)
npm start

故障排除

Firebase初始化错误

  • 确保您的服务帐户JSON文件有效且位置正确
  • 检查Firebase项目是否启用了Firestore
  • 验证服务帐户是否具有适当的权限

MCP连接问题

  • 验证配置文件中的路径是否绝对正确
  • 确保项目建成(npm run build)
  • 添加/更新MCP配置后重新启动Cursor
  • 在输出面板中检查Cursor的MCP服务器日志(查看→ 输出→ 选择“MCP”)
  • 验证Node.js在PATH中是否可访问: which node 或 node --version

权限错误

  • 确保您的Firebase服务帐户具有“Cloud Datastore User”角色
  • 验证Firestore安全规则是否允许服务帐户读取数据

许可证

麻省理工学院