| 141 | } |
| 142 | |
| 143 | func runClient(url string) { |
| 144 | ctx := context.Background() |
| 145 | |
| 146 | // Create the URL for the server. |
| 147 | log.Printf("Connecting to MCP server at %s", url) |
| 148 | |
| 149 | // Create an MCP client. |
| 150 | client := mcp.NewClient(&mcp.Implementation{ |
| 151 | Name: "time-client", |
| 152 | Version: "1.0.0", |
| 153 | }, nil) |
| 154 | |
| 155 | // Connect to the server. |
| 156 | session, err := client.Connect(ctx, &mcp.StreamableClientTransport{Endpoint: url}, nil) |
| 157 | if err != nil { |
| 158 | log.Fatalf("Failed to connect: %v", err) |
| 159 | } |
| 160 | defer session.Close() |
| 161 | |
| 162 | log.Printf("Connected to server (session ID: %s)", session.ID()) |
| 163 | |
| 164 | // First, list available tools. |
| 165 | log.Println("Listing available tools...") |
| 166 | toolsResult, err := session.ListTools(ctx, nil) |
| 167 | if err != nil { |
| 168 | log.Fatalf("Failed to list tools: %v", err) |
| 169 | } |
| 170 | |
| 171 | for _, tool := range toolsResult.Tools { |
| 172 | log.Printf(" - %s: %s\n", tool.Name, tool.Description) |
| 173 | } |
| 174 | |
| 175 | // Call the cityTime tool for each city. |
| 176 | cities := []string{"nyc", "sf", "boston"} |
| 177 | |
| 178 | log.Println("Getting time for each city...") |
| 179 | for _, city := range cities { |
| 180 | // Call the tool. |
| 181 | result, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 182 | Name: "cityTime", |
| 183 | Arguments: map[string]any{ |
| 184 | "city": city, |
| 185 | }, |
| 186 | }) |
| 187 | if err != nil { |
| 188 | log.Printf("Failed to get time for %s: %v\n", city, err) |
| 189 | continue |
| 190 | } |
| 191 | |
| 192 | // Print the result. |
| 193 | for _, content := range result.Content { |
| 194 | if textContent, ok := content.(*mcp.TextContent); ok { |
| 195 | log.Printf(" %s", textContent.Text) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | log.Println("Client completed successfully") |