Skip to main content
Glama
michaelyuwh

Enhanced MCP MSSQL Server

by michaelyuwh

mssql_get_relationships

Retrieve foreign key relationships between tables in a Microsoft SQL Server database to understand data dependencies and schema structure.

Instructions

Get foreign key relationships for tables

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
serverYesMSSQL Server hostname or IP address
portNoPort number (default: 1433)
userYesUsername for authentication
passwordYesPassword for authentication
databaseYesDatabase name
tableNoTable name (optional - if not provided, gets all relationships)
encryptNoUse encrypted connection (default: true)
trustServerCertificateNoTrust server certificate (default: true)

Implementation Reference

  • The handler function that implements the core logic of the mssql_get_relationships tool by querying the database's INFORMATION_SCHEMA views for foreign key relationships.
    private async handleGetRelationships(args: any) { const config = ConnectionSchema.parse(args); const { table } = args; const pool = await this.getConnection(config); const request = pool.request(); let whereClause = ''; if (table) { whereClause = `AND fk.TABLE_NAME = '${table}'`; } const result = await request.query(` USE [${config.database}]; SELECT fk.TABLE_NAME as table_name, fk.COLUMN_NAME as column_name, pk.TABLE_NAME as referenced_table, pk.COLUMN_NAME as referenced_column, fk.CONSTRAINT_NAME as constraint_name FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fk ON rc.CONSTRAINT_NAME = fk.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pk ON rc.UNIQUE_CONSTRAINT_NAME = pk.CONSTRAINT_NAME WHERE 1=1 ${whereClause} ORDER BY fk.TABLE_NAME, fk.COLUMN_NAME `); return { content: [ { type: 'text', text: JSON.stringify({ server: config.server, database: config.database, table: table || 'all', relationships: result.recordset, }, null, 2), }, ], }; }
  • src/index.ts:447-448 (registration)
    The switch case in the CallToolRequestHandler that dispatches calls to the mssql_get_relationships handler function.
    case 'mssql_get_relationships': return await this.handleGetRelationships(args);
  • src/index.ts:361-378 (registration)
    The tool registration in ListToolsRequestHandler, defining the name, description, and input schema for mssql_get_relationships.
    { name: 'mssql_get_relationships', description: 'Get foreign key relationships for tables', inputSchema: { type: 'object', properties: { server: { type: 'string', description: 'MSSQL Server hostname or IP address' }, port: { type: 'number', description: 'Port number (default: 1433)', default: 1433 }, user: { type: 'string', description: 'Username for authentication' }, password: { type: 'string', description: 'Password for authentication' }, database: { type: 'string', description: 'Database name' }, table: { type: 'string', description: 'Table name (optional - if not provided, gets all relationships)' }, encrypt: { type: 'boolean', description: 'Use encrypted connection (default: true)', default: true }, trustServerCertificate: { type: 'boolean', description: 'Trust server certificate (default: true)', default: true }, }, required: ['server', 'user', 'password', 'database'], }, },
  • Shared Zod schema used for parsing connection parameters in the mssql_get_relationships handler.
    const ConnectionSchema = z.object({ server: z.string().describe('MSSQL Server hostname or IP address'), port: z.number().default(1433).describe('Port number (default: 1433)'), user: z.string().describe('Username for authentication'), password: z.string().describe('Password for authentication'), database: z.string().optional().describe('Database name (optional)'), encrypt: z.boolean().default(true).describe('Use encrypted connection'), trustServerCertificate: z.boolean().default(true).describe('Trust server certificate'), });

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/michaelyuwh/mcp-mssql-connector'

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