Skip to content

Commit dc54551

Browse files
authored
feat(genkit-tools/mcp): Add genkit:init command with support for runtime tools (#3891)
1 parent 1e4d614 commit dc54551

File tree

9 files changed

+381
-134
lines changed

9 files changed

+381
-134
lines changed

‎genkit-tools/cli/context/GENKIT.go.md‎

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,6 @@ This document provides rules and examples for building with the Genkit API in Go
1414

1515
NOTE: For the sake of brevity, the snippets below use the Google AI plugin, but you should follow the user's preference as mentioned above.
1616

17-
## Core Setup
18-
19-
1. **Initialize Project**
20-
21-
```bash
22-
mkdir my-genkit-app && cd my-genkit-app
23-
go mod init my-genkit-app
24-
```
25-
26-
2. **Install Dependencies**
27-
28-
```bash
29-
go get github.com/firebase/genkit/go/genkit
30-
go get github.com/firebase/genkit/go/plugins/googlegenai
31-
go get github.com/firebase/genkit/go/ai
32-
go get google.golang.org/genai
33-
```
34-
35-
3. **Install Genkit CLI**
36-
37-
```bash
38-
curl -sL cli.genkit.dev | bash
39-
```
40-
41-
4. **Configure Genkit**
42-
43-
All code should be in a single `main.go` file or properly structured Go package.
44-
45-
```go
46-
package main
47-
48-
import (
49-
"context"
50-
"github.com/firebase/genkit/go/genkit"
51-
"github.com/firebase/genkit/go/plugins/googlegenai"
52-
)
53-
54-
func main() {
55-
ctx := context.Background()
56-
g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
57-
// Your flows and logic here
58-
<-ctx.Done()
59-
}
60-
```
61-
6217
## Best Practices
6318

6419
1. **Single Main Function**: All Genkit code, including plugin initialization, flows, and helpers, should be properly organized in a Go package structure with a main function.
@@ -216,23 +171,24 @@ func main() {
216171

217172
## Running and Inspecting Flows
218173

219-
1. **Start Genkit**: Run this command from your terminal to start the Genkit Developer UI.
174+
**Start Genkit**: Genkit can be started locally by using the `genkit start` command, along with the process startup command:
220175

221-
```bash
222-
genkit start -- <command to run your code>
223-
```
176+
```bash
177+
genkit start -- <command to run your code>
178+
```
224179

225-
For Go applications:
180+
For e.g.:
226181

227-
```bash
228-
# Running a Go application directly
229-
genkit start -- go run main.go
182+
```bash
183+
genkit start -- go run main.go
184+
```
230185

231-
# Running a compiled binary
232-
genkit start -- ./my-genkit-app
233-
```
186+
You can can automate starting genkit using the following steps:
234187

235-
The command should output a URL for the Genkit Dev UI. Direct the user to visit this URL to run and inspect their Genkit app.
188+
1. Identify the command to start the user's project's (e.g., `go run main.go`)
189+
2. Use the `start_runtime` tool to start the runtime process. This is required for Genkit to discover flows.
190+
- Example: If the project uses `go run main.go`, call `start_runtime` with `{ command: "go", args: ["run", "main.go"] }`.
191+
3. After starting the runtime, instruct the user to run `genkit start` in their terminal to launch the Developer UI.
236192

237193
## Suggested Models
238194

‎genkit-tools/cli/context/GENKIT.js.md‎

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,6 @@ This document provides rules and examples for building with the Genkit API in No
1414

1515
NOTE: For the sake of brevity, the snippets below use the Google AI plugin, but you should follow the user's preference as mentioned above.
1616

17-
## Core Setup
18-
19-
1. **Initialize Project**
20-
21-
```bash
22-
mkdir my-genkit-app && cd my-genkit-app
23-
npm init -y
24-
npm install -D typescript tsx \@types/node
25-
```
26-
27-
2. **Install Dependencies**
28-
29-
```bash
30-
npm install genkit \@genkit-ai/google-genai data-urls node-fetch
31-
```
32-
33-
3. **Install Genkit CLI**
34-
35-
```bash
36-
npm install -g genkit-cli
37-
```
38-
39-
4. **Configure Genkit**
40-
41-
All code should be in a single `src/index.ts` file.
42-
43-
```ts
44-
// src/index.ts
45-
import { genkit, z } from 'genkit';
46-
import { googleAI } from '@genkit-ai/google-genai';
47-
48-
export const ai = genkit({
49-
plugins: [googleAI()],
50-
});
51-
```
52-
5317
## Best Practices
5418

5519
1. **Single File Structure**: All Genkit code, including plugin initialization, flows, and helpers, must be placed in a single `src/index.ts` file. This ensures all components are correctly registered with the Genkit runtime.
@@ -289,29 +253,24 @@ export const videoGenerationFlow = ai.defineFlow(
289253

290254
## Running and Inspecting Flows
291255

292-
1. **Start Genkit**: Run this command from your terminal to start the Genkit Developer UI.
256+
**Start Genkit**: Genkit can be started locally by using the `genkit start` command, along with the process startup command:
293257

294-
```bash
295-
genkit start -- <command to run your code>
296-
```
297-
298-
The <command to run your code> will vary based on the project’s setup and
299-
the file you want to execute. For e.g.:
258+
```bash
259+
genkit start -- <command to run your code>
260+
```
300261

301-
```bash
302-
# Running a typical development server
303-
genkit start -- npm run dev
262+
For e.g.:
304263

305-
# Running a TypeScript file directly
306-
genkit start -- npx tsx --watch src/index.ts
264+
```bash
265+
genkit start -- npm run dev
266+
```
307267

308-
# Running a JavaScript file directly
309-
genkit start -- node --watch src/index.js
310-
```
268+
You can can automate starting genkit using the following steps:
311269

312-
Analyze the users project and build tools to use the right command for the
313-
project. The command should output a URL for the Genkit Dev UI. Direct the
314-
user to visit this URL to run and inspect their Genkit app.
270+
1. Identify the command to start the user's project's (e.g., `npm run dev`)
271+
2. Use the `start_runtime` tool to start the runtime process. This is required for Genkit to discover flows.
272+
- Example: If the project uses `npm run dev`, call `start_runtime` with `{ command: "npm", args: ["run", "dev"] }`.
273+
3. After starting the runtime, instruct the user to run `genkit start` in their terminal to launch the Developer UI.
315274

316275
## Suggested Models
317276

‎genkit-tools/cli/src/mcp/flows.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ import { record } from '@genkit-ai/tools-common/utils';
1818
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
1919
import z from 'zod';
2020
import { McpRunToolEvent } from './analytics.js';
21-
import { lazyLoadManager } from './util.js';
21+
import { McpRuntimeManager } from './util.js';
2222

23-
export function defineFlowTools(
24-
server: McpServer,
25-
manager: ReturnType<typeof lazyLoadManager>
26-
) {
23+
export function defineFlowTools(server: McpServer, manager: McpRuntimeManager) {
2724
server.registerTool(
2825
'list_flows',
2926
{
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)