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,
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. While it mentions the tool retrieves context, it lacks details on permissions, rate limits, error handling, or what the output looks like (e.g., format, size limits). For a tool with no annotation coverage, this leaves significant gaps in understanding its behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is highly concise and front-loaded, with two sentences that directly state the purpose and usage guidelines without any wasted words. Every sentence earns its place by providing essential information efficiently.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (3 parameters, no output schema, no annotations), the description covers purpose and usage well but is incomplete. It lacks details on parameters, behavioral traits, and output format, which are necessary for full understanding. The description is adequate as a minimum but has clear gaps in context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate for undocumented parameters. It only vaguely references 'a specific search result' (implied to relate to 'chunk_id') and 'before or after a result' (implied to relate to 'direction'), but provides no specifics on parameter meanings, formats, or constraints. This fails to adequately explain the three parameters beyond basic schema hints.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Retrieve more conversation context') and resource ('around a specific search result'), distinguishing it from siblings like 'search' (which finds results) or 'list_sessions' (which lists sessions). It explicitly defines the tool's scope as fetching contextual conversation snippets.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool ('Use ONLY after calling search, when you need to see what was discussed before or after a result'), including a prerequisite (must call 'search' first) and a clear use-case (viewing surrounding context). It effectively differentiates from alternatives by specifying its post-search role.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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