: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mcp): order regex tool search by the field that matched (#125520)
Meikel Ratz1bf2e7017541
diff --git a/services/mcp/src/tools/tool-search.ts b/services/mcp/src/tools/tool-search.tsindex deb176c027eb..9c52fa123fd7 100644--- a/services/mcp/src/tools/tool-search.ts+++ b/services/mcp/src/tools/tool-search.ts@@ -29,11 +29,20 @@ export function isRegexPattern(pattern: string): boolean {return REGEX_METACHARACTER.test(pattern)}-/** The original `exec search` predicate, kept verbatim. Throws if the pattern- * is not a valid regex, so callers surface their own error message. */+/** The `exec search` predicate for regex patterns, ordered by the field that matched so a name+ * hit comes before a description mention, the way `searchToolsRanked` already orders words.+ * Ties keep catalog order. 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))+ const matched: { tool: T; rank: number }[] = []+ for (const tool of tools) {+ const rank = ORDERED_FIELDS.findIndex((field) => regex.test(tool[field]))+ if (rank !== -1) {+ matched.push({ tool, rank })+ }+ }+ return matched.sort((a, b) => a.rank - b.rank).map((m) => m.tool)}/** Multi-word queries like "create dashboard insight" match nothing as a singlediff --git a/services/mcp/tests/unit/tool-search.test.ts b/services/mcp/tests/unit/tool-search.test.tsindex 8c9e0bf8ce3e..0609d742a42a 100644--- a/services/mcp/tests/unit/tool-search.test.ts+++ b/services/mcp/tests/unit/tool-search.test.ts@@ -43,6 +43,24 @@ describe('tool-search', () => {expect(searchToolsRegex(TOOLS, 'feature-flag').map((t) => t.name)).toEqual(['feature-flag-get-all'])})+ it('puts a name match ahead of a tool that only mentions the term in prose', () => {+ // `search persons-` reaches cohort tools through their descriptions. Catalog order+ // listed them first, so the tools the search named came back below them.+ const tools: SearchableTool[] = [+ { name: 'cohorts-create', title: 'Create cohort', description: 'Build a cohort of persons-.' },+ { name: 'persons-list', title: 'List persons', description: 'List people in the project.' },+ ]++ expect(searchToolsRegex(tools, 'persons-').map((t) => t.name)).toEqual(['persons-list', 'cohorts-create'])+ })++ it('keeps catalog order between two tools that match on the same field', () => {+ expect(searchToolsRegex(TOOLS, 'dashboard-').map((t) => t.name)).toEqual([+ 'dashboard-create',+ 'dashboard-get-all',+ ])+ })+it('throws on an invalid regex so callers can surface their own message', () => {expect(() => searchToolsRegex(TOOLS, '[invalid')).toThrow(/regular expression/i)})