Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions src/bin/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { markdownDocsOfResources } from "../mcp/resources/index.js";
import { markdownDocsOfTools } from "../mcp/tools/index.js";
import { SERVER_FEATURES, ServerFeature } from "../mcp/types";

const STARTUP_MESSAGE = `
const STDIO_STARTUP_MESSAGE = `
This is a running process of the Firebase MCP server. This command should only be executed by an MCP client. An example MCP client configuration might be:

{
Expand All @@ -23,12 +23,44 @@ This is a running process of the Firebase MCP server. This command should only b
}
`;

const HTTP_STARTUP_MESSAGE = (host: string, port: number) => `
Firebase MCP server is running in Streamable HTTP mode.

Endpoint: http://${host}:${port}/mcp

Example MCP client configuration:

{
"mcpServers": {
"firebase": {
"transport": {
"type": "streamable-http",
"url": "http://${host}:${port}/mcp"
}
}
}
}

Press Ctrl+C to stop the server.
`;

const HELP_TEXT = `Usage: firebase mcp [options]

Description:
Starts the Model Context Protocol (MCP) server for the Firebase CLI. This server provides a
standardized way for AI agents and IDEs to interact with your Firebase project.

Transport Modes:
The server supports two transport modes:

1. STDIO (Default):
- Uses standard input/output for communication
- Suitable for local MCP clients that spawn the server as a subprocess

2. Streamable HTTP:
- Uses HTTP POST/GET with Server-Sent Events (SSE) for streaming
- Suitable for remote connections, production deployments, and horizontal scaling

Tool Discovery & Loading:
The server automatically determines which tools to expose based on your project context.

Expand All @@ -47,15 +79,26 @@ Options:
If specified, auto-detection is disabled for other features.
--tools <tools> Comma-separated list of specific tools to enable. Disables
auto-detection entirely.
--transport <type> Transport mode: 'stdio' (default) or 'streamable-http'.
--port <number> HTTP server port (default: 8000). Only used with streamable-http.
--host <string> HTTP server host (default: 127.0.0.1). Only used with streamable-http.
--stateless Enable stateless mode for horizontal scaling. Only used with
streamable-http. Sessions are not tracked server-side.
-h, --help Show this help message.
`;

export type TransportType = "stdio" | "streamable-http";

export async function mcp(): Promise<void> {
const { values } = parseArgs({
options: {
only: { type: "string", default: "" },
tools: { type: "string", default: "" },
dir: { type: "string" },
transport: { type: "string", default: "stdio" },
port: { type: "string", default: "8000" },
host: { type: "string", default: "127.0.0.1" },
stateless: { type: "boolean", default: false },
"generate-tool-list": { type: "boolean", default: false },
"generate-prompt-list": { type: "boolean", default: false },
"generate-resource-list": { type: "boolean", default: false },
Expand Down Expand Up @@ -93,11 +136,34 @@ export async function mcp(): Promise<void> {
.split(",")
.map((t) => t.trim())
.filter((t) => t.length > 0);

// Validate transport type
const transport = values.transport as TransportType;
if (transport !== "stdio" && transport !== "streamable-http") {
console.error(`Invalid transport type: ${transport}. Must be 'stdio' or 'streamable-http'.`);
process.exit(1);
}

const port = parseInt(values.port, 10);
if (isNaN(port) || port < 1 || port > 65535) {
console.error(`Invalid port: ${values.port}. Must be a number between 1 and 65535.`);
process.exit(1);
}

const host = values.host;
const stateless = values.stateless;

const server = new FirebaseMcpServer({
activeFeatures,
enabledTools,
projectRoot: values.dir ? resolve(values.dir) : undefined,
});
await server.start();
if (process.stdin.isTTY) process.stderr.write(STARTUP_MESSAGE);

await server.start({ transport, port, host, stateless });

if (transport === "stdio" && process.stdin.isTTY) {
process.stderr.write(STDIO_STARTUP_MESSAGE);
} else if (transport === "streamable-http") {
process.stderr.write(HTTP_STARTUP_MESSAGE(host, port));
}
}
57 changes: 56 additions & 1 deletion src/mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ Make sure you have a working installation of [Node.js](http://nodejs.org/) and [

### Basic Configuration

The Firebase MCP server can work with any MCP client that supports standard I/O (stdio) as the transport medium. When the Firebase MCP server makes tool calls, it uses the same user credentials that authorize the Firebase CLI in the environment where it's running.
The Firebase MCP server supports two transport modes:

1. **STDIO (Default)**: Uses standard I/O for communication. Suitable for local MCP clients that spawn the server as a subprocess.
2. **Streamable HTTP**: Uses HTTP POST/GET with Server-Sent Events (SSE) for streaming. Suitable for remote connections, production deployments, and horizontal scaling.

When the Firebase MCP server makes tool calls, it uses the same user credentials that authorize the Firebase CLI in the environment where it's running.

Here are configuration instructions for popular AI-assistive tools:

Expand Down Expand Up @@ -138,6 +143,56 @@ To configure Firebase Studio to use the Firebase MCP server, edit or create the
}
```

### HTTP Transport (Streamable HTTP)

For remote connections or production deployments, you can run the Firebase MCP server with Streamable HTTP transport:

```bash
# Start the server with HTTP transport
npx firebase-tools mcp --transport streamable-http --port 8000

# With a specific host binding
npx firebase-tools mcp --transport streamable-http --host 0.0.0.0 --port 8000

# Stateless mode for horizontal scaling (no server-side session tracking)
npx firebase-tools mcp --transport streamable-http --port 8000 --stateless
```

#### HTTP Transport Options

| Option | Description | Default |
|--------|-------------|---------|
| `--transport` | Transport mode: `stdio` or `streamable-http` | `stdio` |
| `--port` | HTTP server port | `8000` |
| `--host` | HTTP server host | `127.0.0.1` |
| `--stateless` | Enable stateless mode for horizontal scaling | `false` |

#### MCP Client Configuration for HTTP Transport

Configure your MCP client to connect via HTTP:

```json
{
"mcpServers": {
"firebase": {
"transport": {
"type": "streamable-http",
"url": "http://localhost:8000/mcp"
}
}
}
}
```

#### HTTP Endpoints

When running in HTTP mode, the server exposes:

- `POST /mcp` - Main MCP endpoint for JSON-RPC requests
- `GET /mcp` - SSE endpoint for server-initiated messages
- `DELETE /mcp` - Session termination (when not in stateless mode)
- `GET /health` - Health check endpoint

## Usage

Once configured, the MCP server will automatically provide Firebase capabilities to your AI assistant. You can:
Expand Down
Loading