Skip to main content
Glama

Gemini MCP Server

by mintmcqueen
batch-integration.md15.8 kB
# Gemini Batch API Integration - Implementation Complete ✅ **Status:** IMPLEMENTED in v0.3.0 **Date:** 2025-10-26 ## Overview Complete Gemini Batch API support with 11 tools: low-level control tools, high-level convenience tools, intelligent content ingestion, and embedding-specific workflows. All tools tested and operational. **⚠️ Breaking Change (v0.3.0):** Renamed `batch_upload_files` → `upload_multiple_files` to eliminate naming collision. The "batch" prefix is reserved exclusively for Gemini Batch API operations (async content generation at 50% cost). --- ## Tools Implemented (11 Total) ✅ ### Content Generation Batch Tools (5) **1. `batch_create` - Create content generation batch job** - **API Method:** `genAI.batches.create()` - Parameters (camelCase): - `model` (default: "gemini-2.5-flash") - `requests` (inline array, optional) - `inputFileUri` (from upload_file, optional) - `displayName` (optional) - `config` (optional: systemInstruction, temperature, etc.) - **Parameter Structure:** - Inline mode: `{model, requests: [...], config}` - File mode: `{model, src: fileName, config}` where fileName is string - Returns: Batch job object with `name`, state, estimated completion time **2. `batch_process` - Complete content generation workflow** - Parameters: - `inputFile` (path to any text file or JSONL) - `model` (default: "gemini-2.5-flash") - `displayName` (optional) - Workflow: 1. If NOT JSONL → call `batch_ingest_content` to convert 2. Upload JSONL via `upload_file` → get fileUri 3. Create batch job via batches.create() 4. Return job info (no polling in current implementation) - Returns: Batch job details with name for status tracking - **Note:** Field name is `fileUri` not `uri` (bug fixed in v0.3.0) **3. `batch_ingest_content` - Intelligent content ingestion & JSONL conversion** - Parameters: - `inputFile` (any structured/unstructured text file) - `outputFile` (optional, default: source_name + `.jsonl`) - `textField` (for CSV/JSON: field name containing text) - **JSONL Format:** `{"key": "...", "request": {"contents": [{"parts": [{"text": "..."}]}]}}` - Note: "contents" (plural) for content generation - Supports: CSV, JSON, TXT, MD - Returns: `{outputFile, requestCount, validationPassed, sourceFormat}` **4. `batch_get_status` - Get batch job status with optional auto-polling** - Parameters: - `batchName` (job ID from batch_create) - `autoPoll` (boolean, default: false) - renamed from wait_for_completion - If `autoPoll=true`: polls until terminal state - Returns: Batch job object with state, progress, stats **5. `batch_download_results` - Download and parse batch results** - Parameters: - `batchName` (required) - Downloads result file from Gemini File API - Parses JSONL/inline responses to structured format - Returns: Parsed results array + metadata ### Embedding Batch Tools (4) **⚠️ CRITICAL DIFFERENCES FROM CONTENT BATCHES:** **6. `batch_create_embeddings` - Create embeddings batch job** - **API Method:** `genAI.batches.createEmbeddings()` (NOT batches.create()) - Parameters: - `model` (default: "gemini-embedding-001") - `requests` (inline array, optional) - `inputFileUri` (from upload_file, optional) - `taskType` (REQUIRED - passed through but goes in JSONL, not batch config) - `displayName` (optional) - **Parameter Structure (DIFFERENT from content):** - Inline mode: `{model, src: {inlinedRequests: [...]}}` (object wrapper) - File mode: `{model, src: {fileName: ...}}` (object wrapper, not string) - **taskType Handling:** - taskType is passed to ensure consistency - Actual taskType must be in each JSONL request line - No automatic prompting (user must specify) - Supports all 8 task types: SEMANTIC_SIMILARITY, CLASSIFICATION, CLUSTERING, RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, CODE_RETRIEVAL_QUERY, QUESTION_ANSWERING, FACT_VERIFICATION - Returns: Batch job object with `name` **7. `batch_process_embeddings` - Complete embeddings workflow** - Parameters: - `inputFile` (path to any text file or JSONL) - `model` (default: "gemini-embedding-001") - `taskType` (REQUIRED) - `displayName` (optional) - Same workflow as batch_process but uses batch_create_embeddings - Returns: Batch job details for tracking **8. `batch_ingest_embeddings` - Content ingestion for embeddings** - Parameters: - `inputFile` (required) - `taskType` (REQUIRED - not optional) - `outputFile` (optional) - `textField` (for CSV/JSON: field name) - **JSONL Format (DIFFERENT):** `{"key": "...", "request": {"content": {"parts": [{"text": "..."}]}, "task_type": "..."}}` - Note: "content" (singular) + "task_type" field for embeddings - Returns: `{outputFile, requestCount, validationPassed, taskType}` **9. `batch_query_task_type` - Interactive task type selector** - Parameters: - `context` (optional - user describes use case) - `sampleContent` (optional - array of example texts) - Analyzes context and recommends task_type - Returns: `{selectedTaskType, recommendation: {confidence, reasoning}, taskTypeDescriptions}` - **Implementation:** Provides all 8 task type descriptions + recommendation based on context keywords ### Universal Batch Tools (2) **10. `batch_cancel` - Cancel running batch job** - Parameters: `batchName` - Calls: `genAI.batches.cancel()` - Returns: Cancellation confirmation **11. `batch_delete` - Delete batch job** - Parameters: `batchName` - Calls: `genAI.batches.delete()` - Returns: Deletion confirmation with job details --- ## Critical Implementation Details ### API Method Differences ⚠️ This was the root cause of our embeddings bug: | Operation | Correct Method | Parameters | |-----------|---------------|------------| | **Content Generation** | `batches.create()` | `{model, src: string \| requests: []}` | | **Embeddings** | `batches.createEmbeddings()` | `{model, src: {fileName: ...} \| {inlinedRequests: []}}` | ### Parameter Structure Comparison **Content Batches:** ```typescript // File mode genAI.batches.create({ model: "gemini-2.5-flash", src: "files/abc123", // STRING config: {...} }); // Inline mode genAI.batches.create({ model: "gemini-2.5-flash", requests: [...], // ARRAY at top level config: {...} }); ``` **Embeddings Batches:** ```typescript // File mode genAI.batches.createEmbeddings({ model: "gemini-embedding-001", src: { // OBJECT wrapper fileName: "files/abc123" } }); // Inline mode genAI.batches.createEmbeddings({ model: "gemini-embedding-001", src: { // OBJECT wrapper inlinedRequests: [...] } }); ``` ### JSONL Format Differences **Content Generation JSONL:** ```jsonl {"key": "request-1", "request": {"contents": [{"parts": [{"text": "Hello"}]}]}} {"key": "request-2", "request": {"contents": [{"parts": [{"text": "World"}]}]}} ``` - Field: `contents` (plural) - No task_type field **Embeddings JSONL:** ```jsonl {"key": "embed-1", "request": {"content": {"parts": [{"text": "Hello"}]}, "task_type": "RETRIEVAL_DOCUMENT"}} {"key": "embed-2", "request": {"content": {"parts": [{"text": "World"}]}, "task_type": "RETRIEVAL_DOCUMENT"}} ``` - Field: `content` (singular) - Required: `task_type` in each request ### Validation System The `validateJSONL()` function accepts both formats: ```typescript if (!parsed.request || (!parsed.request.contents && !parsed.request.content)) { errors.push(`Line ${index + 1}: missing required field`); } ``` --- ## Task Type Decision Matrix for Embeddings | Task Type | Purpose | Use Cases | |-----------|---------|-----------| | **SEMANTIC_SIMILARITY** | "Embeddings optimized to assess text similarity" | Recommendation systems, duplicate detection | | **CLASSIFICATION** | "Embeddings optimized to classify texts according to preset labels" | Sentiment analysis, spam detection | | **CLUSTERING** | "Embeddings optimized to cluster texts based on their similarities" | Document organization, market research, anomaly detection | | **RETRIEVAL_DOCUMENT** | "Embeddings optimized for document search" | Indexing articles, books, or web pages | | **RETRIEVAL_QUERY** | "Embeddings optimized for general search queries" | Custom search (pair with RETRIEVAL_DOCUMENT for documents) | | **CODE_RETRIEVAL_QUERY** | "Embeddings optimized for retrieval of code blocks based on natural language queries" | Code suggestions and search (pair with RETRIEVAL_DOCUMENT) | | **QUESTION_ANSWERING** | "Embeddings for questions in a question-answering system" | Chatbots (use for questions; pair with RETRIEVAL_DOCUMENT) | | **FACT_VERIFICATION** | "Embeddings for statements that need to be verified" | Automated fact-checking (use for statements; pair with RETRIEVAL_DOCUMENT) | --- ## Implementation Status ### Type Definitions (`src/types/gemini.ts`) ✅ **Implemented:** - `BatchJob` interface (lines 88-110) - `BatchJobState` enum (lines 79-86) - `EmbeddingTaskType` enum (lines 112-121) - `ContentIngestionReport` interface (lines 123-132) - `BatchCreateParams` interface (lines 134-146) - `BatchEmbeddingParams` interface (lines 148-155) ### Helper Functions (`src/index.ts`) ✅ **Implemented:** - `validateJSONL()` - Lines 1114-1132 - Validates both content and embeddings JSONL formats - `convertToJSONL()` - Integrated into ingestion handlers - Content/embeddings ingestion logic in respective handlers ### Tool Handlers (`src/index.ts`) ✅ **Content Generation:** - `handleBatchIngestContent()` - Lines 2463-2542 - `handleBatchCreate()` - Lines 2228-2284 - `handleBatchProcess()` - Lines 2133-2226 - `handleBatchGetStatus()` - Lines 2286-2326 - `handleBatchDownloadResults()` - Lines 2419-2461 **Embeddings:** - `handleBatchIngestEmbeddings()` - Lines 2544-2653 - `handleBatchCreateEmbeddings()` - Lines 2328-2381 - `handleBatchProcessEmbeddings()` - Lines 2383-2417 - `handleBatchQueryTaskType()` - Lines 2655-2719 **Universal:** - `handleBatchCancel()` - Lines 2721-2751 - `handleBatchDelete()` - Lines 2753-2791 ### Tool Schema Definitions ✅ All 11 tools added to tools array (lines 625-902) with comprehensive descriptions and parameter schemas. --- ## Testing Results ✅ ### Individual Tool Tests (11/11 PASSING) ``` ✓ batch_ingest_content - CSV input ✓ batch_ingest_content - JSON input ✓ batch_ingest_content - TXT input ✓ batch_ingest_embeddings - TXT input ✓ upload_file - Upload JSONL file ✓ batch_create - Create content batch job ✓ batch_get_status - Check batch job status ✓ batch_query_task_type - Get task type recommendation ✓ batch_cancel - Cancel batch job ✓ batch_delete - Delete batch job ✓ batch_download_results - Error handling verification ``` ### Workflow Tests (5/5 PASSING) ``` ✓ Manual Workflow - Content Generation (5 steps) ✓ Automated Workflow - batch_process (single call) ✓ Manual Workflow - Embeddings (6 steps) 🎉 ✓ Workflow Data Integrity - JSONL Format Validation ✓ Job Management - Cancel and Delete ``` **Key Success:** Embeddings workflow fully operational after fixing API method and parameter structure bugs. --- ## Expected User Experience ### Example 1: Simple Batch Content Generation from CSV ```javascript // User has a CSV file with prompts const ingest = await batch_ingest_content({ inputFile: "prompts.csv", outputFile: "prompts.jsonl" }); const upload = await upload_file({ filePath: ingest.outputFile }); const batch = await batch_create({ model: "gemini-2.5-flash", inputFileUri: upload.fileUri, displayName: "CSV Batch Job" }); // Returns: { // batchName: "batches/xyz789...", // state: "JOB_STATE_PENDING", // message: "Batch job created successfully" // } ``` ### Example 2: Embeddings with Task Type ```javascript const ingest = await batch_ingest_embeddings({ inputFile: "documents.txt", taskType: "RETRIEVAL_DOCUMENT", outputFile: "embeddings.jsonl" }); const upload = await upload_file({ filePath: ingest.outputFile }); const batch = await batch_create_embeddings({ model: "gemini-embedding-001", inputFileUri: upload.fileUri, taskType: "RETRIEVAL_DOCUMENT", displayName: "Document Embeddings" }); // Console output shows: // [Batch Embeddings] Creating embeddings batch job... // [Batch Embeddings] Using file-based mode with file: files/abc123 // batches.createEmbeddings() is experimental and may change without notice. // [Batch Embeddings] Batch job created: batches/xyz789 ``` ### Example 3: Task Type Recommendation ```javascript const recommendation = await batch_query_task_type({ context: "Building a document search engine", sampleContent: ["How to train neural networks", "Machine learning basics"] }); // Returns: { // selectedTaskType: "RETRIEVAL_DOCUMENT", // recommendation: { // confidence: 0.9, // reasoning: "Context mentions search and documents - RETRIEVAL_DOCUMENT is optimal" // }, // taskTypeDescriptions: { // SEMANTIC_SIMILARITY: "Embeddings optimized to assess text similarity", // RETRIEVAL_DOCUMENT: "Embeddings optimized for document search", // ... (all 8 types) // } // } ``` --- ## Cost Analysis ### Pricing Comparison | Operation | Standard API | Batch API | Savings | |-----------|-------------|-----------|---------| | 1,000 prompts | $X | $X/2 | 50% | | 10,000 embeddings | $Y | $Y/2 | 50% | | 100,000 requests | $Z | $Z/2 | 50% | ### When to Use Batch API ✅ **Use Batch API when:** - Processing >100 requests - No urgency (can wait 1-24 hours) - Cost optimization is priority - Running evaluations/benchmarks - Bulk data preprocessing ❌ **Use Standard API when:** - Need immediate results - Interactive user-facing application - <100 requests - Real-time responses required --- ## Known Issues & Solutions ✅ ### Issue 1: Embeddings Batch API Failure (FIXED in v0.3.0) **Symptom:** `"models/gemini-embedding-001 is not found for API version v1beta, or is not supported for batchGenerateContent"` **Root Cause:** Using `batches.create()` (content method) instead of `batches.createEmbeddings()` **Solution:** - Use correct API method: `genAI.batches.createEmbeddings()` - Use correct parameter structure: `src: {fileName: ...}` not `src: string` ### Issue 2: JSONL Format Confusion (FIXED in v0.3.0) **Symptom:** Validation failing for embeddings JSONL **Root Cause:** Content uses "contents" (plural), embeddings use "content" (singular) + "task_type" **Solution:** - Updated `validateJSONL()` to accept both formats - batch_ingest_embeddings generates correct format with task_type field ### Issue 3: Field Name Mismatch (FIXED in v0.3.0) **Symptom:** batch_process failing to find fileUri **Root Cause:** upload_file returns `fileUri` but code checked for `uri` **Solution:** Changed to correct field name `fileUri` (line 2196) --- ## Version History - **v0.2.11**: File upload, chat, conversations - **v0.3.0**: ✅ Batch API integration complete - **BREAKING CHANGE**: `batch_upload_files` renamed to `upload_multiple_files` - Added 11 new Batch API tools (all tested and operational) - Fixed embeddings API method (batches.createEmbeddings) - Fixed parameter structures for file/inline modes - Fixed JSONL format for embeddings (content + task_type) - Added comprehensive E2E test suite (11 individual + 5 workflow tests) - Cost savings: 50% vs standard API for batch operations --- ## Future Enhancements ### Potential Improvements - Add persistent batch job tracking across server restarts - Add batch job list/history resource - Add retry logic for failed batch requests within a job - Add batch job scheduling - Add cost estimation before batch creation - Add progress notifications via webhooks - Add streaming support for large result downloads --- **Last Updated:** 2025-10-26 **Status:** Implementation Complete & Tested ✅ **Version:** v0.3.0

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mintmcqueen/gemini-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server