collection-schema
Analyze MongoDB collection structure to identify field types, patterns, and data formats for schema documentation and validation.
Instructions
Describe the schema for a collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name | |
| sampleSize | No | Number of documents to sample for schema inference | |
| responseBytesLimit | No | The maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. |
Implementation Reference
- The main handler function that connects to MongoDB, samples documents from the specified collection, infers the schema using getSimplifiedSchema, and returns a formatted response with the schema or an error message if empty.protected async execute( { database, collection, sampleSize, responseBytesLimit }: ToolArgs<typeof this.argsShape>, { signal }: ToolExecutionContext ): Promise<CallToolResult> { const provider = await this.ensureConnected(); const cursor = provider.aggregate(database, collection, [ { $sample: { size: Math.min(sampleSize, MAXIMUM_SAMPLE_SIZE_HARD_LIMIT) } }, ]); const { cappedBy, documents } = await collectCursorUntilMaxBytesLimit({ cursor, configuredMaxBytesPerQuery: this.config.maxBytesPerQuery, toolResponseBytesLimit: responseBytesLimit, abortSignal: signal, }); const schema = await getSimplifiedSchema(documents); if (isObjectEmpty(schema)) { return { content: [ { text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`, type: "text", }, ], }; } const fieldsCount = Object.keys(schema).length; const header = `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`; const cappedWarning = cappedBy !== undefined ? `\nThe schema was inferred from a subset of documents due to the response size limit. (${cappedBy})` : ""; return { content: formatUntrustedData(`${header}${cappedWarning}`, JSON.stringify(schema)), }; }
- Input schema definition using Zod, extending DbOperationArgs with sampleSize and responseBytesLimit parameters.protected argsShape = { ...DbOperationArgs, sampleSize: z.number().optional().default(50).describe("Number of documents to sample for schema inference"), responseBytesLimit: z .number() .optional() .default(ONE_MB) .describe( `The maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded.` ), };
- src/tools/index.ts:1-11 (registration)Registers the CollectionSchemaTool (via MongoDbTools) by including it in the AllTools array of all available tool classes.import * as AtlasTools from "./atlas/tools.js"; import * as AtlasLocalTools from "./atlasLocal/tools.js"; import * as MongoDbTools from "./mongodb/tools.js"; import type { ToolClass } from "./tool.js"; // Export the collection of tools for easier reference export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
- src/tools/mongodb/tools.ts:6-6 (registration)Re-exports the CollectionSchemaTool to make it available in the MongoDB tools namespace used by the main tools index.export { CollectionSchemaTool } from "./metadata/collectionSchema.js";