MCP SQL Server image field

Describe the problem/error/question

Hello,

I have set up an MCP server that retrieves information from a Microsoft SQL Server database. It works perfectly for text data.

However, I am having trouble returning images. In my SQL Server (2022) database, the field type is “Image”. My sub-workflow correctly returns a JSON object to my MCP server as follows:

[{
"Photo": 
{  
"type": 
"Buffer",  
"data": [255,216,255,225,xxxxx,14]
}
}
]

What is the error message (if any)?

OpenWebUi ask to MCP and get Json but show that it can’t display it. Is there a way ton convert it ?

Thanks

Hi @Cedric19 , the problem is the raw Buffer data isn’t directly displayable. You need to convert it to a base64-encoded string or provide a URL to the image.

here’s a Code node to convert the Buffer to base64:

Code Node:

// Assuming your input has the Photo field with Buffer data
const items = $input.all();

return items.map(item => {
  const photoBuffer = item.json.Photo;
  
  // Convert Buffer to base64
  let base64Image = '';
  
  if (photoBuffer && photoBuffer.data) {
    // Create Buffer from the data array
    const buffer = Buffer.from(photoBuffer.data);
    // Convert to base64
    base64Image = buffer.toString('base64');
  }
  
  return {
    json: {
      Photo: base64Image,
      // Include data URI format for direct display
      PhotoDataURI: `data:image/jpeg;base64,${base64Image}`
    }
  };
});

Does this help!

Sorry I’m just starting N8N, but where should I use this python script ?

No problem! Let me walk you through.

Step by step:

1. Find Your Current Workflow; open the workflow that connects to your SQL Server, you should see your Microsoft SQL node that retrieves the image data

2. Add a Code Node; click the + button after your SQL node, search for “Code”, select “Code” node (it lets you write JavaScript)

3. Paste the JavaScript; click on the Code node you just added, you’ll see a text editor, delete any default code, paste the code.

I’ve added Javascript Convertion to my workflow. It returns now in openwebui :

{
“results”: [
{
“Photo”: “/9j/4Q4vRXhpZgAASUkqAAgAAAAOAAABAwABAAAAzxxxxx”
}
]
}

But openwebui won’t display it :frowning: I’ve do exactly the same in python FastMCP serv and it works

@mcp.tool()
def get_employe_picture(employee_id: str) -> ImageContent:
    """
    Retourne la photo d'un employé au format Image MCP par son employee_id.
    Ce tool nécessite un employee_id obtenu via rechercher_employe.
    """
    logging.info("get_employe_picture called" + " " + employee_id)
    photo_bytes = tools.get_employee_photo(employee_id)

    if photo_bytes is None:
        raise ValueError(f"Aucune photo trouvée pour l'employé {employee_id}")


    image = Image(
        data=photo_bytes,
        format="png"  # adapte si nécessaire : "jpeg", "webp", etc.
    )

    # Conversion explicite en ImageContent MCP
    return image.to_image_content()