Skip to main content
Glama

extract_conversation_elements

Extract specific elements like file references, code blocks, programming languages, and metadata from conversations. Use to analyze patterns, build knowledge bases, or prepare data for documentation and further analysis.

Instructions

Extract specific elements from conversations such as file references, code blocks, programming languages, folder paths, metadata, or conversation structure. Use this to build knowledge bases, analyze code patterns, extract reusable snippets, understand project file usage, or prepare data for further analysis and documentation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
conversationIdsNoSpecific conversation IDs to extract elements from (if not provided, extracts from all conversations)
elementsNoTypes of elements to extract from conversations
filtersNoFilters to apply when extracting elements
groupByNoHow to group the extracted elements in the outputconversation
includeContextNoInclude surrounding context for extracted elements
outputModeNoOutput format: "json" for formatted JSON (default), "compact-json" for minified JSONjson

Implementation Reference

  • The primary handler function that executes the tool's core logic: connects to the database reader, fetches conversation IDs if not provided, extracts elements using the reader, and applies grouping/flattening based on input parameters.
    export async function extractConversationElements( input: ExtractConversationElementsInput ): Promise<ExtractedElements> { const reader = new CursorDatabaseReader(); try { await reader.connect(); // Get conversation IDs to process let conversationIds = input.conversationIds; if (!conversationIds || conversationIds.length === 0) { // Get all conversation IDs if none specified conversationIds = await reader.getConversationIds({ format: 'both', minLength: 1000 }); } // Extract elements from conversations const extractedData = await reader.extractConversationElements( conversationIds, input.elements, { includeContext: input.includeContext, filters: input.filters } ); // Group data based on groupBy parameter if (input.groupBy === 'conversation') { return { conversations: extractedData }; } else if (input.groupBy === 'element') { // Group by element type const groupedData: Record<string, any[]> = {}; for (const elementType of input.elements) { groupedData[elementType] = []; for (const conversation of extractedData) { if (conversation.elements[elementType]) { if (Array.isArray(conversation.elements[elementType])) { groupedData[elementType].push(...conversation.elements[elementType]); } else { groupedData[elementType].push(conversation.elements[elementType]); } } } } return { conversations: groupedData } as any; } else { // Flatten all data const flatData: any[] = []; for (const conversation of extractedData) { for (const elementType of input.elements) { if (conversation.elements[elementType]) { if (Array.isArray(conversation.elements[elementType])) { flatData.push(...conversation.elements[elementType].map((item: any) => ({ ...item, conversationId: conversation.composerId, elementType }))); } else { flatData.push({ ...conversation.elements[elementType], conversationId: conversation.composerId, elementType }); } } } } return { conversations: flatData } as any; } } catch (error) { throw new DatabaseError(`Failed to extract conversation elements: ${error instanceof Error ? error.message : 'Unknown error'}`); } finally { reader.close(); } }
  • src/server.ts:297-338 (registration)
    MCP server registration of the 'extract_conversation_elements' tool, including description, input schema definition, and thin wrapper handler that maps input and calls the main extraction function.
    server.tool( 'extract_conversation_elements', 'Extract specific elements from conversations such as file references, code blocks, programming languages, folder paths, metadata, or conversation structure. Use this to build knowledge bases, analyze code patterns, extract reusable snippets, understand project file usage, or prepare data for further analysis and documentation.', { conversationIds: z.array(z.string()).optional().describe('Specific conversation IDs to extract elements from (if not provided, extracts from all conversations)'), elements: z.array(z.enum(['files', 'folders', 'languages', 'codeblocks', 'metadata', 'structure'])).optional().default(['files', 'codeblocks']).describe('Types of elements to extract from conversations'), includeContext: z.boolean().optional().default(false).describe('Include surrounding context for extracted elements'), groupBy: z.enum(['conversation', 'element', 'none']).optional().default('conversation').describe('How to group the extracted elements in the output'), filters: z.object({ minCodeLength: z.number().optional().describe('Minimum length for code blocks to include'), fileExtensions: z.array(z.string()).optional().describe('Only include files with these extensions'), languages: z.array(z.string()).optional().describe('Only include code blocks in these programming languages') }).optional().describe('Filters to apply when extracting elements'), outputMode: z.enum(['json', 'compact-json']).optional().default('json').describe('Output format: "json" for formatted JSON (default), "compact-json" for minified JSON') }, async (input) => { try { const mappedInput = { conversationIds: input.conversationIds, elements: input.elements, includeContext: input.includeContext, groupBy: input.groupBy, filters: input.filters }; const result = await extractConversationElements(mappedInput); return { content: [{ type: 'text', text: formatResponse(result, input.outputMode) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error instanceof Error ? error.message : 'Unknown error occurred'}` }] }; } } );
  • Zod schema for validating the input parameters to the extractConversationElements function.
    export const extractConversationElementsSchema = z.object({ conversationIds: z.array(z.string()).optional(), elements: z.array(z.enum(['files', 'folders', 'languages', 'codeblocks', 'metadata', 'structure'])).optional().default(['files', 'codeblocks']), includeContext: z.boolean().optional().default(false), groupBy: z.enum(['conversation', 'element', 'none']).optional().default('conversation'), filters: z.object({ minCodeLength: z.number().optional(), fileExtensions: z.array(z.string()).optional(), languages: z.array(z.string()).optional() }).optional() });
  • Core helper method in CursorDatabaseReader class that performs the actual extraction of specified elements (files, folders, languages, codeblocks, metadata, structure) from conversations by delegating to specialized extract* methods.
    async extractConversationElements( conversationIds: string[], elements: Array<'files' | 'folders' | 'languages' | 'codeblocks' | 'metadata' | 'structure'>, options?: { includeContext?: boolean; filters?: { minCodeLength?: number; fileExtensions?: string[]; languages?: string[]; }; } ): Promise<Array<{ composerId: string; format: 'legacy' | 'modern'; elements: any; }>> { this.ensureConnected(); const results: Array<{ composerId: string; format: 'legacy' | 'modern'; elements: any; }> = []; for (const composerId of conversationIds) { try { const conversation = await this.getConversationById(composerId); if (!conversation) continue; const format = isLegacyConversation(conversation) ? 'legacy' : 'modern'; const extractedElements: any = {}; // Extract files if (elements.includes('files')) { extractedElements.files = await this.extractFiles(conversation, options); } // Extract folders if (elements.includes('folders')) { extractedElements.folders = await this.extractFolders(conversation, options); } // Extract languages if (elements.includes('languages')) { extractedElements.languages = await this.extractLanguages(conversation, options); } // Extract code blocks if (elements.includes('codeblocks')) { extractedElements.codeblocks = await this.extractCodeBlocks(conversation, options); } // Extract metadata if (elements.includes('metadata')) { extractedElements.metadata = await this.extractMetadata(conversation); } // Extract structure if (elements.includes('structure')) { extractedElements.structure = await this.extractStructure(conversation); } results.push({ composerId, format, elements: extractedElements }); } catch (error) { console.error(`Failed to extract elements from conversation ${composerId}:`, error); } } return results; }

Other Tools

Related 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/vltansky/cursor-conversations-mcp'

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