switch-database
Switch between databases on the MSSQL MCP Server by specifying the target database name. Designed for efficient database management and query execution.
Instructions
切換到指定的資料庫
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"database": "<database>"
},
"name": "switch-database"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | 要切換到的資料庫名稱 |
Implementation Reference
- src/index.ts:161-195 (handler)The handler function for the 'switch-database' tool. It checks connection, sanitizes the database name, switches the database using MSSQLManager, and returns success or error messages.
async ({ database }) => { try { if (!mssqlManager.isConnected()) { return { content: [ { type: 'text' as const, text: '錯誤: 尚未連接到資料庫伺服器。請先使用 connect-database 工具建立連接。' } ] } } const sanitizedDatabase = sanitizeDatabaseName(database) await mssqlManager.switchDatabase(sanitizedDatabase) return { content: [ { type: 'text' as const, text: `成功切換到資料庫: ${sanitizedDatabase}` } ] } } catch (error) { return { content: [ { type: 'text' as const, text: `切換資料庫失敗: ${formatError(error)}` } ] } } } - src/index.ts:157-159 (schema)Zod input schema for the tool, requiring a 'database' string parameter.
inputSchema: { database: z.string().describe('要切換到的資料庫名稱'), } - src/index.ts:152-196 (registration)Registration of the 'switch-database' tool using server.registerTool, including title, description, schema, and handler.
server.registerTool( 'switch-database', { title: '切換資料庫', description: '切換到指定的資料庫', inputSchema: { database: z.string().describe('要切換到的資料庫名稱'), } }, async ({ database }) => { try { if (!mssqlManager.isConnected()) { return { content: [ { type: 'text' as const, text: '錯誤: 尚未連接到資料庫伺服器。請先使用 connect-database 工具建立連接。' } ] } } const sanitizedDatabase = sanitizeDatabaseName(database) await mssqlManager.switchDatabase(sanitizedDatabase) return { content: [ { type: 'text' as const, text: `成功切換到資料庫: ${sanitizedDatabase}` } ] } } catch (error) { return { content: [ { type: 'text' as const, text: `切換資料庫失敗: ${formatError(error)}` } ] } } } ) - src/database.ts:75-90 (helper)MSSQLManager.switchDatabase method: implements database switching by creating a new connection config with the target database and reconnecting.
async switchDatabase(databaseName: string): Promise<void> { if (!this.isConnected() || !this.config) { throw new Error('尚未建立資料庫連接') } try { // 建立新的連接配置 const newConfig = { ...this.config, database: databaseName } await this.connect(newConfig) this.currentDatabase = databaseName console.log(`已切換到資料庫: ${databaseName}`) } catch (error) { throw new Error(`切換資料庫失敗: ${error instanceof Error ? error.message : String(error)}`) } } - src/utils.ts:157-160 (helper)Utility function to sanitize database names by removing special characters.
export function sanitizeDatabaseName(name: string): string { // 移除特殊字符,只保留字母、數字、底線和連字號 return name.replace(/[^a-zA-Z0-9_-]/g, '') }