Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (a *Agent) Stop() {
a.Lock()
defer a.Unlock()
xlog.Debug("Stopping agent", "agent", a.Character.Name)
a.closeMCPSTDIOServers()
a.closeMCPServers()
a.context.Cancel()
}

Expand Down
19 changes: 13 additions & 6 deletions core/agent/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"os"
Expand Down Expand Up @@ -61,11 +62,19 @@ func (m *mcpAction) Run(ctx context.Context, sharedState *types.AgentSharedState
}

result := ""
imageBase64Result := ""
for _, c := range resp.Content {
result += c.(*mcp.TextContent).Text
switch content := c.(type) {
case *mcp.TextContent:
result += content.Text
case *mcp.ImageContent:
imageBase64Result = "data:" + content.MIMEType + ";base64," + base64.StdEncoding.EncodeToString(content.Data)
default:
log.Error().Msgf("[Unknown content type received: %T]", content)
}
}

return types.ActionResult{Result: result}, nil
return types.ActionResult{Result: result, ImageBase64Result: imageBase64Result}, nil
}

func (m *mcpAction) Definition() types.ActionDefinition {
Expand Down Expand Up @@ -170,7 +179,7 @@ func (a *Agent) initMCPActions() error {
generatedActions := types.Actions{}
client := mcp.NewClient(&mcp.Implementation{Name: "LocalAI", Version: "v1.0.0"}, nil)

// Connect to a server over stdin/stdout.
a.closeMCPServers() // Make sure we stop all previous servers if any is active

// MCP HTTP Servers
for _, mcpServer := range a.options.mcpServers {
Expand Down Expand Up @@ -200,8 +209,6 @@ func (a *Agent) initMCPActions() error {

// MCP STDIO Servers

a.closeMCPSTDIOServers() // Make sure we stop all previous servers if any is active

if a.options.mcpPrepareScript != "" {
xlog.Debug("Preparing MCP", "script", a.options.mcpPrepareScript)

Expand Down Expand Up @@ -240,7 +247,7 @@ func (a *Agent) initMCPActions() error {
return err
}

func (a *Agent) closeMCPSTDIOServers() {
func (a *Agent) closeMCPServers() {
for _, s := range a.mcpSessions {
s.Close()
}
Expand Down