Skip to main content
Glama
NakiriYuuzu

MSSQL MCP Server

by NakiriYuuzu

connect-database

Connect to an MSSQL database server by providing server address, user credentials, and optional settings like encryption and database name to establish secure database interactions.

Instructions

連接到 MSSQL 資料庫伺服器

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseNo資料庫名稱 (可選)
encryptNo是否加密連接 (預設: true)
passwordYes密碼
portNo連接埠號 (預設: 1433)
serverYesMSSQL 伺服器位址
trustServerCertificateNo是否信任伺服器憑證 (預設: false)
userYes使用者名稱

Implementation Reference

  • The main handler function for the 'connect-database' tool. It validates the input configuration, sanitizes the database name, parses the config using Zod schema, establishes the connection via MSSQLManager, and returns success or error messages.
    async ({ server, port, database, user, password, encrypt, trustServerCertificate }) => { try { // 驗證連接配置 const configValidation = validateConnectionConfig({ server, user, password, port }) if (!configValidation.isValid) { return { content: [ { type: 'text' as const, text: `連接配置錯誤:\n${configValidation.errors.join('\n')}` } ] } } const config = DatabaseConfigSchema.parse({ server, port, database: database ? sanitizeDatabaseName(database) : undefined, user, password, options: { encrypt, trustServerCertificate, } }) await mssqlManager.connect(config) return { content: [ { type: 'text' as const, text: `成功連接到 MSSQL 伺服器: ${server}${database ? `\n目前資料庫: ${database}` : '\n未指定資料庫'}` } ] } } catch (error) { return { content: [ { type: 'text' as const, text: `連接失敗: ${formatError(error)}` } ] } } }
  • Input schema definition for the connect-database tool, specifying parameters like server, port, database, credentials, and connection options using Zod.
    inputSchema: { server: z.string().describe('MSSQL 伺服器位址'), port: z.number().optional().default(1433).describe('連接埠號 (預設: 1433)'), database: z.string().optional().describe('資料庫名稱 (可選)'), user: z.string().describe('使用者名稱'), password: z.string().describe('密碼'), encrypt: z.boolean().optional().default(true).describe('是否加密連接 (預設: true)'), trustServerCertificate: z.boolean().optional().default(false).describe('是否信任伺服器憑證 (預設: false)'), }
  • src/index.ts:28-91 (registration)
    Registration of the 'connect-database' tool with the MCP server, including title, description, input schema, and handler function.
    server.registerTool( 'connect-database', { title: '連接資料庫', description: '連接到 MSSQL 資料庫伺服器', inputSchema: { server: z.string().describe('MSSQL 伺服器位址'), port: z.number().optional().default(1433).describe('連接埠號 (預設: 1433)'), database: z.string().optional().describe('資料庫名稱 (可選)'), user: z.string().describe('使用者名稱'), password: z.string().describe('密碼'), encrypt: z.boolean().optional().default(true).describe('是否加密連接 (預設: true)'), trustServerCertificate: z.boolean().optional().default(false).describe('是否信任伺服器憑證 (預設: false)'), } }, async ({ server, port, database, user, password, encrypt, trustServerCertificate }) => { try { // 驗證連接配置 const configValidation = validateConnectionConfig({ server, user, password, port }) if (!configValidation.isValid) { return { content: [ { type: 'text' as const, text: `連接配置錯誤:\n${configValidation.errors.join('\n')}` } ] } } const config = DatabaseConfigSchema.parse({ server, port, database: database ? sanitizeDatabaseName(database) : undefined, user, password, options: { encrypt, trustServerCertificate, } }) await mssqlManager.connect(config) return { content: [ { type: 'text' as const, text: `成功連接到 MSSQL 伺服器: ${server}${database ? `\n目前資料庫: ${database}` : '\n未指定資料庫'}` } ] } } catch (error) { return { content: [ { type: 'text' as const, text: `連接失敗: ${formatError(error)}` } ] } } } )
  • Zod schema for DatabaseConfig used in the handler to parse and validate the connection configuration before passing to MSSQLManager.connect.
    export const DatabaseConfigSchema = z.object({ server: z.string(), port: z.number().optional().default(1433), database: z.string().optional(), user: z.string(), password: z.string(), options: z.object({ encrypt: z.boolean().optional().default(true), trustServerCertificate: z.boolean().optional().default(false), connectionTimeout: z.number().optional().default(30000), requestTimeout: z.number().optional().default(30000), }).optional() })
  • MSSQLManager.connect method called by the tool handler to establish the actual SQL Server connection pool using the 'mssql' library.
    async connect(config: DatabaseConfig): Promise<void> { try { // 如果已有連接,先關閉 if (this.pool) { await this.disconnect() } this.config = config this.currentDatabase = config.database || null const poolConfig: sql.config = { server: config.server, port: config.port, database: config.database, user: config.user, password: config.password, options: config.options || { encrypt: true, trustServerCertificate: false, connectionTimeout: 30000, requestTimeout: 30000, } } this.pool = new sql.ConnectionPool(poolConfig) await this.pool.connect() console.log(`已連接到 MSSQL 伺服器: ${config.server}${config.database ? `, 資料庫: ${config.database}` : ''}`) } catch (error) { throw new Error(`連接資料庫失敗: ${error instanceof Error ? error.message : String(error)}`) } }

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