get_table_relationships
Extract and analyze foreign key relationships between tables in Cloudflare D1 databases to understand data connections and dependencies.
Instructions
Extract and analyze foreign key relationships between tables in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| environment | Yes | Database environment to analyze | |
| tableName | No | Optional: Filter relationships for specific table |
Implementation Reference
- Core handler function that fetches cached schema, extracts relationships via analyzer, filters by optional tableName if provided, formats into DTOs, and returns the structured response.async execute(request: GetRelationshipsRequest): Promise<RelationshipsResponse> { const environment = request.environment; // Get or fetch schema (with caching) const schema = await this.getSchema(environment); // Extract relationships using domain service const allRelationships = this.relationshipAnalyzer.extractRelationships([...schema.tables]); // Filter by table if specified const relationships = request.tableName ? this.filterRelationshipsByTable(allRelationships, request.tableName) : allRelationships; // Format and return response return { databaseName: schema.name, environment: schema.environment, relationships: relationships.map((rel) => this.formatRelationship(rel)), relationshipCount: relationships.length, }; }
- MCP-specific tool handler that validates/parses input arguments, invokes the GetRelationshipsUseCase, serializes the result to JSON, and returns it in MCP content format.private async handleGetRelationships(args: unknown) { const { environment, tableName } = args as { environment: string; tableName?: string; }; const result = await this.getRelationshipsUseCase.execute({ environment: parseEnvironment(environment), tableName, }); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/presentation/mcp/MCPServer.ts:103-122 (registration)Registers the "get_table_relationships" MCP tool including its name, description, and input schema validation.{ name: 'get_table_relationships', description: 'Extract and analyze foreign key relationships between tables in the database', inputSchema: { type: 'object', properties: { environment: { type: 'string', enum: ['development', 'staging', 'production'], description: 'Database environment to analyze', }, tableName: { type: 'string', description: 'Optional: Filter relationships for specific table', }, }, required: ['environment'], }, },
- TypeScript interface defining the input parameters for the relationships extraction use case, matching the MCP tool inputSchema.export interface GetRelationshipsRequest { environment: Environment; tableName?: string; }
- Domain service method that constructs Relationship domain entities from foreign key constraints in table metadata.extractRelationships(tables: TableInfo[]): Relationship[] { const relationships: Relationship[] = []; for (const table of tables) { for (const fk of table.foreignKeys) { relationships.push( new Relationship(fk.table, fk.column, fk.referencesTable, fk.referencesColumn, fk.onDelete, fk.onUpdate) ); } } return relationships; }