@@ -20,9 +20,10 @@ import (
2020 "github.com/mark3labs/mcp-go/mcp"
2121)
2222
23−type mcpTool struct {
23+type McpTool struct {
2424 mcpName string
2525 tool mcp.Tool
26+ client MCPClient
2627 mcpConfig config.MCPConfig
2728 permissions permission.Service
2829 workingDir string
@@ -38,11 +39,11 @@ type MCPClient interface {
3839 Close() error
3940}
4041
41−func (b *mcpTool) Name() string {
42+func (b *McpTool) Name() string {
4243 return fmt.Sprintf("mcp_%s_%s", b.mcpName, b.tool.Name)
4344}
4445
45−func (b *mcpTool) Info() tools.ToolInfo {
46+func (b *McpTool) Info() tools.ToolInfo {
4647 required := b.tool.InputSchema.Required
4748 if required == nil {
4849 required = make([]string, 0)
@@ -56,7 +57,6 @@ func (b *mcpTool) Info() tools.ToolInfo {
5657}
5758
5859func runTool(ctx context.Context, c MCPClient, toolName string, input string) (tools.ToolResponse, error) {
59− defer c.Close()
6060 initRequest := mcp.InitializeRequest{}
6161 initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
6262 initRequest.Params.ClientInfo = mcp.Implementation{
@@ -93,7 +93,7 @@ func runTool(ctx context.Context, c MCPClient, toolName string, input string) (t
9393 return tools.NewTextResponse(output), nil
9494}
9595
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) {
9797 sessionID, messageID := tools.GetContextValues(ctx)
9898 if sessionID == "" || messageID == "" {
9999 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
114114 return tools.ToolResponse{}, permission.ErrorPermissionDenied
115115 }
116116
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)
146− }
147−
148− return tools.NewTextErrorResponse("invalid mcp type"), nil
117+ return runTool(ctx, b.client, b.tool.Name, params.Input)
149118}
150119
151−func NewMcpTool(name string, tool mcp.Tool, permissions permission.Service, mcpConfig config.MCPConfig, workingDir string) tools.BaseTool {
152− return &mcpTool{
120+func NewMcpTool(name string, c MCPClient, tool mcp.Tool, permissions permission.Service, mcpConfig config.MCPConfig, workingDir string) tools.BaseTool {
121+ return &McpTool{
153122 mcpName: name,
123+ client: c,
154124 tool: tool,
155125 mcpConfig: mcpConfig,
156126 permissions: permissions,
@@ -179,9 +149,8 @@ func getTools(ctx context.Context, name string, m config.MCPConfig, permissions
179149 return stdioTools
180150 }
181151 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))
183153 }
184− defer c.Close()
185154 return stdioTools
186155}
187156