Skip to main content
Glama
NakiriYuuzu

MSSQL MCP Server

by NakiriYuuzu

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

切換到指定的資料庫

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYes要切換到的資料庫名稱

Implementation Reference

  • 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)}` } ] } } }
  • 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)}` } ] } } } )
  • 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)}`) } }
  • Utility function to sanitize database names by removing special characters.
    export function sanitizeDatabaseName(name: string): string { // 移除特殊字符,只保留字母、數字、底線和連字號 return name.replace(/[^a-zA-Z0-9_-]/g, '') }

Other Tools

Related Tools

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/NakiriYuuzu/Mssql-Mcp'

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