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

Open
wants to merge 6 commits 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: 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
71 changes: 70 additions & 1 deletion src/gcp/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from "../apiv2";
import { FirebaseError } from "../error";
import { runOrigin } from "../api";
import { cloudloggingOrigin, runOrigin } from "../api";
import * as proto from "./proto";
import * as iam from "./iam";
import { backoff } from "../throttler/throttler";
Expand Down Expand Up @@ -150,10 +150,10 @@
try {
const response = await client.get<Service>(name);
return response.body;
} catch (err: any) {

Check warning on line 153 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
throw new FirebaseError(`Failed to fetch Run service ${name}`, {
original: err,

Check warning on line 155 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
status: err?.context?.response?.statusCode,

Check warning on line 156 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .context on an `any` value

Check warning on line 156 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
});
}
}
Expand All @@ -163,7 +163,7 @@
*/
export async function updateService(name: string, service: Service): Promise<Service> {
delete service.status;
service = await exports.replaceService(name, service);

Check warning on line 166 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .replaceService on an `any` value

Check warning on line 166 in src/gcp/run.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

// Now we need to wait for reconciliation or we might delete the docker
// image while the service is still rolling out a new revision.
Expand Down Expand Up @@ -344,3 +344,72 @@
};
await setIamPolicy(serviceName, policy, httpClient);
}

interface EntriesListRequest {
resourceNames: string[];
filter?: string;
orderBy?: string;
pageSize?: number;
pageToken?: string;
}

interface EntriesListResponse {
entries?: LogEntry[];
nextPageToken?: string;
}

interface LogEntry {
logName: string;
resource: unknown;
timestamp: string;
receiveTimestamp: string;
httpRequest?: unknown;

protoPayload?: unknown;
textPayload?: string;
jsonPayload?: unknown;

severity:
| "DEFAULT"
| "DEBUG"
| "INFO"
| "NOTICE"
| "WARNING"
| "ERROR"
| "CRITICAL"
| "ALERT"
| "EMERGENCY";
}

/**
* Fetches recent logs for a given Cloud Run service using the Cloud Logging API.
* @param projectId The Google Cloud project ID.
* @param serviceId The resource name of the Cloud Run service.
* @return A promise that resolves with the log entries.
*/
export async function fetchServiceLogs(projectId: string, serviceId: string): Promise<LogEntry[]> {
const loggingClient = new Client({
urlPrefix: cloudloggingOrigin(),
apiVersion: "v2",
});

const requestBody: EntriesListRequest = {
resourceNames: [`projects/${projectId}`],
filter: `resource.type="cloud_run_revision" AND resource.labels.service_name="${serviceId}"`,
orderBy: "timestamp desc",
pageSize: 100,
};

try {
const response = await loggingClient.post<EntriesListRequest, EntriesListResponse>(
"/entries:list",
requestBody,
);
return response.body.entries || [];
} catch (err: any) {
throw new FirebaseError(`Failed to fetch logs for Cloud Run service ${serviceId}`, {
original: err,
status: err?.context?.response?.statusCode,
});
}
}
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];
51 changes: 51 additions & 0 deletions src/mcp/tools/apphosting/list_backends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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. An empty list means that there are no backends. " +
"The `uri` is the public URL of the backend. " +
"A working backend will have a `managed_resources` array that will contain a `run_service` entry. That `run_service.service` " +
"is the resource name of the Cloud Run service serving the App Hosting backend. The last segment of that name is the service ID.",
inputSchema: z.object({
location: z
.string()
.optional()
.default("-")
.describe(
"Limit the listed backends to this region. By default, it will list all backends across all regions.",
),
}),
annotations: {
title: "List App Hosting backends.",
readOnlyHint: true,
},
_meta: {
requiresAuth: true,
requiresProject: true,
},
},
async ({ location } = {}, { projectId }) => {
if (!projectId) return NO_PROJECT_ERROR;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is already taken care of by the caller.

if (tool.mcp._meta?.requiresProject && !projectId) return NO_PROJECT_ERROR;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but I still have to assert that projectId isn't undefined, otherwise I have to do checks all over the place. Does this hurt that much to bother?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it's a bit annoying that projectId is nullable even if requiresProject is true above.

Maybe we can do a clean up to supply a dummy projectId: "missing-project" if there there is no project present.

if (!location) location = "-";
const data: (Backend & { traffic: Traffic })[] = [];
const backends = await listBackends(projectId, location);
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want a different message for no backends - ie

Suggested change
const backends = await listBackends(projectId, location);
const backends = await listBackends(projectId, location);
if (!backends.length) return toContent("No backends exist on project ${projectId} ${location !== "-" ? `in ${location}` :""}

Not sure what the LLMs will like more

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);
},
);
4 changes: 4 additions & 0 deletions src/mcp/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { storageTools } from "./storage/index.js";
import { messagingTools } from "./messaging/index.js";
import { remoteConfigTools } from "./remoteconfig/index.js";
import { crashlyticsTools } from "./crashlytics/index.js";
import { appHostingTools } from "./apphosting/index.js";
import { runTools } from "./run/index.js";

/** availableTools returns the list of MCP tools available given the server flags */
export function availableTools(activeFeatures?: ServerFeature[]): ServerTool[] {
Expand All @@ -30,6 +32,8 @@ const tools: Record<ServerFeature, ServerTool[]> = {
messaging: addFeaturePrefix("messaging", messagingTools),
remoteconfig: addFeaturePrefix("remoteconfig", remoteConfigTools),
crashlytics: addFeaturePrefix("crashlytics", crashlyticsTools),
apphosting: addFeaturePrefix("apphosting", appHostingTools),
run: addFeaturePrefix("run", runTools),
};

function addFeaturePrefix(feature: string, tools: ServerTool[]): ServerTool[] {
Expand Down
30 changes: 30 additions & 0 deletions src/mcp/tools/run/fetch_logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from "zod";
import { tool } from "../../tool.js";
import { toContent } from "../../util.js";
import { NO_PROJECT_ERROR } from "../../errors.js";
import { fetchServiceLogs } from "../../../gcp/run.js";

export const fetch_logs = tool(
{
name: "fetch_logs",
description:
"Fetches recent logs for a Cloud Run service. Includes details such as the message, severity, and timestamp.",
inputSchema: z.object({
serviceId: z.string().describe("The Cloud Run service ID."),
}),
annotations: {
title: "Fetch recent Cloud Run service logs.",
readOnlyHint: true,
},
_meta: {
requiresAuth: true,
requiresProject: true,
},
},
async ({ serviceId } = {}, { projectId }) => {
if (!projectId) return NO_PROJECT_ERROR;
if (!serviceId) return toContent("A Cloud Run service ID must be provided.");
const data = await fetchServiceLogs(projectId, serviceId);
return toContent(data);
},
);
4 changes: 4 additions & 0 deletions src/mcp/tools/run/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ServerTool } from "../../tool";
import { fetch_logs } from "./fetch_logs";

export const runTools: ServerTool[] = [fetch_logs];
2 changes: 2 additions & 0 deletions src/mcp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const SERVER_FEATURES = [
"messaging",
"remoteconfig",
"crashlytics",
"apphosting",
"run",
] as const;
export type ServerFeature = (typeof SERVER_FEATURES)[number];

Expand Down
4 changes: 4 additions & 0 deletions src/mcp/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { dump } from "js-yaml";
import { platform } from "os";
import { ServerFeature } from "./types";
import {
apphostingOrigin,
authManagementOrigin,
dataconnectOrigin,
firestoreOrigin,
messagingApiOrigin,
remoteConfigApiOrigin,
storageOrigin,
crashlyticsApiOrigin,
cloudRunApiOrigin,
} from "../api";
import { check } from "../ensureApiEnabled";

Expand Down Expand Up @@ -87,6 +89,8 @@ const SERVER_FEATURE_APIS: Record<ServerFeature, string> = {
messaging: messagingApiOrigin(),
remoteconfig: remoteConfigApiOrigin(),
crashlytics: crashlyticsApiOrigin(),
apphosting: apphostingOrigin(),
run: cloudRunApiOrigin(),
};

/**
Expand Down
Loading