m9k_context
Retrieve a document chunk with neighboring chunks from the same session. Use after searching to understand the conversation flow.
Instructions
Get a chunk with surrounding context (adjacent chunks in the same session). Use after m9k_search to understand the conversation flow.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chunkId | Yes | The chunk ID to get context for | |
| window | No | Number of chunks before/after to include |
Implementation Reference
- src/tools/search.ts:69-127 (handler)Registration and handler for the m9k_context tool. Accepts chunkId and optional window parameter. Queries the conv_chunks table for the target chunk, then retrieves adjacent chunks in the same session to provide surrounding conversation context.
server.registerTool( 'm9k_context', { description: 'Get a chunk with surrounding context (adjacent chunks in the same session). Use after m9k_search to understand the conversation flow.', inputSchema: { chunkId: z.string().describe('The chunk ID to get context for'), window: z .number() .int() .min(1) .max(10) .default(3) .describe('Number of chunks before/after to include'), }, annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async ({ chunkId, window }) => { const target = ctx.db .prepare('SELECT session_id, idx FROM conv_chunks WHERE id = ? AND deleted_at IS NULL') .get(chunkId) as { session_id: string; idx: number } | undefined; if (!target) { return { content: [ { type: 'text' as const, text: JSON.stringify({ error: 'Chunk not found', chunkId }), }, ], isError: true, }; } const minIdx = Math.max(0, target.idx - window); const maxIdx = target.idx + window; const rows = ctx.db .prepare( `SELECT id, session_id, idx, kind, user_content, assistant_content, hash, timestamp, token_count, tags, metadata_json FROM conv_chunks WHERE session_id = ? AND idx BETWEEN ? AND ? AND deleted_at IS NULL ORDER BY idx`, ) .all(target.session_id, minIdx, maxIdx); return { content: [ { type: 'text' as const, text: JSON.stringify({ target: chunkId, chunks: rows }), }, ], }; }, ); - src/tools/search.ts:69-89 (schema)Input schema for m9k_context: chunkId (string, required) and window (number, 1-10, default 3) defining how many chunks before/after to include.
server.registerTool( 'm9k_context', { description: 'Get a chunk with surrounding context (adjacent chunks in the same session). Use after m9k_search to understand the conversation flow.', inputSchema: { chunkId: z.string().describe('The chunk ID to get context for'), window: z .number() .int() .min(1) .max(10) .default(3) .describe('Number of chunks before/after to include'), }, annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, - src/tools/search.ts:69-127 (registration)Tool registration via server.registerTool('m9k_context', ...) with schema, annotations (readOnly, idempotent), and async handler. Exported by registerSearchTools function.
server.registerTool( 'm9k_context', { description: 'Get a chunk with surrounding context (adjacent chunks in the same session). Use after m9k_search to understand the conversation flow.', inputSchema: { chunkId: z.string().describe('The chunk ID to get context for'), window: z .number() .int() .min(1) .max(10) .default(3) .describe('Number of chunks before/after to include'), }, annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async ({ chunkId, window }) => { const target = ctx.db .prepare('SELECT session_id, idx FROM conv_chunks WHERE id = ? AND deleted_at IS NULL') .get(chunkId) as { session_id: string; idx: number } | undefined; if (!target) { return { content: [ { type: 'text' as const, text: JSON.stringify({ error: 'Chunk not found', chunkId }), }, ], isError: true, }; } const minIdx = Math.max(0, target.idx - window); const maxIdx = target.idx + window; const rows = ctx.db .prepare( `SELECT id, session_id, idx, kind, user_content, assistant_content, hash, timestamp, token_count, tags, metadata_json FROM conv_chunks WHERE session_id = ? AND idx BETWEEN ? AND ? AND deleted_at IS NULL ORDER BY idx`, ) .all(target.session_id, minIdx, maxIdx); return { content: [ { type: 'text' as const, text: JSON.stringify({ target: chunkId, chunks: rows }), }, ], }; }, ); - src/tools/search.ts:1-9 (helper)Imports and setup for the search module: zod for schema validation, McpServer type, getStat for DB stats, search function, and ToolContext interface.
/** * Progressive retrieval tools: m9k_search, m9k_context, m9k_full. */ import { z } from 'zod'; import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { getStat } from '../db.js'; import { search } from '../search.js'; import type { ToolContext } from './context.js';