mirror of
https://github.com/mudler/LocalAI.git
synced 2026-04-01 05:36:49 -04:00
* feat: add distributed mode (experimental) Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix data races, mutexes, transactions Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactorings Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fixups Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix events and tool stream in agent chat Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * use ginkgo Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(cron): compute correctly time boundaries avoiding re-triggering Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * enhancements, refactorings Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * do not flood of healthy checks Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * do not list obvious backends as text backends Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * tests fixups Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactoring and consolidation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Drop redundant healthcheck Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * enhancements, refactorings Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package agents
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
|
|
gomcp "github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/mudler/xlog"
|
|
)
|
|
|
|
// setupMCPSessions creates MCP client sessions from the agent config.
|
|
// Returns the sessions, a cleanup function, and any error.
|
|
func setupMCPSessions(ctx context.Context, cfg *AgentConfig) ([]*gomcp.ClientSession, func()) {
|
|
var sessions []*gomcp.ClientSession
|
|
|
|
client := gomcp.NewClient(&gomcp.Implementation{
|
|
Name: "localai-agent",
|
|
Version: "v1.0.0",
|
|
}, nil)
|
|
|
|
// HTTP MCP servers (using SSE client transport)
|
|
for _, srv := range cfg.MCPServers {
|
|
if srv.URL == "" {
|
|
continue
|
|
}
|
|
transport := &gomcp.SSEClientTransport{Endpoint: srv.URL}
|
|
session, err := client.Connect(ctx, transport, nil)
|
|
if err != nil {
|
|
xlog.Warn("Failed to connect to HTTP MCP server", "url", srv.URL, "error", err)
|
|
continue
|
|
}
|
|
sessions = append(sessions, session)
|
|
}
|
|
|
|
// STDIO MCP servers (using command transport)
|
|
for _, srv := range cfg.MCPSTDIOServers {
|
|
if srv.Cmd == "" {
|
|
continue
|
|
}
|
|
cmd := exec.CommandContext(ctx, srv.Cmd, srv.Args...)
|
|
if len(srv.Env) > 0 {
|
|
cmd.Env = append(cmd.Environ(), srv.Env...)
|
|
}
|
|
transport := &gomcp.CommandTransport{Command: cmd}
|
|
session, err := client.Connect(ctx, transport, nil)
|
|
if err != nil {
|
|
xlog.Warn("Failed to connect to STDIO MCP server", "cmd", srv.Cmd, "error", err)
|
|
continue
|
|
}
|
|
sessions = append(sessions, session)
|
|
}
|
|
|
|
cleanup := func() {
|
|
for _, s := range sessions {
|
|
if err := s.Close(); err != nil {
|
|
xlog.Warn("Failed to close MCP session", "error", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return sessions, cleanup
|
|
}
|