|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/mark3labs/mcp-go/client" |
| 12 | + "github.com/mark3labs/mcp-go/client/transport" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | +) |
| 15 | + |
| 16 | +// MockRootsHandler implements client.RootsHandler for demonstration. |
| 17 | +// In a real implementation, this would integrate with an actual LLM API. |
| 18 | +type MockRootsHandler struct{} |
| 19 | + |
| 20 | +func (h *MockRootsHandler) ListRoots(ctx context.Context, request mcp.ListRootsRequest) (*mcp.ListRootsResult, error) { |
| 21 | + result := &mcp.ListRootsResult{ |
| 22 | + Roots: []mcp.Root{ |
| 23 | + { |
| 24 | + Name: "app", |
| 25 | + URI: "file:///User/haxxx/app", |
| 26 | + }, |
| 27 | + { |
| 28 | + Name: "test-project", |
| 29 | + URI: "file:///User/haxxx/projects/test-project", |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + return result, nil |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + if len(os.Args) < 2 { |
| 38 | + log.Fatal("Usage: roots_client <server_command>") |
| 39 | + } |
| 40 | + |
| 41 | + serverCommand := os.Args[1] |
| 42 | + serverArgs := os.Args[2:] |
| 43 | + |
| 44 | + // Create stdio transport to communicate with the server |
| 45 | + stdio := transport.NewStdio(serverCommand, nil, serverArgs...) |
| 46 | + |
| 47 | + // Create roots handler |
| 48 | + rootsHandler := &MockRootsHandler{} |
| 49 | + |
| 50 | + // Create client with roots capability |
| 51 | + mcpClient := client.NewClient(stdio, client.WithRootsHandler(rootsHandler)) |
| 52 | + |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + // Start the client |
| 56 | + if err := mcpClient.Start(ctx); err != nil { |
| 57 | + log.Fatalf("Failed to start client: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Setup graceful shutdown |
| 61 | + sigChan := make(chan os.Signal, 1) |
| 62 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 63 | + |
| 64 | + // Create a context that cancels on signal |
| 65 | + ctx, cancel := context.WithCancel(ctx) |
| 66 | + go func() { |
| 67 | + <-sigChan |
| 68 | + log.Println("Received shutdown signal, closing client...") |
| 69 | + cancel() |
| 70 | + }() |
| 71 | + |
| 72 | + // Move defer after error checking |
| 73 | + defer func() { |
| 74 | + if err := mcpClient.Close(); err != nil { |
| 75 | + log.Printf("Error closing client: %v", err) |
| 76 | + } |
| 77 | + }() |
| 78 | + |
| 79 | + // Initialize the connection |
| 80 | + initResult, err := mcpClient.Initialize(ctx, mcp.InitializeRequest{ |
| 81 | + Params: mcp.InitializeParams{ |
| 82 | + ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, |
| 83 | + ClientInfo: mcp.Implementation{ |
| 84 | + Name: "roots-stdio-server", |
| 85 | + Version: "1.0.0", |
| 86 | + }, |
| 87 | + Capabilities: mcp.ClientCapabilities{ |
| 88 | + // Sampling capability will be automatically added by WithSamplingHandler |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + log.Fatalf("Failed to initialize: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("Connected to server: %s v%s", initResult.ServerInfo.Name, initResult.ServerInfo.Version) |
| 97 | + log.Printf("Server capabilities: %+v", initResult.Capabilities) |
| 98 | + |
| 99 | + // list tools |
| 100 | + toolsResult, err := mcpClient.ListTools(ctx, mcp.ListToolsRequest{}) |
| 101 | + if err != nil { |
| 102 | + log.Fatalf("Failed to list tools: %v", err) |
| 103 | + } |
| 104 | + log.Printf("Available tools:") |
| 105 | + for _, tool := range toolsResult.Tools { |
| 106 | + log.Printf(" - %s: %s", tool.Name, tool.Description) |
| 107 | + } |
| 108 | + |
| 109 | + // mock the root change |
| 110 | + if err := mcpClient.RootListChanges(ctx); err != nil { |
| 111 | + log.Printf("fail to notify root list change: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + // call server tool |
| 115 | + request := mcp.CallToolRequest{} |
| 116 | + request.Params.Name = "roots" |
| 117 | + request.Params.Arguments = "{\"testonly\": \"yes\"}" |
| 118 | + result, err := mcpClient.CallTool(ctx, request) |
| 119 | + if err != nil { |
| 120 | + log.Fatalf("failed to call tool roots: %v", err) |
| 121 | + } else if len(result.Content) > 0 { |
| 122 | + resultStr := "" |
| 123 | + for _, content := range result.Content { |
| 124 | + if textContent, ok := content.(mcp.TextContent); ok { |
| 125 | + resultStr += fmt.Sprintf("%s\n", textContent.Text) |
| 126 | + } |
| 127 | + } |
| 128 | + fmt.Printf("client call tool result: %s", resultStr) |
| 129 | + } |
| 130 | +} |
0 commit comments