Skip to main content
Glama

query-knowledge-graph

Extract and analyze code context by querying a knowledge graph, enabling insights into repository structures, dependencies, and external references in text, JSON, or visualization formats.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contextDepthNo
includeExternalKnowledgeNo
outputFormatNojson
queryYes
repositoryUrlNo

Implementation Reference

  • Core implementation of the knowledge graph query logic. Traverses the graph starting from the repository node, expanding relationships up to the specified context depth, using database helper functions to fetch nodes and relationships.
    export async function queryKnowledgeGraph(query: GraphQuery): Promise<GraphQueryResult> { // Simple implementation for demonstration // A real implementation would use a graph query language const { repositoryUrl, contextDepth = 1 } = query; // Start with a basic implementation that returns nodes related to a repository let nodes: GraphNode[] = []; let relationships: GraphRelationship[] = []; if (repositoryUrl) { // Get all nodes and relationships for the repository const repoNodes = await findNodesByAttribute("url", repositoryUrl); if (repoNodes.length === 0) { return { nodes: [], relationships: [] }; } const repoId = repoNodes[0].id; // Get direct relationships const directRelationships = await findRelationshipsBySourceId(repoId); relationships.push(...directRelationships); // Get target nodes of direct relationships const directNodeIds = directRelationships.map(rel => rel.targetId); const directNodes = await findNodesById(directNodeIds); nodes.push(...repoNodes, ...directNodes); // If depth > 1, get additional levels of relationships if (contextDepth > 1) { for (let i = 1; i < contextDepth; i++) { const currentNodeIds = nodes.map(node => node.id); const relationshipsResult = await findRelationshipsBySourceIds(currentNodeIds); const nextRelationships = relationshipsResult.filter((rel: GraphRelationship) => !relationships.some(r => r.id === rel.id)); if (nextRelationships.length === 0) { break; } relationships.push(...nextRelationships); const nextNodeIds = nextRelationships.map((rel: GraphRelationship) => rel.targetId) .filter((id: string) => !nodes.some(node => node.id === id)); if (nextNodeIds.length === 0) { break; } const nextNodes = await findNodesById(nextNodeIds); nodes.push(...nextNodes); } } } return { nodes, relationships }; }
  • MCP tool registration for 'query-knowledge-graph'. Defines input schema using Zod and provides the top-level handler that processes parameters, calls the core queryKnowledgeGraph function, handles output formatting, and returns MCP-compatible response.
    server.tool( "query-knowledge-graph", { query: z.string(), repositoryUrl: z.string().optional(), contextDepth: z.number().default(2), includeExternalKnowledge: z.boolean().default(true), outputFormat: z.enum(["text", "json", "visualization"]).default("json") }, async ({ query, repositoryUrl, contextDepth, includeExternalKnowledge, outputFormat }) => { try { const results = await queryKnowledgeGraph({ query, repositoryUrl, contextDepth, includeExternalKnowledge }); if (outputFormat === "visualization") { // Convert results to a visualization const visualization = await exportKnowledgeGraph(results, "mermaid"); return { content: [{ type: "text", text: visualization, _metadata: { format: "mermaid" } }] }; } if (outputFormat === "text") { // Format results as descriptive text return { content: [{ type: "text", text: formatGraphResultsAsText(results) }] }; } return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Error querying knowledge graph: ${(error as Error).message}` }], isError: true }; } } );
  • TypeScript type definitions for knowledge graph entities: GraphNode, GraphRelationship, GraphQuery, and GraphQueryResult, used throughout the tool implementation for type safety.
    /** * Represents a node in the knowledge graph */ export interface GraphNode { id: string; type: "function" | "file" | "class" | "variable" | "dependency" | "concept" | "repository"; name: string; attributes: Record<string, any>; } /** * Represents a relationship between nodes in the knowledge graph */ export interface GraphRelationship { id: string; type: "imports" | "calls" | "defines" | "extends" | "implements" | "uses" | "contains" | "relates_to"; sourceId: string; targetId: string; attributes: Record<string, any>; } /** * Structure for query results from the knowledge graph */ export interface GraphQueryResult { nodes: GraphNode[]; relationships: GraphRelationship[]; } /** * Parameters for querying the knowledge graph */ export interface GraphQuery { query: string; repositoryUrl?: string; contextDepth?: number; includeExternalKnowledge?: boolean; }
  • Helper function used by the tool handler to format graph query results into human-readable text when outputFormat is 'text'.
    function formatGraphResultsAsText(results: any): string { const { nodes, relationships } = results; let text = `Query returned ${nodes.length} nodes and ${relationships.length} relationships.\n\n`; // Add node information text += "Nodes:\n"; nodes.forEach((node: any, index: number) => { text += `${index + 1}. [${node.type}] ${node.name}\n`; if (Object.keys(node.attributes).length > 0) { text += ` Attributes: ${JSON.stringify(node.attributes)}\n`; } }); // Add relationship information text += "\nRelationships:\n"; relationships.forEach((rel: any, index: number) => { const source = nodes.find((n: any) => n.id === rel.sourceId); const target = nodes.find((n: any) => n.id === rel.targetId); text += `${index + 1}. ${source?.name || rel.sourceId} [${rel.type}] ${target?.name || rel.targetId}\n`; }); return text; }

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/0xjcf/MCP_CodeAnalysis'

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