Skip to content

Commit 8c8874c

Browse files
bkendallTrCaM
authored andcommitted
initial apphosting mcp tool (#8605)
* initial apphosting mcp tool * add fetchServiceLogs function * add run tool to fetch logs * add a little more description for location * cleaning up logic a bit * add new tools to smoke test * don't reinvent the cloud run logs wheel * back out cloud run tool * creates a new logs helper for app hosting for both build and runtime logs * lint issues * some funny business got into my code. undoing it * add changelog
1 parent 249d35a commit 8c8874c

File tree

10 files changed

+188
-1
lines changed

10 files changed

+188
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Adds MCP tools for App Hosting (#8605)
12
- Fixed crash when starting the App Hosting emulator in certain applications (#8624)
23
- Fixed issue where, with `webframeworks` enabled, `firebase init hosting` re-prompts users for source. (#8587)
34
- Update typescript version in functions template to avoid build issue with @google-cloud/storage depedency (#8194)

scripts/mcp-tests/gemini-smoke-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ await client.connect(
1717
args: [
1818
"experimental:mcp",
1919
"--only",
20-
"firestore,dataconnect,messaging,remoteconfig,crashlytics,auth,storage",
20+
"firestore,dataconnect,messaging,remoteconfig,crashlytics,auth,storage,apphosting",
2121
],
2222
}),
2323
);

src/gcp/apphosting.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export interface Backend {
4242
uri: string;
4343
serviceAccount?: string;
4444
appId?: string;
45+
managedResources?: ManagedResource[];
46+
}
47+
48+
export interface ManagedResource {
49+
runService: { service: string };
4550
}
4651

4752
export type BackendOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri";
@@ -333,6 +338,19 @@ export async function getBackend(
333338
return res.body;
334339
}
335340

341+
/**
342+
* Gets traffic details.
343+
*/
344+
export async function getTraffic(
345+
projectId: string,
346+
location: string,
347+
backendId: string,
348+
): Promise<Traffic> {
349+
const name = `projects/${projectId}/locations/${location}/backends/${backendId}/traffic`;
350+
const res = await client.get<Traffic>(name);
351+
return res.body;
352+
}
353+
336354
/**
337355
* List all backends present in a project and location.
338356
*/

src/gcp/run.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as proto from "./proto";
55
import * as iam from "./iam";
66
import { backoff } from "../throttler/throttler";
77
import { logger } from "../logger";
8+
import { listEntries, LogEntry } from "./cloudlogging";
89

910
const API_VERSION = "v1";
1011

@@ -344,3 +345,25 @@ export async function setInvokerUpdate(
344345
};
345346
await setIamPolicy(serviceName, policy, httpClient);
346347
}
348+
349+
/**
350+
* Fetches recent logs for a given Cloud Run service using the Cloud Logging API.
351+
* @param projectId The Google Cloud project ID.
352+
* @param serviceId The resource name of the Cloud Run service.
353+
* @return A promise that resolves with the log entries.
354+
*/
355+
export async function fetchServiceLogs(projectId: string, serviceId: string): Promise<LogEntry[]> {
356+
const filter = `resource.type="cloud_run_revision" AND resource.labels.service_name="${serviceId}"`;
357+
const pageSize = 100;
358+
const order = "desc";
359+
360+
try {
361+
const entries = await listEntries(projectId, filter, pageSize, order);
362+
return entries || [];
363+
} catch (err: any) {
364+
throw new FirebaseError(`Failed to fetch logs for Cloud Run service ${serviceId}`, {
365+
original: err,
366+
status: (err as any)?.context?.response?.statusCode,
367+
});
368+
}
369+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { z } from "zod";
2+
import { tool } from "../../tool.js";
3+
import { toContent } from "../../util.js";
4+
import { Backend, getBackend, getTraffic, listBuilds, Traffic } from "../../../gcp/apphosting.js";
5+
import { last } from "../../../utils.js";
6+
import { FirebaseError } from "../../../error.js";
7+
import { fetchServiceLogs } from "../../../gcp/run.js";
8+
import { listEntries } from "../../../gcp/cloudlogging.js";
9+
10+
export const fetch_logs = tool(
11+
{
12+
name: "fetch_logs",
13+
description:
14+
"Fetches the most recent logs for a specified App Hosting backend. If `buildLogs` is specified, the logs from the build process for the latest build are returned. The most recent logs are listed first.",
15+
inputSchema: z.object({
16+
buildLogs: z
17+
.boolean()
18+
.default(false)
19+
.describe(
20+
"If specified, the logs for the most recent build will be returned instead of the logs for the service. The build logs are returned 'in order', to be read from top to bottom.",
21+
),
22+
backendId: z.string().describe("The ID of the backend for which to fetch logs."),
23+
location: z
24+
.string()
25+
.describe(
26+
"The specific region for the backend. By default, if a backend is uniquely named across all locations, that one will be used.",
27+
),
28+
}),
29+
annotations: {
30+
title: "Fetch logs for App Hosting backends and builds.",
31+
readOnlyHint: true,
32+
},
33+
_meta: {
34+
requiresAuth: true,
35+
requiresProject: true,
36+
},
37+
},
38+
async ({ buildLogs, backendId, location } = {}, { projectId }) => {
39+
projectId ||= "";
40+
location ||= "";
41+
if (!backendId) {
42+
return toContent(`backendId must be specified.`);
43+
}
44+
const backend = await getBackend(projectId, location, backendId);
45+
const traffic = await getTraffic(projectId, location, backendId);
46+
const data: Backend & { traffic: Traffic } = { ...backend, traffic };
47+
48+
if (buildLogs) {
49+
const builds = await listBuilds(projectId, location, backendId);
50+
builds.builds.sort(
51+
(a, b) => new Date(a.createTime).getTime() - new Date(b.createTime).getTime(),
52+
);
53+
const build = last(builds.builds);
54+
const r = new RegExp(`region=${location}/([0-9a-f-]+)?`);
55+
const match = r.exec(build.buildLogsUri ?? "");
56+
if (!match) {
57+
throw new FirebaseError("Unable to determine the build ID.");
58+
}
59+
const buildId = match[1];
60+
// Thirty days ago makes sure we get any saved data within the default retention period.
61+
const thirtyDaysAgo = new Date();
62+
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
63+
const timestampFilter = `timestamp >= "${thirtyDaysAgo.toISOString()}"`;
64+
const filter = `resource.type="build" resource.labels.build_id="${buildId}" ${timestampFilter}`;
65+
const entries = await listEntries(projectId, filter, 100, "asc");
66+
if (!Array.isArray(entries) || !entries.length) {
67+
return toContent("No logs found.");
68+
}
69+
return toContent(entries);
70+
}
71+
72+
const serviceName = last(data.managedResources)?.runService.service;
73+
if (!serviceName) {
74+
throw new FirebaseError("Unable to get service name from managedResources.");
75+
}
76+
const serviceId = last(serviceName.split("/"));
77+
const logs = await fetchServiceLogs(projectId, serviceId);
78+
return toContent(logs);
79+
},
80+
);

src/mcp/tools/apphosting/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ServerTool } from "../../tool";
2+
import { fetch_logs } from "./fetch_logs";
3+
import { list_backends } from "./list_backends";
4+
5+
export const appHostingTools: ServerTool[] = [fetch_logs, list_backends];
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { z } from "zod";
2+
import { tool } from "../../tool.js";
3+
import { toContent } from "../../util.js";
4+
import {
5+
Backend,
6+
getTraffic,
7+
listBackends,
8+
parseBackendName,
9+
Traffic,
10+
} from "../../../gcp/apphosting.js";
11+
12+
export const list_backends = tool(
13+
{
14+
name: "list_backends",
15+
description:
16+
"Retrieves a list of App Hosting backends in the current project. An empty list means that there are no backends. " +
17+
"The `uri` is the public URL of the backend. " +
18+
"A working backend will have a `managed_resources` array that will contain a `run_service` entry. That `run_service.service` " +
19+
"is the resource name of the Cloud Run service serving the App Hosting backend. The last segment of that name is the service ID.",
20+
inputSchema: z.object({
21+
location: z
22+
.string()
23+
.optional()
24+
.default("-")
25+
.describe(
26+
"Limit the listed backends to this region. By default, it will list all backends across all regions.",
27+
),
28+
}),
29+
annotations: {
30+
title: "List App Hosting backends.",
31+
readOnlyHint: true,
32+
},
33+
_meta: {
34+
requiresAuth: true,
35+
requiresProject: true,
36+
},
37+
},
38+
async ({ location } = {}, { projectId }) => {
39+
projectId = projectId || "";
40+
if (!location) location = "-";
41+
const data: (Backend & { traffic: Traffic })[] = [];
42+
const backends = await listBackends(projectId, location);
43+
for (const backend of backends.backends) {
44+
const { location, id } = parseBackendName(backend.name);
45+
const traffic = await getTraffic(projectId, location, id);
46+
data.push({ ...backend, traffic: traffic });
47+
}
48+
if (!data.length) {
49+
return toContent(
50+
`No backends exist for project ${projectId}${location !== "-" ? ` in ${location}` : ""}.`,
51+
);
52+
}
53+
return toContent(data);
54+
},
55+
);

src/mcp/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { storageTools } from "./storage/index.js";
88
import { messagingTools } from "./messaging/index.js";
99
import { remoteConfigTools } from "./remoteconfig/index.js";
1010
import { crashlyticsTools } from "./crashlytics/index.js";
11+
import { appHostingTools } from "./apphosting/index.js";
1112

1213
/** availableTools returns the list of MCP tools available given the server flags */
1314
export function availableTools(activeFeatures?: ServerFeature[]): ServerTool[] {
@@ -30,6 +31,7 @@ const tools: Record<ServerFeature, ServerTool[]> = {
3031
messaging: addFeaturePrefix("messaging", messagingTools),
3132
remoteconfig: addFeaturePrefix("remoteconfig", remoteConfigTools),
3233
crashlytics: addFeaturePrefix("crashlytics", crashlyticsTools),
34+
apphosting: addFeaturePrefix("apphosting", appHostingTools),
3335
};
3436

3537
function addFeaturePrefix(feature: string, tools: ServerTool[]): ServerTool[] {

src/mcp/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const SERVER_FEATURES = [
66
"messaging",
77
"remoteconfig",
88
"crashlytics",
9+
"apphosting",
910
] as const;
1011
export type ServerFeature = (typeof SERVER_FEATURES)[number];
1112

src/mcp/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { dump } from "js-yaml";
44
import { platform } from "os";
55
import { ServerFeature } from "./types";
66
import {
7+
apphostingOrigin,
78
authManagementOrigin,
89
dataconnectOrigin,
910
firestoreOrigin,
@@ -87,6 +88,7 @@ const SERVER_FEATURE_APIS: Record<ServerFeature, string> = {
8788
messaging: messagingApiOrigin(),
8889
remoteconfig: remoteConfigApiOrigin(),
8990
crashlytics: crashlyticsApiOrigin(),
91+
apphosting: apphostingOrigin(),
9092
};
9193

9294
/**

0 commit comments

Comments
 (0)