|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +import { tool } from "../../tool"; |
| 4 | +import { mcpError, toContent } from "../../util"; |
| 5 | +import * as backend from "../../../deploy/functions/backend"; |
| 6 | +import { getErrMsg } from "../../../error"; |
| 7 | +import * as args from "../../../deploy/functions/args"; |
| 8 | + |
| 9 | +export const list_functions = tool( |
| 10 | + "functions", |
| 11 | + { |
| 12 | + name: "list_functions", |
| 13 | + description: "List all deployed functions in your Firebase project.", |
| 14 | + inputSchema: z.object({}), |
| 15 | + annotations: { |
| 16 | + title: "List Deployed Functions", |
| 17 | + readOnlyHint: true, |
| 18 | + openWorldHint: true, |
| 19 | + }, |
| 20 | + _meta: { |
| 21 | + requiresAuth: true, |
| 22 | + requiresProject: true, |
| 23 | + }, |
| 24 | + }, |
| 25 | + async (_, { projectId }) => { |
| 26 | + const context = { |
| 27 | + projectId, |
| 28 | + } as args.Context; |
| 29 | + |
| 30 | + try { |
| 31 | + // fetches info about all currently deployed functions for the project |
| 32 | + const existing = await backend.existingBackend(context); |
| 33 | + // extracts all the function endpoints and sorts them |
| 34 | + const endpointsList = backend.allEndpoints(existing).sort(backend.compareFunctions); |
| 35 | + |
| 36 | + // below format differs from Firebase CLI command output to be more suitable format for agents |
| 37 | + const formattedList = endpointsList.map((endpoint) => ({ |
| 38 | + function: endpoint.id, |
| 39 | + version: endpoint.platform === "gcfv2" ? "v2" : "v1", |
| 40 | + trigger: backend.endpointTriggerType(endpoint), |
| 41 | + location: endpoint.region, |
| 42 | + memory: endpoint.availableMemoryMb || "---", |
| 43 | + runtime: endpoint.runtime, |
| 44 | + })); |
| 45 | + |
| 46 | + if (!formattedList.length) { |
| 47 | + return toContent([], { |
| 48 | + contentPrefix: "No functions found in this project.\n\n", |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + return toContent(formattedList); |
| 53 | + } catch (err) { |
| 54 | + const errMsg = getErrMsg((err as any)?.original || err, "Failed to list functions."); |
| 55 | + return mcpError(errMsg); |
| 56 | + } |
| 57 | + }, |
| 58 | +); |
0 commit comments