-
Notifications
You must be signed in to change notification settings - Fork 712
docs(client): improve server reliability and error handling #560
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
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 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -121,18 +121,24 @@ func main() { | |||||||||||||||||||||||||||||||||||||
| serverInfo.ServerInfo.Version) | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Server capabilities: %+v\n", serverInfo.Capabilities) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Perform health check using ping | ||||||||||||||||||||||||||||||||||||||
| fmt.Println("Performing health check...") | ||||||||||||||||||||||||||||||||||||||
| if err := c.Ping(ctx); err != nil { | ||||||||||||||||||||||||||||||||||||||
| log.Fatalf("Health check failed: %v", err) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| fmt.Println("Server is alive and responding") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // List available tools if the server supports them | ||||||||||||||||||||||||||||||||||||||
| if serverInfo.Capabilities.Tools != nil { | ||||||||||||||||||||||||||||||||||||||
| fmt.Println("Fetching available tools...") | ||||||||||||||||||||||||||||||||||||||
| toolsRequest := mcp.ListToolsRequest{} | ||||||||||||||||||||||||||||||||||||||
| toolsResult, err := c.ListTools(ctx, toolsRequest) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| log.Printf("Failed to list tools: %v", err) | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Server has %d tools available\n", len(toolsResult.Tools)) | ||||||||||||||||||||||||||||||||||||||
| for i, tool := range toolsResult.Tools { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %d. %s - %s\n", i+1, tool.Name, tool.Description) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| log.Fatalf("Failed to list tools: %v", err) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Server has %d tools available\n", len(toolsResult.Tools)) | ||||||||||||||||||||||||||||||||||||||
| for i, tool := range toolsResult.Tools { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %d. %s - %s\n", i+1, tool.Name, tool.Description) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+137
to
142
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. 🛠️ Refactor suggestion Guard ListTools result against nil and ensure cleanup on fatal. If err is nil but toolsResult is unexpectedly nil, len(toolsResult.Tools) will panic. Also replace log.Fatalf with fatalf to close the client. - log.Fatalf("Failed to list tools: %v", err)
+ fatalf(c, "Failed to list tools: %v", err)
}
- fmt.Printf("Server has %d tools available\n", len(toolsResult.Tools))
- for i, tool := range toolsResult.Tools {
+ if toolsResult == nil {
+ fatalf(c, "ListTools returned nil result without error")
+ }
+ fmt.Printf("Server has %d tools available\n", len(toolsResult.Tools))
+ for i, tool := range toolsResult.Tools {
fmt.Printf(" %d. %s - %s\n", i+1, tool.Name, tool.Description)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -142,12 +148,11 @@ func main() { | |||||||||||||||||||||||||||||||||||||
| resourcesRequest := mcp.ListResourcesRequest{} | ||||||||||||||||||||||||||||||||||||||
| resourcesResult, err := c.ListResources(ctx, resourcesRequest) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| log.Printf("Failed to list resources: %v", err) | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Server has %d resources available\n", len(resourcesResult.Resources)) | ||||||||||||||||||||||||||||||||||||||
| for i, resource := range resourcesResult.Resources { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %d. %s - %s\n", i+1, resource.URI, resource.Name) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| log.Fatalf("Failed to list resources: %v", err) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Server has %d resources available\n", len(resourcesResult.Resources)) | ||||||||||||||||||||||||||||||||||||||
| for i, resource := range resourcesResult.Resources { | ||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %d. %s - %s\n", i+1, resource.URI, resource.Name) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+151
to
156
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. 🛠️ Refactor suggestion Guard ListResources result against nil and ensure cleanup on fatal. Same panic risk and cleanup concern as tools; harden the branch. - log.Fatalf("Failed to list resources: %v", err)
+ fatalf(c, "Failed to list resources: %v", err)
}
- fmt.Printf("Server has %d resources available\n", len(resourcesResult.Resources))
- for i, resource := range resourcesResult.Resources {
+ if resourcesResult == nil {
+ fatalf(c, "ListResources returned nil result without error")
+ }
+ fmt.Printf("Server has %d resources available\n", len(resourcesResult.Resources))
+ for i, resource := range resourcesResult.Resources {
fmt.Printf(" %d. %s - %s\n", i+1, resource.URI, resource.Name)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.