Skip to content

Commit bcb2b8c

Browse files
committed
Enhance mcpproxy with tools command for debugging and server management
- Introduced `mcpproxy tools list` command to list tools from upstream servers with detailed logging options. - Refactored client architecture into modular components: CLI Client, Managed Client, and Core Client for better separation of concerns. - Updated documentation to include new CLI commands and examples for debugging server connections. - Enhanced logging capabilities for tool discovery and connection issues. - Updated dependencies in go.mod for improved functionality.
1 parent 42c107e commit bcb2b8c

19 files changed

Lines changed: 2091 additions & 1693 deletions

File tree

DESIGN.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ mcpproxy implements comprehensive per-upstream-server logging to facilitate debu
193193
- Main application log: `main.log`
194194
- Per-server logs: `server-{name}.log`
195195

196+
**Debug Commands:**
197+
- `mcpproxy tools list --server=NAME --log-level=trace`: Debug individual server connections
198+
- Enhanced trace logging shows all JSON-RPC frames and transport details
199+
196200
**Log Rotation:**
197201
- Automatic rotation based on file size (10MB default)
198202
- Configurable retention (5 backup files, 30 days default)
@@ -212,6 +216,136 @@ mcpproxy implements comprehensive per-upstream-server logging to facilitate debu
212216
* Auto‑update channel.
213217
* GUI front‑end built with Wails.
214218

219+
## 12 Client Architecture (Refactored)
220+
221+
### 12.1 Modular Client Design
222+
223+
The upstream client architecture has been refactored into three distinct layers for better separation of concerns, testability, and reusability:
224+
225+
```
226+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
227+
│ CLI Client │ │ Managed Client │ │ Core Client │
228+
│ │ │ │ │ │
229+
│ • CLI-specific │ ──▶│ • State mgmt │ ──▶│ • Basic MCP │
230+
│ • Debug output │ │ • Concurrency │ │ • Connection │
231+
│ • Tool display │ │ • Background │ │ • Auth fallback │
232+
│ • Stderr monitor│ │ recovery │ │ • No state │
233+
└─────────────────┘ └─────────────────┘ └─────────────────┘
234+
```
235+
236+
**Core Interfaces:**
237+
```go
238+
// MCPClient - Basic MCP operations
239+
type MCPClient interface {
240+
Connect(ctx context.Context) error
241+
Disconnect() error
242+
IsConnected() bool
243+
ListTools(ctx context.Context) ([]*config.ToolMetadata, error)
244+
CallTool(ctx context.Context, toolName string, args map[string]interface{}) (*mcp.CallToolResult, error)
245+
GetConnectionInfo() types.ConnectionInfo
246+
GetServerInfo() *mcp.InitializeResult
247+
}
248+
249+
// StatefulClient - Adds state management
250+
type StatefulClient interface {
251+
MCPClient
252+
GetState() types.ConnectionState
253+
IsConnecting() bool
254+
ShouldRetry() bool
255+
SetStateChangeCallback(callback func(oldState, newState types.ConnectionState, info *types.ConnectionInfo))
256+
}
257+
```
258+
259+
### 12.2 Core Client (`internal/upstream/core/`)
260+
261+
**Purpose:** Minimal, stateless MCP client implementation
262+
- **Responsibility:** Direct MCP protocol communication
263+
- **Features:**
264+
- Transport-agnostic (HTTP, SSE, stdio)
265+
- Authentication fallback (headers → no-auth → OAuth)
266+
- Environment variable filtering for stdio processes
267+
- No background processes or state management
268+
269+
**Key Components:**
270+
- `client.go`: Main client implementation
271+
- `auth.go`: Authentication strategies and fallback logic
272+
273+
### 12.3 Managed Client (`internal/upstream/managed/`)
274+
275+
**Purpose:** Stateful wrapper for daemon/long-running use
276+
- **Responsibility:** Production-ready client for `mcpproxy serve`
277+
- **Features:**
278+
- Connection state machine with retry logic
279+
- Background health monitoring and recovery
280+
- Concurrency control for ListTools operations
281+
- Exponential backoff for failed connections
282+
- State change notifications
283+
284+
**Key Features:**
285+
```go
286+
type ManagedClient struct {
287+
coreClient *core.CoreClient
288+
StateManager *types.StateManager
289+
// Concurrency control
290+
listToolsMu sync.Mutex
291+
// Background monitoring
292+
stopMonitoring chan struct{}
293+
}
294+
```
295+
296+
### 12.4 CLI Client (`internal/upstream/cli/`)
297+
298+
**Purpose:** Specialized client for CLI debugging operations
299+
- **Responsibility:** Enhanced debugging for `mcpproxy tools list`
300+
- **Features:**
301+
- Detailed output formatting with emojis
302+
- JSON-RPC frame logging at trace level
303+
- Stderr monitoring for stdio processes
304+
- Single-shot operations (connect → list → disconnect)
305+
306+
**Debug Output Features:**
307+
- **Transport Details:** All JSON-RPC request/response frames
308+
- **Stderr Capture:** Real-time stderr output from stdio processes
309+
- **Connection Events:** Detailed state transitions and timing
310+
- **Error Context:** Enhanced error messages with troubleshooting hints
311+
312+
### 12.5 Shared Types (`internal/upstream/types/`)
313+
314+
**Purpose:** Common data structures to break import cycles
315+
- **Connection States:** `Disconnected`, `Connecting`, `Authenticating`, `Discovering`, `Ready`, `Error`
316+
- **State Manager:** Handles state transitions, retry logic, and callbacks
317+
- **Connection Info:** Detailed connection metadata and error tracking
318+
319+
**State Machine:**
320+
```
321+
Disconnected ──▶ Connecting ──▶ Authenticating ──▶ Discovering ──▶ Ready
322+
▲ │ │ │ │
323+
└────────────────┴───────────────┴───────────────┴────────────┘
324+
Error
325+
```
326+
327+
### 12.6 Benefits of Refactored Architecture
328+
329+
1. **Separation of Concerns:**
330+
- Core: Pure MCP protocol implementation
331+
- Managed: Production state management
332+
- CLI: Debug-focused single operations
333+
334+
2. **Reusability:**
335+
- Core client shared between managed and CLI variants
336+
- State management logic isolated and testable
337+
- Transport logic decoupled from application logic
338+
339+
3. **Testability:**
340+
- Each layer can be unit tested independently
341+
- Mock interfaces for integration testing
342+
- Isolated state machine testing
343+
344+
4. **Maintainability:**
345+
- Clear responsibilities and boundaries
346+
- Smaller, focused code files
347+
- Type-safe interfaces between layers
348+
215349
## Upstream Server Management
216350

217351
### Dynamic Server Configuration

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,15 @@ Edit `mcp_config.json` (see below). Or **ask LLM** to add servers (see [doc](htt
121121
| `tools_limit` | Max tools returned to client | `15` |
122122
| `tool_response_limit` | Auto-truncate responses above N chars (`0` disables) | `20000` |
123123

124-
### CLI flags
124+
### CLI Commands
125125

126+
**Main Commands:**
127+
```bash
128+
mcpproxy serve # Start proxy server with system tray
129+
mcpproxy tools list --server=NAME # Debug tool discovery for specific server
130+
```
131+
132+
**Serve Command Flags:**
126133
```text
127134
mcpproxy serve --help
128135
-c, --config <file> path to mcp_config.json
@@ -136,6 +143,28 @@ mcpproxy serve --help
136143
--allow-server-remove allow removing servers (default true)
137144
```
138145

146+
**Tools Command Flags:**
147+
```text
148+
mcpproxy tools list --help
149+
-s, --server <name> upstream server name (required)
150+
-l, --log-level <level> trace|debug|info|warn|error (default: info)
151+
-t, --timeout <duration> connection timeout (default: 30s)
152+
-o, --output <format> output format: table|json|yaml (default: table)
153+
-c, --config <file> path to mcp_config.json
154+
```
155+
156+
**Debug Examples:**
157+
```bash
158+
# List tools with trace logging to see all JSON-RPC frames
159+
mcpproxy tools list --server=github-server --log-level=trace
160+
161+
# List tools with custom timeout for slow servers
162+
mcpproxy tools list --server=slow-server --timeout=60s
163+
164+
# Output tools in JSON format for scripting
165+
mcpproxy tools list --server=weather-api --output=json
166+
```
167+
139168
---
140169

141170
## OAuth Authentication Support

cmd/mcpproxy/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ func main() {
9090
// Add search-servers command
9191
searchCmd := createSearchServersCommand()
9292

93+
// Add tools command
94+
toolsCmd := GetToolsCommand()
95+
9396
// Add commands to root
9497
rootCmd.AddCommand(serverCmd)
9598
rootCmd.AddCommand(searchCmd)
99+
rootCmd.AddCommand(toolsCmd)
96100

97101
// Default to server command for backward compatibility
98102
rootCmd.RunE = runServer

0 commit comments

Comments
 (0)