|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/mark3labs/mcp-go/mcp" |
| 7 | + "github.com/mark3labs/mcp-go/server" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHTTPClient(t *testing.T) { |
| 13 | + hooks := &server.Hooks{} |
| 14 | + hooks.AddAfterCallTool(func(ctx context.Context, id any, message *mcp.CallToolRequest, result *mcp.CallToolResult) { |
| 15 | + clientSession := server.ClientSessionFromContext(ctx) |
| 16 | + // wait until all the notifications are handled |
| 17 | + for len(clientSession.NotificationChannel()) > 0 { |
| 18 | + } |
| 19 | + time.Sleep(time.Millisecond * 50) |
| 20 | + }) |
| 21 | + |
| 22 | + // Create MCP server with capabilities |
| 23 | + mcpServer := server.NewMCPServer( |
| 24 | + "test-server", |
| 25 | + "1.0.0", |
| 26 | + server.WithToolCapabilities(true), |
| 27 | + server.WithHooks(hooks), |
| 28 | + ) |
| 29 | + |
| 30 | + mcpServer.AddTool( |
| 31 | + mcp.NewTool("notify"), |
| 32 | + func( |
| 33 | + ctx context.Context, |
| 34 | + request mcp.CallToolRequest, |
| 35 | + ) (*mcp.CallToolResult, error) { |
| 36 | + server := server.ServerFromContext(ctx) |
| 37 | + err := server.SendNotificationToClient( |
| 38 | + ctx, |
| 39 | + "notifications/progress", |
| 40 | + map[string]any{ |
| 41 | + "progress": 10, |
| 42 | + "total": 10, |
| 43 | + "progressToken": 0, |
| 44 | + }, |
| 45 | + ) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to send notification: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + return &mcp.CallToolResult{ |
| 51 | + Content: []mcp.Content{ |
| 52 | + mcp.TextContent{ |
| 53 | + Type: "text", |
| 54 | + Text: "notification sent successfully", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, nil |
| 58 | + }, |
| 59 | + ) |
| 60 | + |
| 61 | + testServer := server.NewTestStreamableHTTPServer(mcpServer) |
| 62 | + defer testServer.Close() |
| 63 | + |
| 64 | + t.Run("Can receive notification from server", func(t *testing.T) { |
| 65 | + client, err := NewStreamableHttpClient(testServer.URL) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("create client failed %v", err) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + notificationNum := 0 |
| 72 | + client.OnNotification(func(notification mcp.JSONRPCNotification) { |
| 73 | + notificationNum += 1 |
| 74 | + }) |
| 75 | + |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + if err := client.Start(ctx); err != nil { |
| 79 | + t.Fatalf("Failed to start client: %v", err) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Initialize |
| 84 | + initRequest := mcp.InitializeRequest{} |
| 85 | + initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION |
| 86 | + initRequest.Params.ClientInfo = mcp.Implementation{ |
| 87 | + Name: "test-client", |
| 88 | + Version: "1.0.0", |
| 89 | + } |
| 90 | + |
| 91 | + _, err = client.Initialize(ctx, initRequest) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("Failed to initialize: %v\n", err) |
| 94 | + } |
| 95 | + |
| 96 | + request := mcp.CallToolRequest{} |
| 97 | + request.Params.Name = "notify" |
| 98 | + result, err := client.CallTool(ctx, request) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("CallTool failed: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + if len(result.Content) != 1 { |
| 104 | + t.Errorf("Expected 1 content item, got %d", len(result.Content)) |
| 105 | + } |
| 106 | + |
| 107 | + if notificationNum != 1 { |
| 108 | + t.Errorf("Expected 1 notification item, got %d", notificationNum) |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
0 commit comments