44 "context"
55 "encoding/json"
66 "fmt"
7- "log"
87 "time"
98)
109
@@ -30,7 +29,6 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
3029
3130 // STEP 1: Initialize
3231 initReq := []byte (`{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "AgentGate", "version": "1.0.0"}}}` )
33- log .Printf ("[MCPClient] Sending Initialize: %s" , string (initReq ))
3432 if err := c .transport .Send (initReq ); err != nil {
3533 return nil , fmt .Errorf ("initialize send failed: %w" , err )
3634 }
@@ -40,20 +38,17 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
4038 var lastErr error
4139 for i := 0 ; i < 10 ; i ++ { // Try reading up to 10 messages safely without hanging
4240 raw , err := c .transport .Receive (ctx )
43-
41+
4442 if err != nil {
45- log .Printf ("[MCPClient] Initialize Receive Error: %v" , err )
4643 lastErr = err
4744 break
4845 }
49- log .Printf ("[MCPClient] Initialize Received Raw: %s" , string (raw ))
50-
46+
5147 resp , err := parseTolerantResponse (raw )
5248 if err != nil {
53- log .Printf ("[MCPClient] Initialize Parse Error (Ignoring): %v" , err )
5449 continue // Ignore garbage preamble prints
5550 }
56-
51+
5752 if resp .ID != nil {
5853 idVal := fmt .Sprintf ("%v" , resp .ID )
5954 if idVal == "1" || idVal == "1.0" {
@@ -65,7 +60,7 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
6560 } else if sess , ok := resp .Result ["sessionId" ].(string ); ok {
6661 c .SessionID = sess
6762 }
68-
63+
6964 // Ensure HTTP transport picks it up for subsequent stateful loops
7065 if httpT , ok := c .transport .(* HTTPTransport ); ok {
7166 if httpT .SessionID == "" {
@@ -87,7 +82,6 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
8782
8883 // STEP 3: Initialized Notification
8984 notifyReq := []byte (`{"jsonrpc": "2.0", "method": "notifications/initialized"}` )
90- log .Printf ("[MCPClient] Sending Initialized Notification: %s" , string (notifyReq ))
9185 if err := c .transport .Send (notifyReq ); err != nil {
9286 // Just a notification broadcast, ignore send errors natively
9387 }
@@ -108,38 +102,32 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
108102 var toolsFound []MCPTool
109103
110104 for _ , v := range variants {
111- log .Printf ("[MCPClient] Sending Tools Request Variant (ID: %d): %s" , v .ID , v .Req )
112105 if err := c .transport .Send ([]byte (v .Req )); err != nil {
113- log .Printf ("[MCPClient] Sending Variant Failed: %v" , err )
114106 continue
115107 }
116-
117- // Attempt to receive for this fallback variant
108+
118109 var innerBreak bool
119110 var matchedVariant bool
120-
111+
121112 for j := 0 ; j < 5 ; j ++ {
122- receiveCtx , cancel := context .WithTimeout (ctx , 3 * time .Second ) // Force 3-second rapid discard
113+ receiveCtx , cancel := context .WithTimeout (ctx , 3 * time .Second )
123114 raw , err := c .transport .Receive (receiveCtx )
124115 cancel ()
125-
116+
126117 if err != nil {
127- log .Printf ("[MCPClient] Tools Request Receive Error: %v" , err )
128118 innerBreak = true
129119 break
130120 }
131- log .Printf ("[MCPClient] Tools Request Received Raw: %s" , string (raw ))
132-
121+
133122 resp , err := parseTolerantResponse (raw )
134123 if err != nil {
135- log .Printf ("[MCPClient] Tools Request Parse Error (Ignoring): %v" , err )
136124 continue
137125 }
138-
126+
139127 if resp .ID != nil {
140128 idVal := fmt .Sprintf ("%v" , resp .ID )
141129 expectedID := fmt .Sprintf ("%d" , v .ID )
142-
130+
143131 if idVal == expectedID || idVal == expectedID + ".0" {
144132 matchedVariant = true
145133 if len (resp .Result ) > 0 {
@@ -155,11 +143,11 @@ func (c *MCPClient) Discover(ctx context.Context) ([]MCPTool, error) {
155143 }
156144 }
157145 }
158-
146+
159147 if matchedVariant || innerBreak {
160148 continue
161149 }
162150 }
163-
151+
164152 return nil , fmt .Errorf ("failed to discover tools across all fallback variants" )
165153}
0 commit comments