Skip to content

feat(mcp): Adds "get_environment" tool #8555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 7 additions & 6 deletions src/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
process.env.PROJECT_ROOT ??
process.cwd();
if (options.projectRoot) this.fixedRoot = true;
this.detectActiveFeatures();

Check warning on line 48 in src/mcp/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}

async detectActiveFeatures(): Promise<ServerFeature[]> {
Expand Down Expand Up @@ -97,12 +97,11 @@
return getProjectId(await this.resolveOptions());
}

async getAuthenticated(): Promise<boolean> {
async getAuthenticated(): Promise<string | null> {
try {
await requireAuth(await this.resolveOptions());
return true;
return (await requireAuth(await this.resolveOptions())) ?? null;
} catch (e) {
return false;
return null;
}
}

Expand All @@ -115,7 +114,7 @@
_meta: {
projectRoot: this.projectRoot,
projectDetected: hasActiveProject,
authenticated: await this.getAuthenticated(),
authenticatedUser: await this.getAuthenticated(),
activeFeatures: this.activeFeatures,
detectedFeatures: this.detectedFeatures,
},
Expand All @@ -129,7 +128,8 @@
if (!tool) throw new Error(`Tool '${toolName}' could not be found.`);

const projectId = await this.getProjectId();
if (tool.mcp._meta?.requiresAuth && !(await this.getAuthenticated())) return mcpAuthError();
const accountEmail = await this.getAuthenticated();
if (tool.mcp._meta?.requiresAuth && !accountEmail) return mcpAuthError();
if (tool.mcp._meta?.requiresProject && !projectId) return NO_PROJECT_ERROR;

try {
Expand All @@ -141,6 +141,7 @@
host: this,
config,
rc,
accountEmail,
});
await trackGA4("mcp_tool_call", { tool_name: toolName, error: res.isError ? 1 : 0 });
return res;
Expand Down
1 change: 1 addition & 0 deletions src/mcp/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

export interface ServerToolContext {
projectId?: string;
accountEmail?: string | null;
config: Config;
host: FirebaseMcpServer;
rc: RC;
Expand All @@ -16,7 +17,7 @@
mcp: {
name: string;
description?: string;
inputSchema: any;

Check warning on line 20 in src/mcp/tool.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
annotations?: {
title?: string;
readOnlyHint?: boolean;
Expand All @@ -34,7 +35,7 @@
fn: (input: z.infer<InputSchema>, ctx: ServerToolContext) => Promise<CallToolResult>;
}

export function tool<InputSchema extends ZodTypeAny>(

Check warning on line 38 in src/mcp/tool.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
options: Omit<ServerTool<InputSchema>["mcp"], "inputSchema"> & {
inputSchema: InputSchema;
},
Expand Down
48 changes: 48 additions & 0 deletions src/mcp/tools/core/get_environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { z } from "zod";
import { tool } from "../../tool.js";
import { toContent } from "../../util.js";
import { getAliases } from "../../../projectUtils.js";
import { dump } from "js-yaml";
import { getAllAccounts } from "../../../auth.js";

export const get_environment = tool(
{
name: "get_environment",
description:
"Retrieves information about the current Firebase environment including current user, project directory, active project, and more.",
inputSchema: z.object({}),
annotations: {
title: "Get Firebase Environment Info",
readOnlyHint: true,
},
_meta: {
requiresAuth: false,
requiresProject: false,
},
},
async (_, { projectId, host, accountEmail, rc, config }) => {

Check failure on line 23 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Async arrow function has no 'await' expression

Check failure on line 23 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / unit (22)

Async arrow function has no 'await' expression

Check failure on line 23 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Async arrow function has no 'await' expression

Check failure on line 23 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / unit (22)

Async arrow function has no 'await' expression

Check failure on line 23 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / unit (20)

Async arrow function has no 'await' expression
const aliases = projectId ? getAliases({ rc }, projectId) : [];
return toContent(`# Environment Information

Project Directory: ${host.projectRoot}${host.fixedRoot ? " (immutable - set via startup flag)" : ""}

Check warning on line 27 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "string | undefined" of template literal expression
Project Config Path: ${config.path("firebase.json")}
Active Project ID: ${
projectId ? `${projectId}${aliases.length ? ` (alias: ${aliases.join(",")})` : ""}` : "<NONE>"
}
Authenticated User: ${accountEmail || "<NONE>"}

# Available Project Aliases (format: '[alias]: [projectId]')

${dump(rc.projects).trim()}

# Available Accounts:

${dump(getAllAccounts().map((account) => account.user.email)).trim()}

# firebase.json contents:

\`\`\`json
${config.readProjectFile("firebase.json")}

Check warning on line 45 in src/mcp/tools/core/get_environment.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "any" of template literal expression
\`\`\``);
},
);
2 changes: 2 additions & 0 deletions src/mcp/tools/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import { use_project } from "./use_project.js";
import { get_sdk_config } from "./get_sdk_config.js";
import { list_apps } from "./list_apps.js";
import { get_environment } from "./get_environment.js";

Check failure on line 8 in src/mcp/tools/core/index.ts

View workflow job for this annotation

GitHub Actions / unit (20)

'get_environment' is defined but never used

Check failure on line 8 in src/mcp/tools/core/index.ts

View workflow job for this annotation

GitHub Actions / unit (22)

'get_environment' is defined but never used

Check failure on line 8 in src/mcp/tools/core/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'get_environment' is defined but never used

Check failure on line 8 in src/mcp/tools/core/index.ts

View workflow job for this annotation

GitHub Actions / unit (22)

'get_environment' is defined but never used

Check failure on line 8 in src/mcp/tools/core/index.ts

View workflow job for this annotation

GitHub Actions / unit (20)

'get_environment' is defined but never used

export const coreTools: ServerTool[] = [
get_project,
use_project,
list_apps,
get_sdk_config,
consult_assistant,
// get_environment, // leaving commented out for the moment
];
Loading