Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(mcp): remove narration comments from tool search (#108820)

posthog[bot]772e197e9a87
diff --git a/services/mcp/src/tools/tool-search.ts b/services/mcp/src/tools/tool-search.ts
index 4f0a252da9e3..deb176c027eb 100644
--- a/services/mcp/src/tools/tool-search.ts
+++ b/services/mcp/src/tools/tool-search.ts
@@ -1,43 +1,21 @@
-/**
- * Tool-search logic for `exec search` (src/tools/exec.ts).
- *
- * Two strategies live here:
- *
- * - searchToolsRegex — the original `exec search` predicate: one
- * case-insensitive RegExp tested against name/title/description.
- * - searchToolsRanked — a forgiving, field-weighted token ranking so
- * multi-word natural-language queries ("create dashboard insight") surface
- * the relevant tools instead of matching nothing.
- *
- * Callers route between them via `isRegexPattern`.
- */
-
-/** Minimal structural shape both searches operate on. The runtime `Tool` and
- * the catalog's `ScopeGatedTool` both satisfy it, so a single signature serves
- * every caller. */
+/** The runtime `Tool` and the catalog's `ScopeGatedTool` both satisfy this shape. */
export interface SearchableTool {
name: string
title: string
description: string
}
-/** A match in a tool's name outweighs the same token in its title, which in
- * turn outweighs a hit buried in the description. */
const SEARCH_FIELD_WEIGHT = { name: 3, title: 2, description: 1 } as const
export type SearchField = keyof typeof SEARCH_FIELD_WEIGHT
-/** Fields in descending weight — a token is counted once, at its highest-weight
- * field, so a name hit doesn't also score for a description mention. */
+/** A token counts once, at its highest-weight field, so a name hit does not
+ * also score for a description mention. */
const ORDERED_FIELDS: readonly SearchField[] = ['name', 'title', 'description']
export interface RankedToolMatch {
name: string
- /** Distinct query tokens found in any field — the relevance signal a tie
- * breaks on after the weighted score. */
tokensMatched: number
- /** Field-weighted score; the primary ranking key. */
score: number
- /** Which fields contributed, for debug/inspection output. */
fields: SearchField[]
}
@@ -51,19 +29,15 @@ export function isRegexPattern(pattern: string): boolean {
return REGEX_METACHARACTER.test(pattern)
}
-/** The original `exec search` predicate, verbatim: a single case-insensitive
- * RegExp tested against each tool's name, title, and description. Throws if the
- * pattern is not a valid regex — callers surface their own error message. */
+/** The original `exec search` predicate, kept verbatim. Throws if the pattern
+ * is not a valid regex, so callers surface their own error message. */
export function searchToolsRegex<T extends SearchableTool>(tools: readonly T[], pattern: string): T[] {
const regex = new RegExp(pattern, 'i')
return tools.filter((t) => regex.test(t.name) || regex.test(t.title) || regex.test(t.description))
}
-/** Forgiving, field-weighted token search. Splits the query on whitespace and
- * ranks each tool by a weighted score over the distinct query tokens it
- * contains, so a multi-word intent like "create dashboard insight" surfaces
- * dashboard-create / insight-create instead of returning nothing. Results are
- * sorted by score, then by distinct token coverage, then by name. */
+/** Multi-word queries like "create dashboard insight" match nothing as a single
+ * regex, so rank tools by the query tokens they contain instead. */
export function searchToolsRanked<T extends SearchableTool>(tools: readonly T[], query: string): RankedToolMatch[] {
const tokens = [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))]
if (tokens.length === 0) {
@@ -80,8 +54,6 @@ export function searchToolsRanked<T extends SearchableTool>(tools: readonly T[],
let score = 0
const fields = new Set<SearchField>()
for (const token of tokens) {
- // Count each token once, at its highest-weight field, so a token in
- // the name outweighs the same token buried in a description.
const field = ORDERED_FIELDS.find((f) => haystack[f].includes(token))
if (field) {
tokensMatched += 1
@@ -93,10 +65,7 @@ export function searchToolsRanked<T extends SearchableTool>(tools: readonly T[],
scored.push({ name: t.name, tokensMatched, score, fields: [...fields] })
}
}
- // Field-weighted score first: a token in the tool name beats the same token
- // buried in a description, so dashboard-create outranks a tool that merely
- // mentions "create"/"dashboard"/"insight" in prose. Token coverage breaks
- // ties, then name for a stable order.
+ // Name is the last key so equal matches keep a stable order.
scored.sort((a, b) => b.score - a.score || b.tokensMatched - a.tokensMatched || a.name.localeCompare(b.name))
return scored
}