Skip to main content
Glama
cskwork

Knowledge Retrieval Server

by cskwork

get-chunk-with-context

Retrieve specific content chunks with surrounding context from documents using document and chunk IDs. Adjust window size to control context scope, aiding precise information extraction.

Instructions

Get specific chunk with surrounding context.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chunkIdYesChunk ID
documentIdYesDocument ID
windowSizeNoContext window size (default: 1)

Implementation Reference

  • The handler function for the 'get-chunk-with-context' tool. It retrieves the document by ID from the repository, calls getChunkWithWindow on the document to get the chunk and context, handles errors if not found, and returns the concatenated chunk texts as MCP TextContent.
    case 'get-chunk-with-context': { const { documentId, chunkId, windowSize } = args as { documentId: number; chunkId: number; windowSize?: number; }; const document = repository.getDocumentById(documentId); if (!document) { const content: TextContent[] = [{ type: 'text', text: `Document with ID ${documentId} not found.` }]; return { content }; } const chunks = document.getChunkWithWindow(chunkId, windowSize || 1); if (chunks.length === 0) { const content: TextContent[] = [{ type: 'text', text: `Chunk with ID ${chunkId} not found.` }]; return { content }; } const content = chunks.map(chunk => chunk.text).join('\n\n---\n\n'); const textContent: TextContent[] = [{ type: 'text', text: content }]; return { content: textContent }; }
  • src/index.ts:261-283 (registration)
    Tool registration in the listTools response, including name, description, and input schema.
    { name: 'get-chunk-with-context', description: 'Get specific chunk with surrounding context.', inputSchema: { type: 'object', properties: { documentId: { type: 'number', description: 'Document ID' }, chunkId: { type: 'number', description: 'Chunk ID' }, windowSize: { type: 'number', description: 'Context window size (default: 1)', default: 1 } }, required: ['documentId', 'chunkId'] } }
  • Input schema definition for the tool, specifying parameters documentId, chunkId, and optional windowSize.
    { name: 'get-chunk-with-context', description: 'Get specific chunk with surrounding context.', inputSchema: { type: 'object', properties: { documentId: { type: 'number', description: 'Document ID' }, chunkId: { type: 'number', description: 'Chunk ID' }, windowSize: { type: 'number', description: 'Context window size (default: 1)', default: 1 } }, required: ['documentId', 'chunkId'] } }
  • Core helper method in KnowledgeDocument class that implements the chunk retrieval with context window: finds chunk index and slices surrounding chunks.
    getChunkWithWindow(chunkId: number, windowSize: number): DocumentChunk[] { const chunkIndex = this.chunks.findIndex( (chunk) => chunk.chunkId === chunkId ); if (chunkIndex === -1) { return []; } const start = Math.max(0, chunkIndex - windowSize); const end = Math.min(this.chunks.length, chunkIndex + windowSize + 1); return this.chunks.slice(start, end); }
  • Helper method in DocumentRepository to retrieve a document by its ID from the internal Map.
    getDocumentById(id: number): KnowledgeDocument | null { this.ensureInitialized(); return this.documents.get(id) || null; }

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/cskwork/keyword-rag-mcp'

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