|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "syscall" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/mark3labs/mcp-go/client/transport" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + // Create a new Streamable HTTP transport with a longer timeout |
| 18 | + trans, err := transport.NewStreamableHTTP("http://localhost:8080/mcp", |
| 19 | + transport.WithHTTPTimeout(30*time.Second)) |
| 20 | + if err != nil { |
| 21 | + fmt.Printf("Failed to create transport: %v\n", err) |
| 22 | + os.Exit(1) |
| 23 | + } |
| 24 | + defer trans.Close() |
| 25 | + |
| 26 | + // Set up notification handler |
| 27 | + trans.SetNotificationHandler(func(notification mcp.JSONRPCNotification) { |
| 28 | + fmt.Printf("\nReceived notification: %s\n", notification.Method) |
| 29 | + params, _ := json.MarshalIndent(notification.Params, "", " ") |
| 30 | + fmt.Printf("Params: %s\n", params) |
| 31 | + }) |
| 32 | + |
| 33 | + // Create a context with timeout |
| 34 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + // Initialize the connection |
| 38 | + fmt.Println("Initializing connection...") |
| 39 | + initRequest := transport.JSONRPCRequest{ |
| 40 | + JSONRPC: "2.0", |
| 41 | + ID: 1, |
| 42 | + Method: "initialize", |
| 43 | + } |
| 44 | + |
| 45 | + initResponse, err := trans.SendRequest(ctx, initRequest) |
| 46 | + if err != nil { |
| 47 | + fmt.Printf("Failed to initialize: %v\n", err) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + // Print the initialization response |
| 52 | + initResponseJSON, _ := json.MarshalIndent(initResponse, "", " ") |
| 53 | + fmt.Printf("Initialization response: %s\n", initResponseJSON) |
| 54 | + fmt.Printf("Session ID: %s\n", trans.GetSessionId()) |
| 55 | + |
| 56 | + // List available tools |
| 57 | + fmt.Println("\nListing available tools...") |
| 58 | + listToolsRequest := transport.JSONRPCRequest{ |
| 59 | + JSONRPC: "2.0", |
| 60 | + ID: 2, |
| 61 | + Method: "tools/list", |
| 62 | + } |
| 63 | + |
| 64 | + listToolsResponse, err := trans.SendRequest(ctx, listToolsRequest) |
| 65 | + if err != nil { |
| 66 | + fmt.Printf("Failed to list tools: %v\n", err) |
| 67 | + os.Exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + // Print the tools list response |
| 71 | + toolsResponseJSON, _ := json.MarshalIndent(listToolsResponse, "", " ") |
| 72 | + fmt.Printf("Tools list response: %s\n", toolsResponseJSON) |
| 73 | + |
| 74 | + // Extract tool information |
| 75 | + var toolsResult struct { |
| 76 | + Result struct { |
| 77 | + Tools []struct { |
| 78 | + Name string `json:"name"` |
| 79 | + Description string `json:"description"` |
| 80 | + } `json:"tools"` |
| 81 | + } `json:"result"` |
| 82 | + } |
| 83 | + if err := json.Unmarshal(listToolsResponse.Result, &toolsResult); err != nil { |
| 84 | + fmt.Printf("Failed to parse tools list: %v\n", err) |
| 85 | + } else { |
| 86 | + fmt.Println("\nAvailable tools:") |
| 87 | + for _, tool := range toolsResult.Result.Tools { |
| 88 | + fmt.Printf("- %s: %s\n", tool.Name, tool.Description) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Call the echo tool |
| 93 | + fmt.Println("\nCalling echo tool...") |
| 94 | + echoRequest := transport.JSONRPCRequest{ |
| 95 | + JSONRPC: "2.0", |
| 96 | + ID: 3, |
| 97 | + Method: "tools/call", |
| 98 | + Params: map[string]interface{}{ |
| 99 | + "name": "echo", |
| 100 | + "arguments": map[string]interface{}{ |
| 101 | + "message": "Hello from Streamable HTTP client!", |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + echoResponse, err := trans.SendRequest(ctx, echoRequest) |
| 107 | + if err != nil { |
| 108 | + fmt.Printf("Failed to call echo tool: %v\n", err) |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + // Print the echo response |
| 113 | + echoResponseJSON, _ := json.MarshalIndent(echoResponse, "", " ") |
| 114 | + fmt.Printf("Echo response: %s\n", echoResponseJSON) |
| 115 | + |
| 116 | + // Wait for notifications (the echo tool sends a notification after 1 second) |
| 117 | + fmt.Println("\nWaiting for notifications...") |
| 118 | + fmt.Println("(The server should send a notification about 1 second after the tool call)") |
| 119 | + |
| 120 | + // Set up a signal channel to handle Ctrl+C |
| 121 | + sigChan := make(chan os.Signal, 1) |
| 122 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 123 | + |
| 124 | + // Wait for either a signal or a timeout |
| 125 | + select { |
| 126 | + case <-sigChan: |
| 127 | + fmt.Println("Received interrupt signal, exiting...") |
| 128 | + case <-time.After(5 * time.Second): |
| 129 | + fmt.Println("Timeout reached, exiting...") |
| 130 | + } |
| 131 | +} |
0 commit comments