UNPKG

pagespace-mcp

Version:

Model Context Protocol (MCP) server for PageSpace with complete workspace management, powerful search, batch operations, and AI agent capabilities. Provides external access to all core PageSpace functionality.

187 lines (168 loc) 5.95 kB
/** * Search handler for PageSpace MCP * Implements regex search, glob search, and multi-drive search functionality */ export function createSearchHandler(api) { return { async handleRegexSearch(args) { try { const { driveId, pattern, searchIn = 'content', maxResults = 50 } = args; console.error('[MCP] Performing regex search:', { driveId, pattern, searchIn, maxResults }); // Build search endpoint based on searchIn parameter let endpoint = `/api/drives/${driveId}/search/regex`; const params = new URLSearchParams({ pattern, searchIn, maxResults: maxResults.toString() }); const response = await api.request('GET', `${endpoint}?${params}`); if (!response.success) { throw new Error(response.error || 'Regex search failed'); } return { content: [ { type: 'text', text: JSON.stringify({ success: true, driveId, pattern, searchIn, results: response.results || [], totalResults: response.totalResults || 0, summary: response.summary || `Found ${response.results?.length || 0} matches`, stats: response.stats || {}, nextSteps: response.nextSteps || [ 'Use read_page with the pageId to examine full content', 'Use edit tools to modify matching pages' ] }, null, 2) } ] }; } catch (error) { console.error('[MCP] Regex search error:', error); return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: `Regex search failed: ${error.message}`, driveId: args.driveId, pattern: args.pattern }, null, 2) } ], isError: true }; } }, async handleGlobSearch(args) { try { const { driveId, pattern, includeTypes, maxResults = 100 } = args; console.error('[MCP] Performing glob search:', { driveId, pattern, includeTypes, maxResults }); let endpoint = `/api/drives/${driveId}/search/glob`; const params = new URLSearchParams({ pattern, maxResults: maxResults.toString() }); if (includeTypes && includeTypes.length > 0) { params.append('includeTypes', includeTypes.join(',')); } const response = await api.request('GET', `${endpoint}?${params}`); if (!response.success) { throw new Error(response.error || 'Glob search failed'); } return { content: [ { type: 'text', text: JSON.stringify({ success: true, driveId, pattern, results: response.results || [], totalResults: response.totalResults || 0, summary: response.summary || `Found ${response.results?.length || 0} matches`, stats: response.stats || {}, nextSteps: response.nextSteps || [ 'Use read_page with the pageId to examine content', 'Use the semantic paths to understand the structure' ] }, null, 2) } ] }; } catch (error) { console.error('[MCP] Glob search error:', error); return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: `Glob search failed: ${error.message}`, driveId: args.driveId, pattern: args.pattern }, null, 2) } ], isError: true }; } }, async handleMultiDriveSearch(args) { try { const { searchQuery, searchType = 'text', maxResultsPerDrive = 20 } = args; console.error('[MCP] Performing multi-drive search:', { searchQuery, searchType, maxResultsPerDrive }); const endpoint = '/api/search/multi-drive'; const params = new URLSearchParams({ searchQuery, searchType, maxResultsPerDrive: maxResultsPerDrive.toString() }); const response = await api.request('GET', `${endpoint}?${params}`); if (!response.success) { throw new Error(response.error || 'Multi-drive search failed'); } return { content: [ { type: 'text', text: JSON.stringify({ success: true, searchQuery, searchType, results: response.results || [], totalDrives: response.totalDrives || 0, totalMatches: response.totalMatches || 0, summary: response.summary || `Found ${response.totalMatches || 0} matches across ${response.totalDrives || 0} drives`, stats: response.stats || {}, nextSteps: response.nextSteps || [ 'Use read_page with specific pageIds to examine content', 'Focus on a specific drive for more detailed search' ] }, null, 2) } ] }; } catch (error) { console.error('[MCP] Multi-drive search error:', error); return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: `Multi-drive search failed: ${error.message}`, searchQuery: args.searchQuery }, null, 2) } ], isError: true }; } } }; }