Skip to content

Commit bc8ec76

Browse files
committed
add fk information in describe table
1 parent 3d88d52 commit bc8ec76

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

MssqlMcp/dotnet/MssqlMcp/Tools/DescribeTable.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class Tools
1919
public async Task<DbOperationResult> DescribeTable(
2020
[Description("Name of table")] string name)
2121
{
22-
string schema = null;
22+
string? schema = null;
2323
if (name.Contains('.'))
2424
{
2525
// If the table name contains a schema, split it into schema and table name
@@ -62,6 +62,33 @@ FROM sys.indexes i
6262
FROM sys.key_constraints kc
6363
WHERE kc.parent_object_id = (SELECT object_id FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @TableName and (s.name = @TableSchema or @TableSchema IS NULL ) )";
6464

65+
66+
const string ForeignKeyInformation = @"SELECT
67+
fk.name AS name,
68+
SCHEMA_NAME(tp.schema_id) AS [schema],
69+
tp.name AS table_name,
70+
STRING_AGG(cp.name, ', ') WITHIN GROUP (ORDER BY fkc.constraint_column_id) AS column_names,
71+
SCHEMA_NAME(tr.schema_id) AS referenced_schema,
72+
tr.name AS referenced_table,
73+
STRING_AGG(cr.name, ', ') WITHIN GROUP (ORDER BY fkc.constraint_column_id) AS referenced_column_names
74+
FROM
75+
sys.foreign_keys AS fk
76+
JOIN
77+
sys.foreign_key_columns AS fkc ON fk.object_id = fkc.constraint_object_id
78+
JOIN
79+
sys.tables AS tp ON fkc.parent_object_id = tp.object_id
80+
JOIN
81+
sys.columns AS cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
82+
JOIN
83+
sys.tables AS tr ON fkc.referenced_object_id = tr.object_id
84+
JOIN
85+
sys.columns AS cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id
86+
WHERE
87+
( SCHEMA_NAME(tp.schema_id) = @TableSchema OR @TableSchema IS NULL )
88+
AND tp.name = @TableName
89+
GROUP BY
90+
fk.name, tp.schema_id, tp.name, tr.schema_id, tr.name;
91+
";
6592
var conn = await _connectionFactory.GetOpenConnectionAsync();
6693
try
6794
{
@@ -149,6 +176,30 @@ FROM sys.key_constraints kc
149176
}
150177
result["constraints"] = constraints;
151178
}
179+
180+
// Foreign Keys
181+
using (var cmd = new SqlCommand(ForeignKeyInformation, conn))
182+
{
183+
var _ = cmd.Parameters.AddWithValue("@TableName", name);
184+
_ = cmd.Parameters.AddWithValue("@TableSchema", schema == null ? DBNull.Value : schema);
185+
using var reader = await cmd.ExecuteReaderAsync();
186+
var foreignKeys = new List<object>();
187+
while (await reader.ReadAsync())
188+
{
189+
foreignKeys.Add(new
190+
{
191+
name = reader["name"],
192+
schema = reader["schema"],
193+
table_name = reader["table_name"],
194+
column_name = reader["column_names"],
195+
referenced_schema = reader["referenced_schema"],
196+
referenced_table = reader["referenced_table"],
197+
referenced_column = reader["referenced_column_names"],
198+
});
199+
}
200+
result["foreignKeys"] = foreignKeys;
201+
}
202+
152203
return new DbOperationResult(success: true, data: result);
153204
}
154205
}

0 commit comments

Comments
 (0)