|
2 | 2 | package client |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "encoding/json" |
6 | | - "fmt" |
7 | | - "net/http" |
8 | | - "time" |
9 | | - |
10 | | - "github.com/co-browser/agent-browser/internal/backend/models" |
11 | | - "github.com/co-browser/agent-browser/internal/log" // Import log |
| 5 | + "github.com/mark3labs/mcp-go/client" |
12 | 6 | ) |
13 | 7 |
|
14 | | -// Client defines the interface for fetching tool metadata from an MCP server. |
15 | | -type Client interface { |
16 | | - // FetchTools retrieves the list of tools from the given MCP server URL. |
17 | | - // It should return an error if the fetch fails or the response is invalid. |
18 | | - FetchTools(serverURL string) ([]models.FetchedTool, error) |
19 | | -} |
20 | | - |
21 | | -// httpClient implements the Client interface using standard net/http. |
22 | | -type httpClient struct { |
23 | | - client *http.Client |
24 | | - logger log.Logger // Inject logger |
25 | | -} |
26 | | - |
27 | | -// NewHTTPClient creates a new MCP client with a default HTTP timeout. |
28 | | -func NewHTTPClient(logger log.Logger) Client { // Accept logger |
29 | | - return &httpClient{ |
30 | | - client: &http.Client{ |
31 | | - Timeout: 15 * time.Second, // Sensible default timeout |
32 | | - }, |
33 | | - logger: logger, // Store logger |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -// FetchTools implements the Client interface. |
38 | | -// This assumes the MCP server exposes a simple GET endpoint (e.g., /tools) returning JSON. |
39 | | -// This might need significant adjustments based on the actual MCP protocol. |
40 | | -func (c *httpClient) FetchTools(serverURL string) ([]models.FetchedTool, error) { |
41 | | - // Assume a standard endpoint like /tools, trim trailing slash if present |
42 | | - baseURL := serverURL |
43 | | - if baseURL[len(baseURL)-1] == '/' { |
44 | | - baseURL = baseURL[:len(baseURL)-1] |
45 | | - } |
46 | | - targetURL := baseURL + "/tools" // TODO: Make this configurable or discoverable? |
47 | | - |
48 | | - c.logger.Debug().Str("url", targetURL).Msg("Fetching tools") // Use logger |
49 | | - req, err := http.NewRequest("GET", targetURL, nil) |
50 | | - if err != nil { |
51 | | - // Log error if desired, but return wrapped error regardless |
52 | | - return nil, fmt.Errorf("failed to create request for %s: %w", targetURL, err) |
53 | | - } |
54 | | - // TODO: Add any necessary headers (e.g., Accept: application/json) |
55 | | - req.Header.Set("Accept", "application/json") |
56 | | - |
57 | | - resp, err := c.client.Do(req) |
58 | | - if err != nil { |
59 | | - return nil, fmt.Errorf("failed to fetch tools from %s: %w", targetURL, err) |
60 | | - } |
61 | | - defer func() { |
62 | | - _ = resp.Body.Close() // Explicitly ignore Close error |
63 | | - }() |
64 | | - |
65 | | - if resp.StatusCode != http.StatusOK { |
66 | | - // TODO: Read response body for more error details? |
67 | | - return nil, fmt.Errorf("failed to fetch tools from %s: received status code %d", targetURL, resp.StatusCode) |
68 | | - } |
69 | | - |
70 | | - var fetchedTools []models.FetchedTool |
71 | | - if err := json.NewDecoder(resp.Body).Decode(&fetchedTools); err != nil { |
72 | | - return nil, fmt.Errorf("failed to decode tools response from %s: %w", targetURL, err) |
73 | | - } |
| 8 | +// Re-export the SSEMCPClient from mark3labs/mcp-go/client |
| 9 | +type SSEMCPClient = client.SSEMCPClient |
74 | 10 |
|
75 | | - c.logger.Info().Int("count", len(fetchedTools)).Str("url", targetURL).Msg("Successfully fetched tools") // Use logger |
76 | | - return fetchedTools, nil |
77 | | -} |
| 11 | +// NewSSEMCPClient creates a new SSE MCP client |
| 12 | +var NewSSEMCPClient = client.NewSSEMCPClient |
0 commit comments