Skip to content

initial apphosting mcp tool #8605

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 18 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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: 13 additions & 0 deletions src/gcp/apphosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
done: boolean;
// oneof result
error?: Status;
response?: any;

Check warning on line 269 in src/gcp/apphosting.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
// end oneof result
}

Expand Down Expand Up @@ -333,6 +333,19 @@
return res.body;
}

/**
* Gets traffic details.
*/
export async function getTraffic(
projectId: string,
location: string,
backendId: string,
): Promise<Traffic> {
const name = `projects/${projectId}/locations/${location}/backends/${backendId}/traffic`;
const res = await client.get<Traffic>(name);
return res.body;
}

/**
* List all backends present in a project and location.
*/
Expand Down Expand Up @@ -548,14 +561,14 @@
/**
* Ensure that the App Hosting API is enabled on the project.
*/
export async function ensureApiEnabled(options: any): Promise<void> {

Check warning on line 564 in src/gcp/apphosting.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
const projectId = needProjectId(options);

Check warning on line 565 in src/gcp/apphosting.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `{ projectId?: string | undefined; project?: string | undefined; rc?: RC | undefined; }`
return await ensure(projectId, apphostingOrigin(), "app hosting", true);
}

/**
* Generates the next build ID to fit with the naming scheme of the backend API.
* @param counter Overrides the counter to use, avoiding an API call.

Check warning on line 571 in src/gcp/apphosting.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Expected @param names to be "projectId, location, backendId, counter". Got "counter"
*/
export async function getNextRolloutId(
projectId: string,
Expand Down
4 changes: 4 additions & 0 deletions src/mcp/tools/apphosting/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ServerTool } from "../../tool";
import { list_backends } from "./list_backends";

export const appHostingTools: ServerTool[] = [list_backends];
46 changes: 46 additions & 0 deletions src/mcp/tools/apphosting/list_backends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { z } from "zod";
import { tool } from "../../tool.js";
import { toContent } from "../../util.js";
import { NO_PROJECT_ERROR } from "../../errors.js";
import {
Backend,
getTraffic,
listBackends,
parseBackendName,
Traffic,
} from "../../../gcp/apphosting.js";

export const list_backends = tool(
{
name: "list_backends",
description:
"Retrieves a list of App Hosting backends in the current project. The `uri` is the public URL of the backend.",
inputSchema: z.object({
location: z
.string()
.optional()
.default("-")
.describe("Limit the listed backends to this region."),
}),
annotations: {
title: "List App Hosting backends.",
readOnlyHint: true,
},
_meta: {
requiresAuth: true,
requiresProject: true,
},
},
async ({ location } = {}, { projectId }) => {
if (!projectId) return NO_PROJECT_ERROR;
if (!location) location = "-";
const data: (Backend & { traffic: Traffic })[] = [];
const backends = await listBackends(projectId, location);
for (const backend of backends.backends) {
const { location, id } = parseBackendName(backend.name);
const traffic = await getTraffic(projectId, location, id);
data.push({ ...backend, traffic: traffic });
}
return toContent(data);
},
);
2 changes: 2 additions & 0 deletions src/mcp/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import { storageTools } from "./storage/index.js";
import { messagingTools } from "./messaging/index.js";
import { remoteConfigTools } from "./remoteconfig/index.js";
import { appHostingTools } from "./apphosting/index.js";

/** availableTools returns the list of MCP tools available given the server flags */
export function availableTools(activeFeatures?: ServerFeature[]): ServerTool[] {
// Core tools are always present.
const toolDefs: ServerTool[] = addFeaturePrefix("firebase", coreTools);
if (!activeFeatures?.length) {
activeFeatures = Object.keys(tools) as ServerFeature[];

Check warning on line 17 in src/mcp/tools/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'tools' was used before it was defined
}
for (const key of activeFeatures) {
toolDefs.push(...tools[key]);

Check warning on line 20 in src/mcp/tools/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'tools' was used before it was defined
}
return toolDefs;
}
Expand All @@ -28,6 +29,7 @@
storage: addFeaturePrefix("storage", storageTools),
messaging: addFeaturePrefix("messaging", messagingTools),
remoteconfig: addFeaturePrefix("remoteconfig", remoteConfigTools),
apphosting: addFeaturePrefix("apphosting", appHostingTools),
};

function addFeaturePrefix(feature: string, tools: ServerTool[]): ServerTool[] {
Expand Down
1 change: 1 addition & 0 deletions src/mcp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const SERVER_FEATURES = [
"auth",
"messaging",
"remoteconfig",
"apphosting",
] as const;
export type ServerFeature = (typeof SERVER_FEATURES)[number];

Expand Down
2 changes: 2 additions & 0 deletions src/mcp/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { platform } from "os";
import { ServerFeature } from "./types";
import {
apphostingOrigin,
authManagementOrigin,
dataconnectOrigin,
firestoreOrigin,
Expand All @@ -17,7 +18,7 @@
* Converts data to a CallToolResult.
*/
export function toContent(
data: any,

Check warning on line 21 in src/mcp/util.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
options?: { format?: "json" | "yaml"; contentPrefix?: string; contentSuffix?: string },
): CallToolResult {
if (typeof data === "string") return { content: [{ type: "text", text: data }] };
Expand Down Expand Up @@ -85,6 +86,7 @@
auth: authManagementOrigin(),
messaging: messagingApiOrigin(),
remoteconfig: remoteConfigApiOrigin(),
apphosting: apphostingOrigin(),
};

/**
Expand All @@ -94,10 +96,10 @@
export async function checkFeatureActive(
feature: ServerFeature,
projectId?: string,
options?: any,

Check warning on line 99 in src/mcp/util.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
): Promise<boolean> {
// if the feature is configured in firebase.json, it's active
if (feature in (options?.config?.data || {})) return true;

Check warning on line 102 in src/mcp/util.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .config on an `any` value
// if the feature's api is active in the project, it's active
try {
if (projectId) return await check(projectId, SERVER_FEATURE_APIS[feature], "", true);
Expand Down
Loading