create_collection
Define a new data structure in PocketBase by specifying collection name, type, fields, and access rules to organize and manage database records.
Instructions
Create a new collection in PocketBase note never use created and updated because these are already created
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Unique collection name (used as a table name for the records table) | |
| type | No | Type of the collection | base |
| fields | Yes | List with the collection fields | |
| createRule | No | API rule for creating records | |
| updateRule | No | API rule for updating records | |
| deleteRule | No | API rule for deleting records | |
| listRule | No | API rule for listing and viewing records | |
| viewRule | No | API rule for viewing a single record | |
| viewQuery | No | SQL query for view collections | |
| passwordAuth | No | Password authentication options |
Implementation Reference
- src/index.ts:713-760 (handler)The handler function that implements the core logic for the 'create_collection' tool: authenticates as admin, appends default 'created' and 'updated' autodate fields, creates the collection using PocketBase API, and returns the result.private async createCollection(args: any) { try { // Authenticate with PocketBase await this.pb.collection("_superusers").authWithPassword(process.env.POCKETBASE_ADMIN_EMAIL ?? '', process.env.POCKETBASE_ADMIN_PASSWORD ?? ''); const defaultFields = [ { hidden: false, id: "autodate_created", name: "created", onCreate: true, onUpdate: false, presentable: false, system: false, type: "autodate" }, { hidden: false, id: "autodate_updated", name: "updated", onCreate: true, onUpdate: true, presentable: false, system: false, type: "autodate" } ]; const collectionData = { ...args, fields: [...(args.fields || []), ...defaultFields] }; const result = await this.pb.collections.create(collectionData as any); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } catch (error: unknown) { throw new McpError( ErrorCode.InternalError, `Failed to create collection: ${pocketbaseErrorMessage(error)}` ); }
- src/index.ts:54-122 (schema)Input schema/JSON Schema definition for the 'create_collection' tool parameters, including collection name, type, fields, rules, and auth options.inputSchema: { type: 'object', properties: { name: { type: 'string', description: 'Unique collection name (used as a table name for the records table)', }, type: { type: 'string', description: 'Type of the collection', enum: ['base', 'view', 'auth'], default: 'base', }, fields: { type: 'array', description: 'List with the collection fields', items: { type: 'object', properties: { name: { type: 'string', description: 'Field name' }, type: { type: 'string', description: 'Field type', enum: ['bool', 'date', 'number', 'text', 'email', 'url', 'editor', 'autodate', 'select', 'file', 'relation', 'json'] }, required: { type: 'boolean', description: 'Is field required?' }, values: { type: 'array', items: { type: 'string' }, description: 'Allowed values for select type fields', }, collectionId: { type: 'string', description: 'Collection ID for relation type fields' } }, }, }, createRule: { type: 'string', description: 'API rule for creating records', }, updateRule: { type: 'string', description: 'API rule for updating records', }, deleteRule: { type: 'string', description: 'API rule for deleting records', }, listRule: { type: 'string', description: 'API rule for listing and viewing records', }, viewRule: { type: 'string', description: 'API rule for viewing a single record', }, viewQuery: { type: 'string', description: 'SQL query for view collections', }, passwordAuth: { type: 'object', description: 'Password authentication options', properties: { enabled: { type: 'boolean', description: 'Is password authentication enabled?' }, identityFields: { type: 'array', items: { type: 'string' }, description: 'Fields used for identity in password authentication', }, }, }, }, required: ['name', 'fields'],
- src/index.ts:51-123 (registration)Tool registration in the ListTools handler: defines name, description, and inputSchema for 'create_collection'.{ name: 'create_collection', description: 'Create a new collection in PocketBase note never use created and updated because these are already created', inputSchema: { type: 'object', properties: { name: { type: 'string', description: 'Unique collection name (used as a table name for the records table)', }, type: { type: 'string', description: 'Type of the collection', enum: ['base', 'view', 'auth'], default: 'base', }, fields: { type: 'array', description: 'List with the collection fields', items: { type: 'object', properties: { name: { type: 'string', description: 'Field name' }, type: { type: 'string', description: 'Field type', enum: ['bool', 'date', 'number', 'text', 'email', 'url', 'editor', 'autodate', 'select', 'file', 'relation', 'json'] }, required: { type: 'boolean', description: 'Is field required?' }, values: { type: 'array', items: { type: 'string' }, description: 'Allowed values for select type fields', }, collectionId: { type: 'string', description: 'Collection ID for relation type fields' } }, }, }, createRule: { type: 'string', description: 'API rule for creating records', }, updateRule: { type: 'string', description: 'API rule for updating records', }, deleteRule: { type: 'string', description: 'API rule for deleting records', }, listRule: { type: 'string', description: 'API rule for listing and viewing records', }, viewRule: { type: 'string', description: 'API rule for viewing a single record', }, viewQuery: { type: 'string', description: 'SQL query for view collections', }, passwordAuth: { type: 'object', description: 'Password authentication options', properties: { enabled: { type: 'boolean', description: 'Is password authentication enabled?' }, identityFields: { type: 'array', items: { type: 'string' }, description: 'Fields used for identity in password authentication', }, }, }, }, required: ['name', 'fields'], },
- src/index.ts:671-672 (registration)Dispatch case in the central CallToolRequestHandler that routes 'create_collection' calls to the handler function.case 'create_collection': return await this.createCollection(request.params.arguments);