Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

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
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
sampleSizeNoNumber of documents to sample for schema inference
responseBytesLimitNoThe 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.` ), };
  • 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, });
  • 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";

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/mongodb-js/mongodb-mcp-server'

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