-
Notifications
You must be signed in to change notification settings - Fork 1k
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
+188
−1
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cc064c0
initial apphosting mcp tool
bkendall 214d190
Merge branch 'master' into bk-mcp-apphosting
bkendall a51bd5c
Merge branch 'master' of github.com:firebase/firebase-tools into bk-m…
bkendall a4088ba
add fetchServiceLogs function
bkendall 98d42f3
add run tool to fetch logs
bkendall 9cb48c1
add a little more description for location
bkendall b6916b9
cleaning up logic a bit
bkendall 5f8ed3a
Merge remote-tracking branch 'origin/master' into bk-mcp-apphosting
bkendall 77575ea
Merge remote-tracking branch 'origin' into bk-mcp-apphosting
bkendall d757d2d
add new tools to smoke test
bkendall dccc983
don't reinvent the cloud run logs wheel
bkendall 4312b49
back out cloud run tool
bkendall 6a092a6
creates a new logs helper for app hosting for both build and runtime …
bkendall c0de917
Merge branch 'master' of github.com:firebase/firebase-tools into bk-m…
bkendall 049df71
lint issues
bkendall c020877
some funny business got into my code. undoing it
bkendall 4dad065
add changelog
bkendall 67e3d14
formatting.
bkendall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
- Adds MCP tools for App Hosting (#8605) | ||
- Fixed crash when starting the App Hosting emulator in certain applications (#8624) | ||
- Fixed issue where, with `webframeworks` enabled, `firebase init hosting` re-prompts users for source. (#8587) | ||
- Update typescript version in functions template to avoid build issue with @google-cloud/storage depedency (#8194) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,11 @@ | |
uri: string; | ||
serviceAccount?: string; | ||
appId?: string; | ||
managedResources?: ManagedResource[]; | ||
} | ||
|
||
export interface ManagedResource { | ||
runService: { service: string }; | ||
} | ||
|
||
export type BackendOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri"; | ||
|
@@ -266,7 +271,7 @@ | |
done: boolean; | ||
// oneof result | ||
error?: Status; | ||
response?: any; | ||
// end oneof result | ||
} | ||
|
||
|
@@ -333,6 +338,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. | ||
*/ | ||
|
@@ -548,14 +566,14 @@ | |
/** | ||
* Ensure that the App Hosting API is enabled on the project. | ||
*/ | ||
export async function ensureApiEnabled(options: any): Promise<void> { | ||
const projectId = needProjectId(options); | ||
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 576 in src/gcp/apphosting.ts
|
||
*/ | ||
export async function getNextRolloutId( | ||
projectId: string, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { z } from "zod"; | ||
import { tool } from "../../tool.js"; | ||
import { toContent } from "../../util.js"; | ||
import { Backend, getBackend, getTraffic, listBuilds, Traffic } from "../../../gcp/apphosting.js"; | ||
import { last } from "../../../utils.js"; | ||
import { FirebaseError } from "../../../error.js"; | ||
import { fetchServiceLogs } from "../../../gcp/run.js"; | ||
import { listEntries } from "../../../gcp/cloudlogging.js"; | ||
|
||
export const fetch_logs = tool( | ||
{ | ||
name: "fetch_logs", | ||
description: | ||
"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.", | ||
inputSchema: z.object({ | ||
buildLogs: z | ||
.boolean() | ||
.default(false) | ||
.describe( | ||
"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.", | ||
), | ||
backendId: z.string().describe("The ID of the backend for which to fetch logs."), | ||
location: z | ||
.string() | ||
.describe( | ||
"The specific region for the backend. By default, if a backend is uniquely named across all locations, that one will be used.", | ||
), | ||
}), | ||
annotations: { | ||
title: "Fetch logs for App Hosting backends and builds.", | ||
readOnlyHint: true, | ||
}, | ||
_meta: { | ||
requiresAuth: true, | ||
requiresProject: true, | ||
}, | ||
}, | ||
async ({ buildLogs, backendId, location } = {}, { projectId }) => { | ||
projectId ||= ""; | ||
location ||= ""; | ||
if (!backendId) { | ||
return toContent(`backendId must be specified.`); | ||
} | ||
const backend = await getBackend(projectId, location, backendId); | ||
const traffic = await getTraffic(projectId, location, backendId); | ||
const data: Backend & { traffic: Traffic } = { ...backend, traffic }; | ||
|
||
if (buildLogs) { | ||
const builds = await listBuilds(projectId, location, backendId); | ||
builds.builds.sort( | ||
(a, b) => new Date(a.createTime).getTime() - new Date(b.createTime).getTime(), | ||
); | ||
const build = last(builds.builds); | ||
const r = new RegExp(`region=${location}/([0-9a-f-]+)?`); | ||
const match = r.exec(build.buildLogsUri ?? ""); | ||
if (!match) { | ||
throw new FirebaseError("Unable to determine the build ID."); | ||
} | ||
const buildId = match[1]; | ||
// Thirty days ago makes sure we get any saved data within the default retention period. | ||
const thirtyDaysAgo = new Date(); | ||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30); | ||
const timestampFilter = `timestamp >= "${thirtyDaysAgo.toISOString()}"`; | ||
const filter = `resource.type="build" resource.labels.build_id="${buildId}" ${timestampFilter}`; | ||
const entries = await listEntries(projectId, filter, 100, "asc"); | ||
if (!Array.isArray(entries) || !entries.length) { | ||
return toContent("No logs found."); | ||
} | ||
return toContent(entries); | ||
} | ||
|
||
const serviceName = last(data.managedResources)?.runService.service; | ||
if (!serviceName) { | ||
throw new FirebaseError("Unable to get service name from managedResources."); | ||
} | ||
const serviceId = last(serviceName.split("/")); | ||
const logs = await fetchServiceLogs(projectId, serviceId); | ||
return toContent(logs); | ||
}, | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ServerTool } from "../../tool"; | ||
import { fetch_logs } from "./fetch_logs"; | ||
import { list_backends } from "./list_backends"; | ||
|
||
export const appHostingTools: ServerTool[] = [fetch_logs, list_backends]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { z } from "zod"; | ||
import { tool } from "../../tool.js"; | ||
import { toContent } from "../../util.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 }) => { | ||
projectId = projectId || ""; | ||
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 }); | ||
} | ||
if (!data.length) { | ||
return toContent( | ||
`No backends exist for project ${projectId}${location !== "-" ? ` in ${location}` : ""}.`, | ||
); | ||
} | ||
return toContent(data); | ||
}, | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.