article
README
🚀 Firebase MCP 协议指南
Firebase MCP(Model Context Protocol)为开发者提供了通过模型上下文协议与 Firebase 服务进行交互的能力,涵盖身份验证、Firestore 数据库和云存储等功能。本指南将详细介绍其安装、配置、使用方法以及相关开发信息。
🚀 快速开始
安装
若要开始使用 Firebase MCP,请按以下步骤操作:
- 下载并安装 Node.js:请确保安装的是 LTS 版本的 Node.js。
- 安装 Firebase 工具:
npm install -g firebase-tools
- 初始化 Firebase 项目:
firebase init
配置
环境变量
在 src/index.ts 文件中,添加如下环境变量:
import { config } from 'dotenv';
config({
serviceAccountKeyPath: './path/to/service-account.json',
projectId: 'your-project-id',
storageBucket: 'your-storage-bucket.appspot.com'
});
✨ 主要特性
- 支持通过模型上下文协议与 Firebase 服务进行交互。
- 涵盖身份验证、Firestore 数据库和云存储等核心功能。
💻 使用示例
身份验证
基础用法
- 创建用户:使用
auth.createUser工具创建新用户。
{
"tool": "auth.createUser",
"params": {
"email": "user@example.com",
"password": "secure-password"
}
}
- 登录用户:使用
auth.signInUser工具让用户登录。
{
"tool": "auth.signInUser",
"params": {
"email": "user@example.com",
"password": "secure-password"
}
}
Firestore 数据库
基础用法
- 添加文档:在指定集合中添加新文档。
{
"tool": "firestore.addDoc",
"params": {
"collectionPath": "users",
"data": {
"name": "John Doe",
"email": "john@example.com"
}
}
}
- 查询数据:根据条件查询集合中的文档。
{
"tool": "firestore.queryCollection",
"params": {
"collectionPath": "users",
"filters": [
{
"field": "name",
"operator": "==",
"value": "John Doe"
}
]
}
}
云存储
基础用法
- 上传文件:将文件上传到指定目录。
{
"tool": "storage.uploadFile",
"params": {
"filePath": "uploads/profile.jpg",
"data": "base64-encoded-string"
}
}
- 获取文件信息:获取特定文件的元数据和下载链接。
{
"tool": "storage.getFileInfo",
"params": {
"filePath": "uploads/profile.jpg"
}
}
🔧 技术细节
开发信息
模块导出
在 src/index.ts 文件中,导出以下模块:
export const config = require('dotenv').config();
export { default as auth } from './auth';
export { default as firestore } from './firestore';
export { default as storage } from './storage';
接口定义
- 身份验证接口
interface AuthConfig {
serviceAccountKeyPath: string;
projectId: string;
}
interface CreateUserParams {
email: string;
password: string;
}
- Firestore 接口
interface FirestoreConfig extends AuthConfig {
storageBucket: string;
}
interface FirestoreDoc<T> {
id?: string;
data: T;
}
测试说明
初始化测试环境
在项目根目录中创建 testing.json 文件:
{
"serviceAccountKeyPath": "./test/service-account.json",
"projectId": "test-project-id",
"storageBucket": "test-storage-bucket.appspot.com"
}
运行测试
使用以下命令运行测试:
npm test
架构
核心模块
- Auth Module:处理身份验证相关操作。
- Firestore Module:管理数据库交互。
- Storage Module:负责云存储操作。
接口设计
-
Auth Interface:
createUser(params: CreateUserParams)signInUser(params: SignInUserParams)
-
Firestore Interface:
addDoc(collectionPath: string, doc: FirestoreDoc<any>)queryCollection(collectionPath: string, filters: Filter[])
🤝 贡献指南
提交代码
使用以下命令提交更改:
git add .
git commit -m "添加新功能"
git push origin main
📄 许可证
本项目遵循 MIT 许可证。请查看 LICENSE 文件以获取详细信息。
❓ 故障排除
常见问题
1. Firebase 初始化失败
- 原因:服务账户密钥路径不正确。
- 解决方法:检查
serviceAccountKeyPath是否指向正确的 JSON 文件。
2. 数据库查询无结果
- 原因:集合路径或过滤条件错误。
- 解决方法:确认集合路径和过滤条件是否正确。
联系方式
有问题请联系支持团队:
firebase support contact
感谢使用 Firebase MCP 协议!如需更多帮助,请参考 Firebase 官方文档。
微信扫一扫