@ -205,23 +205,28 @@ def ask_image_question(file_path: str, prompt: str) -> str:
return f " Error processing image question: { str ( e ) } "
return f " Error processing image question: { str ( e ) } "
@mcp . tool ( )
@mcp . tool ( )
def generate_image_dalle ( prompt : str , size : str = " 1024x1024 " , quality : str = " standard " , style : str = " vivid " , n : int = 1 ) - > str :
def generate_image_dalle ( prompt : str , save_path : str , s ize : str = " 1024x1024 " , quality : str = " standard " , style : str = " vivid " , n : int = 1 ) - > str :
"""
"""
Generate an image using DALL - E API
Generate an image using DALL - E API and save it to the specified path
Args :
Args :
prompt : Description of the image to generate
prompt : Description of the image to generate
save_path : Absolute path where to save the generated image ( s )
size : Image size - options : " 1024x1024 " , " 1792x1024 " , " 1024x1792 " ( default : " 1024x1024 " )
size : Image size - options : " 1024x1024 " , " 1792x1024 " , " 1024x1792 " ( default : " 1024x1024 " )
quality : Image quality - options : " standard " , " hd " ( default : " standard " )
quality : Image quality - options : " standard " , " hd " ( default : " standard " )
style : Image style - options : " vivid " , " natural " ( default : " vivid " )
style : Image style - options : " vivid " , " natural " ( default : " vivid " )
n : Number of images to generate ( 1 - 10 , default : 1 )
n : Number of images to generate ( 1 - 10 , default : 1 )
Returns :
Returns :
JSON response with generated image URL s and metadata
Success message with saved file path s and metadata
"""
"""
import requests
from pathlib import Path
try :
try :
logger . debug ( f " Generating image with DALL-E " )
logger . debug ( f " Generating image with DALL-E " )
logger . debug ( f " Prompt: { prompt } " )
logger . debug ( f " Prompt: { prompt } " )
logger . debug ( f " Save path: { save_path } " )
logger . debug ( f " Size: { size } , Quality: { quality } , Style: { style } , Count: { n } " )
logger . debug ( f " Size: { size } , Quality: { quality } , Style: { style } , Count: { n } " )
# Validate parameters
# Validate parameters
@ -240,6 +245,21 @@ def generate_image_dalle(prompt: str, size: str = "1024x1024", quality: str = "s
if not ( 1 < = n < = 10 ) :
if not ( 1 < = n < = 10 ) :
return " Error: Number of images must be between 1 and 10 "
return " Error: Number of images must be between 1 and 10 "
# Validate save path
try :
save_path = os . path . abspath ( save_path )
save_dir = os . path . dirname ( save_path )
# Create directory if it doesn't exist
os . makedirs ( save_dir , exist_ok = True )
# Check if directory is writable
if not os . access ( save_dir , os . W_OK ) :
return f " Error: Directory ' { save_dir } ' is not writable "
except Exception as e :
return f " Error: Invalid save path ' { save_path } ' : { str ( e ) } "
# Check if OpenAI is available
# Check if OpenAI is available
if not HAS_OPENAI :
if not HAS_OPENAI :
return " Error: OpenAI API key not configured. Please set OPENAI_API_KEY to use DALL-E image generation. "
return " Error: OpenAI API key not configured. Please set OPENAI_API_KEY to use DALL-E image generation. "
@ -255,30 +275,63 @@ def generate_image_dalle(prompt: str, size: str = "1024x1024", quality: str = "s
n = n
n = n
)
)
# Format response
saved_files = [ ]
result = {
" prompt " : prompt ,
" parameters " : {
" size " : size ,
" quality " : quality ,
" style " : style ,
" count " : n
} ,
" images " : [ ]
}
for i , image_data in enumerate ( response . data ) :
for i , image_data in enumerate ( response . data ) :
result [ " images " ] . append ( {
try :
" index " : i + 1 ,
# Download the image
" url " : image_data . url ,
image_response = requests . get ( image_data . url , timeout = 30 )
" revised_prompt " : getattr ( image_data , ' revised_prompt ' , None )
image_response . raise_for_status ( )
# Determine file path for multiple images
if n == 1 :
file_path = save_path
else :
# For multiple images, add index to filename
path_obj = Path ( save_path )
file_path = str ( path_obj . parent / f " { path_obj . stem } _ { i + 1 } { path_obj . suffix } " )
# Ensure file has proper extension
if not file_path . lower ( ) . endswith ( ( ' .png ' , ' .jpg ' , ' .jpeg ' ) ) :
file_path + = ' .png '
# Save the image
with open ( file_path , ' wb ' ) as f :
f . write ( image_response . content )
saved_files . append ( {
' index ' : i + 1 ,
' path ' : file_path ,
' revised_prompt ' : getattr ( image_data , ' revised_prompt ' , None ) ,
' size_bytes ' : len ( image_response . content )
} )
} )
logger . info ( f " Successfully generated { len ( response . data ) } image(s) " )
logger . info ( f " Image { i + 1 } saved to: { file_path } " )
return f " Successfully generated { len ( response . data ) } image(s): \n \n " + " \n " . join ( [
f " Image { img [ ' index ' ] } : \n URL: { img [ ' url ' ] } \n Revised prompt: { img [ ' revised_prompt ' ] or ' N/A ' } "
except Exception as e :
for img in result [ " images " ]
logger . error ( f " Error saving image { i + 1 } : { str ( e ) } " )
] )
return f " Error saving image { i + 1 } : { str ( e ) } "
# Format success message
if len ( saved_files ) == 1 :
file_info = saved_files [ 0 ]
message = f " Successfully generated and saved image: \n "
message + = f " Path: { file_info [ ' path ' ] } \n "
message + = f " Size: { file_info [ ' size_bytes ' ] : , } bytes \n "
if file_info [ ' revised_prompt ' ] :
message + = f " Revised prompt: { file_info [ ' revised_prompt ' ] } \n "
else :
message = f " Successfully generated and saved { len ( saved_files ) } images: \n \n "
for file_info in saved_files :
message + = f " Image { file_info [ ' index ' ] } : \n "
message + = f " Path: { file_info [ ' path ' ] } \n "
message + = f " Size: { file_info [ ' size_bytes ' ] : , } bytes \n "
if file_info [ ' revised_prompt ' ] :
message + = f " Revised prompt: { file_info [ ' revised_prompt ' ] } \n "
message + = " \n "
logger . info ( f " Successfully generated and saved { len ( saved_files ) } image(s) " )
return message . strip ( )
except Exception as e :
except Exception as e :
logger . error ( f " DALL-E API error: { str ( e ) } " , exc_info = True )
logger . error ( f " DALL-E API error: { str ( e ) } " , exc_info = True )