Skip to main content
Glama

query-database

Retrieve and filter Notion database entries using specific criteria, sorting options, and pagination controls to access structured workspace data.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
database_idYesThe ID of the database to query
filterNoJSON string of filter criteria
sortsNoJSON string of sort criteria
page_sizeNoNumber of results to return (max 100)
start_cursorNoPagination cursor

Implementation Reference

  • The main handler function for the 'query-database' MCP tool. Parses JSON strings for filter and sorts, calls NotionService.queryDatabase, formats results as JSON text content or error response.
    async ({ database_id, filter, sorts, page_size, start_cursor }) => {
      const params: any = { database_id, page_size, start_cursor };
    
      // Parse filter and sorts from JSON strings to objects
      // Example filter: "{\"property\":\"Status\",\"select\":{\"equals\":\"Done\"}}"
      // Example sorts: "[{\"property\":\"Priority\",\"direction\":\"descending\"}]"
      try {
        if (filter) params.filter = JSON.parse(filter);
        if (sorts) params.sorts = JSON.parse(sorts);
      } catch (parseError) {
        console.error("Error parsing JSON parameters:", parseError);
        return {
          content: [
            {
              type: "text",
              text: `Error: Failed to parse JSON parameters - ${
                (parseError as Error).message
              }. Make sure filter and sorts are valid JSON strings.`,
            },
          ],
          isError: true,
        };
      }
    
      try {
        const results = await this.notionService.queryDatabase(params);
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(results, null, 2),
            },
          ],
        };
      } catch (error) {
        console.error("Error in query-database tool:", error);
        return {
          content: [
            {
              type: "text",
              text: `Error: Failed to query database - ${
                (error as Error).message
              }`,
            },
          ],
          isError: true,
        };
      }
    }
  • Zod input schema for the 'query-database' tool defining parameters like database_id, filter (JSON), sorts (JSON), page_size, and start_cursor.
    {
      database_id: z.string().describe("The ID of the database to query"),
      filter: z
        .string()
        .optional()
        .describe("JSON string of filter criteria"),
      sorts: z.string().optional().describe("JSON string of sort criteria"),
      page_size: z
        .number()
        .min(1)
        .max(100)
        .optional()
        .describe("Number of results to return (max 100)"),
      start_cursor: z.string().optional().describe("Pagination cursor"),
    },
  • Registration of the 'query-database' tool on the McpServer instance within registerDatabaseTools().
    this.server.tool(
      "query-database",
      {
        database_id: z.string().describe("The ID of the database to query"),
        filter: z
          .string()
          .optional()
          .describe("JSON string of filter criteria"),
        sorts: z.string().optional().describe("JSON string of sort criteria"),
        page_size: z
          .number()
          .min(1)
          .max(100)
          .optional()
          .describe("Number of results to return (max 100)"),
        start_cursor: z.string().optional().describe("Pagination cursor"),
      },
      async ({ database_id, filter, sorts, page_size, start_cursor }) => {
        const params: any = { database_id, page_size, start_cursor };
    
        // Parse filter and sorts from JSON strings to objects
        // Example filter: "{\"property\":\"Status\",\"select\":{\"equals\":\"Done\"}}"
        // Example sorts: "[{\"property\":\"Priority\",\"direction\":\"descending\"}]"
        try {
          if (filter) params.filter = JSON.parse(filter);
          if (sorts) params.sorts = JSON.parse(sorts);
        } catch (parseError) {
          console.error("Error parsing JSON parameters:", parseError);
          return {
            content: [
              {
                type: "text",
                text: `Error: Failed to parse JSON parameters - ${
                  (parseError as Error).message
                }. Make sure filter and sorts are valid JSON strings.`,
              },
            ],
            isError: true,
          };
        }
    
        try {
          const results = await this.notionService.queryDatabase(params);
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(results, null, 2),
              },
            ],
          };
        } catch (error) {
          console.error("Error in query-database tool:", error);
          return {
            content: [
              {
                type: "text",
                text: `Error: Failed to query database - ${
                  (error as Error).message
                }`,
              },
            ],
            isError: true,
          };
        }
      }
    );
  • Utility function to stringify filter objects into JSON for use as the 'filter' parameter in query-database tool.
     * Helper function to create a properly formatted filter string for the query-database tool
     * @param filter The filter object
     * @returns A JSON string representation of the filter
     *
     * @example
     * const filter = createFilterString({
     *   property: "Status",
     *   select: {
     *     equals: "Done"
     *   }
     * });
     * // Returns: "{\"property\":\"Status\",\"select\":{\"equals\":\"Done\"}}"
     */
    export function createFilterString(filter: any): string {
      return JSON.stringify(filter);
    }
  • NotionService.queryDatabase method invoked by the tool handler, which constructs params and calls the Notion SDK's databases.query API.
    async queryDatabase(params: DatabaseQuery) {
      try {
        const queryParams: Parameters<typeof this.client.databases.query>[0] = {
          database_id: params.database_id,
          page_size: params.page_size,
          start_cursor: params.start_cursor,
        };
    
        // Only add filter and sorts if they exist to avoid type issues
        if (params.filter) {
          queryParams.filter = params.filter as any;
        }
    
        if (params.sorts) {
          queryParams.sorts = params.sorts as any;
        }
    
        return await this.client.databases.query(queryParams);
      } catch (error) {
        this.handleError(error);
      }
    }
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/ramidecodes/mcp-server-notion'

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