MCP Firebase Server
A bridge that enables Large Language Models to read from and write to Firebase Firestore collections through Model Context Protocol (MCP) tools.
README
MCP Firebase Server (Model Context Protocol)
This server implements the Model Context Protocol (MCP) to act as a bridge between a Large Language Model (LLM) like Claude and Firebase (Firestore). It allows the LLM to read from and write to Firestore collections by exposing these operations as MCP "tools."
This server is built using the official mcp Python SDK.
Prerequisites
- Python 3.7+ (preferably 3.8+ for
asynccontextmanagerand full type hinting features used by MCP) - Pip (Python package installer) or
uv(recommended by MCP docs for project management) - A Firebase project with Firestore enabled.
- A Firebase service account key JSON file.
Setup
-
Clone/Download: Ensure you have the server file (
mcp_firebase_server.py),requirements.txt, etc., in a local directory. -
Service Account Key:
- The server needs a Firebase service account key to authenticate.
- Option 1 (Recommended for MCP Client Configuration): Set the
SERVICE_ACCOUNT_KEY_PATHenvironment variable to the absolute path of your service account JSON file. This is the most flexible method when the server is launched by an MCP client. - Option 2 (Fallback): If the
SERVICE_ACCOUNT_KEY_PATHenvironment variable is not set, the server will look for a file namedserviceAccountKey.jsonin its own directory (the same directory asmcp_firebase_server.py). If using this method, rename your key file accordingly. - Important: Ensure your service account key file (however it's named or accessed) is kept secure and ideally listed in your
.gitignoreif a local copy exists in the project.
-
Firebase Storage Bucket (Optional):
- If you intend to use Firebase Storage functionalities with this server (currently no tools use it, but it can be added), set the
FIREBASE_STORAGE_BUCKETenvironment variable to your Firebase project's storage bucket name (e.g.,your-project-id.appspot.com). The server will read and print this value if set.
- If you intend to use Firebase Storage functionalities with this server (currently no tools use it, but it can be added), set the
-
Create a Virtual Environment (Recommended): Using
venv:python3 -m venv venv source venv/bin/activate # On macOS/Linux # venv\\Scripts\\activate # On WindowsOr, if using
uv(as suggested by MCP docs for new projects):uv venv source .venv/bin/activate # Or similar, depending on your uv setup -
Install Dependencies: Using
pip:pip install -r requirements.txtOr, if using
uv:uv pip install -r requirements.txtThis will install
mcp[cli]andfirebase-admin.
Running the Server
There are a couple of ways to run this MCP server:
-
Direct Execution (for stdio transport via
run_server.sh): Arun_server.shscript is provided to simplify launching the server. This script handles activating the virtual environment (if namedvenvand present in the project root) before running the Python script.First, make the script executable:
chmod +x run_server.shThen, run the server using the script:
./run_server.shThis is how an MCP client would typically be configured to launch the server (see "Using with Claude" section below).
-
**Using MCP CLI for Development and Inspection (
mcp dev): ThemcpCLI (installed as part ofmcp[cli]) provides a development server and inspector tool. This is highly recommended during development.mcp dev mcp_firebase_server.pyThis will start the server and often provide a web interface for inspecting its capabilities (tools, resources) and making test calls.
MCP Tools Exposed
This server, named MCPFirebaseServer, exposes the following tools:
1. query_firestore_collection
- Description (from docstring): Retrieves documents from a specified Firestore collection.
- Arguments:
collection_name(string, required): The name of the Firestore collection to query.limit(integer, optional, default: 50): The maximum number of documents to return.
- Returns: (List[Dict[str, Any]])
A list of documents from the collection. Each document is a dictionary including an
idfield. Returns a list containing a single error dictionary if an error occurs (e.g.,[{"error": "Firestore not initialized..."}]or[{"error": "Failed to query..."}]).
2. add_document_to_firestore
- Description (from docstring): Adds a new document with an auto-generated ID to the specified Firestore collection.
- Arguments:
collection_name(string, required): The name of the Firestore collection where the document will be added.document_data(object/dictionary, required): A dictionary representing the document to add.
- Returns: (Dict[str, Any])
A dictionary containing
success(boolean) and eitherid(string) andmessage(string) on success, orerror(string) on failure. Example success:{"success": True, "id": "newDocId", "message": "Document added to 'logs'"}Example failure:{"success": False, "error": "Firestore not initialized..."}
Using with Claude (or other MCP Clients)
This MCP Firebase Server is designed to be run as a separate process, typically launched by an MCP client application (such as Claude Desktop or a custom application built with a platform like Windsurf that can manage MCP servers). The client then communicates with this server, usually over stdio (standard input/output) for locally run servers.
General Integration Steps:
-
Server Availability: Ensure
mcp_firebase_server.pyand its dependencies (includingserviceAccountKey.json) are accessible on the system where the MCP client will run or can launch processes. -
Client Configuration: The MCP client application needs to be configured to know how to start your
MCPFirebaseServer. This configuration usually involves specifying:- A command to execute (e.g.,
pythonoruv run python). - Arguments for that command (e.g., the path to
mcp_firebase_server.py). - Optionally, any environment variables the server might need (though our current server expects
serviceAccountKey.jsonin the same directory, an environment variable for the key path could be an alternative).
- A command to execute (e.g.,
-
Launching and Communication:
- When the MCP client needs to use a tool provided by this server, it will launch
mcp_firebase_server.pyusing the configured command. - The client and server then communicate over the MCP protocol (e.g., via
stdio). The client can discover available tools (query_firestore_collection,add_document_to_firestore) and call them.
- When the MCP client needs to use a tool provided by this server, it will launch
Conceptual Configuration Example (for an MCP Client like Claude Desktop):
Many MCP-compatible client applications (like Claude Desktop, as referenced in MCP documentation) use a configuration file (often JSON) to define how to launch and manage MCP servers. While the exact format can vary by client, the principle is similar.
Below is a conceptual example based on patterns seen in MCP documentation. You would need to adapt this to the specific configuration mechanism of your chosen MCP client (Claude Desktop, Windsurf, etc.).
{
"mcpServers": {
"my_firebase_mcp_connector": { // A unique name you assign to this server instance in the client's config
"command": "/full/path/to/your/mc-firebase-server/run_server.sh", // IMPORTANT: Use the absolute path to the script
"args": [], // Typically empty if run_server.sh handles everything
// "cwd": "/full/path/to/your/mc-firebase-server/", // Usually not needed if run_server.sh cds to its own dir
"env": {
"SERVICE_ACCOUNT_KEY_PATH": "/Users/davidloor/projects/firebase/examcoachai/examcoachai-firebase-adminsdk-qwhk9-5c7a5b82e2.json",
"FIREBASE_STORAGE_BUCKET": "examcoachai.appspot.com" // Example bucket name, adjust to your actual bucket
}
}
}
}
Key points for the configuration:
"command": The executable to run (e.g.,python). Make sure it's in the system's PATH or provide the full path to the Python interpreter."args": A list of arguments. The first argument is typically the script to execute. It is crucial to use the full, absolute path tomcp_firebase_server.pyto ensure the client can find it, regardless of where the client itself is launched from."cwd"(Current Working Directory): Sometimes, you might need to specify the working directory for the server process, especially if it relies on relative paths for other files (though ourserviceAccountKey.jsonpath is relative to the script itself, which is generally robust if the script path is absolute)."env": For passing environment variables. While our current server locatesserviceAccountKey.jsonrelative to its own path, a common pattern for more configurable servers is to pass credential paths or other settings via environment variables.
Interaction Flow (Recap):
- Client Starts Server: The MCP client (using the configuration above) starts
mcp_firebase_server.py. - Server Initializes: Our server attempts to connect to Firebase.
- Tool Discovery & Calls: The client discovers and calls tools like
query_firestore_collectionoradd_document_to_firestoreas needed. - Server Responds: Results are sent back to the client via
stdio.
Specific Instructions for Claude Desktop or Windsurf:
- Claude Desktop: If you are using Claude Desktop, refer to its documentation on how to add and configure custom MCP servers. The JSON structure above is a common pattern you might adapt.
- Windsurf: If Windsurf is your orchestrator and it supports managing MCP servers, it will have its own method for defining and launching these external tool servers. You would need to consult Windsurf's documentation for the specifics, but the core information (command, arguments to run
mcp_firebase_server.py) will be the same.
If your client doesn't have a dedicated MCP server management UI/config file, but can execute shell commands and interact via stdio, you would programmatically launch the mcp_firebase_server.py script and then use an MCP client library (like the one in mcp.client.stdio) to communicate with it.
Development and Testing
- Use
mcp dev mcp_firebase_server.pyto run the server with the MCP Inspector. This allows you to see discovered tools and test them interactively. - Ensure
serviceAccountKey.jsonis correctly placed OR theSERVICE_ACCOUNT_KEY_PATHenvironment variable is set when the server is launched by an MCP client. - Check the server's console output for Firebase initialization messages and any runtime errors.
The run_server.sh Script:
The run_server.sh script in the project root is designed to:
- Determine its own location and change the current directory to there.
- Locate and activate a Python virtual environment named
venvif it exists in the project root. - Execute the
mcp_firebase_server.pyscript using thepythoninterpreter (ideally from the activated venv).
This script ensures that the MCP server runs in its intended environment. Remember to make it executable (chmod +x run_server.sh).
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.