Skip to content

Commit d3f9759

Browse files
committed
refactor debug messages
1 parent 5d83df4 commit d3f9759

File tree

13 files changed

+1871
-1529
lines changed

13 files changed

+1871
-1529
lines changed

cmd/root.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/mark3labs/mcphost/internal/models"
1919
"github.com/mark3labs/mcphost/internal/session"
2020
"github.com/mark3labs/mcphost/internal/tokens"
21+
"github.com/mark3labs/mcphost/internal/tools"
2122
"github.com/mark3labs/mcphost/internal/ui"
2223
"github.com/spf13/cobra"
2324
"github.com/spf13/viper"
@@ -386,15 +387,23 @@ func runNormalMode(ctx context.Context) error {
386387
}
387388

388389
// Create the agent using the factory
389-
mcpAgent, err := agent.CreateAgent(ctx, &agent.AgentCreationOptions{
390-
ModelConfig: modelConfig,
390+
// Use a buffered debug logger to capture messages during initialization
391+
var bufferedLogger *tools.BufferedDebugLogger
392+
var debugLogger tools.DebugLogger
393+
if viper.GetBool("debug") {
394+
bufferedLogger = tools.NewBufferedDebugLogger(true)
395+
debugLogger = bufferedLogger
396+
}
397+
398+
mcpAgent, err := agent.CreateAgent(ctx, &agent.AgentCreationOptions{ModelConfig: modelConfig,
391399
MCPConfig: mcpConfig,
392400
SystemPrompt: systemPrompt,
393401
MaxSteps: viper.GetInt("max-steps"),
394402
StreamingEnabled: viper.GetBool("stream"),
395403
ShowSpinner: true,
396404
Quiet: quietFlag,
397405
SpinnerFunc: spinnerFunc,
406+
DebugLogger: debugLogger,
398407
})
399408
if err != nil {
400409
return fmt.Errorf("failed to create agent: %v", err)
@@ -441,6 +450,16 @@ func runNormalMode(ctx context.Context) error {
441450
return fmt.Errorf("failed to setup CLI: %v", err)
442451
}
443452

453+
// Display buffered debug messages if any
454+
if bufferedLogger != nil && cli != nil {
455+
messages := bufferedLogger.GetMessages()
456+
if len(messages) > 0 {
457+
// Combine all messages into a single debug output
458+
combinedMessage := strings.Join(messages, "\n ")
459+
cli.DisplayDebugMessage(combinedMessage)
460+
}
461+
}
462+
444463
// Display debug configuration if debug mode is enabled
445464
if !quietFlag && cli != nil && viper.GetBool("debug") {
446465
debugConfig := map[string]any{

cmd/script.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/mark3labs/mcphost/internal/config"
1616
"github.com/mark3labs/mcphost/internal/hooks"
1717
"github.com/mark3labs/mcphost/internal/models"
18+
"github.com/mark3labs/mcphost/internal/tools"
1819
"github.com/mark3labs/mcphost/internal/ui"
1920
"github.com/spf13/cobra"
2021
"github.com/spf13/viper"
@@ -596,6 +597,12 @@ func runScriptMode(ctx context.Context, mcpConfig *config.Config, prompt string,
596597
}
597598

598599
// Create the agent using the factory (scripts don't need spinners)
600+
// Use a simple debug logger for scripts
601+
var debugLogger tools.DebugLogger
602+
if finalDebug {
603+
debugLogger = tools.NewSimpleDebugLogger(true)
604+
}
605+
599606
mcpAgent, err := agent.CreateAgent(ctx, &agent.AgentCreationOptions{
600607
ModelConfig: modelConfig,
601608
MCPConfig: mcpConfig,
@@ -605,6 +612,7 @@ func runScriptMode(ctx context.Context, mcpConfig *config.Config, prompt string,
605612
ShowSpinner: false, // Scripts don't need spinners
606613
Quiet: quietFlag,
607614
SpinnerFunc: nil, // No spinner function needed
615+
DebugLogger: debugLogger,
608616
})
609617
if err != nil {
610618
return fmt.Errorf("failed to create agent: %v", err)

internal/agent/agent.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AgentConfig struct {
2323
SystemPrompt string
2424
MaxSteps int
2525
StreamingEnabled bool
26+
DebugLogger tools.DebugLogger // Optional debug logger
2627
}
2728

2829
// ToolCallHandler is a function type for handling tool calls as they happen
@@ -68,6 +69,11 @@ func NewAgent(ctx context.Context, config *AgentConfig) (*Agent, error) {
6869
// Set the model for sampling support
6970
toolManager.SetModel(providerResult.Model)
7071

72+
// Set the debug logger if provided
73+
if config.DebugLogger != nil {
74+
toolManager.SetDebugLogger(config.DebugLogger)
75+
}
76+
7177
if err := toolManager.LoadTools(ctx, config.MCPConfig); err != nil {
7278
return nil, fmt.Errorf("failed to load MCP tools: %v", err)
7379
}

internal/agent/factory.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/mark3labs/mcphost/internal/config"
99
"github.com/mark3labs/mcphost/internal/models"
10+
"github.com/mark3labs/mcphost/internal/tools"
1011
)
1112

1213
// SpinnerFunc is a function type for showing spinners during agent creation
@@ -19,9 +20,10 @@ type AgentCreationOptions struct {
1920
SystemPrompt string
2021
MaxSteps int
2122
StreamingEnabled bool
22-
ShowSpinner bool // For Ollama models
23-
Quiet bool // Skip spinner if quiet
24-
SpinnerFunc SpinnerFunc // Function to show spinner (provided by caller)
23+
ShowSpinner bool // For Ollama models
24+
Quiet bool // Skip spinner if quiet
25+
SpinnerFunc SpinnerFunc // Function to show spinner (provided by caller)
26+
DebugLogger tools.DebugLogger // Optional debug logger
2527
}
2628

2729
// CreateAgent creates an agent with optional spinner for Ollama models
@@ -32,6 +34,7 @@ func CreateAgent(ctx context.Context, opts *AgentCreationOptions) (*Agent, error
3234
SystemPrompt: opts.SystemPrompt,
3335
MaxSteps: opts.MaxSteps,
3436
StreamingEnabled: opts.StreamingEnabled,
37+
DebugLogger: opts.DebugLogger,
3538
}
3639

3740
var agent *Agent

0 commit comments

Comments
 (0)