filter

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package filter provides keyword relevance scoring for MCP item filtering.

Design goals:

  • Precise: word-boundary tokenization, not substring matching ("go" does NOT match "google" or "cargo")
  • Relevant: weighted scoring — name > tags > description; stem + prefix matching
  • Reproducible: stable sort (score desc, then original order for ties)
  • Efficient: stateless pure functions, no allocations beyond the sort slice

Scoring pipeline:

  1. Tokenize + stopword-filter query and all fields
  2. Match each query token per field via exact → stem → prefix
  3. Coverage bonus: +10% per additional matched query term (beyond first)
  4. Co-occurrence bonus: +30% per field where 2+ query terms match
  5. Phrase adjacency bonus: +25%/+15%/+10% per adjacent pair in name/tags/desc
  6. Short-tag exact bonus: raw tag string match for short tags ("go", "ci"…)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Score

func Score(query, name, description string, tags []string) int

Score computes a relevance score for name/description/tags against query.

The scoring pipeline:

  1. Tokenize + stopword-filter query and all fields
  2. Match each query token per field via exact → stem → prefix
  3. Coverage bonus: +10% per additional matched query term (beyond first)
  4. Co-occurrence bonus: +30% per field where 2+ query terms match together
  5. Phrase adjacency bonus: +25%/+15%/+10% when adjacent query terms appear adjacent in name/tags/desc ("code review" → "code-review" +25%)
  6. Short-tag bonus: raw tag == raw query word gives +8 (enables "go", "ci")
  7. Name precision bonus: +20% when ≥75% of name tokens are covered by query (rewards "jwt-auth" over "jwt-auth-middleware-guide" for query "jwt auth")

Returns 0 if no query term matches (item excluded). Returns 1 if query is empty or reduces to all stopwords (pass-through).

func SortByScore

func SortByScore[T any](items []T, scoreFn func(T) int) []T

SortByScore filters and sorts items by relevance to query.

  • Items scoring 0 are excluded when query is non-empty.
  • Items are sorted by score descending.
  • Equal-score items keep their original relative order (stable, reproducible).
  • If query is empty, all items are returned in original order unchanged.

func Tokenize

func Tokenize(s string) []string

Tokenize splits s into lowercase alphanumeric tokens on non-alphanumeric boundaries. Single-character tokens and stopwords are discarded.

Examples:

"JWT Authentication"      → ["jwt", "authentication"]
"how to use mcp-skills"   → ["mcp", "skills"]    (stopwords filtered)
"mcp-instructions"        → ["mcp", "instructions"]
"**/*.go"                 → ["go"]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL