@@ -2,6 +2,9 @@ package client
22
33import (
44 "context"
5+ "encoding/json"
6+ "regexp"
7+ "strings"
58 "sync"
69 "testing"
710 "time"
@@ -21,12 +24,14 @@ func setupMCPClient(t *testing.T, url string) *MCPConnection {
2124 // Create a context for this specific connection
2225 ctx , cancel := context .WithCancel (context .Background ())
2326
27+ t .Logf ("Creating new SSE client for %s" , url )
2428 client , err := NewSSEMCPClient (url )
2529 if err != nil {
2630 cancel ()
2731 t .Fatalf ("Failed to create client for %s: %v" , url , err )
2832 }
2933
34+ t .Logf ("Starting SSE client..." )
3035 if err := client .Start (ctx ); err != nil {
3136 cancel ()
3237 t .Fatalf ("Failed to start client for %s: %v" , url , err )
@@ -40,12 +45,13 @@ func setupMCPClient(t *testing.T, url string) *MCPConnection {
4045 Version : "1.0.0" ,
4146 }
4247
48+ t .Logf ("Initializing SSE client..." )
4349 result , err := client .Initialize (ctx , initRequest )
4450 if err != nil {
4551 cancel ()
4652 t .Fatalf ("Failed to initialize client for %s: %v" , url , err )
4753 }
48- t .Logf ("Connected to server at %s: %s" , url , result .ServerInfo .Name )
54+ t .Logf ("Successfully connected to server at %s: %s" , url , result .ServerInfo .Name )
4955
5056 return & MCPConnection {
5157 client : client ,
@@ -55,11 +61,27 @@ func setupMCPClient(t *testing.T, url string) *MCPConnection {
5561 }
5662}
5763
64+ func checkConnection (t * testing.T , conn * MCPConnection ) error {
65+ t .Log ("Checking connection status..." )
66+ // Create a context with a short timeout for the check
67+ checkCtx , cancel := context .WithTimeout (conn .ctx , 5 * time .Second )
68+ defer cancel ()
69+
70+ // Try a simple ListTools request to verify connection
71+ toolsRequest := mcp.ListToolsRequest {}
72+ _ , err := conn .client .ListTools (checkCtx , toolsRequest )
73+ if err != nil {
74+ t .Logf ("Connection check failed: %v" , err )
75+ return err
76+ }
77+ t .Log ("Connection check successful" )
78+ return nil
79+ }
80+
5881func TestMultipleMCPServers (t * testing.T ) {
5982 // Define our MCP servers
6083 servers := []string {
61- "http://0.0.0.0:8000/sse" ,
62- }
84+ "http://0.0.0.0:8000/sse" }
6385
6486 // Create connections to all servers
6587 connections := make ([]* MCPConnection , 0 , len (servers ))
@@ -105,3 +127,121 @@ func TestMultipleMCPServers(t *testing.T) {
105127 // Wait for all operations to complete
106128 wg .Wait ()
107129}
130+
131+ func TestBrowserTools (t * testing.T ) {
132+ // Setup connection to the MCP server
133+ conn := setupMCPClient (t , "http://0.0.0.0:8000/sse" )
134+ defer func () {
135+ t .Log ("Cleaning up connection..." )
136+ conn .cancel () // Cancel the context first
137+ conn .client .Close () // Then close the client
138+ t .Log ("Connection cleanup complete" )
139+ }()
140+
141+ // Create a context with timeout for the first operation
142+ ctx1 , cancel1 := context .WithTimeout (conn .ctx , 60 * time .Second )
143+ defer cancel1 ()
144+
145+ // First tool call: browser_use
146+ t .Log ("Making browser_use tool call..." )
147+ browserUseRequest := mcp.CallToolRequest {
148+ Params : struct {
149+ Name string `json:"name"`
150+ Arguments map [string ]interface {} `json:"arguments,omitempty"`
151+ Meta * struct {
152+ ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"`
153+ } `json:"_meta,omitempty"`
154+ }{
155+ Name : "browser_use" ,
156+ Arguments : map [string ]interface {}{
157+ "action" : "top 5 headlines" ,
158+ "url" : "https://news.google.com/home?hl=en-US&gl=US&ceid=US:en" ,
159+ },
160+ },
161+ }
162+
163+ browserUseResult , err := conn .client .CallTool (ctx1 , browserUseRequest )
164+ if err != nil {
165+ t .Fatalf ("Failed to call browser_use: %v" , err )
166+ }
167+
168+ // Log the raw result for debugging
169+ t .Logf ("Raw browser_use result: %+v" , browserUseResult )
170+
171+ // Extract task ID from the result
172+ var taskID string
173+ if len (browserUseResult .Content ) > 0 {
174+ content := browserUseResult .Content [0 ]
175+ // Convert the content to string for regex matching
176+ contentBytes , err := json .Marshal (content )
177+ if err != nil {
178+ t .Fatalf ("Failed to marshal content: %v" , err )
179+ }
180+ contentStr := string (contentBytes )
181+
182+ // First extract the text field content
183+ textRegex := regexp .MustCompile (`"text":\s*"({[^}]+})"` )
184+ textMatches := textRegex .FindStringSubmatch (contentStr )
185+ if len (textMatches ) < 2 {
186+ t .Fatalf ("Could not find text content in response: %s" , contentStr )
187+ }
188+
189+ // The text field contains escaped JSON, so we need to unescape it
190+ textContent := textMatches [1 ]
191+ textContent = strings .ReplaceAll (textContent , `\n` , "\n " )
192+ textContent = strings .ReplaceAll (textContent , `\"` , `"` )
193+
194+ // Now extract the task_id from the unescaped JSON
195+ taskIDRegex := regexp .MustCompile (`"task_id":\s*"([^"]+)"` )
196+ matches := taskIDRegex .FindStringSubmatch (textContent )
197+ if len (matches ) < 2 {
198+ t .Fatalf ("Could not find task_id in text content: %s" , textContent )
199+ }
200+ taskID = matches [1 ]
201+ t .Logf ("Got task ID: %s" , taskID )
202+ } else {
203+ t .Fatalf ("No content in result" )
204+ }
205+
206+ // Wait 40 seconds before checking result
207+ t .Log ("Waiting 40 seconds before checking result..." )
208+ time .Sleep (40 * time .Second )
209+
210+ // Create a fresh context for the second operation
211+ ctx2 , cancel2 := context .WithTimeout (conn .ctx , 60 * time .Second )
212+ defer cancel2 ()
213+
214+ // Second tool call: browser_get_result
215+ t .Log ("Making browser_get_result tool call..." )
216+ browserGetResultRequest := mcp.CallToolRequest {
217+ Params : struct {
218+ Name string `json:"name"`
219+ Arguments map [string ]interface {} `json:"arguments,omitempty"`
220+ Meta * struct {
221+ ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"`
222+ } `json:"_meta,omitempty"`
223+ }{
224+ Name : "browser_get_result" ,
225+ Arguments : map [string ]interface {}{
226+ "task_id" : taskID ,
227+ },
228+ },
229+ }
230+
231+ browserGetResultResult , err := conn .client .CallTool (ctx2 , browserGetResultRequest )
232+ if err != nil {
233+ t .Fatalf ("Failed to call browser_get_result: %v" , err )
234+ }
235+
236+ // Log the final result
237+ if len (browserGetResultResult .Content ) > 0 {
238+ content := browserGetResultResult .Content [0 ]
239+ contentBytes , err := json .Marshal (content )
240+ if err != nil {
241+ t .Fatalf ("Failed to marshal content: %v" , err )
242+ }
243+ t .Logf ("Final result: %s" , string (contentBytes ))
244+ } else {
245+ t .Fatalf ("No content in final result" )
246+ }
247+ }
0 commit comments