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.

fix(mcp): exclude community questions from docs-search results (#78017)

posthog[bot]493b4e0d160d
diff --git a/ee/hogai/tools/search.py b/ee/hogai/tools/search.py
index 11a5470594ce..b1aa8b51a840 100644
--- a/ee/hogai/tools/search.py
+++ b/ee/hogai/tools/search.py
@@ -1,5 +1,6 @@
import asyncio
from typing import Literal
+from urllib.parse import urlparse
from django.conf import settings
@@ -264,6 +265,19 @@ URL: {url}
{text}
""".strip()
+COMMUNITY_QUESTIONS_HOSTNAMES = frozenset({"posthog.com", "www.posthog.com"})
+COMMUNITY_QUESTIONS_PATH = "/questions"
+
+
+def is_community_question_url(url: str) -> bool:
+ """Community Questions are user-submitted and often outdated, so docs search must never surface them."""
+ parsed = urlparse(url)
+ if parsed.hostname not in COMMUNITY_QUESTIONS_HOSTNAMES:
+ return False
+ path = parsed.path.rstrip("/")
+ # `/docs/…/common-questions` pages are real docs, so match the path prefix rather than the substring
+ return path == COMMUNITY_QUESTIONS_PATH or path.startswith(f"{COMMUNITY_QUESTIONS_PATH}/")
+
async def perform_inkeep_docs_search(query: str, *, include_system_reminder: bool = True) -> str:
model = ChatOpenAI(
@@ -287,6 +301,8 @@ def format_inkeep_docs_response(rag_context_raw: dict | None, *, include_system_
Shared between the LangChain `InkeepDocsSearchTool` (which uses ChatOpenAI) and the
typed `MCPToolsViewSet.docs_search` endpoint (which uses the plain openai client).
+
+ Community Questions are dropped here so the results match what the tool prompts promise.
"""
docs: list[str] = []
if rag_context_raw and rag_context_raw.get("content"):
@@ -294,6 +310,8 @@ def format_inkeep_docs_response(rag_context_raw: dict | None, *, include_system_
for doc in rag_context.content:
if doc.type != "document":
continue
+ if is_community_question_url(doc.url):
+ continue
text = doc.source.content[0].text if doc.source.content else ""
docs.append(DOC_ITEM_TEMPLATE.format(title=doc.title, url=doc.url, text=text))
diff --git a/ee/hogai/tools/test/test_search.py b/ee/hogai/tools/test/test_search.py
index c32b1c58632a..90f960ff7d02 100644
--- a/ee/hogai/tools/test/test_search.py
+++ b/ee/hogai/tools/test/test_search.py
@@ -3,14 +3,22 @@ from uuid import uuid4
from posthog.test.base import ClickhouseTestMixin, NonAtomicBaseTest
from unittest.mock import AsyncMock, MagicMock, patch
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
from langchain_core import messages
from langchain_core.runnables import RunnableConfig
+from parameterized import parameterized
from ee.hogai.context.context import AssistantContextManager
from ee.hogai.tool_errors import MaxToolFatalError, MaxToolRetryableError
-from ee.hogai.tools.search import DOC_ITEM_TEMPLATE, DOCS_SEARCH_RESULTS_TEMPLATE, InkeepDocsSearchTool, SearchTool
+from ee.hogai.tools.search import (
+ DOC_ITEM_TEMPLATE,
+ DOCS_SEARCH_NO_RESULTS_TEMPLATE,
+ DOCS_SEARCH_RESULTS_TEMPLATE,
+ InkeepDocsSearchTool,
+ SearchTool,
+ format_inkeep_docs_response,
+)
from ee.hogai.utils.tests import FakeChatOpenAI
from ee.hogai.utils.types import AssistantState
from ee.hogai.utils.types.base import NodePath
@@ -129,3 +137,41 @@ class TestInkeepDocsSearchTool(ClickhouseTestMixin, NonAtomicBaseTest):
self.assertEqual(mock_llm_class.call_args.kwargs["base_url"], "https://api.inkeep.com/v1/")
self.assertEqual(mock_llm_class.call_args.kwargs["api_key"], "test-inkeep-key")
self.assertEqual(mock_llm_class.call_args.kwargs["streaming"], False)
+
+
+class TestFormatInkeepDocsResponse(SimpleTestCase):
+ @staticmethod
+ def _payload(*urls: str) -> dict:
+ return {
+ "content": [
+ {
+ "type": "document",
+ "record_type": "page",
+ "url": url,
+ "title": f"Title for {url}",
+ "source": {"type": "text", "content": [{"type": "text", "text": "Body"}]},
+ }
+ for url in urls
+ ]
+ }
+
+ @parameterized.expand(
+ [
+ ("community_question", "https://posthog.com/questions/how-do-i-mask-inputs", False),
+ ("community_question_www", "https://www.posthog.com/questions/how-do-i-mask-inputs", False),
+ ("community_questions_index", "https://posthog.com/questions", False),
+ ("docs_page", "https://posthog.com/docs/session-replay/privacy", True),
+ ("docs_common_questions", "https://posthog.com/docs/data/common-questions", True),
+ ("tutorial", "https://posthog.com/tutorials/session-recordings-for-support", True),
+ ("github_issue", "https://github.com/PostHog/posthog-js/issues/2292", True),
+ ]
+ )
+ def test_filters_community_questions(self, _name: str, url: str, expected_included: bool) -> None:
+ result = format_inkeep_docs_response(self._payload(url, "https://posthog.com/docs/getting-started"))
+
+ self.assertEqual(url in result, expected_included)
+
+ def test_returns_no_results_when_every_result_is_a_community_question(self) -> None:
+ result = format_inkeep_docs_response(self._payload("https://posthog.com/questions/why-no-recordings"))
+
+ self.assertEqual(result, DOCS_SEARCH_NO_RESULTS_TEMPLATE)