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
| Name | Required | Description | Default |
|---|---|---|---|
| server | Yes | MSSQL Server hostname or IP address | |
| port | No | Port number (default: 1433) | |
| user | Yes | Username for authentication | |
| password | Yes | Password for authentication | |
| database | Yes | Database name | |
| table | No | Table name (optional - if not provided, gets all relationships) | |
| encrypt | No | Use encrypted connection (default: true) | |
| trustServerCertificate | No | Trust server certificate (default: true) |
Implementation Reference
- src/index.ts:658-699 (handler)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'], }, },
- src/index.ts:76-84 (schema)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'), });