on main help sign in
fix: fix mcp clients
428d71aKujtim Hoxha
internal/app/app.go+1
@@ -256,6 +256,7 @@ func (app *App) InitCoderAgent() error {
256 }
256 }
257 var err error
257 var err error
258 app.CoderAgent, err = agent.NewAgent(
258 app.CoderAgent, err = agent.NewAgent(
259+ app.globalCtx,
259 coderAgentCfg,
260 coderAgentCfg,
260 app.Permissions,
261 app.Permissions,
261 app.Sessions,
262 app.Sessions,
internal/llm/agent/agent.go+3−2
@@ -67,6 +67,7 @@ type agent struct {
67 agentCfg config.Agent
67 agentCfg config.Agent
68 sessions session.Service
68 sessions session.Service
69 messages message.Service
69 messages message.Service
70+ mcpTools []McpTool
70
71
71 tools *csync.LazySlice[tools.BaseTool]
72 tools *csync.LazySlice[tools.BaseTool]
72
73
@@ -86,6 +87,7 @@ var agentPromptMap = map[string]prompt.PromptID{
86}
87}
87
88
88func NewAgent(
89func NewAgent(
90+ ctx context.Context,
89 agentCfg config.Agent,
91 agentCfg config.Agent,
90 // These services are needed in the tools
92 // These services are needed in the tools
91 permissions permission.Service,
93 permissions permission.Service,
@@ -94,7 +96,6 @@ func NewAgent(
94 history history.Service,
96 history history.Service,
95 lspClients map[string]*lsp.Client,
97 lspClients map[string]*lsp.Client,
96) (Service, error) {
98) (Service, error) {
97− ctx := context.Background()
98 cfg := config.Get()
99 cfg := config.Get()
99
100
100 var agentTool tools.BaseTool
101 var agentTool tools.BaseTool
@@ -103,7 +104,7 @@ func NewAgent(
103 if taskAgentCfg.ID == "" {
104 if taskAgentCfg.ID == "" {
104 return nil, fmt.Errorf("task agent not found in config")
105 return nil, fmt.Errorf("task agent not found in config")
105 }
106 }
106− taskAgent, err := NewAgent(taskAgentCfg, permissions, sessions, messages, history, lspClients)
107+ taskAgent, err := NewAgent(ctx, taskAgentCfg, permissions, sessions, messages, history, lspClients)
107 if err != nil {
108 if err != nil {
108 return nil, fmt.Errorf("failed to create task agent: %w", err)
109 return nil, fmt.Errorf("failed to create task agent: %w", err)
109 }
110 }
internal/llm/agent/mcp-tools.go+10−41
@@ -20,9 +20,10 @@ import (
20 "github.com/mark3labs/mcp-go/mcp"
20 "github.com/mark3labs/mcp-go/mcp"
21)
21)
22
22
23−type mcpTool struct {
23+type McpTool struct {
24 mcpName string
24 mcpName string
25 tool mcp.Tool
25 tool mcp.Tool
26+ client MCPClient
26 mcpConfig config.MCPConfig
27 mcpConfig config.MCPConfig
27 permissions permission.Service
28 permissions permission.Service
28 workingDir string
29 workingDir string
@@ -38,11 +39,11 @@ type MCPClient interface {
38 Close() error
39 Close() error
39}
40}
40
41
41−func (b *mcpTool) Name() string {
42+func (b *McpTool) Name() string {
42 return fmt.Sprintf("mcp_%s_%s", b.mcpName, b.tool.Name)
43 return fmt.Sprintf("mcp_%s_%s", b.mcpName, b.tool.Name)
43}
44}
44
45
45−func (b *mcpTool) Info() tools.ToolInfo {
46+func (b *McpTool) Info() tools.ToolInfo {
46 required := b.tool.InputSchema.Required
47 required := b.tool.InputSchema.Required
47 if required == nil {
48 if required == nil {
48 required = make([]string, 0)
49 required = make([]string, 0)
@@ -56,7 +57,6 @@ func (b *mcpTool) Info() tools.ToolInfo {
56}
57}
57
58
58func runTool(ctx context.Context, c MCPClient, toolName string, input string) (tools.ToolResponse, error) {
59func runTool(ctx context.Context, c MCPClient, toolName string, input string) (tools.ToolResponse, error) {
59− defer c.Close()
60 initRequest := mcp.InitializeRequest{}
60 initRequest := mcp.InitializeRequest{}
61 initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
61 initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
62 initRequest.Params.ClientInfo = mcp.Implementation{
62 initRequest.Params.ClientInfo = mcp.Implementation{
@@ -93,7 +93,7 @@ func runTool(ctx context.Context, c MCPClient, toolName string, input string) (t
93 return tools.NewTextResponse(output), nil
93 return tools.NewTextResponse(output), nil
94}
94}
95
95
96−func (b *mcpTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolResponse, error) {
96+func (b *McpTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolResponse, error) {
97 sessionID, messageID := tools.GetContextValues(ctx)
97 sessionID, messageID := tools.GetContextValues(ctx)
98 if sessionID == "" || messageID == "" {
98 if sessionID == "" || messageID == "" {
99 return tools.ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
99 return tools.ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
@@ -114,43 +114,13 @@ func (b *mcpTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolRes
114 return tools.ToolResponse{}, permission.ErrorPermissionDenied
114 return tools.ToolResponse{}, permission.ErrorPermissionDenied
115 }
115 }
116
116
117− switch b.mcpConfig.Type {
118− case config.MCPStdio:
119− c, err := client.NewStdioMCPClient(
120− b.mcpConfig.Command,
121− b.mcpConfig.ResolvedEnv(),
122− b.mcpConfig.Args...,
123− )
124− if err != nil {
125− return tools.NewTextErrorResponse(err.Error()), nil
126− }
127− return runTool(ctx, c, b.tool.Name, params.Input)
128− case config.MCPHttp:
129− c, err := client.NewStreamableHttpClient(
130− b.mcpConfig.URL,
131− transport.WithHTTPHeaders(b.mcpConfig.ResolvedHeaders()),
132− )
133− if err != nil {
134− return tools.NewTextErrorResponse(err.Error()), nil
135− }
136− return runTool(ctx, c, b.tool.Name, params.Input)
137− case config.MCPSse:
138− c, err := client.NewSSEMCPClient(
139− b.mcpConfig.URL,
140− client.WithHeaders(b.mcpConfig.ResolvedHeaders()),
141− )
142− if err != nil {
143− return tools.NewTextErrorResponse(err.Error()), nil
144− }
145− return runTool(ctx, c, b.tool.Name, params.Input)
117+ return runTool(ctx, b.client, b.tool.Name, params.Input)
146− }
147−
148− return tools.NewTextErrorResponse("invalid mcp type"), nil
149}
118}
150
119
151−func NewMcpTool(name string, tool mcp.Tool, permissions permission.Service, mcpConfig config.MCPConfig, workingDir string) tools.BaseTool {
120+func NewMcpTool(name string, c MCPClient, tool mcp.Tool, permissions permission.Service, mcpConfig config.MCPConfig, workingDir string) tools.BaseTool {
152− return &mcpTool{
121+ return &McpTool{
153 mcpName: name,
122 mcpName: name,
123+ client: c,
154 tool: tool,
124 tool: tool,
155 mcpConfig: mcpConfig,
125 mcpConfig: mcpConfig,
156 permissions: permissions,
126 permissions: permissions,
@@ -179,9 +149,8 @@ func getTools(ctx context.Context, name string, m config.MCPConfig, permissions
179 return stdioTools
149 return stdioTools
180 }
150 }
181 for _, t := range tools.Tools {
151 for _, t := range tools.Tools {
182− stdioTools = append(stdioTools, NewMcpTool(name, t, permissions, m, workingDir))
152+ stdioTools = append(stdioTools, NewMcpTool(name, c, t, permissions, m, workingDir))
183 }
153 }
184− defer c.Close()
185 return stdioTools
154 return stdioTools
186}
155}
187
156