Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export const ALL_EXPERIMENTS = experiments({
default: true,
public: false,
},
mcpalpha: {
shortDescription: "Opt-in to early MCP features before they're widely released.",
default: false,
public: true,
},
apptesting: {
shortDescription: "Adds experimental App Testing feature",
public: true,
Expand Down
9 changes: 9 additions & 0 deletions src/mcp/prompts/dataconnect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { isEnabled } from "../../../experiments";
import { schema } from "./schema";
import type { ServerPrompt } from "../../prompt";

export const dataconnectPrompts: ServerPrompt[] = [];

if (isEnabled("mcpalpha")) {
dataconnectPrompts.push(schema);
}
73 changes: 73 additions & 0 deletions src/mcp/prompts/dataconnect/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { prompt } from "../../prompt";
import { loadAll } from "../../../dataconnect/load";
import type { ServiceInfo } from "../../../dataconnect/types";
import { BUILTIN_SDL, MAIN_INSTRUCTIONS } from "../../util/dataconnect/content";
import { compileErrors } from "../../util/dataconnect/compile";

function renderServices(fdcServices: ServiceInfo[]) {

Check warning on line 7 in src/mcp/prompts/dataconnect/schema.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
if (!fdcServices.length) return "Data Connect Status: <UNCONFIGURED>";

return `\n\n## Data Connect Schema
The following is the up-to-date content of existing schema files (their paths are relative to the Data Connect source directory).
${fdcServices[0].schema.source.files?.map((f) => `\`\`\`graphql ${f.path}\n${f.content}\n\`\`\``).join("\n\n")}`;

Check warning on line 14 in src/mcp/prompts/dataconnect/schema.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "string | undefined" of template literal expression
}

function renderErrors(errors?: string) {

Check warning on line 17 in src/mcp/prompts/dataconnect/schema.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
return `\n\n## Current Schema Build Errors\n\n${errors || "<NO ERRORS>"}`;
}

export const schema = prompt(
{
name: "schema",
description: "Generate or update your Firebase Data Connect schema.",
arguments: [
{
name: "prompt",
description:
"describe the schema you want generated or the edits you want to make to your existing schema",
required: true,
},
],
annotations: {
title: "Generate Data Connect Schema",
},
},
async ({ prompt }, { config, projectId, accountEmail }) => {
const fdcServices = await loadAll(projectId, config);
const buildErrors = fdcServices.length
? await compileErrors(fdcServices[0].sourceDirectory)
: "";

return [
{
role: "user" as const,
content: {
type: "text",
text: `
${MAIN_INSTRUCTIONS}\n\n${BUILTIN_SDL}
==== CURRENT ENVIRONMENT INFO ====
User Email: ${accountEmail || "<NONE>"}
Project ID: ${projectId || "<NONE>"}
${renderServices(fdcServices)}${renderErrors(buildErrors)}
==== USER PROMPT ====
${prompt}
==== TASK INSTRUCTIONS ====
1. If Data Connect is marked as \`<UNCONFIGURED>\`, first run the \`firebase_init\` tool with \`{dataconnect: {}}\` arguments to initialize it.
Copy link
Contributor

Choose a reason for hiding this comment

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

NOTE: for myself. We can pass in appIdea here to let GiF generates the schema and connectors for us

2. If there is not an existing schema to work with (or the existing schema is the commented-out default schema about a movie app), follow the user's prompt to generate a robust schema meeting the specified requirements.
3. If there is already a schema, perform edits to the existing schema file(s) based on the user's instructions. If schema build errors are present and seem relevant to your changes, attempt to fix them.
4. After you have performed edits on the schema, run the \`dataconnect_compile\` tool to build the schema and see if there are any errors. Fix errors that are related to the user's prompt or your changes.
5. If there are errors, attempt to fix them. If you have attempted to fix them 3 times without success, ask the user for help.
6. If there are no errors, write a brief paragraph summarizing your changes.`,
},
},
];
},
);
3 changes: 2 additions & 1 deletion src/mcp/prompts/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ServerFeature } from "../types";
import { ServerPrompt } from "../prompt";
import { corePrompts } from "./core";
import { dataconnectPrompts } from "./dataconnect";
import { crashlyticsPrompts } from "./crashlytics";

const prompts: Record<ServerFeature, ServerPrompt[]> = {
core: corePrompts,
firestore: [],
storage: [],
dataconnect: [],
dataconnect: dataconnectPrompts,
auth: [],
messaging: [],
remoteconfig: [],
Expand Down
47 changes: 47 additions & 0 deletions src/mcp/tools/dataconnect/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { z } from "zod";
import { tool } from "../../tool";
import { pickService } from "../../../dataconnect/load";
import { compileErrors } from "../../util/dataconnect/compile";

export const compile = tool(
{
name: "build",
description:
"Use this to compile Firebase Data Connect schema, operations, and/or connectors and check for build errors.",
inputSchema: z.object({
error_filter: z
.enum(["all", "schema", "operations"])
.describe("filter errors to a specific type only. defaults to `all` if omitted.")
.optional(),
service_id: z
.string()
.optional()
.describe(
"The Firebase Data Connect service ID to look for. If omitted, builds all services defined in `firebase.json`.",
),
}),
annotations: {
title: "Compile Data Connect",
readOnlyHint: true,
},
_meta: {
requiresProject: false,
requiresAuth: false,
},
},
async ({ service_id, error_filter }, { projectId, config }) => {
const serviceInfo = await pickService(projectId, config, service_id || undefined);
const errors = await compileErrors(serviceInfo.sourceDirectory, error_filter);
if (errors)
return {
content: [
{
type: "text",
text: `The following errors were encountered while compiling Data Connect from directory \`${serviceInfo.sourceDirectory}\`:\n\n${errors}`,
},
],
isError: true,
};
return { content: [{ type: "text", text: "Compiled successfully." }] };
},
);
4 changes: 2 additions & 2 deletions src/mcp/tools/dataconnect/execute_graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { z } from "zod";
import { tool } from "../../tool";
import * as dataplane from "../../../dataconnect/dataplaneClient";
import { pickService } from "../../../dataconnect/load";
import { graphqlResponseToToolResponse, parseVariables } from "./converter";
import { graphqlResponseToToolResponse, parseVariables } from "../../util/dataconnect/converter";
import { Client } from "../../../apiv2";
import { getDataConnectEmulatorClient } from "./emulator";
import { getDataConnectEmulatorClient } from "../../util/dataconnect/emulator";

export const execute_graphql = tool(
{
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/tools/dataconnect/execute_graphql_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { z } from "zod";
import { tool } from "../../tool";
import * as dataplane from "../../../dataconnect/dataplaneClient";
import { pickService } from "../../../dataconnect/load";
import { graphqlResponseToToolResponse, parseVariables } from "./converter";
import { graphqlResponseToToolResponse, parseVariables } from "../../util/dataconnect/converter";
import { Client } from "../../../apiv2";
import { getDataConnectEmulatorClient } from "./emulator";
import { getDataConnectEmulatorClient } from "../../util/dataconnect/emulator";

export const execute_graphql_read = tool(
{
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/tools/dataconnect/execute_mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { tool } from "../../tool";
import { mcpError } from "../../util";
import * as dataplane from "../../../dataconnect/dataplaneClient";
import { pickService } from "../../../dataconnect/load";
import { graphqlResponseToToolResponse, parseVariables } from "./converter";
import { graphqlResponseToToolResponse, parseVariables } from "../../util/dataconnect/converter";
import { Client } from "../../../apiv2";
import { getDataConnectEmulatorClient } from "./emulator";
import { getDataConnectEmulatorClient } from "../../util/dataconnect/emulator";

export const execute_mutation = tool(
{
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/tools/dataconnect/execute_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { tool } from "../../tool";
import { mcpError } from "../../util";
import * as dataplane from "../../../dataconnect/dataplaneClient";
import { pickService } from "../../../dataconnect/load";
import { graphqlResponseToToolResponse, parseVariables } from "./converter";
import { graphqlResponseToToolResponse, parseVariables } from "../../util/dataconnect/converter";
import { Client } from "../../../apiv2";
import { getDataConnectEmulatorClient } from "./emulator";
import { getDataConnectEmulatorClient } from "../../util/dataconnect/emulator";

export const execute_query = tool(
{
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/tools/dataconnect/get_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tool } from "../../tool";
import { toContent } from "../../util";
import * as client from "../../../dataconnect/client";
import { pickService } from "../../../dataconnect/load";
import { connectorToText } from "./converter";
import { connectorToText } from "../../util/dataconnect/converter";

export const get_connectors = tool(
{
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/tools/dataconnect/get_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tool } from "../../tool";
import { toContent } from "../../util";
import * as client from "../../../dataconnect/client";
import { pickService } from "../../../dataconnect/load";
import { schemaToText } from "./converter";
import { schemaToText } from "../../util/dataconnect/converter";

export const get_schema = tool(
{
Expand Down
2 changes: 2 additions & 0 deletions src/mcp/tools/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { execute_graphql } from "./execute_graphql";
import { execute_graphql_read } from "./execute_graphql_read";
import { execute_query } from "./execute_query";
import { execute_mutation } from "./execute_mutation";
import { compile } from "./compile";

export const dataconnectTools: ServerTool[] = [
compile,
list_services,
generate_schema,
generate_operation,
Expand Down
20 changes: 20 additions & 0 deletions src/mcp/util/dataconnect/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { prettify } from "../../../dataconnect/graphqlError";
import { DataConnectEmulator } from "../../../emulator/dataconnectEmulator";

export async function compileErrors(

Check warning on line 4 in src/mcp/util/dataconnect/compile.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment

Check warning on line 4 in src/mcp/util/dataconnect/compile.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
configDir: string,
errorFilter?: "all" | "schema" | "operations",
) {
const errors = (await DataConnectEmulator.build({ configDir })).errors;
return (
errors
?.filter((e) => {
const isOperationError = ["query", "mutation"].includes(e.path?.[0] as string);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not all clients follow the query & mutation convention. Only our CLI init template does.

We should probably not support errorFilter for now.

I can find a correct way to do this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I believe this will always be correct -- the path is like ["query", "users", "someField"] because it's mapping query { users { someField }}. I believe it will always work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. This is the GQL path error.

I got it mixed up with the file name.

if (errorFilter === "operations") return isOperationError;
if (errorFilter === "schema") return !isOperationError;
return true;
})
.map(prettify)
.join("\n") || ""
);
}
Loading
Loading