Skip to content

Add metrics for MCP #8497

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

Merged
merged 6 commits into from
May 5, 2025
Merged
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
11 changes: 10 additions & 1 deletion src/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { requireAuth } from "../requireAuth.js";
import { Options } from "../options.js";
import { getProjectId } from "../projectUtils.js";
import { mcpAuthError, NO_PROJECT_ERROR } from "./errors.js";
import { trackGA4 } from "../track.js";
import { Config } from "../config.js";

const SERVER_VERSION = "0.0.1";
Expand Down Expand Up @@ -62,6 +63,7 @@ export class FirebaseMcpServer {

async mcpListTools(): Promise<ListToolsResult> {
const hasActiveProject = !!(await this.getProjectId());
await trackGA4("mcp_list_tools", {});
return {
tools: this.availableTools.map((t) => t.mcp),
_meta: {
Expand Down Expand Up @@ -117,8 +119,15 @@ export class FirebaseMcpServer {

try {
const config = Config.load({ cwd: this.projectRoot });
return tool.fn(toolArgs, { projectId: await this.getProjectId(), host: this, config });
const res = await tool.fn(toolArgs, {
projectId: await this.getProjectId(),
host: this,
config,
});
await trackGA4("mcp_tool_call", { tool_name: toolName, error: res.isError ? 1 : 0 });
return res;
} catch (err: unknown) {
await trackGA4("mcp_tool_call", { tool_name: toolName, error: 1 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also mark as an error if isError is true in the returned content.

return mcpError(err);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { configstore } from "./configstore";
import { logger } from "./logger";
const pkg = require("../package.json");

Check warning on line 7 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Require statement not part of import statement

Check warning on line 7 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

type cliEventNames =
| "command_execution"
Expand All @@ -18,7 +18,9 @@
| "extensions_emulated"
| "function_deploy"
| "codebase_deploy"
| "function_deploy_group";
| "function_deploy_group"
| "mcp_tool_call"
| "mcp_list_tools";
type GA4Property = "cli" | "emulator" | "vscode";
interface GA4Info {
measurementId: string;
Expand Down Expand Up @@ -66,7 +68,7 @@
value: process.version,
},
cli_version: {
value: pkg.version,

Check warning on line 71 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .version on an `any` value

Check warning on line 71 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
},
firepit_version: {
value: process.env.FIREPIT_VERSION || "none",
Expand Down Expand Up @@ -104,10 +106,10 @@
[key: string]: string | number | undefined;
}

export async function trackGA4(

Check warning on line 109 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
eventName: cliEventNames,
params: AnalyticsParams,
duration: number = 1, // Default to 1ms duration so that events show up in realtime view.

Check warning on line 112 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Type number trivially inferred from a number literal, remove type annotation
): Promise<void> {
const session = cliSession();
if (!session) {
Expand All @@ -123,7 +125,7 @@
}

/**
* Record an emulator-related event for Analytics.

Check warning on line 128 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Expected only 0 line after block description
*
* @param eventName the event name in snake_case. (Formal requirement:
* length <= 40, alpha-numeric characters and underscores only
Expand Down Expand Up @@ -157,7 +159,7 @@
}

/**
* Record a vscode-related event for Analytics.

Check warning on line 162 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Expected only 0 line after block description
*
* @param eventName the event name in snake_case. (Formal requirement:
* length <= 40, alpha-numeric characters and underscores only
Expand Down Expand Up @@ -302,11 +304,11 @@
commandName?: string;
}

export function emulatorSession(): AnalyticsSession | undefined {

Check warning on line 307 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
return session("emulator");
}

export function vscodeSession(): AnalyticsSession | undefined {

Check warning on line 311 in src/track.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
return session("vscode");
}

Expand Down
Loading