get_context
Retrieve conversation context before or after a search result to understand the full discussion. Use after searching to see surrounding messages.
Instructions
Retrieve more conversation context around a specific search result. Use ONLY after calling search, when you need to see what was discussed before or after a result.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chunk_id | Yes | ||
| direction | No | ||
| count | No |
Implementation Reference
- src/tools/get-context.ts:11-85 (handler)The handler function that executes the get_context tool logic by fetching adjacent chunks from the database.
export async function handleGetContext( db: Database.Database, params: GetContextParams ): Promise<{ content: Array<{ type: string; text: string }> }> { if (!params.chunk_id) { return { content: [ { type: "text", text: JSON.stringify({ error: "chunk_id is required" }) }, ], }; } const direction = params.direction || "both"; const count = Math.min(params.count || 3, 10); // Look up the anchor chunk const anchor = db .prepare( "SELECT id, session_id, chunk_index, role, content, timestamp, turn_start, turn_end, token_count FROM chunks WHERE id = ?" ) .get(params.chunk_id) as ChunkRow | undefined; if (!anchor) { return { content: [ { type: "text", text: JSON.stringify({ error: "Chunk not found" }) }, ], }; } // Fetch adjacent chunks according to direction const beforeCount = direction === "after" ? 0 : count; const afterCount = direction === "before" ? 0 : count; const { before, after } = getAdjacentChunks(db, { session_id: anchor.session_id, chunk_index: anchor.chunk_index, before: beforeCount, after: afterCount, }); // Get session info for the anchor chunk const session = db .prepare( `SELECT s.session_id, p.path as project FROM sessions s JOIN projects p ON p.id = s.project_id WHERE s.id = ?` ) .get(anchor.session_id) as { session_id: string; project: string } | undefined; const result = { anchor: { chunk_id: anchor.id, content: anchor.content, timestamp: anchor.timestamp, role: anchor.role, }, before: before.map((c) => ({ chunk_id: c.id, content: c.content, timestamp: c.timestamp, role: c.role, })), after: after.map((c) => ({ chunk_id: c.id, content: c.content, timestamp: c.timestamp, role: c.role, })), session_id: session?.session_id || "", project: session?.project || "", }; return { content: [{ type: "text", text: JSON.stringify(result) }] }; } - src/tools/get-context.ts:5-9 (schema)Type definition for the parameters of the get_context tool.
export interface GetContextParams { chunk_id: number; direction?: "before" | "after" | "both"; count?: number; } - src/server.ts:48-60 (registration)Registration of the get_context tool in the MCP server.
// get_context tool server.tool( "get_context", "Retrieve more conversation context around a specific search result. Use ONLY after calling search, when you need to see what was discussed before or after a result.", { chunk_id: z.number(), direction: z.enum(["before", "after", "both"]).optional(), count: z.number().optional(), }, async (args): Promise<ToolResult> => { return handleGetContext(db, { chunk_id: args.chunk_id, direction: args.direction,