Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

collection-schema

Analyze and describe the schema of a specified MongoDB collection to understand its structure and field types for database optimization and management.

Instructions

Describe the schema for a collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name

Implementation Reference

  • Executes the tool by sampling documents from the MongoDB collection, inferring the schema using getSimplifiedSchema, and returning a formatted response with field count and any capping warnings.
    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)), }; }
  • Zod schema defining the tool's input arguments: database, collection (from DbOperationArgs), sampleSize (default 50), and responseBytesLimit (default 1MB).
    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 import chain) along with all other tools into the AllTools array, which is used to initialize the MCP tool server.
    export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
  • Exports the CollectionSchemaTool class for inclusion in the MongoDbTools namespace used in src/tools/index.ts.
    export { CollectionSchemaTool } from "./metadata/collectionSchema.js";
  • Defines the tool's name and description properties.
    public name = "collection-schema"; protected description = "Describe the schema for a collection";

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