Skip to content

Commit aa55338

Browse files
committed
Add LSP multiplexer to support multiple LSP toolsets
When multiple LSP toolsets are configured (e.g., gopls for Go and pyright for Python), each produces tools with identical names (lsp_hover, lsp_definition, etc.), causing LLM APIs to reject the request because tool names must be unique. Add an LSPMultiplexer that combines multiple LSP backends into a single toolset. File-based tools (hover, definition, references, etc.) are routed to the correct backend by matching the file extension. Non-file tools (workspace, workspace_symbols) are broadcast to all backends and results are merged. The teamloader detects multiple LSP toolsets during agent loading and automatically wraps them in a multiplexer. Per-toolset config (tool filters, instructions, toon) is preserved through wrapping. Fixes #1969 Assisted-By: cagent
1 parent 3791869 commit aa55338

File tree

4 files changed

+423
-2
lines changed

4 files changed

+423
-2
lines changed

pkg/teamloader/teamloader.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,9 @@ func getFallbackModelsForAgent(ctx context.Context, cfg *latest.Config, a *lates
423423
// getToolsForAgent returns the tool definitions for an agent based on its configuration
424424
func getToolsForAgent(ctx context.Context, a *latest.AgentConfig, parentDir string, runConfig *config.RuntimeConfig, registry *ToolsetRegistry) ([]tools.ToolSet, []string) {
425425
var (
426-
toolSets []tools.ToolSet
427-
warnings []string
426+
toolSets []tools.ToolSet
427+
warnings []string
428+
lspBackends []builtin.LSPBackend
428429
)
429430

430431
deferredToolset := builtin.NewDeferredToolset()
@@ -456,9 +457,29 @@ func getToolsForAgent(ctx context.Context, a *latest.AgentConfig, parentDir stri
456457
}
457458
}
458459

460+
// Collect LSP backends for multiplexing when there are multiple.
461+
// Instead of adding them individually (which causes duplicate tool names),
462+
// they are combined into a single LSPMultiplexer after the loop.
463+
if toolset.Type == "lsp" {
464+
if lspTool, ok := tool.(*builtin.LSPTool); ok {
465+
lspBackends = append(lspBackends, builtin.LSPBackend{LSP: lspTool, Toolset: wrapped})
466+
continue
467+
}
468+
slog.Warn("Toolset configured as type 'lsp' but registry returned unexpected type; treating as regular toolset",
469+
"type", fmt.Sprintf("%T", tool), "command", toolset.Command)
470+
}
471+
459472
toolSets = append(toolSets, wrapped)
460473
}
461474

475+
// Merge LSP backends: if there are multiple, combine them into a single
476+
// multiplexer so the LLM sees one set of lsp_* tools instead of duplicates.
477+
if len(lspBackends) > 1 {
478+
toolSets = append(toolSets, builtin.NewLSPMultiplexer(lspBackends))
479+
} else if len(lspBackends) == 1 {
480+
toolSets = append(toolSets, lspBackends[0].Toolset)
481+
}
482+
462483
if deferredToolset.HasSources() {
463484
toolSets = append(toolSets, deferredToolset)
464485
}

pkg/teamloader/teamloader_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,85 @@ agents:
386386
assert.Equal(t, expected, rootAgent.AddPromptFiles())
387387
}
388388

389+
func TestGetToolsForAgent_MultipleLSPToolsetsAreCombined(t *testing.T) {
390+
t.Parallel()
391+
392+
a := &latest.AgentConfig{
393+
Instruction: "test",
394+
Toolsets: []latest.Toolset{
395+
{
396+
Type: "lsp",
397+
Command: "gopls",
398+
FileTypes: []string{".go"},
399+
},
400+
{
401+
Type: "lsp",
402+
Command: "gopls",
403+
FileTypes: []string{".mod"},
404+
},
405+
},
406+
}
407+
408+
runConfig := config.RuntimeConfig{
409+
EnvProviderForTests: &noEnvProvider{},
410+
}
411+
412+
got, warnings := getToolsForAgent(t.Context(), a, ".", &runConfig, NewDefaultToolsetRegistry())
413+
require.Empty(t, warnings)
414+
415+
// Should have exactly one toolset (the multiplexer)
416+
require.Len(t, got, 1)
417+
418+
// Verify that we get no duplicate tool names
419+
allTools, err := got[0].Tools(t.Context())
420+
require.NoError(t, err)
421+
422+
seen := make(map[string]bool)
423+
for _, tool := range allTools {
424+
assert.False(t, seen[tool.Name], "duplicate tool name: %s", tool.Name)
425+
seen[tool.Name] = true
426+
}
427+
428+
// Verify LSP tools are present
429+
assert.True(t, seen["lsp_hover"])
430+
assert.True(t, seen["lsp_definition"])
431+
}
432+
433+
func TestGetToolsForAgent_SingleLSPToolsetNotWrapped(t *testing.T) {
434+
t.Parallel()
435+
436+
a := &latest.AgentConfig{
437+
Instruction: "test",
438+
Toolsets: []latest.Toolset{
439+
{
440+
Type: "lsp",
441+
Command: "gopls",
442+
FileTypes: []string{".go"},
443+
},
444+
},
445+
}
446+
447+
runConfig := config.RuntimeConfig{
448+
EnvProviderForTests: &noEnvProvider{},
449+
}
450+
451+
got, warnings := getToolsForAgent(t.Context(), a, ".", &runConfig, NewDefaultToolsetRegistry())
452+
require.Empty(t, warnings)
453+
454+
// Should have exactly one toolset that provides LSP tools.
455+
require.Len(t, got, 1)
456+
457+
allTools, err := got[0].Tools(t.Context())
458+
require.NoError(t, err)
459+
460+
var names []string
461+
for _, tool := range allTools {
462+
names = append(names, tool.Name)
463+
}
464+
assert.Contains(t, names, "lsp_hover")
465+
assert.Contains(t, names, "lsp_definition")
466+
}
467+
389468
func TestExternalDepthContext(t *testing.T) {
390469
t.Parallel()
391470

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package builtin
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"strings"
9+
10+
"github.com/docker/cagent/pkg/tools"
11+
)
12+
13+
// LSPMultiplexer combines multiple LSP backends into a single toolset.
14+
// It presents one set of lsp_* tools and routes each call to the appropriate
15+
// backend based on the file extension in the tool arguments.
16+
type LSPMultiplexer struct {
17+
backends []LSPBackend
18+
}
19+
20+
// LSPBackend pairs a raw LSPTool (used for file-type routing) with an
21+
// optionally-wrapped ToolSet (used for tool enumeration, so that per-toolset
22+
// config like tool filters, instructions, or toon wrappers are respected).
23+
type LSPBackend struct {
24+
LSP *LSPTool
25+
Toolset tools.ToolSet
26+
}
27+
28+
// lspRouteTarget pairs a backend with the tool handler it produced for a given tool name.
29+
type lspRouteTarget struct {
30+
lsp *LSPTool
31+
handler tools.ToolHandler
32+
}
33+
34+
// Verify interface compliance.
35+
var (
36+
_ tools.ToolSet = (*LSPMultiplexer)(nil)
37+
_ tools.Startable = (*LSPMultiplexer)(nil)
38+
_ tools.Instructable = (*LSPMultiplexer)(nil)
39+
)
40+
41+
// NewLSPMultiplexer creates a multiplexer that routes LSP tool calls
42+
// to the appropriate backend based on file type.
43+
func NewLSPMultiplexer(backends []LSPBackend) *LSPMultiplexer {
44+
return &LSPMultiplexer{backends: append([]LSPBackend{}, backends...)}
45+
}
46+
47+
func (m *LSPMultiplexer) Start(ctx context.Context) error {
48+
var started int
49+
for _, b := range m.backends {
50+
if err := b.LSP.Start(ctx); err != nil {
51+
// Clean up previously started backends to avoid resource leaks.
52+
for _, s := range m.backends[:started] {
53+
_ = s.LSP.Stop(ctx)
54+
}
55+
return fmt.Errorf("starting LSP backend %q: %w", b.LSP.handler.command, err)
56+
}
57+
started++
58+
}
59+
return nil
60+
}
61+
62+
func (m *LSPMultiplexer) Stop(ctx context.Context) error {
63+
var errs []error
64+
for _, b := range m.backends {
65+
if err := b.LSP.Stop(ctx); err != nil {
66+
errs = append(errs, fmt.Errorf("stopping LSP backend %q: %w", b.LSP.handler.command, err))
67+
}
68+
}
69+
return errors.Join(errs...)
70+
}
71+
72+
func (m *LSPMultiplexer) Instructions() string {
73+
// Combine instructions from all backends, deduplicating identical ones.
74+
// Typically they share the same base LSP instructions, but individual
75+
// toolsets may override them via the Instruction config field.
76+
var parts []string
77+
seen := make(map[string]bool)
78+
for _, b := range m.backends {
79+
instr := tools.GetInstructions(b.Toolset)
80+
if instr != "" && !seen[instr] {
81+
seen[instr] = true
82+
parts = append(parts, instr)
83+
}
84+
}
85+
return strings.Join(parts, "\n\n")
86+
}
87+
88+
func (m *LSPMultiplexer) Tools(ctx context.Context) ([]tools.Tool, error) {
89+
// Collect each backend's tools keyed by name. We build the union of all
90+
// tool names (not just the first backend's) so that per-backend tool
91+
// filters don't accidentally hide tools that other backends expose.
92+
handlersByName := make(map[string][]lspRouteTarget)
93+
seenTools := make(map[string]tools.Tool) // first definition wins (for schema/description)
94+
var toolOrder []string // preserve insertion order
95+
for _, b := range m.backends {
96+
bTools, err := b.Toolset.Tools(ctx)
97+
if err != nil {
98+
return nil, fmt.Errorf("getting tools from LSP backend %q: %w", b.LSP.handler.command, err)
99+
}
100+
for _, t := range bTools {
101+
handlersByName[t.Name] = append(handlersByName[t.Name], lspRouteTarget{b.LSP, t.Handler})
102+
if _, exists := seenTools[t.Name]; !exists {
103+
seenTools[t.Name] = t
104+
toolOrder = append(toolOrder, t.Name)
105+
}
106+
}
107+
}
108+
109+
result := make([]tools.Tool, 0, len(toolOrder))
110+
for _, name := range toolOrder {
111+
t := seenTools[name]
112+
handlers := handlersByName[name]
113+
if name == ToolNameLSPWorkspace || name == ToolNameLSPWorkspaceSymbols {
114+
t.Handler = broadcastLSP(handlers)
115+
} else {
116+
t.Handler = routeByFile(handlers)
117+
}
118+
result = append(result, t)
119+
}
120+
return result, nil
121+
}
122+
123+
// routeByFile returns a handler that extracts the "file" field from the JSON
124+
// arguments and dispatches to the backend whose file-type filter matches.
125+
func routeByFile(handlers []lspRouteTarget) tools.ToolHandler {
126+
return func(ctx context.Context, tc tools.ToolCall) (*tools.ToolCallResult, error) {
127+
var args struct {
128+
File string `json:"file"`
129+
}
130+
if err := json.Unmarshal([]byte(tc.Function.Arguments), &args); err != nil {
131+
return tools.ResultError(fmt.Sprintf("failed to parse file argument: %s", err)), nil
132+
}
133+
if args.File == "" {
134+
return tools.ResultError("file argument is required"), nil
135+
}
136+
for _, h := range handlers {
137+
if h.lsp.HandlesFile(args.File) {
138+
return h.handler(ctx, tc)
139+
}
140+
}
141+
return tools.ResultError(fmt.Sprintf("no LSP server configured for file: %s", args.File)), nil
142+
}
143+
}
144+
145+
// broadcastLSP returns a handler that calls every backend best-effort and
146+
// merges the outputs. Individual backend failures are collected rather than
147+
// aborting the entire operation.
148+
func broadcastLSP(handlers []lspRouteTarget) tools.ToolHandler {
149+
return func(ctx context.Context, tc tools.ToolCall) (*tools.ToolCallResult, error) {
150+
var sections []string
151+
var errs []error
152+
for _, h := range handlers {
153+
result, err := h.handler(ctx, tc)
154+
if err != nil {
155+
errs = append(errs, fmt.Errorf("backend %s: %w", h.lsp.handler.command, err))
156+
continue
157+
}
158+
if result.IsError {
159+
sections = append(sections, fmt.Sprintf("[LSP %s] Error: %s", h.lsp.handler.command, result.Output))
160+
} else if result.Output != "" {
161+
sections = append(sections, result.Output)
162+
}
163+
}
164+
if len(sections) == 0 && len(errs) > 0 {
165+
return nil, errors.Join(errs...)
166+
}
167+
if len(sections) == 0 {
168+
return tools.ResultSuccess("No results"), nil
169+
}
170+
return tools.ResultSuccess(strings.Join(sections, "\n---\n")), nil
171+
}
172+
}

0 commit comments

Comments
 (0)