dataview_query
Execute Dataview-style queries to filter, sort, and analyze notes from your Obsidian vault based on metadata, tags, and content criteria.
Instructions
Execute a Dataview-style query on notes
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from | No | Source filter (folder path or #tag) | |
| groupBy | No | Field to group by | |
| limit | No | Maximum results | |
| select | No | Fields to select | |
| sort | No | Sort order | |
| vault | Yes | Vault name | |
| where | No | Filter conditions |
Implementation Reference
- src/services/DataviewService.ts:30-75 (handler)Core handler function that executes the full Dataview query logic: loads notes, applies FROM/WHERE/SORT/GROUP/SELECT/LIMIT filters, and returns results.async executeQuery(query: DataviewQuery): Promise<QueryExecutionResult> { let results = this.notes.map(note => this.noteToDataviewResult(note)); // Apply FROM clause (source filtering) if (query.from) { results = this.applyFromFilter(results, query.from); } // Apply WHERE clause (filters) if (query.where && query.where.length > 0) { results = this.applyFilters(results, query.where); } // Apply SORT if (query.sort && query.sort.length > 0) { results = this.applySorting(results, query.sort); } // Apply field selection if (query.select && query.select.length > 0) { results = this.applyFieldSelection(results, query.select); } const totalCount = results.length; // Apply GROUP BY if (query.groupBy) { const grouped = this.applyGrouping(results, query.groupBy); return { results: grouped, totalCount, grouped: true }; } // Apply LIMIT if (query.limit && query.limit > 0) { results = results.slice(0, query.limit); } return { results, totalCount, grouped: false }; }
- src/index.ts:746-772 (handler)MCP tool dispatch handler for 'dataview_query': fetches notes from vault connector, updates DataviewService, constructs query from arguments, calls executeQuery, and formats response.case 'dataview_query': { const connector = this.connectors.get(args?.vault as string); if (!connector) { throw new Error(`Vault "${args?.vault}" not found`); } const notesResult = await connector.getAllNotes(); if (!notesResult.success || !notesResult.data) { throw new Error('Failed to get notes'); } this.dataviewService.updateNotes(notesResult.data); const query: any = {}; if (args?.from) query.from = args.from; if (args?.where) query.where = args.where; if (args?.sort) query.sort = args.sort; if (args?.groupBy) query.groupBy = args.groupBy; if (args?.select) query.select = args.select; if (args?.limit) query.limit = args.limit; const result = await this.dataviewService.executeQuery(query); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; }
- src/types/dataview.ts:5-23 (schema)TypeScript interface defining the structure of DataviewQuery used by the handler, matching the tool's inputSchema.export interface DataviewQuery { // Source selection from?: string | string[]; // Folder, tag, or file paths // Filters where?: QueryFilter[]; // Sorting sort?: QuerySort[]; // Limiting limit?: number; // Grouping groupBy?: string; // Fields to select select?: string[]; }
- src/index.ts:290-326 (registration)Tool registration in ListToolsRequestHandler, defining name, description, and inputSchema for 'dataview_query'.name: 'dataview_query', description: 'Execute a Dataview-style query on notes', inputSchema: { type: 'object', properties: { vault: { type: 'string', description: 'Vault name' }, from: { type: 'string', description: 'Source filter (folder path or #tag)' }, where: { type: 'array', items: { type: 'object', properties: { field: { type: 'string' }, operator: { type: 'string', enum: ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'contains', 'startsWith', 'endsWith', 'exists'] }, value: { type: 'string' } } }, description: 'Filter conditions' }, sort: { type: 'array', items: { type: 'object', properties: { field: { type: 'string' }, direction: { type: 'string', enum: ['asc', 'desc'] } } }, description: 'Sort order' }, groupBy: { type: 'string', description: 'Field to group by' }, select: { type: 'array', items: { type: 'string' }, description: 'Fields to select' }, limit: { type: 'number', description: 'Maximum results' } }, required: ['vault'], }, },