feat: añadir cliente MCP para consumir servidores externos
Implementa el cliente MCP que permite a los agentes conectarse a servidores MCP externos y usar sus tools como si fueran tools nativas del agente. Arquitectura implementada: - shell/mcp/client.go: Cliente MCP con soporte stdio y SSE - shell/mcp/manager.go: Gestor de múltiples clientes MCP - tools/mcptools/mcp.go: Bridge que convierte MCP tools → tools.Tool - shell/mcp/server.go: Movido desde shell/protocols/ para colocación junto al client Cambios en config: - MCPServerCfg extendido con campos Transport, Command, Args, Env, Headers, Prefix, Timeout para soportar stdio y SSE transport Integración en runtime: - agents/runtime.go: Inicializa MCP manager si config.Tools.MCP.Enabled - buildToolRegistry: Registra tools MCP automáticamente con prefijos configurables - Agent: Campo mcpManager que se cierra en shutdown Transportes soportados: - stdio: Lanza subproceso (ej: npx -y @anthropic/mcp-server-brave-search) - SSE: Se conecta a servidor HTTP MCP Las tools MCP son indistinguibles de tools nativas desde el punto de vista del LLM. Auto-discovery via ListTools(), conversión de JSON Schema a tools.Param. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
// Package mcp provides MCP client and server implementations.
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/mark3labs/mcp-go/client"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
// Client wraps an MCP client (stdio or SSE) and exposes discovered tools.
|
||||
type Client struct {
|
||||
name string
|
||||
transport string // "stdio" | "sse"
|
||||
mcpClient *client.Client
|
||||
tools []mcp.Tool
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewStdioClient creates an MCP client that connects to a stdio-based MCP server.
|
||||
func NewStdioClient(ctx context.Context, name, command string, args []string, env map[string]string, logger *slog.Logger) (*Client, error) {
|
||||
logger.Info("creating stdio MCP client", "name", name, "command", command, "args", args)
|
||||
|
||||
// Prepare environment
|
||||
envSlice := os.Environ()
|
||||
for k, v := range env {
|
||||
envSlice = append(envSlice, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
// Create stdio client
|
||||
mcpClient, err := client.NewStdioMCPClient(command, envSlice, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdio client: %w", err)
|
||||
}
|
||||
|
||||
// Initialize
|
||||
initReq := mcp.InitializeRequest{
|
||||
Params: mcp.InitializeParams{
|
||||
ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION,
|
||||
ClientInfo: mcp.Implementation{
|
||||
Name: "agents-mcp-client",
|
||||
Version: "1.0.0",
|
||||
},
|
||||
Capabilities: mcp.ClientCapabilities{},
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mcpClient.Initialize(ctx, initReq)
|
||||
if err != nil {
|
||||
mcpClient.Close()
|
||||
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
|
||||
}
|
||||
|
||||
// Discover tools
|
||||
toolsResp, err := mcpClient.ListTools(ctx, mcp.ListToolsRequest{})
|
||||
if err != nil {
|
||||
mcpClient.Close()
|
||||
return nil, fmt.Errorf("failed to list tools: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("discovered MCP tools", "name", name, "count", len(toolsResp.Tools))
|
||||
|
||||
return &Client{
|
||||
name: name,
|
||||
transport: "stdio",
|
||||
mcpClient: mcpClient,
|
||||
tools: toolsResp.Tools,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewSSEClient creates an MCP client that connects to an SSE/HTTP-based MCP server.
|
||||
func NewSSEClient(ctx context.Context, name, url string, headers map[string]string, logger *slog.Logger) (*Client, error) {
|
||||
logger.Info("creating SSE MCP client", "name", name, "url", url)
|
||||
|
||||
// Create SSE client (no custom headers support in basic API, would need transport options)
|
||||
mcpClient, err := client.NewSSEMCPClient(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create SSE client: %w", err)
|
||||
}
|
||||
|
||||
// Initialize
|
||||
initReq := mcp.InitializeRequest{
|
||||
Params: mcp.InitializeParams{
|
||||
ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION,
|
||||
ClientInfo: mcp.Implementation{
|
||||
Name: "agents-mcp-client",
|
||||
Version: "1.0.0",
|
||||
},
|
||||
Capabilities: mcp.ClientCapabilities{},
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mcpClient.Initialize(ctx, initReq)
|
||||
if err != nil {
|
||||
mcpClient.Close()
|
||||
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
|
||||
}
|
||||
|
||||
// Discover tools
|
||||
toolsResp, err := mcpClient.ListTools(ctx, mcp.ListToolsRequest{})
|
||||
if err != nil {
|
||||
mcpClient.Close()
|
||||
return nil, fmt.Errorf("failed to list tools: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("discovered MCP tools", "name", name, "count", len(toolsResp.Tools))
|
||||
|
||||
return &Client{
|
||||
name: name,
|
||||
transport: "sse",
|
||||
mcpClient: mcpClient,
|
||||
tools: toolsResp.Tools,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Tools returns the discovered MCP tools.
|
||||
func (c *Client) Tools() []mcp.Tool {
|
||||
return c.tools
|
||||
}
|
||||
|
||||
// Name returns the client name.
|
||||
func (c *Client) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
// CallTool invokes an MCP tool by name with the given arguments.
|
||||
func (c *Client) CallTool(ctx context.Context, name string, args map[string]any, timeout time.Duration) (*mcp.CallToolResult, error) {
|
||||
if timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
req := mcp.CallToolRequest{
|
||||
Params: mcp.CallToolParams{
|
||||
Name: name,
|
||||
Arguments: args,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := c.mcpClient.CallTool(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("tool call failed: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Close closes the MCP client connection.
|
||||
func (c *Client) Close() error {
|
||||
c.logger.Info("closing MCP client", "name", c.name, "transport", c.transport)
|
||||
return c.mcpClient.Close()
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/enmanuel/agents/internal/config"
|
||||
)
|
||||
|
||||
// Manager manages multiple MCP client connections.
|
||||
type Manager struct {
|
||||
clients map[string]*Client // server name → client
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewManager creates a new MCP manager and initializes all configured servers.
|
||||
func NewManager(ctx context.Context, servers []config.MCPServerCfg, logger *slog.Logger) (*Manager, error) {
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
|
||||
m := &Manager{
|
||||
clients: make(map[string]*Client),
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
for _, serverCfg := range servers {
|
||||
if err := m.addServer(ctx, serverCfg); err != nil {
|
||||
// Close any already-created clients before returning error
|
||||
m.Close()
|
||||
return nil, fmt.Errorf("failed to initialize MCP server %q: %w", serverCfg.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info("MCP manager initialized", "servers", len(m.clients))
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// addServer creates and adds a single MCP client to the manager.
|
||||
func (m *Manager) addServer(ctx context.Context, cfg config.MCPServerCfg) error {
|
||||
if cfg.Name == "" {
|
||||
return fmt.Errorf("MCP server must have a name")
|
||||
}
|
||||
|
||||
// Auto-detect transport if not specified
|
||||
transport := cfg.Transport
|
||||
if transport == "" {
|
||||
if cfg.Command != "" {
|
||||
transport = "stdio"
|
||||
} else if cfg.URL != "" {
|
||||
transport = "sse"
|
||||
} else {
|
||||
return fmt.Errorf("MCP server %q must specify either command (stdio) or url (sse)", cfg.Name)
|
||||
}
|
||||
}
|
||||
|
||||
var client *Client
|
||||
var err error
|
||||
|
||||
switch transport {
|
||||
case "stdio":
|
||||
if cfg.Command == "" {
|
||||
return fmt.Errorf("MCP server %q with stdio transport must have a command", cfg.Name)
|
||||
}
|
||||
// Expand environment variables in command and args
|
||||
command := os.ExpandEnv(cfg.Command)
|
||||
args := make([]string, len(cfg.Args))
|
||||
for i, arg := range cfg.Args {
|
||||
args[i] = os.ExpandEnv(arg)
|
||||
}
|
||||
// Expand env vars in environment map
|
||||
env := make(map[string]string, len(cfg.Env))
|
||||
for k, v := range cfg.Env {
|
||||
env[k] = os.ExpandEnv(v)
|
||||
}
|
||||
client, err = NewStdioClient(ctx, cfg.Name, command, args, env, m.logger)
|
||||
|
||||
case "sse":
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("MCP server %q with sse transport must have a url", cfg.Name)
|
||||
}
|
||||
url := os.ExpandEnv(cfg.URL)
|
||||
headers := make(map[string]string, len(cfg.Headers))
|
||||
for k, v := range cfg.Headers {
|
||||
headers[k] = os.ExpandEnv(v)
|
||||
}
|
||||
client, err = NewSSEClient(ctx, cfg.Name, url, headers, m.logger)
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknown transport %q for MCP server %q (must be stdio or sse)", transport, cfg.Name)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.clients[cfg.Name] = client
|
||||
m.logger.Info("MCP server connected", "name", cfg.Name, "transport", transport, "tools", len(client.Tools()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClient returns an MCP client by name, or nil if not found.
|
||||
func (m *Manager) GetClient(name string) *Client {
|
||||
return m.clients[name]
|
||||
}
|
||||
|
||||
// AllClients returns all MCP clients managed by this manager.
|
||||
func (m *Manager) AllClients() map[string]*Client {
|
||||
return m.clients
|
||||
}
|
||||
|
||||
// Close closes all MCP client connections.
|
||||
func (m *Manager) Close() error {
|
||||
var errs []string
|
||||
for name, client := range m.clients {
|
||||
if err := client.Close(); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%s: %v", name, err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("errors closing MCP clients: %s", strings.Join(errs, "; "))
|
||||
}
|
||||
|
||||
m.logger.Info("MCP manager closed", "servers", len(m.clients))
|
||||
return nil
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// Package protocols contains adapters for external agent protocols.
|
||||
package protocols
|
||||
// Package mcp provides MCP client and server implementations.
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
Reference in New Issue
Block a user