Skip to content
Open
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
119 changes: 119 additions & 0 deletions examples/bigquery_mcp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package provides an example ADK agent that uses BigQuery via MCP.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

According to Go documentation conventions, the package comment for a main package should start with // Command <command-name> ... or // Package main .... The current comment is missing the package/command name.

Suggested change
// Package provides an example ADK agent that uses BigQuery via MCP.
// Command bigquery_mcp provides an example ADK agent that uses BigQuery via MCP.

package main

import (
"context"
"log"
"net/http"
"os"
"os/signal"

"github.com/modelcontextprotocol/go-sdk/mcp"
"golang.org/x/oauth2"
"google.golang.org/genai"

"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/cmd/launcher/full"
"google.golang.org/adk/model/gemini"
"google.golang.org/adk/tool"
"google.golang.org/adk/tool/mcptoolset"
)

// In order to run this example you need to:
// - set the environment variable `GCP_PROJECT_ID`
// - set the environment variable `GOOGLE_API_KEY`
// - set the environment variable `TEST_TOKEN` (you can use command like "export TEST_TOKEN=$(gcloud auth print-access-token)")
// - ensure you have enabled "BigQuery API" (bigquery.googleapis.com) for project indicated in `GCP_PROJECT_ID`
// - ensure you have access to the project indidated in `GCP_PROJECT_ID`
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There is a typo in the comment. indidated should be indicated.

Suggested change
// - ensure you have access to the project indidated in `GCP_PROJECT_ID`
// - ensure you have access to the project indicated in `GCP_PROJECT_ID`

// You can try prompt like:
// `select server date using googlesql from project ` + value of `GCP_PROJECT_ID`

// TransportWithHeaders adds "X-Goog-User-Project" header to BigQuery MCP calls
type TransportWithHeaders struct {
parent http.RoundTripper
project string // GCP Project ID
}

func (t *TransportWithHeaders) RoundTrip(req *http.Request) (*http.Response, error) {
if t.project != "" {
req = req.Clone(req.Context())
req.Header.Add("X-Goog-User-Project", t.project)
}
return t.parent.RoundTrip(req)
}

func main() {
project := os.Getenv("GCP_PROJECT_ID")
apiKey := os.Getenv("GOOGLE_API_KEY")
token := os.Getenv("TEST_TOKEN")
Comment on lines +62 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The application expects several environment variables to be set, but it doesn't check for their existence. If they are missing, the program will fail with a less-than-clear error message later on. It's better to check for these variables at startup and provide a clear error message to the user if they are not set.

project := os.Getenv("GCP_PROJECT_ID")
	if project == "" {
		log.Fatal("GCP_PROJECT_ID environment variable must be set")
	}
	apiKey := os.Getenv("GOOGLE_API_KEY")
	if apiKey == "" {
		log.Fatal("GOOGLE_API_KEY environment variable must be set")
	}
	token := os.Getenv("TEST_TOKEN")
	if token == "" {
		log.Fatal("TEST_TOKEN environment variable must be set")
	}


// Create context that cancels on interrupt signal (Ctrl+C)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

model, err := gemini.NewModel(ctx, "gemini-2.5-flash", &genai.ClientConfig{
APIKey: apiKey,
})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}

oauthClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))

transport := &mcp.StreamableClientTransport{
Endpoint: "https://bigquery.googleapis.com/mcp",

HTTPClient: &http.Client{
Transport: &TransportWithHeaders{parent: oauthClient.Transport, project: project},
},
}
mcpToolSet, err := mcptoolset.New(mcptoolset.Config{
Transport: transport,
// WARNING: we need to filter out "get_table_info" tool because of errors is causes ("reference to undefined schema at $defs.RangePartitioning.properties.range")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There is a grammatical error in the comment. is causes should be it causes.

// WARNING: we need to filter out "get_table_info" tool because of errors it causes ("reference to undefined schema at $defs.RangePartitioning.properties.range")

// we need now just 'execute_sql' tool
ToolFilter: tool.StringPredicate([]string{"execute_sql"}),
})
if err != nil {
log.Fatalf("Failed to create MCP tool set: %v", err)
}

// Create LLMAgent with MCP tool set
a, err := llmagent.New(llmagent.Config{
Name: "helper_agent",
Model: model,
Description: "Helper agent.",
Instruction: "You are a helpful assistant that helps users with various tasks.",
Toolsets: []tool.Toolset{
mcpToolSet,
},
})
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}

config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(a),
}
l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}
}
Loading