Skip to main content
Glama

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
NameRequiredDescriptionDefault
chunk_idYes
directionNo
countNo

Implementation Reference

  • 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) }] };
    }
  • 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,

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/hyunjae-labs/lore'

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