Skip to content

Commit 95e766d

Browse files
committed
better logging
1 parent ab5d2dc commit 95e766d

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

go/samples/mcp-client/mcp_ception.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ import (
3030
"github.com/firebase/genkit/go/plugins/mcp"
3131
)
3232

33-
// MCP-ception Example: Genkit serves itself through MCP!
33+
// MCP self-hosting example: Genkit serves itself through MCP
3434
// 1. Start a Go MCP server that exposes Genkit resources
3535
// 2. Connect to that server as an MCP client
3636
// 3. Use the resources from the server for AI generation
3737

38-
// Step 1: Create the MCP Server (runs in background)
38+
// Create the MCP Server (runs in background)
3939
func createMCPServer() {
4040
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
4141
defer cancel()
4242

43-
logger.FromContext(ctx).Info("🚀 Starting Genkit MCP Server...")
43+
logger.FromContext(ctx).Info("Starting Genkit MCP Server")
4444

4545
// Initialize Genkit for the server
4646
g, err := genkit.Init(ctx)
@@ -56,7 +56,7 @@ func createMCPServer() {
5656
}) (map[string]interface{}, error) {
5757
logger.FromContext(ctx.Context).Debug("Executing genkit-brainstorm tool", "topic", input.Topic)
5858

59-
ideas := fmt.Sprintf(`💡 Creative Ideas for "%s":
59+
ideas := fmt.Sprintf(`Creative Ideas for "%s":
6060
6161
1. Interactive Experience: Create an immersive, hands-on workshop
6262
2. Digital Innovation: Develop a mobile app or web platform
@@ -69,7 +69,7 @@ func createMCPServer() {
6969
9. Creative Challenge: Host competitions or hackathons
7070
10. Multi-media Approach: Combine video, audio, and interactive elements
7171
72-
🎯 These ideas can be mixed, matched, and customized for "%s"!`, input.Topic, input.Topic)
72+
These ideas can be mixed, matched, and customized for "%s".`, input.Topic, input.Topic)
7373

7474
return map[string]interface{}{
7575
"topic": input.Topic,
@@ -124,10 +124,10 @@ Genkit follows a plugin-based architecture where models, retrievers, evaluators,
124124
Version: "1.0.0",
125125
})
126126

127-
logger.FromContext(ctx).Info("Genkit MCP Server configured successfully")
128-
logger.FromContext(ctx).Info("📡 Starting MCP server on stdio...")
129-
logger.FromContext(ctx).Info("🔧 Tools:", "count", len(server.ListRegisteredTools()))
130-
logger.FromContext(ctx).Info("📚 Resources:", "count", len(server.ListRegisteredResources()))
127+
logger.FromContext(ctx).Info("Genkit MCP Server configured successfully")
128+
logger.FromContext(ctx).Info("Starting MCP server on stdio")
129+
logger.FromContext(ctx).Info("Registered tools", "count", len(server.ListRegisteredTools()))
130+
logger.FromContext(ctx).Info("Registered resources", "count", len(server.ListRegisteredResources()))
131131

132132
// Start the server
133133
if err := server.ServeStdio(); err != nil && err != context.Canceled {
@@ -136,12 +136,12 @@ Genkit follows a plugin-based architecture where models, retrievers, evaluators,
136136
}
137137
}
138138

139-
// Step 2: Create the MCP Client that connects to our server
140-
func mcpCeptionDemo() {
139+
// Create the MCP Client that connects to our server
140+
func mcpSelfConnection() {
141141
ctx := context.Background()
142142

143-
logger.FromContext(ctx).Info("🎭 === MCP-CEPTION DEMO ===")
144-
logger.FromContext(ctx).Info("🎯 Genkit will connect to itself via MCP!")
143+
logger.FromContext(ctx).Info("MCP self-connection demo")
144+
logger.FromContext(ctx).Info("Genkit will connect to itself via MCP")
145145

146146
// Initialize Genkit with Google AI for the client
147147
g, err := genkit.Init(ctx,
@@ -152,8 +152,8 @@ func mcpCeptionDemo() {
152152
log.Fatalf("Failed to initialize Genkit client: %v", err)
153153
}
154154

155-
logger.FromContext(ctx).Info("🔌 Connecting to our own MCP server...")
156-
logger.FromContext(ctx).Info("💡 Make sure to run 'go run mcp_ception.go server' in another terminal first!")
155+
logger.FromContext(ctx).Info("Connecting to our own MCP server")
156+
logger.FromContext(ctx).Info("Note: Server process will be spawned automatically")
157157

158158
// Create MCP Host that connects to our Genkit server
159159
host, err := mcp.NewMCPHost(g, mcp.MCPHostOptions{
@@ -178,49 +178,49 @@ func mcpCeptionDemo() {
178178
}
179179

180180
// Get resources from our Genkit server
181-
logger.FromContext(ctx).Info("📚 Getting resources from Genkit MCP server...")
181+
logger.FromContext(ctx).Info("Getting resources from Genkit MCP server")
182182
resources, err := host.GetActiveResources(ctx)
183183
if err != nil {
184184
logger.FromContext(ctx).Error("Failed to get resources", "error", err)
185185
return
186186
}
187187

188-
logger.FromContext(ctx).Info("Retrieved resources from ourselves!", "count", len(resources))
188+
logger.FromContext(ctx).Info("Retrieved resources from server", "count", len(resources))
189189

190-
// DEBUG: Let's examine what resources we actually got
190+
// Debug: examine retrieved resources
191191
for i, resource := range resources {
192-
logger.FromContext(ctx).Info("🔍 DEBUG Resource details", "index", i, "name", resource.Name())
192+
logger.FromContext(ctx).Info("Resource details", "index", i, "name", resource.Name())
193193
// Test if the resource matches our target URI
194194
matches := resource.Matches("knowledge://genkit-docs")
195-
logger.FromContext(ctx).Info("🔍 Resource URI matching", "matches_target_uri", matches)
195+
logger.FromContext(ctx).Info("Resource URI matching", "matches_target_uri", matches)
196196
}
197197

198198
// Get tools from our Genkit server
199-
logger.FromContext(ctx).Info("🔧 Getting tools from Genkit MCP server...")
199+
logger.FromContext(ctx).Info("Getting tools from Genkit MCP server")
200200
tools, err := host.GetActiveTools(ctx, g)
201201
if err != nil {
202202
logger.FromContext(ctx).Error("Failed to get tools", "error", err)
203203
return
204204
}
205205

206-
logger.FromContext(ctx).Info("Retrieved tools from ourselves!", "count", len(tools))
206+
logger.FromContext(ctx).Info("Retrieved tools from server", "count", len(tools))
207207

208208
// Convert tools to refs
209209
var toolRefs []ai.ToolRef
210210
for _, tool := range tools {
211211
toolRefs = append(toolRefs, tool)
212212
}
213213

214-
// Step 3: Use resources and tools from our own server for AI generation
215-
logger.FromContext(ctx).Info("🤖 Asking AI about Genkit using our own MCP resources...")
214+
// Use resources and tools from our own server for AI generation
215+
logger.FromContext(ctx).Info("Asking AI about Genkit using our own MCP resources")
216216

217-
// THE CORRECT WAY: Use ai.NewResourcePart to explicitly reference the resource
218-
logger.FromContext(ctx).Info("🔍 DEBUG Generation call", "resource_count", len(resources), "tool_count", len(toolRefs))
217+
// Use ai.NewResourcePart to explicitly reference the resource
218+
logger.FromContext(ctx).Info("Starting generation call", "resource_count", len(resources), "tool_count", len(toolRefs))
219219

220220
response, err := genkit.Generate(ctx, g,
221221
ai.WithMessages(ai.NewUserMessage(
222222
ai.NewTextPart("Based on this Genkit knowledge:"),
223-
ai.NewResourcePart("knowledge://genkit-docs"), // ← FIXED: Explicit resource reference!
223+
ai.NewResourcePart("knowledge://genkit-docs"), // Explicit resource reference
224224
ai.NewTextPart("What are the key features of Genkit and what models does it support?\n\nAlso, use the brainstorm tool to generate ideas for \"AI-powered cooking assistant\""),
225225
)),
226226
ai.WithResources(resources), // Makes resources available for lookup
@@ -232,27 +232,27 @@ func mcpCeptionDemo() {
232232
return
233233
}
234234

235-
logger.FromContext(ctx).Info("🎉 MCP-CEPTION SUCCESS!")
236-
logger.FromContext(ctx).Info("Genkit used itself via MCP to answer questions!")
237-
fmt.Printf("\n🤖 AI Response using our own MCP resources:\n%s\n\n", response.Text())
235+
logger.FromContext(ctx).Info("MCP self-connection completed successfully")
236+
logger.FromContext(ctx).Info("Genkit used itself via MCP to answer questions")
237+
fmt.Printf("\nAI Response using our own MCP resources:\n%s\n\n", response.Text())
238238

239239
// Clean disconnect (skip for now to avoid hanging)
240-
logger.FromContext(ctx).Info("MCP-ception complete! 🎭")
240+
logger.FromContext(ctx).Info("MCP self-connection complete")
241241
}
242242

243243
func main() {
244244
if len(os.Args) < 2 {
245245
fmt.Println("Usage: go run mcp_ception.go [server|demo]")
246246
fmt.Println(" server - Run as MCP server (exposes Genkit resources)")
247-
fmt.Println(" demo - Run MCP-ception demo (connects to server)")
247+
fmt.Println(" demo - Run MCP self-connection demo (connects to server)")
248248
os.Exit(1)
249249
}
250250

251251
switch os.Args[1] {
252252
case "server":
253253
createMCPServer()
254254
case "demo":
255-
mcpCeptionDemo()
255+
mcpSelfConnection()
256256
default:
257257
fmt.Printf("Unknown command: %s\n", os.Args[1])
258258
fmt.Println("Use 'server' or 'demo'")

0 commit comments

Comments
 (0)