Attach mcp stdio box to agent

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-04-22 23:07:54 +02:00
committed by mudler
parent 02490ea8a2
commit 6d9c58e6c0
4 changed files with 125 additions and 56 deletions

View File

@@ -241,6 +241,7 @@ func (a *Agent) Stop() {
a.Lock() a.Lock()
defer a.Unlock() defer a.Unlock()
xlog.Debug("Stopping agent", "agent", a.Character.Name) xlog.Debug("Stopping agent", "agent", a.Character.Name)
a.closeMCPSTDIOServers()
a.context.Cancel() a.context.Cancel()
} }

View File

@@ -3,12 +3,14 @@ package agent
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
mcp "github.com/metoro-io/mcp-golang" mcp "github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/http" "github.com/metoro-io/mcp-golang/transport/http"
stdioTransport "github.com/metoro-io/mcp-golang/transport/stdio"
"github.com/mudler/LocalAGI/core/types" "github.com/mudler/LocalAGI/core/types"
"github.com/mudler/LocalAGI/pkg/stdio"
"github.com/mudler/LocalAGI/pkg/xlog" "github.com/mudler/LocalAGI/pkg/xlog"
"github.com/sashabaranov/go-openai/jsonschema" "github.com/sashabaranov/go-openai/jsonschema"
) )
@@ -19,6 +21,12 @@ type MCPServer struct {
Token string `json:"token"` Token string `json:"token"`
} }
type MCPSTDIOServer struct {
Args []string `json:"args"`
Env []string `json:"env"`
Cmd string `json:"cmd"`
}
type mcpAction struct { type mcpAction struct {
mcpClient *mcp.Client mcpClient *mcp.Client
inputSchema ToolInputSchema inputSchema ToolInputSchema
@@ -79,6 +87,68 @@ type ToolInputSchema struct {
Required []string `json:"required,omitempty"` Required []string `json:"required,omitempty"`
} }
func (a *Agent) addTools(client *mcp.Client) (types.Actions, error) {
var generatedActions types.Actions
xlog.Debug("Initializing client")
// Initialize the client
response, e := client.Initialize(a.context)
if e != nil {
xlog.Error("Failed to initialize client", "error", e.Error())
return nil, e
}
xlog.Debug("Client initialized: %v", response.Instructions)
var cursor *string
for {
tools, err := client.ListTools(a.context, cursor)
if err != nil {
xlog.Error("Failed to list tools", "error", err.Error())
return nil, err
}
for _, t := range tools.Tools {
desc := ""
if t.Description != nil {
desc = *t.Description
}
xlog.Debug("Tool", "name", t.Name, "description", desc)
dat, err := json.Marshal(t.InputSchema)
if err != nil {
xlog.Error("Failed to marshal input schema", "error", err.Error())
}
xlog.Debug("Input schema", "tool", t.Name, "schema", string(dat))
// XXX: This is a wild guess, to verify (data types might be incompatible)
var inputSchema ToolInputSchema
err = json.Unmarshal(dat, &inputSchema)
if err != nil {
xlog.Error("Failed to unmarshal input schema", "error", err.Error())
}
// Create a new action with Client + tool
generatedActions = append(generatedActions, &mcpAction{
mcpClient: client,
toolName: t.Name,
inputSchema: inputSchema,
toolDescription: desc,
})
}
if tools.NextCursor == nil {
break // No more pages
}
cursor = tools.NextCursor
}
return generatedActions, nil
}
func (a *Agent) initMCPActions() error { func (a *Agent) initMCPActions() error {
a.mcpActions = nil a.mcpActions = nil
@@ -86,6 +156,7 @@ func (a *Agent) initMCPActions() error {
generatedActions := types.Actions{} generatedActions := types.Actions{}
// MCP HTTP Servers
for _, mcpServer := range a.options.mcpServers { for _, mcpServer := range a.options.mcpServers {
transport := http.NewHTTPClientTransport("/mcp") transport := http.NewHTTPClientTransport("/mcp")
transport.WithBaseURL(mcpServer.URL) transport.WithBaseURL(mcpServer.URL)
@@ -95,70 +166,50 @@ func (a *Agent) initMCPActions() error {
// Create a new client // Create a new client
client := mcp.NewClient(transport) client := mcp.NewClient(transport)
xlog.Debug("Adding tools for MCP server", "server", mcpServer)
generatedActions, err = a.addTools(client)
if err != nil {
xlog.Error("Failed to add tools for MCP server", "server", mcpServer, "error", err.Error())
}
}
xlog.Debug("Initializing client", "server", mcpServer.URL) // MCP STDIO Servers
// Initialize the client a.closeMCPSTDIOServers() // Make sure we stop all previous servers if any is active
response, e := client.Initialize(a.context) for _, mcpStdioServer := range a.options.mcpStdioServers {
if e != nil { client := stdio.NewClient(a.options.mcpBoxURL)
xlog.Error("Failed to initialize client", "error", e.Error(), "server", mcpServer) p, err := client.CreateProcess(a.context,
if err == nil { mcpStdioServer.Cmd,
err = e mcpStdioServer.Args,
} else { mcpStdioServer.Env,
err = errors.Join(err, e) a.Character.Name)
} if err != nil {
xlog.Error("Failed to create process", "error", err.Error())
continue
}
read, writer, err := client.GetProcessIO(p.ID)
if err != nil {
xlog.Error("Failed to get process IO", "error", err.Error())
continue continue
} }
xlog.Debug("Client initialized: %v", response.Instructions) transport := stdioTransport.NewStdioServerTransportWithIO(read, writer)
var cursor *string // Create a new client
for { mcpClient := mcp.NewClient(transport)
tools, err := client.ListTools(a.context, cursor)
if err != nil {
xlog.Error("Failed to list tools", "error", err.Error())
return err
}
for _, t := range tools.Tools { xlog.Debug("Adding tools for MCP server (stdio)", "server", mcpStdioServer)
desc := "" generatedActions, err = a.addTools(mcpClient)
if t.Description != nil { if err != nil {
desc = *t.Description xlog.Error("Failed to add tools for MCP server", "server", mcpStdioServer, "error", err.Error())
}
xlog.Debug("Tool", "mcpServer", mcpServer, "name", t.Name, "description", desc)
dat, err := json.Marshal(t.InputSchema)
if err != nil {
xlog.Error("Failed to marshal input schema", "error", err.Error())
}
xlog.Debug("Input schema", "mcpServer", mcpServer, "tool", t.Name, "schema", string(dat))
// XXX: This is a wild guess, to verify (data types might be incompatible)
var inputSchema ToolInputSchema
err = json.Unmarshal(dat, &inputSchema)
if err != nil {
xlog.Error("Failed to unmarshal input schema", "error", err.Error())
}
// Create a new action with Client + tool
generatedActions = append(generatedActions, &mcpAction{
mcpClient: client,
toolName: t.Name,
inputSchema: inputSchema,
toolDescription: desc,
})
}
if tools.NextCursor == nil {
break // No more pages
}
cursor = tools.NextCursor
} }
} }
a.mcpActions = generatedActions a.mcpActions = generatedActions
return err return err
} }
func (a *Agent) closeMCPSTDIOServers() {
client := stdio.NewClient(a.options.mcpBoxURL)
client.StopGroup(a.Character.Name)
}

View File

@@ -50,7 +50,9 @@ type options struct {
conversationsPath string conversationsPath string
mcpServers []MCPServer mcpServers []MCPServer
mcpStdioServers []MCPSTDIOServer
mcpBoxURL string
newConversationsSubscribers []func(openai.ChatCompletionMessage) newConversationsSubscribers []func(openai.ChatCompletionMessage)
@@ -207,6 +209,20 @@ func WithMCPServers(servers ...MCPServer) Option {
} }
} }
func WithMCPSTDIOServers(servers ...MCPSTDIOServer) Option {
return func(o *options) error {
o.mcpStdioServers = servers
return nil
}
}
func WithMCPBoxURL(url string) Option {
return func(o *options) error {
o.mcpBoxURL = url
return nil
}
}
func WithLLMAPIURL(url string) Option { func WithLLMAPIURL(url string) Option {
return func(o *options) error { return func(o *options) error {
o.LLMAPI.APIURL = url o.LLMAPI.APIURL = url

View File

@@ -23,6 +23,7 @@ var apiKeysEnv = os.Getenv("LOCALAGI_API_KEYS")
var imageModel = os.Getenv("LOCALAGI_IMAGE_MODEL") var imageModel = os.Getenv("LOCALAGI_IMAGE_MODEL")
var conversationDuration = os.Getenv("LOCALAGI_CONVERSATION_DURATION") var conversationDuration = os.Getenv("LOCALAGI_CONVERSATION_DURATION")
var localOperatorBaseURL = os.Getenv("LOCALOPERATOR_BASE_URL") var localOperatorBaseURL = os.Getenv("LOCALOPERATOR_BASE_URL")
var mcpboxURL = os.Getenv("LOCALAGI_MCPBOX_URL")
func init() { func init() {
if baseModel == "" { if baseModel == "" {