Skip to main content
Glama

codebase_graph_query

Query the code dependency graph for a specific file to see what it imports and which files depend on it.

Instructions

Query the code dependency graph for a specific file. Returns what the file imports and what files depend on it.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
projectPathNoAbsolute path to the project directory.
filePathYesRelative path of the file to query (e.g. 'src/index.ts').

Implementation Reference

  • src/index.ts:190-203 (registration)
    MCP server registration for the 'codebase_graph_query' tool. Defines the tool name, description, and Zod schema for inputs (projectPath optional, filePath required). Delegates execution to handleGraphTool('codebase_graph_query', args).
    server.tool(
      "codebase_graph_query",
      "Query the code dependency graph for a specific file. Returns what the file imports and what files depend on it.",
      {
        projectPath: z
          .string()
          .describe("Absolute path to the project directory.")
          .optional(),
        filePath: z.string().describe("Relative path of the file to query (e.g. 'src/index.ts')."),
      },
      async (args) => ({
        content: [{ type: "text", text: await handleGraphTool("codebase_graph_query", args) }],
      }),
    );
  • Handler for codebase_graph_query: extracts filePath from args, gets or builds the graph via getOrBuildGraph, calls getFileDependencies, and formats the output showing imports (upstream) and importedBy (downstream) with arrow indicators.
    case "codebase_graph_query": {
      const filePath = args.filePath as string;
      const graph = await getOrBuildGraph(projectPath);
      const deps = getFileDependencies(graph, filePath);
    
      const lines = [`Dependencies for: ${filePath}\n`];
    
      if (deps.imports.length === 0 && deps.importedBy.length === 0) {
        lines.push("No dependency information found for this file.");
        lines.push("Make sure codebase_graph_build has been run and the file path is relative.");
      } else {
        if (deps.imports.length > 0) {
          lines.push(`Imports (${deps.imports.length}):`);
          for (const imp of deps.imports) {
            lines.push(`  → ${imp}`);
          }
        }
    
        if (deps.importedBy.length > 0) {
          lines.push(`\nImported by (${deps.importedBy.length}):`);
          for (const dep of deps.importedBy) {
            lines.push(`  ← ${dep}`);
          }
        }
      }
    
      return lines.join("\n");
    }
  • getFileDependencies helper function: finds the graph node by relativePath and returns its dependencies (imports) and dependents (importedBy) arrays.
    export function getFileDependencies(graph: CodeGraph, relativePath: string): {
      imports: string[];
      importedBy: string[];
    } {
      const node = graph.nodes.find((n) => n.relativePath === relativePath);
      if (!node) {
        return { imports: [], importedBy: [] };
      }
      return {
        imports: node.dependencies,
        importedBy: node.dependents,
      };
    }
  • getOrBuildGraph helper: returns cached graph if available, otherwise loads from Qdrant persistence, or builds a new code graph as a fallback.
    export async function getOrBuildGraph(
      projectPath: string,
      extraExtensions?: Set<string>,
    ): Promise<CodeGraph> {
      const resolved = path.resolve(projectPath);
      const cached = graphCache.get(resolved);
      if (cached) {
        return cached;
      }
    
      // Try loading persisted graph from Qdrant
      const projectId = projectIdFromPath(resolved);
      const graphCollName = graphCollectionName(projectId);
      const persisted = await loadGraphData(graphCollName);
      if (persisted) {
        graphCache.set(resolved, persisted);
        return persisted;
      }
    
      const graph = await buildCodeGraph(resolved, extraExtensions);
      // Strip symbol fields when serving as a plain CodeGraph
      const plain: CodeGraph = { nodes: graph.nodes, edges: graph.edges };
      graphCache.set(resolved, plain);
      return plain;
    }
Behavior3/5

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

No annotations provided, so description carries burden. It explains what is returned but omits side effects, performance, or preconditions like graph existence.

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?

Extremely concise at two sentences, front-loaded with purpose, no wasted words.

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

Completeness4/5

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

Good for a simple query tool; describes output but lacks mention of preconditions like graph building and error handling. No output schema, so slight gap.

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

Parameters3/5

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

Schema coverage is 100% with descriptions. Description adds no extra meaning beyond the schema, so baseline score of 3.

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?

Clearly states the tool queries the code dependency graph for a specific file and describes the output (imports and dependents). Distinguishes from sibling tools like build, circular, etc.

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

Usage Guidelines3/5

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

Implies usage for querying dependencies but does not explicitly state when to use vs other graph tools or mention prerequisites (e.g., graph must be built).

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/giancarloerra/SocratiCode'

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