-
Notifications
You must be signed in to change notification settings - Fork 454
Add example for calling BigQuery via MCP #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| 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` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // 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()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Go documentation conventions, the package comment for a
mainpackage should start with// Command <command-name> ...or// Package main .... The current comment is missing the package/command name.