Skip to main content
Glama
kazuph

MCP Docs RAG Server

by kazuph

rag_query

Query documents with context using a Retrieval-Augmented Generation (RAG) system. Automatically creates an index if it does not exist, enabling quick access to relevant information from stored repositories and text files.

Instructions

Query a document using RAG. Note: If the index does not exist, it will be created when you query, which may take some time.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
document_idYesID of the document to query
queryYesQuery to run against the document

Implementation Reference

  • Main handler for 'rag_query' tool call. Validates inputs, checks document existence, loads/indexes document, sets up Gemini LLM and query engine using LlamaIndex, executes query, handles errors.
    case "rag_query": {
      const documentId = String(request.params.arguments?.document_id);
      const query = String(request.params.arguments?.query);
      
      if (!documentId || !query) {
        throw new Error("Document ID and query are required");
      }
      
      try {
        // ドキュメントが存在するか確認し、存在しなければ自動的に作成を試みる
        let documents = await listDocuments();
        let document = documents.find(c => c.id === documentId);
        
        if (!document) {
          return {
            content: [{
              type: "text",
              text: `Document '${documentId}' not found. Please add it manually using add_git_repository or add_text_file tools.`
            }]
          };
        }
        
        // Load and index document if needed
        const index = await loadDocument(documentId);
      
      // 一時的にGemini LLMを設定
      const originalLLM = Settings.llm;
      const gemini = new Gemini({
        model: GEMINI_MODEL.GEMINI_2_0_FLASH
      });
      
      // グローバル設定に設定
      Settings.llm = gemini;
      
      // クエリエンジンの作成
      const queryEngine = index.asQueryEngine();
      
      // クエリの実行
      const response = await queryEngine.query({
        query
      });
      
      return {
        content: [{
          type: "text",
          text: response.toString()
        }]
      };
      } catch (error: any) {
        console.error(`Error in rag_query:`, error.message);
        return {
          content: [{
            type: "text",
            text: `Error processing query: ${error.message}`
          }]
        };
      }
    }
  • src/index.ts:371-388 (registration)
    Registration of 'rag_query' tool in ListToolsRequestSchema handler, including name, description, and input schema definition.
    {
      name: "rag_query",
      description: "Query a document using RAG. Note: If the index does not exist, it will be created when you query, which may take some time.",
      inputSchema: {
        type: "object",
        properties: {
          document_id: {
            type: "string",
            description: "ID of the document to query"
          },
          query: {
            type: "string",
            description: "Query to run against the document"
          }
        },
        required: ["document_id", "query"]
      }
    },
  • Input schema for rag_query tool defining required document_id (string) and query (string) parameters.
    inputSchema: {
      type: "object",
      properties: {
        document_id: {
          type: "string",
          description: "ID of the document to query"
        },
        query: {
          type: "string",
          description: "Query to run against the document"
        }
      },
      required: ["document_id", "query"]
    }
  • Key helper function called by rag_query handler. Loads document files recursively, creates and persists VectorStoreIndex using LlamaIndex with Gemini embedding/LLM, caches in global indices.
    export async function loadDocument(documentId: string): Promise<VectorStoreIndex> {
      if (indices[documentId]?.index) {
        return indices[documentId].index;
      }
    
      let documents = await listDocuments();
      let document = documents.find(c => c.id === documentId);
      
      // ドキュメントが存在しない場合はエラーをスロー
      if (!document) {
        throw new Error(`Document not found: ${documentId}`);
      }
    
      let documentItems: Document[] = [];
      
      if (fs.statSync(document.path).isDirectory()) {
        // ディレクトリを再帰的に処理
        documentItems = await readDirectoryRecursively(document.path);
        
        // 空のドキュメントリストの場合にフォールバックメッセージを追加
        if (documentItems.length === 0) {
          console.warn(`No documents found in document: ${documentId}`);
          documentItems.push(new Document({ 
            text: `This document (${document.name}) appears to be empty. Please check if files exist at path: ${document.path}`, 
            metadata: { name: 'empty-notice', source: document.path } 
          }));
        }
      } else {
        // Process single file
        const text = fs.readFileSync(document.path, 'utf-8');
        documentItems = [new Document({ text, metadata: { name: document.id, source: document.path } })];
      }
      
      // Gemini埋め込みモデルを設定
      const geminiEmbed = new GeminiEmbedding();
      
      // Gemini LLMモデルを設定
      const gemini = new Gemini({
        model: GEMINI_MODEL.GEMINI_2_0_FLASH
      });
      
      // グローバル設定に埋め込みモデルとLLMを設定(これをグローバルに保持)
      Settings.embedModel = geminiEmbed;
      Settings.llm = gemini;
      
      // Create storage context
      const storageContext = await storageContextFromDefaults({
        persistDir: path.join(DOCS_PATH, '.indices', documentId),
      });
      
      // Create index
      const index = await VectorStoreIndex.fromDocuments(documentItems, {
        storageContext,
      });
      
      // Save index for future use
      indices[documentId] = { 
        index, 
        description: document.description 
      };
      
      return index;
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It helpfully reveals that 'If the index does not exist, it will be created when you query, which may take some time' - this is valuable behavioral context about performance implications and automatic index creation that isn't obvious from the tool name or schema alone.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately concise with two sentences. The first states the core purpose, the second provides important behavioral context. Both sentences earn their place, though the structure could be slightly improved by front-loading the behavioral note more clearly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a query tool with 2 parameters and no output schema, the description provides adequate context about the core operation and an important behavioral note. However, it doesn't describe what the query returns (format, content), error conditions, or authentication requirements, leaving some gaps in completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already fully documents both parameters (document_id and query). The description doesn't add any additional parameter semantics beyond what the schema provides, such as query format examples or document_id constraints. Baseline 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose as 'Query a document using RAG' (verb+resource). It distinguishes from siblings like 'add_git_repository' and 'add_text_file' by focusing on querying rather than adding content. However, it doesn't explicitly differentiate from 'list_documents' in terms of querying vs listing.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides some usage context with the note about index creation, implying this tool should be used for querying documents with RAG. However, it doesn't explicitly state when to use this vs alternatives like 'list_documents' or provide clear when-not-to-use guidance beyond the performance implication mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

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/kazuph/mcp-docs-rag'

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