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:
committed by
mudler
parent
02490ea8a2
commit
6d9c58e6c0
@@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,34 +87,15 @@ type ToolInputSchema struct {
|
|||||||
Required []string `json:"required,omitempty"`
|
Required []string `json:"required,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) initMCPActions() error {
|
func (a *Agent) addTools(client *mcp.Client) (types.Actions, error) {
|
||||||
|
|
||||||
a.mcpActions = nil
|
var generatedActions types.Actions
|
||||||
var err error
|
xlog.Debug("Initializing client")
|
||||||
|
|
||||||
generatedActions := types.Actions{}
|
|
||||||
|
|
||||||
for _, mcpServer := range a.options.mcpServers {
|
|
||||||
transport := http.NewHTTPClientTransport("/mcp")
|
|
||||||
transport.WithBaseURL(mcpServer.URL)
|
|
||||||
if mcpServer.Token != "" {
|
|
||||||
transport.WithHeader("Authorization", "Bearer "+mcpServer.Token)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new client
|
|
||||||
client := mcp.NewClient(transport)
|
|
||||||
|
|
||||||
xlog.Debug("Initializing client", "server", mcpServer.URL)
|
|
||||||
// Initialize the client
|
// Initialize the client
|
||||||
response, e := client.Initialize(a.context)
|
response, e := client.Initialize(a.context)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
xlog.Error("Failed to initialize client", "error", e.Error(), "server", mcpServer)
|
xlog.Error("Failed to initialize client", "error", e.Error())
|
||||||
if err == nil {
|
return nil, e
|
||||||
err = e
|
|
||||||
} else {
|
|
||||||
err = errors.Join(err, e)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Client initialized: %v", response.Instructions)
|
xlog.Debug("Client initialized: %v", response.Instructions)
|
||||||
@@ -116,7 +105,7 @@ func (a *Agent) initMCPActions() error {
|
|||||||
tools, err := client.ListTools(a.context, cursor)
|
tools, err := client.ListTools(a.context, cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Failed to list tools", "error", err.Error())
|
xlog.Error("Failed to list tools", "error", err.Error())
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range tools.Tools {
|
for _, t := range tools.Tools {
|
||||||
@@ -125,14 +114,14 @@ func (a *Agent) initMCPActions() error {
|
|||||||
desc = *t.Description
|
desc = *t.Description
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Tool", "mcpServer", mcpServer, "name", t.Name, "description", desc)
|
xlog.Debug("Tool", "name", t.Name, "description", desc)
|
||||||
|
|
||||||
dat, err := json.Marshal(t.InputSchema)
|
dat, err := json.Marshal(t.InputSchema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Failed to marshal input schema", "error", err.Error())
|
xlog.Error("Failed to marshal input schema", "error", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Input schema", "mcpServer", mcpServer, "tool", t.Name, "schema", string(dat))
|
xlog.Debug("Input schema", "tool", t.Name, "schema", string(dat))
|
||||||
|
|
||||||
// XXX: This is a wild guess, to verify (data types might be incompatible)
|
// XXX: This is a wild guess, to verify (data types might be incompatible)
|
||||||
var inputSchema ToolInputSchema
|
var inputSchema ToolInputSchema
|
||||||
@@ -156,9 +145,71 @@ func (a *Agent) initMCPActions() error {
|
|||||||
cursor = tools.NextCursor
|
cursor = tools.NextCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return generatedActions, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) initMCPActions() error {
|
||||||
|
|
||||||
|
a.mcpActions = nil
|
||||||
|
var err error
|
||||||
|
|
||||||
|
generatedActions := types.Actions{}
|
||||||
|
|
||||||
|
// MCP HTTP Servers
|
||||||
|
for _, mcpServer := range a.options.mcpServers {
|
||||||
|
transport := http.NewHTTPClientTransport("/mcp")
|
||||||
|
transport.WithBaseURL(mcpServer.URL)
|
||||||
|
if mcpServer.Token != "" {
|
||||||
|
transport.WithHeader("Authorization", "Bearer "+mcpServer.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MCP STDIO Servers
|
||||||
|
a.closeMCPSTDIOServers() // Make sure we stop all previous servers if any is active
|
||||||
|
for _, mcpStdioServer := range a.options.mcpStdioServers {
|
||||||
|
client := stdio.NewClient(a.options.mcpBoxURL)
|
||||||
|
p, err := client.CreateProcess(a.context,
|
||||||
|
mcpStdioServer.Cmd,
|
||||||
|
mcpStdioServer.Args,
|
||||||
|
mcpStdioServer.Env,
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
transport := stdioTransport.NewStdioServerTransportWithIO(read, writer)
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
mcpClient := mcp.NewClient(transport)
|
||||||
|
|
||||||
|
xlog.Debug("Adding tools for MCP server (stdio)", "server", mcpStdioServer)
|
||||||
|
generatedActions, err = a.addTools(mcpClient)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Failed to add tools for MCP server", "server", mcpStdioServer, "error", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ 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
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -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 == "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user