Skip to main content
Glama
jimpick

MCP JSON Document Collection Server

by jimpick

query_json_docs_from_db

Query JSON documents from a database and sort them by a specified field to organize and retrieve structured data efficiently.

Instructions

Query JSON documents sorted by a field from a document database. If no sortField is provided, use the _id field.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseNameYes
sortFieldYes

Implementation Reference

  • Handler function for the 'query_json_docs_from_db' tool. Parses input arguments using the schema, retrieves or initializes the Fireproof database, queries documents sorted by the specified field in descending order, and returns the JSON documents as a text response.
    case "query_json_docs_from_db": {
      const parsed = QueryJsonDocsFromDbArgsSchema.safeParse(args);
      if (!parsed.success) {
        throw new Error(`Invalid arguments for query_json_docs_from_db: ${parsed.error}`);
      }
    
      const dbName = parsed.data.databaseName;
      if (!dbs[dbName]) {
        const newDb = fireproof(dbName);
        dbs[dbName] = { db: newDb };
      }
      const db = dbs[dbName].db;
    
      const results = await db.query(parsed.data.sortField, {
        includeDocs: true,
        descending: true,
      });
    
      return {
        content: [
          {
            type: "text",
            text: JSON.stringify(results.rows.map((row) => row.doc)),
          },
        ],
      };
    }
  • Zod schema defining the input arguments for the 'query_json_docs_from_db' tool: databaseName (string) and sortField (string). Used for validation in the handler and converted to JSON schema for tool registration.
    const QueryJsonDocsFromDbArgsSchema = z.object({
      databaseName: z.string(),
      sortField: z.string(),
    });
  • src/index.ts:146-152 (registration)
    Tool registration in the ListTools response, specifying name, description, input schema (derived from Zod schema), and required fields.
    {
      name: "query_json_docs_from_db",
      description: "Query JSON documents sorted by a field from a document database. " +
        "If no sortField is provided, use the _id field.",
      inputSchema: zodToJsonSchema(QueryJsonDocsFromDbArgsSchema) as ToolInput,
      required: ["sortField"],
    },

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/jimpick/mcp-json-db-collection-server'

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