Skip to content

Commit 698dc5c

Browse files
committed
Refactor client structure to unify client types across the codebase
- Replaced instances of ManagedClient with Client in CLI and upstream components for consistency. - Updated client creation methods to reflect the new unified Client type. - Enhanced error handling and logging during client operations to improve traceability. - Streamlined client management in the Manager and CLIClient for better maintainability.
1 parent b8e1dbc commit 698dc5c

7 files changed

Lines changed: 118 additions & 166 deletions

File tree

cmd/mcpproxy/tools_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func runToolsList(_ *cobra.Command, _ []string) error {
9999
fmt.Printf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n")
100100

101101
// Create CLI client
102-
cliClient, err := cli.NewCLIClient(serverName, globalConfig, toolsLogLevel)
102+
cliClient, err := cli.NewClient(serverName, globalConfig, toolsLogLevel)
103103
if err != nil {
104104
return fmt.Errorf("failed to create CLI client: %w", err)
105105
}

internal/upstream/cli/client.go

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cli
33
import (
44
"context"
55
"fmt"
6-
"io"
7-
"os"
86
"time"
97

108
"mcpproxy-go/internal/config"
@@ -20,26 +18,18 @@ const (
2018
transportStdio = "stdio"
2119
)
2220

23-
// CLIClient provides a simple interface for CLI operations with enhanced debugging
24-
type CLIClient struct {
25-
managedClient *managed.ManagedClient
21+
// Client provides a simple interface for CLI operations with enhanced debugging
22+
type Client struct {
23+
managedClient *managed.Client
2624
logger *zap.Logger
2725
config *config.ServerConfig
2826

2927
// Debug output settings
30-
debugMode bool
31-
stderrMonitor *StderrMonitor
28+
debugMode bool
3229
}
3330

34-
// StderrMonitor captures and outputs stderr for stdio processes
35-
type StderrMonitor struct {
36-
reader io.Reader
37-
done chan struct{}
38-
logger *zap.Logger
39-
}
40-
41-
// NewCLIClient creates a new CLI client for debugging and simple operations
42-
func NewCLIClient(serverName string, globalConfig *config.Config, logLevel string) (*CLIClient, error) {
31+
// NewClient creates a new CLI client for debugging and simple operations
32+
func NewClient(serverName string, globalConfig *config.Config, logLevel string) (*Client, error) {
4333
// Find server config by name
4434
var serverConfig *config.ServerConfig
4535
for _, server := range globalConfig.Servers {
@@ -67,12 +57,12 @@ func NewCLIClient(serverName string, globalConfig *config.Config, logLevel strin
6757
}
6858

6959
// Create managed client (which includes Docker-specific optimizations)
70-
managedClient, err := managed.NewManagedClient(serverName, serverConfig, logger, logConfig, globalConfig)
60+
managedClient, err := managed.NewClient(serverName, serverConfig, logger, logConfig, globalConfig)
7161
if err != nil {
7262
return nil, fmt.Errorf("failed to create managed client: %w", err)
7363
}
7464

75-
return &CLIClient{
65+
return &Client{
7666
managedClient: managedClient,
7767
logger: logger,
7868
config: serverConfig,
@@ -81,7 +71,7 @@ func NewCLIClient(serverName string, globalConfig *config.Config, logLevel strin
8171
}
8272

8373
// Connect establishes connection with detailed progress output
84-
func (c *CLIClient) Connect(ctx context.Context) error {
74+
func (c *Client) Connect(ctx context.Context) error {
8575
c.logger.Info("🔗 Starting connection to upstream server",
8676
zap.String("server", c.config.Name),
8777
zap.String("transport", c.getTransportType()),
@@ -117,7 +107,7 @@ func (c *CLIClient) Connect(ctx context.Context) error {
117107
}
118108

119109
// ListTools executes tools/list with detailed output
120-
func (c *CLIClient) ListTools(ctx context.Context) ([]*config.ToolMetadata, error) {
110+
func (c *Client) ListTools(ctx context.Context) ([]*config.ToolMetadata, error) {
121111
c.logger.Info("🔍 Discovering tools from server...")
122112

123113
// Docker containers now use stateless connections with caching in core client
@@ -142,7 +132,7 @@ func (c *CLIClient) ListTools(ctx context.Context) ([]*config.ToolMetadata, erro
142132
}
143133

144134
// Disconnect closes the connection
145-
func (c *CLIClient) Disconnect() error {
135+
func (c *Client) Disconnect() error {
146136
c.logger.Info("🔌 Disconnecting from server...")
147137

148138
// Note: Stderr monitoring not used with ManagedClient
@@ -157,7 +147,7 @@ func (c *CLIClient) Disconnect() error {
157147
}
158148

159149
// displayServerInfo shows detailed server information
160-
func (c *CLIClient) displayServerInfo() {
150+
func (c *Client) displayServerInfo() {
161151
// Get server info from managed client's state
162152
connectionInfo := c.managedClient.StateManager.GetConnectionInfo()
163153
if connectionInfo.ServerName == "" {
@@ -180,7 +170,7 @@ func (c *CLIClient) displayServerInfo() {
180170
}
181171

182172
// displayTools shows the discovered tools in a formatted way
183-
func (c *CLIClient) displayTools(tools []*config.ToolMetadata) {
173+
func (c *Client) displayTools(tools []*config.ToolMetadata) {
184174
if len(tools) == 0 {
185175
c.logger.Warn("⚠️ No tools discovered from server")
186176
return
@@ -209,23 +199,8 @@ func (c *CLIClient) displayTools(tools []*config.ToolMetadata) {
209199
fmt.Printf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n")
210200
}
211201

212-
// startStderrMonitoring starts monitoring stderr output for stdio processes
213-
func (c *CLIClient) startStderrMonitoring() {
214-
// Note: Stderr monitoring not available with ManagedClient
215-
// ManagedClient wraps CoreClient and doesn't expose stderr directly
216-
return
217-
}
218-
219-
// stopStderrMonitoring stops stderr monitoring
220-
func (c *CLIClient) stopStderrMonitoring() {
221-
if c.stderrMonitor != nil {
222-
close(c.stderrMonitor.done)
223-
c.stderrMonitor = nil
224-
}
225-
}
226-
227202
// getTransportType returns the transport type for display
228-
func (c *CLIClient) getTransportType() string {
203+
func (c *Client) getTransportType() string {
229204
if c.config.Protocol != "" && c.config.Protocol != "auto" {
230205
return c.config.Protocol
231206
}
@@ -241,44 +216,8 @@ func (c *CLIClient) getTransportType() string {
241216
return transportStdio
242217
}
243218

244-
// Monitor stderr output for stdio processes
245-
func (m *StderrMonitor) monitor() {
246-
m.logger.Info("📡 Starting stderr monitoring...")
247-
248-
buffer := make([]byte, 4096)
249-
for {
250-
select {
251-
case <-m.done:
252-
m.logger.Info("🛑 Stderr monitoring stopped")
253-
return
254-
default:
255-
// Non-blocking read attempt
256-
if m.reader != nil {
257-
n, err := m.reader.Read(buffer)
258-
if err != nil {
259-
if err != io.EOF {
260-
m.logger.Debug("Stderr read error", zap.Error(err))
261-
}
262-
time.Sleep(100 * time.Millisecond)
263-
continue
264-
}
265-
266-
if n > 0 {
267-
output := string(buffer[:n])
268-
// Output stderr with clear marking
269-
fmt.Fprintf(os.Stderr, "🔴 STDERR: %s", output)
270-
271-
// Also log it
272-
m.logger.Debug("Stderr output captured", zap.String("content", output))
273-
}
274-
}
275-
time.Sleep(10 * time.Millisecond)
276-
}
277-
}
278-
}
279-
280219
// CallTool executes a tool (for future CLI extensions)
281-
func (c *CLIClient) CallTool(ctx context.Context, toolName string, args map[string]interface{}) (*mcp.CallToolResult, error) {
220+
func (c *Client) CallTool(ctx context.Context, toolName string, args map[string]interface{}) (*mcp.CallToolResult, error) {
282221
c.logger.Info("🛠️ Calling tool",
283222
zap.String("tool", toolName),
284223
zap.Any("args", args))
@@ -299,17 +238,17 @@ func (c *CLIClient) CallTool(ctx context.Context, toolName string, args map[stri
299238
}
300239

301240
// IsConnected returns connection status
302-
func (c *CLIClient) IsConnected() bool {
241+
func (c *Client) IsConnected() bool {
303242
return c.managedClient.IsConnected()
304243
}
305244

306245
// GetConnectionInfo returns connection information
307-
func (c *CLIClient) GetConnectionInfo() types.ConnectionInfo {
246+
func (c *Client) GetConnectionInfo() types.ConnectionInfo {
308247
return c.managedClient.StateManager.GetConnectionInfo()
309248
}
310249

311250
// GetServerInfo returns server information
312-
func (c *CLIClient) GetServerInfo() *mcp.InitializeResult {
251+
func (c *Client) GetServerInfo() *mcp.InitializeResult {
313252
// ManagedClient doesn't expose server info directly
314253
// Return nil for now as this isn't used in CLI operations
315254
return nil

internal/upstream/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestClient_Connect_SSE_NotSupported(t *testing.T) {
5959
require.NoError(t, err)
6060

6161
// Create client with all required parameters
62-
client, err := managed.NewManagedClient("test-client", cfg, logger, nil, nil)
62+
client, err := managed.NewClient("test-client", cfg, logger, nil, nil)
6363
require.NoError(t, err)
6464
require.NotNil(t, client)
6565

@@ -122,7 +122,7 @@ func TestClient_Connect_SSE_ErrorContainsAlternatives(t *testing.T) {
122122
logger, err := zap.NewDevelopment()
123123
require.NoError(t, err)
124124

125-
client, err := managed.NewManagedClient("test-client", cfg, logger, nil, nil)
125+
client, err := managed.NewClient("test-client", cfg, logger, nil, nil)
126126
require.NoError(t, err)
127127

128128
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
@@ -212,7 +212,7 @@ func TestClient_Connect_WorkingTransports(t *testing.T) {
212212
logger, err := zap.NewDevelopment()
213213
require.NoError(t, err)
214214

215-
client, err := managed.NewManagedClient("test-client", cfg, logger, nil, nil)
215+
client, err := managed.NewClient("test-client", cfg, logger, nil, nil)
216216
require.NoError(t, err)
217217

218218
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
@@ -303,7 +303,7 @@ func TestClient_Headers_Support(t *testing.T) {
303303
logger, err := zap.NewDevelopment()
304304
require.NoError(t, err)
305305

306-
client, err := managed.NewManagedClient("test-client", cfg, logger, nil, nil)
306+
client, err := managed.NewClient("test-client", cfg, logger, nil, nil)
307307
require.NoError(t, err)
308308
require.NotNil(t, client)
309309

0 commit comments

Comments
 (0)