feat: auto-describe MCP tool result images via VLM
When an MCP tool returns image content items, the agent engine now automatically generates text descriptions using the configured VLM model and appends them to the tool result message. This makes image content accessible to LLMs that receive tool results as text. Images are not passed directly to the LLM because Chat Completions API does not reliably support images in tool role messages across providers (tested: gpt-5.4 silently ignores them, Qwen3.5 processes them). Changes: - Add Images field to ToolResult for preserving base64 data URIs - Add extractContentAndImages() with MIME whitelist, size/count limits - Add ImageDescriberFunc injection to AgentEngine (no vlm package dep) - Add describeImages() with graceful error handling and ctx cancellation - Add VLM model injection in CreateAgentEngine (same pattern as SetAppConfig/SetSkillsManager) - Redact image base64 from Data map to prevent log/SSE exposure
This commit is contained in:
@@ -205,6 +205,19 @@ func (s *agentService) CreateAgentEngine(
|
||||
)
|
||||
engine.SetAppConfig(s.cfg)
|
||||
|
||||
// Set VLM image describer for MCP tool result image analysis.
|
||||
// When an MCP tool returns images, the engine uses VLM to generate text descriptions
|
||||
// and appends them to the tool result content (since Chat Completions API does not
|
||||
// reliably support images in tool role messages across providers).
|
||||
if config.VLMModelID != "" {
|
||||
if vlmModel, err := s.modelService.GetVLMModel(ctx, config.VLMModelID); err == nil {
|
||||
engine.SetImageDescriber(vlmModel.Predict)
|
||||
logger.Infof(ctx, "VLM image describer set for MCP tool result analysis (model: %s)", config.VLMModelID)
|
||||
} else {
|
||||
logger.Warnf(ctx, "Failed to load VLM model %s for MCP image fallback: %v", config.VLMModelID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize skills manager if skills are enabled
|
||||
if config.SkillsEnabled && len(config.SkillDirs) > 0 {
|
||||
skillsManager, err := s.initializeSkillsManager(ctx, config, toolRegistry)
|
||||
|
||||
@@ -70,6 +70,11 @@ func (s *sessionService) AgentQA(
|
||||
return err
|
||||
}
|
||||
|
||||
// Set VLM model ID for tool result image analysis (runtime-only field)
|
||||
if req.CustomAgent != nil && req.CustomAgent.Config.VLMModelID != "" {
|
||||
agentConfig.VLMModelID = req.CustomAgent.Config.VLMModelID
|
||||
}
|
||||
|
||||
// Resolve model ID using shared helper (AgentQA requires a model, so error if not found)
|
||||
effectiveModelID, err := s.resolveChatModelID(ctx, req, agentConfig.KnowledgeBases, agentConfig.KnowledgeIDs)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user