Skip to main content
Glama

API-retrieve-a-database

Fetch structured data from a Notion database using its unique identifier. Designed for AI assistants to efficiently query and retrieve content within the Notion ReadOnly MCP Server framework.

Instructions

Retrieve a database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
database_idYesAn identifier for the Notion database.

Implementation Reference

  • Main handler logic for executing the 'API-retrieve-a-database' tool call. Finds the corresponding OpenAPI operation, executes it via HttpClient, logs the response, updates caches, and returns the result in MCP format.
    // Find the operation in OpenAPI spec const operation = this.findOperation(name) if (!operation) { const error = `Method ${name} not found.` console.error(error) return { content: [ { type: 'text', text: JSON.stringify({ status: 'error', message: error, code: 404 }), }, ], } } // Optimized parallel processing for API-get-block-children if (name === 'API-get-block-children') { // Create basic options for logging control const blockOptions: RecursiveExplorationOptions = { runInBackground: false, // Default to not showing logs for regular API calls }; return await this.handleBlockChildrenParallel(operation, params, blockOptions); } // Other regular API calls console.log(`Notion API call: ${operation.method.toUpperCase()} ${operation.path}`) const response = await this.httpClient.executeOperation(operation, params) // Log response summary console.log('Notion API response code:', response.status) if (response.status !== 200) { console.error('Response error:', response.data) } else { console.log('Response success') } // Update cache with response data this.updateCacheFromResponse(name, response.data); // Convert response to MCP format return { content: [ { type: 'text', text: JSON.stringify(response.data), }, ], }
  • Registers 'API-retrieve-a-database' by converting the OpenAPI specification into MCP-compatible tools and operation lookup table.
    // Convert OpenAPI spec to MCP tools const converter = new OpenAPIToMCPConverter(openApiSpec) const { tools, openApiLookup } = converter.convertToMCPTools() this.tools = tools this.openApiLookup = openApiLookup
  • Registers 'API-retrieve-a-database' in the list of available tools returned by the ListTools MCP request.
    Object.entries(this.tools).forEach(([toolName, def]) => { def.methods.forEach(method => { const toolNameWithMethod = `${toolName}-${method.name}`; const truncatedToolName = this.truncateToolName(toolNameWithMethod); tools.push({ name: truncatedToolName, description: method.description, inputSchema: method.inputSchema as Tool['inputSchema'], }) console.log(`- ${truncatedToolName}: ${method.description}`) }) })
  • Supporting helper function that invokes the 'API-retrieve-a-database' tool to retrieve and cache Notion database details during recursive page exploration.
    private async retrieveDatabase(databaseId: string, options: RecursiveExplorationOptions): Promise<any> { console.log(`Retrieving database information: ${databaseId}`); // Check cache if (!options.skipCache && this.databaseCache.has(databaseId)) { console.log(`Database cache hit: ${databaseId}`); return this.databaseCache.get(databaseId); } // Get database info via API call const operation = this.findOperation('API-retrieve-a-database'); if (!operation) { console.warn('API-retrieve-a-database method not found.'); return { id: databaseId, note: "Database details not available" }; } try { console.log(`Notion API call: ${operation.method.toUpperCase()} ${operation.path} (databaseId: ${databaseId})`); const response = await this.httpClient.executeOperation(operation, { database_id: databaseId }); if (response.status !== 200) { console.error('Error retrieving database information:', response.data); return { id: databaseId, error: "Failed to retrieve database" }; } const databaseData = response.data; this.databaseCache.set(databaseId, databaseData); return databaseData; } catch (error) { console.error('Error retrieving database:', error); return { id: databaseId, error: "Failed to retrieve database" }; } }
  • Helper that specifically caches responses from 'API-retrieve-a-database' calls into databaseCache for performance.
    private updateCacheFromResponse(apiName: string, data: any): void { if (!data || typeof data !== 'object') return; try { // Update appropriate cache based on API response type if (apiName === 'API-retrieve-a-page' && data.object === 'page' && data.id) { this.pageCache.set(data.id, data); } else if (apiName === 'API-retrieve-a-block' && data.object === 'block' && data.id) { this.blockCache.set(data.id, data); } else if (apiName === 'API-retrieve-a-database' && data.object === 'database' && data.id) { this.databaseCache.set(data.id, data); } else if (apiName === 'API-retrieve-a-comment' && data.results) { // Cache comments from result list data.results.forEach((comment: any) => { if (comment.object === 'comment' && comment.id) { this.commentCache.set(comment.id, comment); } }); } else if (apiName === 'API-retrieve-a-page-property' && data.results) { // Page property caching - would need params from call context // Skip this in current context console.log('Page property information has been cached'); } // API-get-block-children handled in handleBlockChildrenParallel } catch (error) { console.warn('Error updating cache:', error); } }

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/Taewoong1378/notion-readonly-mcp-server'

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