From e719f36db3861b2cb48c8de7eaf5db7e2e9be27e Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 5 Dec 2025 23:05:40 -0500 Subject: [PATCH 1/3] Prevent HTTP MCP clients from being closed during initMCPActions() --- core/agent/mcp.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/agent/mcp.go b/core/agent/mcp.go index 62d85b37..9c6dc249 100644 --- a/core/agent/mcp.go +++ b/core/agent/mcp.go @@ -170,7 +170,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 { @@ -200,8 +200,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) @@ -240,7 +238,7 @@ func (a *Agent) initMCPActions() error { return err } -func (a *Agent) closeMCPSTDIOServers() { +func (a *Agent) closeMCPServers() { for _, s := range a.mcpSessions { s.Close() } From eaedbbe848a3392e2075eeebdc7b6dfb096d1461 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 6 Dec 2025 01:27:53 -0500 Subject: [PATCH 2/3] Support image content returned by MCP tool call --- core/agent/agent.go | 4 ++-- core/agent/mcp.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/core/agent/agent.go b/core/agent/agent.go index e492becb..c1ebf2a2 100644 --- a/core/agent/agent.go +++ b/core/agent/agent.go @@ -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() } @@ -1216,7 +1216,7 @@ func (a *Agent) addFunctionResultToConversation(ctx context.Context, chosenActio { Type: openai.ChatMessagePartTypeImageURL, ImageURL: &openai.ChatMessageImageURL{ - URL: result.ImageBase64Result, + URL: "data:image/png;base64," + result.ImageBase64Result, }, }, }, diff --git a/core/agent/mcp.go b/core/agent/mcp.go index 9c6dc249..00a29e0e 100644 --- a/core/agent/mcp.go +++ b/core/agent/mcp.go @@ -2,6 +2,7 @@ package agent import ( "context" + "encoding/base64" "encoding/json" "errors" "os" @@ -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 = 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 { From 1c2b9ceb58414fc5247e94060797c02f1a4c8b82 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 6 Dec 2025 01:48:59 -0500 Subject: [PATCH 3/3] Format the base64 image result with the mimetype from the tool call response --- core/agent/agent.go | 2 +- core/agent/mcp.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/agent/agent.go b/core/agent/agent.go index c1ebf2a2..a15d798e 100644 --- a/core/agent/agent.go +++ b/core/agent/agent.go @@ -1216,7 +1216,7 @@ func (a *Agent) addFunctionResultToConversation(ctx context.Context, chosenActio { Type: openai.ChatMessagePartTypeImageURL, ImageURL: &openai.ChatMessageImageURL{ - URL: "data:image/png;base64," + result.ImageBase64Result, + URL: result.ImageBase64Result, }, }, }, diff --git a/core/agent/mcp.go b/core/agent/mcp.go index 00a29e0e..7c505bff 100644 --- a/core/agent/mcp.go +++ b/core/agent/mcp.go @@ -68,7 +68,7 @@ func (m *mcpAction) Run(ctx context.Context, sharedState *types.AgentSharedState case *mcp.TextContent: result += content.Text case *mcp.ImageContent: - imageBase64Result = base64.StdEncoding.EncodeToString(content.Data) + imageBase64Result = "data:" + content.MIMEType + ";base64," + base64.StdEncoding.EncodeToString(content.Data) default: log.Error().Msgf("[Unknown content type received: %T]", content) }