HTS MCP
Skip to Content
MCP ToolsGraph Tools

Graph Tools

Tools for building and querying the semantic knowledge graph.

generate_graph_edges

Generate edge candidates by finding semantically similar codes using pgvector KNN.

Parameters

ParameterTypeRequiredDefaultDescription
kintegerNo30Number of nearest neighbors per code
min_similarityfloatNo0.65Minimum cosine similarity threshold
levelstringNo"hts8"Embedding level to use
variantstringNo"full"Embedding variant to use

Response

{ "run_id": "abc-123", "total_candidates": 142380, "codes_processed": 12769, "time_seconds": 312.5 }

Details

Uses PostgreSQL CROSS JOIN LATERAL with the <=> cosine distance operator:

SELECT sb.code AS source_code, nn.code AS target_code, 1 - (sb.embedding <=> nn.embedding) AS similarity FROM source_batch sb CROSS JOIN LATERAL ( SELECT e.code FROM hts_embeddings e WHERE e.code != sb.code ORDER BY e.embedding <=> sb.embedding LIMIT k ) nn WHERE 1 - (sb.embedding <=> nn.embedding) >= min_similarity

Candidates are stored in hts_edge_candidates with source/target chapters and headings for efficient filtering.


classify_graph_edges

Classify edge candidates using an LLM into 10 relationship types. Async, resumable pipeline.

Parameters

ParameterTypeRequiredDefaultDescription
concurrencyintegerNo200Maximum concurrent LLM calls
batch_sizeintegerNo500Database flush batch size

Response

{ "run_id": "def-456", "classified": 142380, "errors": 12, "db_flushed": 142368, "time_seconds": 8234.1 }

Details

The pipeline:

  1. Fetches unclassified candidates (LEFT JOIN to hts_semantic_edges for resume support)
  2. Chunks candidates with configurable delay between chunks for rate-limit pacing
  3. Sends each pair to GPT-5.4 Nano with a structured JSON schema
  4. Validates the classification output
  5. Batch-flushes results to hts_semantic_edges

Resumable: If interrupted, re-running picks up where it left off, only unclassified candidates are processed.

Classification Output Schema

Each classified edge includes:

FieldTypeDescription
relationship_typestringOne of 10 types
confidencefloat0-1 classification confidence
reasoningstring2 sentences, 25-30 words
key_differentiatorstringMax 8 words
classification_clarityfloat0-1 how clear-cut the classification was
source_code_inclusionarylistTerms that define the source code
source_code_exclusionarylistTerms that exclude from the source code
target_code_inclusionarylistTerms that define the target code
target_code_exclusionarylistTerms that exclude from the target code
relationship_haikustring5-7-5 syllable haiku
bidirectionalbooleanWhether the relationship is symmetric

get_graph_status

Get pipeline statistics including candidate counts, classification progress, and type breakdown.

Parameters

None.

Response

{ "total_candidates": 142380, "total_classified": 142368, "by_type": { "material_affinity": { "count": 18420, "avg_confidence": 0.82 }, "functional_similarity": { "count": 15230, "avg_confidence": 0.79 }, "manufacturing_process": { "count": 12100, "avg_confidence": 0.76 }, "end_use": { "count": 11890, "avg_confidence": 0.74 }, "substitution": { "count": 8920, "avg_confidence": 0.71 }, "component_assembly": { "count": 7650, "avg_confidence": 0.73 }, "cross_category_bridge": { "count": 3210, "avg_confidence": 0.68 }, "abstraction": { "count": 2890, "avg_confidence": 0.85 }, "weak_association": { "count": 24560, "avg_confidence": 0.45 }, "no_meaningful_relationship": { "count": 37498, "avg_confidence": 0.88 } }, "avg_confidence": 0.69, "recent_runs": [...] }

get_code_relationships

Query classified semantic edges for a specific HTS code.

Parameters

ParameterTypeRequiredDefaultDescription
hts_codestringYes-HTS code to query (any length)
min_confidencefloatNo0.0Minimum confidence threshold
relationship_typeslist[string]NoAll typesFilter to specific relationship types
limitintegerNo20Maximum results

Response

{ "hts_code": "02013080", "edges": [ { "id": 12345, "source_code": "02013080", "target_code": "02023080", "relationship_type": "material_affinity", "confidence": 0.92, "reasoning": "Both classify boneless beef cuts. Source is fresh/chilled while target is frozen, same animal origin.", "key_differentiator": "fresh vs frozen preservation", "bidirectional": true, "similarity": 0.89, "haiku": "Cattle on the range\nFresh and frozen sides of beef\nCold preserves the cut" } ], "count": 15 }

Examples

All relationships for a code:

{ "hts_code": "0201.30.80" }

High-confidence substitution relationships only:

{ "hts_code": "0201.30.80", "min_confidence": 0.8, "relationship_types": ["substitution", "functional_similarity"] }