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, '')
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It states the action ('switch') but doesn't disclose behavioral traits such as whether this requires authentication, if it's destructive (e.g., closes previous connections), error conditions, or what happens on success. This leaves critical operational details unspecified for a mutation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence in Chinese ('切換到指定的資料庫'), which is appropriately sized and front-loaded with the core action. There is no wasted verbiage or structural issues.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (a mutation with no annotations and no output schema), the description is incomplete. It lacks details on behavior, side effects, prerequisites, and expected outcomes, making it inadequate for safe and effective use by an AI agent in this context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds minimal meaning beyond the input schema, which has 100% coverage and documents the single parameter 'database' as '要切換到的資料庫名稱' (the database name to switch to). With zero parameters undocumented, the baseline is 4, as the description doesn't need to compensate but also doesn't enhance parameter understanding significantly.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose3/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description '切換到指定的資料庫' (switch to the specified database) states a clear verb ('switch') and resource ('database'), but it's vague about what 'switch' means operationally. It doesn't distinguish from siblings like 'connect-database' or 'list-databases', leaving ambiguity about whether this establishes a new connection or changes an active session.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

There is no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., needing an existing connection), exclusions, or relationships to siblings like 'connect-database' for initial setup or 'disconnect' for cleanup. The agent must infer usage from context alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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