chat_with_files
Query documents and files using natural language prompts to extract insights, analyze content, and answer questions based on uploaded materials.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | grok-4-1-fast | |
| file_ids | No | ||
| system_prompt | No |
Implementation Reference
- src/server.py:509-533 (handler)The chat_with_files tool handler that creates a chat session with XAI, optionally adds a system prompt, attaches files by their IDs, sends the user prompt, and returns the response with citations.@mcp.tool() async def chat_with_files( prompt: str, model: str = "grok-4-1-fast", file_ids: List[str] = None, system_prompt: Optional[str] = None ): client = Client(api_key=XAI_API_KEY) chat = client.chat.create(model=model) if system_prompt: chat.append(system(system_prompt)) file_attachments = [file(fid) for fid in file_ids] chat.append(user(prompt, *file_attachments)) response = chat.sample() client.close() result = [response.content] if response.citations: result.append("\n\n**Sources:**") for url in response.citations: result.append(f"- {url}") return "\n".join(result)
- src/server.py:1-8 (schema)Import statements including type hints (List, Optional) and XAI SDK imports needed for chat_with_files functionality.from pathlib import Path from typing import List, Optional from datetime import datetime from mcp.server.fastmcp import FastMCP from xai_sdk import Client from xai_sdk.chat import user, system, image, file from xai_sdk.tools import web_search as xai_web_search, x_search as xai_x_search, code_execution from .utils import encode_image_to_base64, encode_video_to_base64, build_params, XAI_API_KEY
- src/server.py:509-509 (registration)Tool registration using the @mcp.tool() decorator that exposes chat_with_files as an MCP tool.@mcp.tool()