on main help sign in
fix(lsp): improve lsp tools (#2089)
4a03cbaCarlos Alexandro Becker
internal/agent/coordinator.go+1−1
@@ -435,7 +435,7 @@ func (c *coordinator) buildTools(ctx context.Context, agent config.Agent) ([]fan
435 tools.NewWriteTool(c.lspClients, c.permissions, c.history, c.filetracker, c.cfg.WorkingDir()),
435 tools.NewWriteTool(c.lspClients, c.permissions, c.history, c.filetracker, c.cfg.WorkingDir()),
436 )
436 )
437
437
438− if len(c.cfg.LSP) > 0 {
438+ if c.lspClients.Len() > 0 {
439 allTools = append(allTools, tools.NewDiagnosticsTool(c.lspClients), tools.NewReferencesTool(c.lspClients), tools.NewLSPRestartTool(c.lspClients))
439 allTools = append(allTools, tools.NewDiagnosticsTool(c.lspClients), tools.NewReferencesTool(c.lspClients), tools.NewLSPRestartTool(c.lspClients))
440 }
440 }
441
441
internal/agent/tools/mcp/init.go+1
@@ -135,6 +135,7 @@ func Close() error {
135
135
136// Initialize initializes MCP clients based on the provided configuration.
136// Initialize initializes MCP clients based on the provided configuration.
137func Initialize(ctx context.Context, permissions permission.Service, cfg *config.Config) {
137func Initialize(ctx context.Context, permissions permission.Service, cfg *config.Config) {
138+ slog.Info("Initializing MCP clients")
138 var wg sync.WaitGroup
139 var wg sync.WaitGroup
139 // Initialize states for all configured MCPs
140 // Initialize states for all configured MCPs
140 for name, m := range cfg.MCP {
141 for name, m := range cfg.MCP {
internal/app/app.go+1−4
@@ -109,10 +109,7 @@ func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
109 // Check for updates in the background.
109 // Check for updates in the background.
110 go app.checkForUpdates(ctx)
110 go app.checkForUpdates(ctx)
111
111
112− go func() {
113− slog.Info("Initializing MCP clients")
114− mcp.Initialize(ctx, app.Permissions, cfg)
112+ go mcp.Initialize(ctx, app.Permissions, cfg)
115− }()
116
113
117 // cleanup database upon app shutdown
114 // cleanup database upon app shutdown
118 app.cleanupFuncs = append(app.cleanupFuncs, conn.Close, mcp.Close)
115 app.cleanupFuncs = append(app.cleanupFuncs, conn.Close, mcp.Close)
internal/app/lsp.go+15−5
@@ -5,6 +5,7 @@ import (
5 "log/slog"
5 "log/slog"
6 "os/exec"
6 "os/exec"
7 "slices"
7 "slices"
8+ "sync"
8 "time"
9 "time"
9
10
10 "github.com/charmbracelet/crush/internal/config"
11 "github.com/charmbracelet/crush/internal/config"
@@ -58,16 +59,25 @@ func (app *App) initLSPClients(ctx context.Context) {
58 updateLSPState(name, lsp.StateDisabled, nil, nil, 0)
59 updateLSPState(name, lsp.StateDisabled, nil, nil, 0)
59 }
60 }
60 }
61 }
62+
63+ var wg sync.WaitGroup
61 for name, server := range filtered {
64 for name, server := range filtered {
62 if app.config.Options.AutoLSP != nil && !*app.config.Options.AutoLSP && !slices.Contains(userConfiguredLSPs, name) {
65 if app.config.Options.AutoLSP != nil && !*app.config.Options.AutoLSP && !slices.Contains(userConfiguredLSPs, name) {
63 slog.Debug("Ignoring non user-define LSP client due to AutoLSP being disabled", "name", name)
66 slog.Debug("Ignoring non user-define LSP client due to AutoLSP being disabled", "name", name)
64 continue
67 continue
65 }
68 }
69+ wg.Go(func() {
66− go app.createAndStartLSPClient(
70+ app.createAndStartLSPClient(
67− ctx, name,
71+ ctx, name,
68− toOurConfig(server),
72+ toOurConfig(server),
69− slices.Contains(userConfiguredLSPs, name),
73+ slices.Contains(userConfiguredLSPs, name),
70− )
74+ )
75+ })
76+ }
77+ wg.Wait()
78+
79+ if err := app.AgentCoordinator.UpdateModels(ctx); err != nil {
80+ slog.Error("Failed to refresh tools after LSP startup", "error", err)
71 }
81 }
72}
82}
73
83