list_foreign_keys
Retrieve all foreign keys that originate from a specified table in the database.
Instructions
List foreign keys originating from a table.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | Yes | ||
| table | Yes |
Implementation Reference
- src/db.ts:174-205 (handler)The `listForeignKeys` method on `DbManager` that executes the SQL query to fetch foreign keys originating from a given schema+table. Returns rows with fk_name, parent_schema, parent_table, parent_column, referenced_schema, referenced_table, referenced_column, and ordinal.
async listForeignKeys(schema: string, table: string) { const r = await (await this.getPool()) .request() .input('schema', sql.NVarChar, schema) .input('table', sql.NVarChar, table).query(` SELECT fk.name AS fk_name, s_parent.name AS parent_schema, t_parent.name AS parent_table, c_parent.name AS parent_column, s_ref.name AS referenced_schema, t_ref.name AS referenced_table, c_ref.name AS referenced_column, fkc.constraint_column_id AS ordinal FROM sys.foreign_keys fk INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id INNER JOIN sys.tables t_parent ON fk.parent_object_id = t_parent.object_id INNER JOIN sys.schemas s_parent ON t_parent.schema_id = s_parent.schema_id INNER JOIN sys.columns c_parent ON fkc.parent_object_id = c_parent.object_id AND fkc.parent_column_id = c_parent.column_id INNER JOIN sys.tables t_ref ON fk.referenced_object_id = t_ref.object_id INNER JOIN sys.schemas s_ref ON t_ref.schema_id = s_ref.schema_id INNER JOIN sys.columns c_ref ON fkc.referenced_object_id = c_ref.object_id AND fkc.referenced_column_id = c_ref.column_id WHERE s_parent.name = @schema AND t_parent.name = @table ORDER BY fk.name, fkc.constraint_column_id `); return r.recordset; } - src/index.ts:103-105 (schema)Schema definition for the 'list_foreign_keys' tool: takes 'schema' (string) and 'table' (string) as input parameters.
{ schema: z.string(), table: z.string(), - src/index.ts:100-108 (registration)Registration of the 'list_foreign_keys' tool with the MCP server, including its description, input schema, and handler that delegates to db.listForeignKeys().
server.tool( 'list_foreign_keys', 'List foreign keys originating from a table.', { schema: z.string(), table: z.string(), }, async ({ schema, table }) => runTool(() => db.listForeignKeys(schema, table)) );