Skip to main content
Glama
NakiriYuuzu

MSSQL MCP Server

by NakiriYuuzu

list-tables

Retrieve and display a list of all tables within the current database on MSSQL MCP Server, simplifying database navigation and management.

Instructions

列出目前資料庫中的所有資料表

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The handler function for the 'list-tables' MCP tool. It checks if connected to a database, verifies a current database is selected, calls mssqlManager.getTables() to fetch the list of tables, formats them as a bulleted list with schema, name, and type, and returns the result in MCP content format. Handles errors appropriately.
    async () => {
      try {
        if (!mssqlManager.isConnected()) {
          return {
            content: [
              {
                type: 'text' as const,
                text: '錯誤: 尚未連接到資料庫伺服器。請先使用 connect-database 工具建立 সংযোগ。'
              }
            ]
          }
        }
    
        const currentDb = mssqlManager.getCurrentDatabase()
        if (!currentDb) {
          return {
            content: [
              {
                type: 'text' as const,
                text: '錯誤: 尚未選擇資料庫。請先使用 switch-database 工具選擇資料庫。'
              }
            ]
          }
        }
    
        const tables = await mssqlManager.getTables()
    
        if (tables.length === 0) {
          return {
            content: [
              {
                type: 'text' as const,
                text: `資料庫 ${currentDb} 中沒有找到資料表。`
              }
            ]
          }
        }
    
        const tableList = tables.map(table =>
          `- ${table.table_schema}.${table.table_name} (${table.table_type})`
        ).join('\n')
    
        return {
          content: [
            {
              type: 'text' as const,
              text: `資料庫 ${currentDb} 中找到 ${tables.length} 個資料表:\n${tableList}`
            }
          ]
        }
      } catch (error) {
        return {
          content: [
            {
              type: 'text' as const,
              text: `列出資料表失敗: ${error instanceof Error ? error.message : String(error)}`
            }
          ]
        }
      }
    }
  • TableInfo interface defining the structure of table metadata returned by getTables(), used in the tool's output processing.
    export interface TableInfo {
      table_name: string
      table_schema: string
      table_type: string
    }
  • src/index.ts:199-266 (registration)
    Registration of the 'list-tables' tool with the MCP server, including title, description (no input schema), and reference to the inline handler function.
    server.registerTool(
      'list-tables',
      {
        title: '列出資料表',
        description: '列出目前資料庫中的所有資料表'
      },
      async () => {
        try {
          if (!mssqlManager.isConnected()) {
            return {
              content: [
                {
                  type: 'text' as const,
                  text: '錯誤: 尚未連接到資料庫伺服器。請先使用 connect-database 工具建立 সংযোগ。'
                }
              ]
            }
          }
    
          const currentDb = mssqlManager.getCurrentDatabase()
          if (!currentDb) {
            return {
              content: [
                {
                  type: 'text' as const,
                  text: '錯誤: 尚未選擇資料庫。請先使用 switch-database 工具選擇資料庫。'
                }
              ]
            }
          }
    
          const tables = await mssqlManager.getTables()
    
          if (tables.length === 0) {
            return {
              content: [
                {
                  type: 'text' as const,
                  text: `資料庫 ${currentDb} 中沒有找到資料表。`
                }
              ]
            }
          }
    
          const tableList = tables.map(table =>
            `- ${table.table_schema}.${table.table_name} (${table.table_type})`
          ).join('\n')
    
          return {
            content: [
              {
                type: 'text' as const,
                text: `資料庫 ${currentDb} 中找到 ${tables.length} 個資料表:\n${tableList}`
              }
            ]
          }
        } catch (error) {
          return {
            content: [
              {
                type: 'text' as const,
                text: `列出資料表失敗: ${error instanceof Error ? error.message : String(error)}`
              }
            ]
          }
        }
      }
    )
  • Core helper method MSSQLManager.getTables() that executes a SQL query against INFORMATION_SCHEMA.TABLES to retrieve all tables (name, schema, type) in the current database, returning typed TableInfo[] array.
    async getTables(): Promise<TableInfo[]> {
      if (!this.currentDatabase) {
        throw new Error('尚未選擇資料庫')
      }
    
      const query = `
        SELECT 
          TABLE_NAME as table_name,
          TABLE_SCHEMA as table_schema,
          TABLE_TYPE as table_type
        FROM INFORMATION_SCHEMA.TABLES
        ORDER BY TABLE_SCHEMA, TABLE_NAME
      `
      
      const result = await this.executeQuery(query)
      return result.recordset as TableInfo[]
    }
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