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
| Name | Required | Description | Default |
|---|---|---|---|
| chunkId | Yes | Chunk ID | |
| documentId | Yes | Document ID | |
| windowSize | No | Context window size (default: 1) |
Implementation Reference
- src/index.ts:383-417 (handler)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'] } }
- src/index.ts:261-283 (schema)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'] } }
- src/models/Document.ts:58-71 (helper)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; }