chat_with_vision
Analyze images and answer questions about visual content using AI vision capabilities. Upload images or provide URLs to extract information, identify objects, or describe scenes through text prompts.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| session | No | ||
| model | No | grok-4-1-fast-reasoning | |
| image_paths | No | ||
| image_urls | No | ||
| detail | No | auto |
Implementation Reference
- src/server.py:199-241 (handler)The handler function for the chat_with_vision MCP tool, which interacts with the XAI client to process text prompts and optional images.
async def chat_with_vision( prompt: str, session: Optional[str] = None, model: str = "grok-4-1-fast-reasoning", image_paths: Optional[List[str]] = None, image_urls: Optional[List[str]] = None, detail: str = "auto" ): history = load_history(session) if session else [] client = Client(api_key=XAI_API_KEY) chat = client.chat.create(model=model, store_messages=False) for message in history: if message["role"] == "user": chat.append(user(message["content"])) elif message["role"] == "assistant": chat.append(assistant(message["content"])) user_content = [] if image_paths: for path in image_paths: ext = Path(path).suffix.lower().replace('.', '') if ext not in ["jpg", "jpeg", "png"]: raise ValueError(f"Unsupported image type: {ext}") base64_img = encode_image_to_base64(path) user_content.append(image(image_url=f"data:image/{ext};base64,{base64_img}", detail=detail)) if image_urls: for url in image_urls: user_content.append(image(image_url=url, detail=detail)) user_content.append(prompt) chat.append(user(*user_content)) response = chat.sample() client.close() if session: history.append({"role": "user", "content": prompt, "time": datetime.now().strftime("%d.%m.%Y %H:%M:%S")}) history.append({"role": "assistant", "content": response.content, "time": datetime.now().strftime("%d.%m.%Y %H:%M:%S")}) save_history(session, history) return response.content - src/server.py:198-198 (registration)Registration of the chat_with_vision tool using the @mcp.tool decorator.
@mcp.tool()