-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs: migration of core package samples from go sdk #2330
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: migrate-go-sdk
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @AnmolShukla2002, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the documentation for the MCP Toolbox Go SDK's core package. It provides developers with a centralized and comprehensive resource for understanding the SDK's capabilities, from basic setup and tool interaction to advanced topics like secure authentication mechanisms and flexible parameter binding. Furthermore, it offers ready-to-use integration samples for leading AI orchestration frameworks, streamlining the development of intelligent applications using the Toolbox. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request adds extensive documentation and samples for the Go SDK core package. The documentation is comprehensive, but I've found several issues in the code samples, including incomplete examples, incorrect API usage, unhandled errors, and typos. These issues could confuse users or cause their code to fail if they copy the examples. I've left specific comments with suggestions for fixes. Addressing these will greatly improve the quality and usability of the documentation.
| } | ||
|
|
||
| var schema *genai.Schema | ||
| _ = json.Unmarshal(inputschema, &schema) |
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.
The error returned by json.Unmarshal is ignored by assigning it to the blank identifier _. This is a bad practice as it can hide issues with JSON parsing, potentially leading to a nil schema and causing nil pointer dereferences later. The error should be checked.
| _ = json.Unmarshal(inputschema, &schema) | |
| if err := json.Unmarshal(inputschema, &schema); err != nil { | |
| log.Printf("failed to unmarshal schema: %v", err) | |
| return &genai.Tool{} | |
| } |
|
|
||
|
|
||
| ```go | ||
| import "github.com/googleapis/mcp-toolbox-sdk-go/core" |
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.
This code example is incomplete as it uses the oauth2 package without importing it. Please add the import for golang.org/x/oauth2 to make the example runnable.
| import "github.com/googleapis/mcp-toolbox-sdk-go/core" | |
| import ( | |
| "github.com/googleapis/mcp-toolbox-sdk-go/core" | |
| "golang.org/x/oauth2" | |
| ) |
| token, err := core.GetGoogleIDToken(ctx, URL) | ||
|
|
||
| client, err := core.NewToolboxClient( | ||
| URL, | ||
| core.WithClientHeaderString("Authorization", token), |
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.
This code snippet has a couple of issues:
- The
URLvariable is used but not defined. It should be replaced with a placeholder to make it clear to the user. - The
Authorizationheader for a bearer token should be in the format"Bearer <token>". The current example is missing the"Bearer "prefix, which will lead to authentication failures.
| token, err := core.GetGoogleIDToken(ctx, URL) | |
| client, err := core.NewToolboxClient( | |
| URL, | |
| core.WithClientHeaderString("Authorization", token), | |
| token, err := core.GetGoogleIDToken(ctx, "your-cloud-run-url") | |
| client, err := core.NewToolboxClient( | |
| "your-cloud-run-url", | |
| core.WithClientHeaderString("Authorization", "Bearer "+token), |
| tool, err := client.LoadTool("my-tool", ctx) | ||
|
|
||
| AuthTool, err := tool.ToolFrom( | ||
| core.WithAuthTokenSource("my-auth", headerTokenSource), |
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.
| log.Fatal("Could not invoke tool", err) | ||
| } | ||
|
|
||
| params.Messages = append(params.Messages, openai.ToolMessage(result.(string), toolCall.ID)) |
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.
The type assertion result.(string) is unsafe and will cause a panic if the result from tool.Invoke is not a string. The Invoke method returns any, so its type is not guaranteed. It's safer to convert the result to a string using fmt.Sprintf to avoid a panic.
| params.Messages = append(params.Messages, openai.ToolMessage(result.(string), toolCall.ID)) | |
| params.Messages = append(params.Messages, openai.ToolMessage(fmt.Sprintf("%v", result), toolCall.ID)) |
| // Load the tools using the MCP Toolbox SDK. | ||
| tools, err := toolboxClient.LoadToolset("my-toolset", ctx) | ||
| if err != nil { | ||
| log.Fatalf("Failed to load tool : %v\nMake sure your Toolbox server is running and the tool is configured.", err) |
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.
There is a minor typo in the log message: an extra space before the colon.
| log.Fatalf("Failed to load tool : %v\nMake sure your Toolbox server is running and the tool is configured.", err) | |
| log.Fatalf("Failed to load tool: %v\nMake sure your Toolbox server is running and the tool is configured.", err) |
| toolsMap[tool.Name()] = tool | ||
|
|
||
| } | ||
| question := "Find hotels in Basel with Basel in it's name " |
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.
| // Make initial chat completion request | ||
| completion, err := openAIClient.Chat.Completions.New(ctx, params) | ||
| if err != nil { | ||
| panic(err) |
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.
This example uses panic(err) for error handling, which is generally discouraged in Go applications unless it's a truly unrecoverable state during initialization. It's also inconsistent with other parts of the example that use log.Fatal. Using log.Fatalf would be more idiomatic and consistent for a main function.
| panic(err) | |
| log.Fatalf("Chat completion failed: %v", err) |
| return | ||
| } | ||
|
|
||
| // If there is a was a function call, continue the conversation |
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.
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > Adding auth tokens during loading only affect the tools loaded within that |
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.
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.
Can we have major sections added as separate pages under the SDKs' sections?
|
There seem to be links to the READMEs of individual packages in the top-level doc (screenshot). Can we have them linked to this docsite? |
| > [!NOTE] | ||
| > | ||
| > - While the SDK itself is synchronous, you can execute its functions within goroutines to achieve asynchronous behavior. |
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.
Can we make the notes render according to hugo/docsy way (check notes/tips in other docs)?
This renders well in github but doesn't work with docsy.
| > [!NOTE] | ||
| > | ||
| > - While the SDK itself is synchronous, you can execute its functions within goroutines to achieve asynchronous behavior. |
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.
Can we make the notes render according to hugo/docsy way (check notes/tips in other docs)?
This renders well in github but doesn't work with docsy.
Same for other notes/warnings/tips below.
|
|
||
| If you encounter issues or have questions, check the existing [GitHub Issues](https://github.com/googleapis/genai-toolbox/issues) for the main Toolbox project. | ||
|
|
||
| ## Samples for Reference |
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.
Could there be a better way for presenting this? How about linking a github repo directory with these samples (so users can use the code review/hover features as well)?
Might also be simpler for samples testing, but I'd leave the final call to you.
No description provided.