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.

feat(api): x-internal schema marker for mcp-only endpoints (#106215)

Alex Lider07e5614a68d2
diff --git a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md b/docs/published/handbook/engineering/ai/implementing-mcp-tools.md
index a9af7dbe46ec..7b11ecdd3306 100644
--- a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md
+++ b/docs/published/handbook/engineering/ai/implementing-mcp-tools.md
@@ -547,6 +547,14 @@ Runtime access still comes from the viewset's `scope_object`,
and any per-action `required_scopes` or `dangerously_get_required_scopes` overrides.
Only mark the actions you actually want PATs, OAuth tokens, and MCP clients to call.
+### MCP-only endpoints
+
+An endpoint that is in the public schema becomes a REST contract: it shows up in Swagger, Redoc and the API docs, and people build on it.
+To generate MCP tools and frontend types for an endpoint without that contract, mark it with `@extend_schema(extensions={"x-internal": True})`.
+The codegen build (`hogli build:openapi`, which sets `OPENAPI_INCLUDE_INTERNAL=1`) keeps the operation.
+The served `/api/schema/` drops it, the same way `@extend_schema(exclude=True)` does.
+The marker only controls schema inclusion. The endpoint stays reachable, and auth and scopes still apply.
+
## HogQL query schemas (WIP)
[`frontend/src/queries/schema/schema-assistant-queries.ts`](https://github.com/PostHog/posthog/blob/master/frontend/src/queries/schema/schema-assistant-queries.ts) defines structured query types
diff --git a/posthog/api/documentation.py b/posthog/api/documentation.py
index b76b904fac3f..73962bb37500 100644
--- a/posthog/api/documentation.py
+++ b/posthog/api/documentation.py
@@ -80,10 +80,22 @@ class _FallbackSerializer(serializers.Serializer):
pass
+def _include_internal_operations() -> bool:
+ return os.environ.get("OPENAPI_INCLUDE_INTERNAL", "").lower() in ("1", "true")
+
+
class PostHogAutoSchema(AutoSchema):
"""AutoSchema subclass that silences path-parameter warnings for params
handled by TeamAndOrgViewSetMixin (project_id, environment_id, etc.)."""
+ def is_excluded(self) -> bool:
+ if super().is_excluded():
+ return True
+ # `x-internal` keeps an operation out of the served schema (Swagger, Redoc, the public
+ # API docs) but in the codegen build, so MCP tools and frontend types still cover it
+ # without a public REST contract.
+ return bool(self.get_extensions().get("x-internal")) and not _include_internal_operations()
+
def _resolve_path_parameters(self, variables):
from drf_spectacular.plumbing import get_view_model, resolve_django_path_parameter, resolve_regex_path_parameter
@@ -746,7 +758,7 @@ def preprocess_exclude_path_format(endpoints, **kwargs):
vs organization_id, etc.).
"""
# For frontend type generation, include INTERNAL views if they have explicit tags
- include_internal = os.environ.get("OPENAPI_INCLUDE_INTERNAL", "").lower() in ("1", "true")
+ include_internal = _include_internal_operations()
# Clear previous mappings
_endpoint_product_mapping.clear()
diff --git a/posthog/api/test/test_api_docs.py b/posthog/api/test/test_api_docs.py
index 6f9521e917f3..dfc21ee8d384 100644
--- a/posthog/api/test/test_api_docs.py
+++ b/posthog/api/test/test_api_docs.py
@@ -1,6 +1,29 @@
+import os
import re
from posthog.test.base import APIBaseTest
+from unittest import mock
+
+from django.urls import path
+
+from drf_spectacular.generators import SchemaGenerator
+from rest_framework import serializers, viewsets
+from rest_framework.request import Request
+from rest_framework.response import Response
+
+from posthog.api.documentation import extend_schema
+
+
+class _XInternalMarkerSerializer(serializers.Serializer):
+ ok = serializers.BooleanField(help_text="Always true.")
+
+
+class _XInternalMarkerViewSet(viewsets.ViewSet):
+ scope_object = "project"
+
+ @extend_schema(responses={200: _XInternalMarkerSerializer}, extensions={"x-internal": True})
+ def list(self, request: Request) -> Response:
+ return Response({"ok": True})
class TestAPIDocsSchema(APIBaseTest):
@@ -19,6 +42,17 @@ class TestAPIDocsSchema(APIBaseTest):
# The same action survives under the live project route
assert any(p.endswith("/tracing_config/") for p in paths)
+ def test_x_internal_operations_are_only_in_the_codegen_schema(self) -> None:
+ patterns = [path("api/x_internal_marker/", _XInternalMarkerViewSet.as_view({"get": "list"}))]
+
+ served_schema = SchemaGenerator(patterns=patterns).get_schema(request=None, public=True)
+ assert "/api/x_internal_marker/" not in served_schema["paths"]
+
+ codegen_env = {"OPENAPI_INCLUDE_INTERNAL": "1", "OPENAPI_MOCK_INTERNAL_API_SECRET": "1"}
+ with mock.patch.dict(os.environ, codegen_env):
+ codegen_schema = SchemaGenerator(patterns=patterns).get_schema(request=None, public=True)
+ assert "/api/x_internal_marker/" in codegen_schema["paths"]
+
def test_can_generate_api_docs_schema(self) -> None:
self.client.logout()