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
| Name | Required | Description | Default |
|---|---|---|---|
| databaseName | Yes | ||
| sortField | Yes |
Implementation Reference
- src/index.ts:406-432 (handler)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)), }, ], }; }
- src/index.ts:78-81 (schema)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"], },