Commit d6b0a24
committed
@mcp.tool
def upload_and_index(session_id: str) -> str:
"""Upload JSON to Gemini File Search deterministically."""
s = _session(session_id)
json_path = s.get("json_path")
if not json_path or not os.path.exists(json_path):
raise ValueError("No JSON found. Run convert_to_json first.")
# --- Create the File Search Store with no args to see what happens ---
try:
store_obj = client.file_search_stores.create()
except TypeError as e:
# If it fails, try to get helpful error message about what it expects
raise TypeError(f"create() signature issue: {e}. Try checking client.file_search_stores.create.__doc__ or __signature__")
# Extract the store name
if hasattr(store_obj, 'name'):
store_name = store_obj.name
elif isinstance(store_obj, dict) and 'name' in store_obj:
store_name = store_obj['name']
else:
raise TypeError(f"Cannot extract name. Type: {type(store_obj)}, Content: {store_obj}")
# --- Upload to File Search Store ---
with open(json_path, 'rb') as f:
op = client.file_search_stores.upload_to_file_search_store(
file_search_store_name=store_name,
file=f,
config={
"display_name": os.path.basename(json_path),
"mime_type": "application/json"
}
)
# --- Poll until indexing is complete ---
while not getattr(op, 'done', False):
time.sleep(2)
op = client.operations.get(name=op.name)
s["store_name"] = store_name
return f"✅ Uploaded and indexed {json_path} to Gemini File Search store: {store_name}"1 parent ba1875c commit d6b0a24
1 file changed
+6
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
| |||
0 commit comments