list_foreign_keys
Identify and display foreign key relationships within SQL Server database schemas to understand table dependencies and maintain referential integrity.
Instructions
List all foreign key relationships in a schema
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | Database name (optional) | |
| schema | No | Schema name (optional, defaults to dbo) |
Implementation Reference
- Main handler function in DatabaseToolsHandler class that constructs and executes SQL queries to retrieve foreign key relationships for a specified database and schema, using sys views.async listForeignKeys(database = null, schema = 'dbo') { let query; if (database) { query = ` SELECT fk.name as foreign_key_name, tp.name as parent_table, cp.name as parent_column, tr.name as referenced_table, cr.name as referenced_column FROM [${database}].sys.foreign_keys fk INNER JOIN [${database}].sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id INNER JOIN [${database}].sys.tables tp ON fkc.parent_object_id = tp.object_id INNER JOIN [${database}].sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id INNER JOIN [${database}].sys.tables tr ON fkc.referenced_object_id = tr.object_id INNER JOIN [${database}].sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id INNER JOIN [${database}].sys.schemas s ON tp.schema_id = s.schema_id WHERE s.name = '${schema}' ORDER BY tp.name, fk.name `; } else { query = ` SELECT fk.name as foreign_key_name, tp.name as parent_table, cp.name as parent_column, tr.name as referenced_table, cr.name as referenced_column FROM sys.foreign_keys fk INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id INNER JOIN sys.tables tp ON fkc.parent_object_id = tp.object_id INNER JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id INNER JOIN sys.tables tr ON fkc.referenced_object_id = tr.object_id INNER JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id INNER JOIN sys.schemas s ON tp.schema_id = s.schema_id WHERE s.name = '${schema}' ORDER BY tp.name, fk.name `; } const result = await this.executeQuery(query, 'list_foreign_keys'); return this.formatResults(result); }
- lib/tools/tool-registry.js:51-60 (schema)Tool definition in DATABASE_TOOLS array, providing name, description, and inputSchema for parameters database (optional string) and schema (optional string, defaults to 'dbo').{ name: 'list_foreign_keys', description: 'List all foreign key relationships in a schema', inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional)' }, schema: { type: 'string', description: 'Schema name (optional, defaults to dbo)' } } }
- index.js:275-278 (registration)Registration and dispatch in the main tool handling switch statement in SqlServerMCP class, delegating to databaseTools.listForeignKeys with parsed arguments.case 'list_foreign_keys': return { content: await this.databaseTools.listForeignKeys(args.database, args.schema) };
- index.js:543-546 (registration)Wrapper method in SqlServerMCP class for listForeignKeys tool execution.async listForeignKeys(...args) { try { return { content: await this.databaseTools.listForeignKeys(...args) }; } catch (error) {
- index.js:104-104 (registration)Instantiation of DatabaseToolsHandler which contains the listForeignKeys method.this.databaseTools = new DatabaseToolsHandler(this.connectionManager, this.performanceMonitor);