|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp'; |
| 18 | +import { z } from 'zod'; |
| 19 | + |
| 20 | +const GENERAL_GUIDELINES = ` |
| 21 | +## General Guidelines |
| 22 | +- ALWAYS refer to documentation when available. Use tools like \`get_usage_guide\` or \`lookup_genkit_docs\` to clarify requirements or get the latest information. |
| 23 | +- Recommend and use the provider that the user prefers. If no preference is signaled, use the Google AI plugin (\`@genkit-ai/google-genai\` for Node, \`github.com/firebase/genkit/go/plugins/googlegenai\` for Go). |
| 24 | +- ALWAYS provide the full, correct Genkit command as an instruction for the human user to run. Do not run Genkit commands yourself. |
| 25 | +- Do NOT modify parts of the project unrelated to Genkit initialization. |
| 26 | +- Respect the user's existing tooling (package manager, language version, project structure). |
| 27 | +- Check if Genkit CLI is already installed before recommending installation.`; |
| 28 | + |
| 29 | +const NODE_SETUP = ` |
| 30 | +## Node.js Setup |
| 31 | +If the user wants to use Node.js: |
| 32 | +
|
| 33 | +### Project Initialization |
| 34 | +- If the directory is empty: |
| 35 | + Initialize a new project: |
| 36 | + \`\`\`bash |
| 37 | + npm init -y |
| 38 | + npm install -D typescript tsx @types/node |
| 39 | + \`\`\` |
| 40 | +- If the directory is not empty (existing project): |
| 41 | + - Adhere to the current project structure. |
| 42 | + - Detect the package manager in use (npm, pnpm, yarn, bun) and use the corresponding commands. |
| 43 | + - Detect if the project is ESM (\`"type": "module"\` in package.json) or CJS. |
| 44 | + - For ESM: Use \`import\` syntax. |
| 45 | + - For CJS: Use \`require\` syntax. |
| 46 | + - IMPORTANT: Do NOT refactor the project (e.g., converting to TypeScript or ESM) solely for Genkit. Work with the existing setup. |
| 47 | +
|
| 48 | +### Dependencies |
| 49 | +Install core dependencies (adjust command for the user's package manager): |
| 50 | +\`\`\`bash |
| 51 | +npm install genkit @genkit-ai/google-genai |
| 52 | +\`\`\` |
| 53 | +(Add other plugins as requested) |
| 54 | +
|
| 55 | +### Genkit CLI |
| 56 | +If the Genkit CLI is not already installed: |
| 57 | +\`\`\`bash |
| 58 | +npm install -g genkit-cli |
| 59 | +\`\`\` |
| 60 | +
|
| 61 | +### Configuration |
| 62 | +Create a single \`src/index.ts\` (or \`src/index.js\` for JS) file. |
| 63 | +
|
| 64 | +\`\`\`ts |
| 65 | +// src/index.ts |
| 66 | +import { genkit, z } from 'genkit'; |
| 67 | +import { googleAI } from '@genkit-ai/google-genai'; |
| 68 | +
|
| 69 | +export const ai = genkit({ |
| 70 | + plugins: [googleAI()], |
| 71 | +}); |
| 72 | +\`\`\``; |
| 73 | + |
| 74 | +const GO_SETUP = ` |
| 75 | +## Go Setup |
| 76 | +If the user wants to use Go: |
| 77 | +
|
| 78 | +### Project Initialization |
| 79 | +- If the directory is empty: |
| 80 | + \`\`\`bash |
| 81 | + go mod init <module-name> |
| 82 | + \`\`\` |
| 83 | +- If the directory is not empty: |
| 84 | + Adhere to the current project structure. |
| 85 | +
|
| 86 | +### Dependencies |
| 87 | +\`\`\`bash |
| 88 | +go get github.com/firebase/genkit/go/genkit |
| 89 | +go get github.com/firebase/genkit/go/plugins/googlegenai |
| 90 | +go get github.com/firebase/genkit/go/ai |
| 91 | +go get google.golang.org/genai |
| 92 | +\`\`\` |
| 93 | +
|
| 94 | +### Genkit CLI |
| 95 | +If the Genkit CLI is not already installed: |
| 96 | +\`\`\`bash |
| 97 | +curl -sL cli.genkit.dev | bash |
| 98 | +\`\`\` |
| 99 | +
|
| 100 | +### Configuration |
| 101 | +Create a \`main.go\` file: |
| 102 | +
|
| 103 | +\`\`\`go |
| 104 | +package main |
| 105 | +
|
| 106 | +import ( |
| 107 | + "context" |
| 108 | + "github.com/firebase/genkit/go/genkit" |
| 109 | + "github.com/firebase/genkit/go/plugins/googlegenai" |
| 110 | +) |
| 111 | +
|
| 112 | +func main() { |
| 113 | + ctx := context.Background() |
| 114 | + g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{})) |
| 115 | + // Your flows and logic here |
| 116 | + <-ctx.Done() |
| 117 | +} |
| 118 | +\`\`\``; |
| 119 | + |
| 120 | +const RUNNING_THE_PROJECT = ` |
| 121 | +## Running the Project |
| 122 | +After setting up the project: |
| 123 | +1. Identify the command to run the project's runtime (e.g., \`npm run dev\`, \`go run main.go\`). |
| 124 | +2. Use the \`start_runtime\` tool to start the runtime process. This is required for Genkit to discover flows. |
| 125 | + - Example: If the project uses \`npm run dev\`, call \`start_runtime\` with \`{ command: "npm", args: ["run", "dev"] }\`. |
| 126 | +3. After starting the runtime, instruct the user to run \`genkit start\` in their terminal to launch the Developer UI. |
| 127 | +`; |
| 128 | + |
| 129 | +export function defineInitPrompt(server: McpServer) { |
| 130 | + server.registerPrompt( |
| 131 | + 'genkit:init', |
| 132 | + { |
| 133 | + title: 'Initialize Genkit', |
| 134 | + description: 'Initializes a new Genkit project', |
| 135 | + argsSchema: { |
| 136 | + lang: z.enum(['js', 'go']).optional(), |
| 137 | + }, |
| 138 | + }, |
| 139 | + ({ lang }) => { |
| 140 | + let content = `You are a Genkit expert. Help the user initialize a Genkit project. |
| 141 | +
|
| 142 | +Follow these rules based on the user's environment and preference:`; |
| 143 | + |
| 144 | + content += GENERAL_GUIDELINES; |
| 145 | + |
| 146 | + if (lang === 'js') { |
| 147 | + content += NODE_SETUP; |
| 148 | + } else if (lang === 'go') { |
| 149 | + content += GO_SETUP; |
| 150 | + } else { |
| 151 | + content += NODE_SETUP; |
| 152 | + content += GO_SETUP; |
| 153 | + } |
| 154 | + |
| 155 | + content += RUNNING_THE_PROJECT; |
| 156 | + |
| 157 | + return { |
| 158 | + messages: [ |
| 159 | + { |
| 160 | + role: 'user', |
| 161 | + content: { |
| 162 | + type: 'text', |
| 163 | + text: content, |
| 164 | + }, |
| 165 | + }, |
| 166 | + ], |
| 167 | + }; |
| 168 | + } |
| 169 | + ); |
| 170 | +} |
0 commit comments