get-chunk-with-context
Retrieve a specific document chunk with surrounding context to understand content in relation to adjacent information.
Instructions
Get specific chunk with surrounding context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| documentId | Yes | Document ID | |
| chunkId | Yes | Chunk 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, fetches the specified chunk with context using getChunkWithWindow, handles errors, and formats the response as text content.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:264-282 (schema)Input schema definition for the 'get-chunk-with-context' tool, specifying parameters: documentId (required), chunkId (required), windowSize (optional, default 1).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:262-283 (registration)Tool registration in the listTools response, including name, description, and inputSchema.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 implementing the chunk retrieval with context window. Finds the target chunk by ID, then returns surrounding chunks within the specified window size.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); }