add_column
Add a new column to an existing table in MySQL or MongoDB databases with specified name, type, and optional attributes like length, nullability, and default value.
Instructions
Add a new column to existing table
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| field | Yes | ||
| table | Yes |
Input Schema (JSON Schema)
{
"properties": {
"field": {
"properties": {
"default": {
"optional": true,
"type": [
"string",
"number",
"null"
]
},
"length": {
"optional": true,
"type": "number"
},
"name": {
"type": "string"
},
"nullable": {
"optional": true,
"type": "boolean"
},
"type": {
"type": "string"
}
},
"required": [
"name",
"type"
],
"type": "object"
},
"table": {
"type": "string"
}
},
"required": [
"table",
"field"
],
"type": "object"
}
Implementation Reference
- src/index.ts:843-873 (handler)The handler function that implements the add_column tool by constructing and executing an ALTER TABLE ADD COLUMN SQL statement with support for column length, nullability, and default values.private async handleAddColumn(args: any) { await this.ensureConnection(); if (!args.table || !args.field) { throw new McpError(ErrorCode.InvalidParams, 'Table name and field are required'); } let sql = `ALTER TABLE \`${args.table}\` ADD COLUMN \`${args.field.name}\` ${args.field.type.toUpperCase()}`; if (args.field.length) sql += `(${args.field.length})`; if (args.field.nullable === false) sql += ' NOT NULL'; if (args.field.default !== undefined) { sql += ` DEFAULT ${args.field.default === null ? 'NULL' : `'${args.field.default}'`}`; } try { await this.connection!.query(sql); return { content: [ { type: 'text', text: `Column ${args.field.name} added to table ${args.table}` } ] }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to add column: ${getErrorMessage(error)}` ); } }
- src/index.ts:376-396 (schema)Input schema defining the parameters for the add_column tool: table name and field specification (name, type, optional length, nullable, default).inputSchema: { type: 'object', properties: { table: { type: 'string' }, field: { type: 'object', properties: { name: { type: 'string' }, type: { type: 'string' }, length: { type: 'number', optional: true }, nullable: { type: 'boolean', optional: true }, default: { type: ['string', 'number', 'null'], optional: true } }, required: ['name', 'type'] } }, required: ['table', 'field'] }
- src/index.ts:373-397 (registration)Registration of the add_column tool in the server's tool list, including name, description, and input schema for ListTools requests.{ name: 'add_column', description: 'Add a new column to existing table', inputSchema: { type: 'object', properties: { table: { type: 'string' }, field: { type: 'object', properties: { name: { type: 'string' }, type: { type: 'string' }, length: { type: 'number', optional: true }, nullable: { type: 'boolean', optional: true }, default: { type: ['string', 'number', 'null'], optional: true } }, required: ['name', 'type'] } }, required: ['table', 'field'] } },