HTS MCP
Skip to Content
ArchitectureDatabase

Database

PostgreSQL with pgvector, hosted on Supabase. 14 tables covering tariff data, embeddings, enrichments, graph edges, references, and resources.

Schema Overview

Core Data

TablePrimary KeyRowsPurpose
tariffshts8~13,000Tariff lines with rates and dates
hts_chapterschapter982-digit chapter descriptions
hts_schedule_sectionssection_roman22Roman numeral section groupings
hts_headingshts4~9614-digit heading descriptions
hts_sectionsid~35,700Raw hierarchical section data
hts_notesid~1,067Interpretive notes
hts_raw_notesid~121Full-text HTML note blobs

AI Pipeline

TablePrimary KeyRowsPurpose
hts_embeddings(code, level, variant)~38,000Multi-level vector embeddings
hts6_enrichmentshts6~5,714AI-generated descriptions and keywords

Knowledge Graph

TablePrimary KeyRowsPurpose
hts_edge_candidates(source_code, target_code, source_level)~142,000KNN candidate pairs
hts_semantic_edges(source_code, target_code)~142,000Classified relationships
hts_graph_runsidVariablePipeline run tracking

References & Resources

TablePrimary KeyRowsPurpose
tariff_code_references(source_hts8, referenced_code_clean)~2,400Cross-references from descriptions
tariff_resourcesid~45Curated trade policy documents

Key Tables

tariffs

The central table with ~13,000 tariff lines:

ColumnTypeDescription
hts8text PK8-digit HTS code
brief_descriptiontextProduct description
quantity_1_codetextUnit of measure
mfn_text_ratetextMFN rate as text (e.g., “6.4%“)
mfn_ad_val_ratefloatMFN ad valorem percentage
mfn_specific_ratefloatMFN specific rate
begin_effect_datedateStart of rate period
end_effective_datedateEnd of rate period
usmca_*, cafta_*, …various50+ FTA rate columns

hts_embeddings

Multi-level vector embeddings for search:

ColumnTypeDescription
codetextHTS code (2-8 digits)
leveltextchapter, hts4, hts6, hts8
varianttextfull, short
embeddingvector(1536)OpenAI embedding
embedding_texttextSource text that was embedded

Primary key: (code, level, variant)

hts_semantic_edges

Classified knowledge graph edges:

ColumnTypeDescription
source_codetextSource HTS code
target_codetextTarget HTS code
relationship_typetextOne of 10 types
confidencefloat0-1 confidence score
reasoningtextLLM reasoning
key_differentiatortextWhat distinguishes the codes
similarityfloatOriginal cosine similarity
bidirectionalbooleanSymmetric relationship
haikutext5-7-5 creative summary
classification_clarityfloatHow clear-cut
source_inclusionarytext[]Defining terms for source
source_exclusionarytext[]Excluding terms for source
target_inclusionarytext[]Defining terms for target
target_exclusionarytext[]Excluding terms for target
run_idtextClassification run ID

pgvector Setup

pgvector must be enabled in the database:

CREATE EXTENSION IF NOT EXISTS vector;

Create an index on the embedding column for fast KNN:

CREATE INDEX ON hts_embeddings USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);

Or for better recall at the cost of more memory:

CREATE INDEX ON hts_embeddings USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);

Connection Pooling

The application uses psycopg2 connection pooling:

SettingDefaultDescription
pool_min_size2Minimum connections
pool_max_size10Maximum connections
pool_timeout5.0sWait time for a connection
pool_max_idle300.0sMax idle time before closing
db_statement_timeout30sQuery timeout

Each connection initializes:

  1. Registers the pgvector type
  2. Sets statement timeout
  3. Sets search path

Caching

Application-level caching reduces database load:

CacheTTLScope
get_tariff_by_code3600sLRU per HTS8 code
get_tariff_count300sSingle value
has_embeddingsSessionBoolean per level
get_embeddings_count300sPer level/variant