Skip to content

Commit 4870c9a

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 176bcb3 commit 4870c9a

File tree

6 files changed

+428
-71
lines changed

6 files changed

+428
-71
lines changed

Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ tasks:
6363
test:
6464
aliases: [t]
6565
desc: Run tests
66-
cmd: DOCKER_AGENT_MODELS_GATEWAY= OPENAI_API_KEY= ANTHROPIC_API_KEY= GOOGLE_API_KEY= GOOGLE_GENAI_USE_VERTEXAI= MISTRAL_API_KEY= GITHUB_TOKEN= go test {{.CLI_ARGS}} ./...
66+
cmd: DOCKER_AGENT_MODELS_GATEWAY= OPENAI_API_KEY= ANTHROPIC_API_KEY= GOOGLE_API_KEY= GOOGLE_GENAI_USE_VERTEXAI= MISTRAL_API_KEY= go test {{.CLI_ARGS}} ./...
6767

6868
test-binary:
6969
deps: ["deploy-local"]
7070
desc: Run tests on build binary
71-
cmd: DOCKER_AGENT_MODELS_GATEWAY= OPENAI_API_KEY= ANTHROPIC_API_KEY= GOOGLE_API_KEY= GOOGLE_GENAI_USE_VERTEXAI= MISTRAL_API_KEY= GITHUB_TOKEN= go test -tags=binary_required {{.CLI_ARGS}} ./e2e/binary/...
71+
cmd: DOCKER_AGENT_MODELS_GATEWAY= OPENAI_API_KEY= ANTHROPIC_API_KEY= GOOGLE_API_KEY= GOOGLE_GENAI_USE_VERTEXAI= MISTRAL_API_KEY= go test -tags=binary_required {{.CLI_ARGS}} ./e2e/binary/...
7272

7373
build-local:
7474
desc: Build binaries for local host platform

examples/podcastgenerator_githubmodel.yaml

Lines changed: 0 additions & 67 deletions
This file was deleted.

pkg/teamloader/teamloader.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ func getFallbackModelsForAgent(ctx context.Context, cfg *latest.Config, a *lates
427427
// getToolsForAgent returns the tool definitions for an agent based on its configuration
428428
func getToolsForAgent(ctx context.Context, a *latest.AgentConfig, parentDir string, runConfig *config.RuntimeConfig, registry *ToolsetRegistry, configName string) ([]tools.ToolSet, []string) {
429429
var (
430-
toolSets []tools.ToolSet
431-
warnings []string
430+
toolSets []tools.ToolSet
431+
warnings []string
432+
lspBackends []builtin.LSPBackend
432433
)
433434

434435
deferredToolset := builtin.NewDeferredToolset()
@@ -460,9 +461,29 @@ func getToolsForAgent(ctx context.Context, a *latest.AgentConfig, parentDir stri
460461
}
461462
}
462463

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

479+
// Merge LSP backends: if there are multiple, combine them into a single
480+
// multiplexer so the LLM sees one set of lsp_* tools instead of duplicates.
481+
if len(lspBackends) > 1 {
482+
toolSets = append(toolSets, builtin.NewLSPMultiplexer(lspBackends))
483+
} else if len(lspBackends) == 1 {
484+
toolSets = append(toolSets, lspBackends[0].Toolset)
485+
}
486+
466487
if deferredToolset.HasSources() {
467488
toolSets = append(toolSets, deferredToolset)
468489
}

pkg/teamloader/teamloader_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,88 @@ 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+
Version: "golang/tools@v0.21.0",
399+
FileTypes: []string{".go"},
400+
},
401+
{
402+
Type: "lsp",
403+
Command: "gopls",
404+
Version: "golang/tools@v0.21.0",
405+
FileTypes: []string{".mod"},
406+
},
407+
},
408+
}
409+
410+
runConfig := config.RuntimeConfig{
411+
EnvProviderForTests: &noEnvProvider{},
412+
}
413+
414+
got, warnings := getToolsForAgent(t.Context(), a, ".", &runConfig, NewDefaultToolsetRegistry(), "test-config")
415+
require.Empty(t, warnings)
416+
417+
// Should have exactly one toolset (the multiplexer)
418+
require.Len(t, got, 1)
419+
420+
// Verify that we get no duplicate tool names
421+
allTools, err := got[0].Tools(t.Context())
422+
require.NoError(t, err)
423+
424+
seen := make(map[string]bool)
425+
for _, tool := range allTools {
426+
assert.False(t, seen[tool.Name], "duplicate tool name: %s", tool.Name)
427+
seen[tool.Name] = true
428+
}
429+
430+
// Verify LSP tools are present
431+
assert.True(t, seen["lsp_hover"])
432+
assert.True(t, seen["lsp_definition"])
433+
}
434+
435+
func TestGetToolsForAgent_SingleLSPToolsetNotWrapped(t *testing.T) {
436+
t.Parallel()
437+
438+
a := &latest.AgentConfig{
439+
Instruction: "test",
440+
Toolsets: []latest.Toolset{
441+
{
442+
Type: "lsp",
443+
Command: "gopls",
444+
Version: "golang/tools@v0.21.0",
445+
FileTypes: []string{".go"},
446+
},
447+
},
448+
}
449+
450+
runConfig := config.RuntimeConfig{
451+
EnvProviderForTests: &noEnvProvider{},
452+
}
453+
454+
got, warnings := getToolsForAgent(t.Context(), a, ".", &runConfig, NewDefaultToolsetRegistry(), "test-config")
455+
require.Empty(t, warnings)
456+
457+
// Should have exactly one toolset that provides LSP tools.
458+
require.Len(t, got, 1)
459+
460+
allTools, err := got[0].Tools(t.Context())
461+
require.NoError(t, err)
462+
463+
var names []string
464+
for _, tool := range allTools {
465+
names = append(names, tool.Name)
466+
}
467+
assert.Contains(t, names, "lsp_hover")
468+
assert.Contains(t, names, "lsp_definition")
469+
}
470+
389471
func TestExternalDepthContext(t *testing.T) {
390472
t.Parallel()
391473

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)