feat: Enhance MCP service configuration with stdio transport support
- Added support for stdio transport type in MCP service configuration, allowing command execution via standard input/output. - Updated MCPService interface to include optional stdio_config and env_vars fields for managing command arguments and environment variables. - Enhanced frontend components to handle stdio transport, including dynamic form fields for command and arguments. - Implemented validation logic for stdio configuration in the service update process, ensuring proper handling of required fields. - Updated backend logic to manage stdio transport connections and ensure proper client lifecycle management.
This commit is contained in:
+35
-3
@@ -95,7 +95,10 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {
|
||||
var err error
|
||||
switch config.Service.TransportType {
|
||||
case types.MCPTransportSSE:
|
||||
mcpClient, err = client.NewSSEMCPClient(config.Service.URL,
|
||||
if config.Service.URL == nil || *config.Service.URL == "" {
|
||||
return nil, fmt.Errorf("URL is required for SSE transport")
|
||||
}
|
||||
mcpClient, err = client.NewSSEMCPClient(*config.Service.URL,
|
||||
client.WithHTTPClient(httpClient),
|
||||
client.WithHeaders(headers),
|
||||
)
|
||||
@@ -103,14 +106,38 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {
|
||||
return nil, fmt.Errorf("failed to create SSE client: %w", err)
|
||||
}
|
||||
case types.MCPTransportHTTPStreamable:
|
||||
if config.Service.URL == nil || *config.Service.URL == "" {
|
||||
return nil, fmt.Errorf("URL is required for HTTP Streamable transport")
|
||||
}
|
||||
// For HTTP streamable, we need to use transport options
|
||||
mcpClient, err = client.NewStreamableHttpClient(config.Service.URL,
|
||||
mcpClient, err = client.NewStreamableHttpClient(*config.Service.URL,
|
||||
transport.WithHTTPBasicClient(httpClient),
|
||||
transport.WithHTTPHeaders(headers),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP streamable client: %w", err)
|
||||
}
|
||||
case types.MCPTransportStdio:
|
||||
if config.Service.StdioConfig == nil {
|
||||
return nil, fmt.Errorf("stdio_config is required for stdio transport")
|
||||
}
|
||||
|
||||
// Convert env vars map to []string format (KEY=value)
|
||||
envVars := make([]string, 0, len(config.Service.EnvVars))
|
||||
for key, value := range config.Service.EnvVars {
|
||||
envVars = append(envVars, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
|
||||
// Create stdio client with options
|
||||
// NewStdioMCPClientWithOptions(command string, env []string, args []string, opts ...transport.StdioOption)
|
||||
mcpClient, err = client.NewStdioMCPClientWithOptions(
|
||||
config.Service.StdioConfig.Command,
|
||||
envVars,
|
||||
config.Service.StdioConfig.Args,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdio client: %w", err)
|
||||
}
|
||||
default:
|
||||
return nil, ErrUnsupportedTransport
|
||||
}
|
||||
@@ -133,7 +160,12 @@ func (c *mcpGoClient) Connect(ctx context.Context) error {
|
||||
}
|
||||
|
||||
c.connected = true
|
||||
logger.GetLogger(ctx).Infof("MCP client connected to %s", c.service.URL)
|
||||
if c.service.TransportType == types.MCPTransportStdio {
|
||||
logger.GetLogger(ctx).Infof("MCP stdio client connected: %s %v",
|
||||
c.service.StdioConfig.Command, c.service.StdioConfig.Args)
|
||||
} else {
|
||||
logger.GetLogger(ctx).Infof("MCP client connected to %s", *c.service.URL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+48
-2
@@ -35,13 +35,20 @@ func NewMCPManager() *MCPManager {
|
||||
}
|
||||
|
||||
// GetOrCreateClient gets an existing client or creates a new one
|
||||
// For stdio transport, always creates a new client (not cached)
|
||||
// For SSE/HTTP Streamable, caches and reuses existing connections
|
||||
func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, error) {
|
||||
// Check if service is enabled
|
||||
if !service.Enabled {
|
||||
return nil, fmt.Errorf("MCP service %s is not enabled", service.Name)
|
||||
}
|
||||
|
||||
// Check if client already exists
|
||||
// For stdio transport, always create a new client (don't cache)
|
||||
if service.TransportType == types.MCPTransportStdio {
|
||||
return m.createStdioClient(service)
|
||||
}
|
||||
|
||||
// For SSE/HTTP Streamable, check if client already exists and reuse
|
||||
m.clientsMu.RLock()
|
||||
client, exists := m.clients[service.ID]
|
||||
m.clientsMu.RUnlock()
|
||||
@@ -95,13 +102,52 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
|
||||
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
|
||||
}
|
||||
|
||||
// Store client
|
||||
// Store client (only for non-stdio transports)
|
||||
m.clients[service.ID] = client
|
||||
|
||||
logger.GetLogger(m.ctx).Infof("MCP client created and initialized for service: %s", service.Name)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// createStdioClient creates a new stdio client (not cached)
|
||||
func (m *MCPManager) createStdioClient(service *types.MCPService) (MCPClient, error) {
|
||||
// Create new client
|
||||
config := &ClientConfig{
|
||||
Service: service,
|
||||
}
|
||||
|
||||
client, err := NewMCPClient(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdio MCP client: %w", err)
|
||||
}
|
||||
|
||||
// For stdio, Connect() starts the subprocess
|
||||
// Use manager's context for the connection lifecycle
|
||||
if err := client.Connect(m.ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to stdio MCP service: %w", err)
|
||||
}
|
||||
|
||||
// Initialize needs a timeout to prevent hanging
|
||||
initTimeout := 30 * time.Second
|
||||
if service.AdvancedConfig != nil && service.AdvancedConfig.Timeout > 0 {
|
||||
initTimeout = time.Duration(service.AdvancedConfig.Timeout) * time.Second
|
||||
if initTimeout > 60*time.Second {
|
||||
initTimeout = 60 * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
initCtx, initCancel := context.WithTimeout(m.ctx, initTimeout)
|
||||
defer initCancel()
|
||||
|
||||
if _, err := client.Initialize(initCtx); err != nil {
|
||||
client.Disconnect()
|
||||
return nil, fmt.Errorf("failed to initialize stdio MCP client: %w", err)
|
||||
}
|
||||
|
||||
logger.GetLogger(m.ctx).Infof("MCP stdio client created and initialized for service: %s", service.Name)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// GetClient gets an existing client
|
||||
func (m *MCPManager) GetClient(serviceID string) (MCPClient, bool) {
|
||||
m.clientsMu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user