Implementing MCP tools
Read the full guide at docs/published/handbook/engineering/ai/implementing-mcp-tools.md.
Quick workflow
# 1. Only for a product with no YAML yet: create one with no tools.
# --product discovers endpoints via their x-product attribution.
# ViewSets in products/<name>/backend/ are auto-attributed via module
# path. ViewSets elsewhere need
# @extend_schema(extensions={"x-product": "<product>"}).
pnpm --filter=@posthog/mcp run scaffold-yaml -- --product your_product \
--output ../../products/your_product/mcp/tools.yaml
# 2. List the product's operations that have no YAML entry, then add the ones agents need.
# --add writes an enabled entry; title and description come from the API unless the YAML sets them.
# Add --file <path> to write to a YAML file other than the product's tools.yaml.
pnpm --filter=@posthog/mcp run scaffold-yaml -- --candidates --product your_product
pnpm --filter=@posthog/mcp run scaffold-yaml -- --add your_product_things_list --product your_product
# 3. Configure the entry — description, and annotations for PATCH/POST/PUT
# (scopes come from the API when omitted)
# Place in products/<product>/mcp/*.yaml (preferred) or services/mcp/definitions/*.yaml
# 4. Add a HogQL system table in posthog/hogql/database/schema/system.py
# and a model reference in products/posthog_ai/skills/querying-posthog-data/references/
# 5. Generate handlers and schemas
hogli build:openapi
# 6. Refresh the tool input schema snapshots (CI unit tests fail on a stale snapshot)
pnpm --filter=@posthog/mcp exec vitest run tests/unit/tool-schema-snapshots.test.ts -u
# A tool behind a new `feature_flag` needs that flag in the test's `featureFlags` map, set to the value that shows the tool:
# true for a plain gate, the variant string for a variant gate, a non-true value for a `disable` gate.
# 7. Only when the YAML uses ui_apps: regenerate the UI apps (CI checks they are current)
pnpm --filter=@posthog/mcp run generate:ui-apps
Before you scaffold: fix the backend first
The codegen pipeline can only generate correct tools if the Django backend exposes correct types.
Read the type system guide for the full picture.
Before scaffolding YAML, verify:
- Serializers have explicit field types and
help_text—
these flow all the way to Zod .describe() in the generated tool.
Missing descriptions = agents guessing at parameters.
Use ListField(child=serializers.CharField()) instead of bare ListField(),
and @extend_schema_field(PydanticModel) on JSONField subclasses to get typed Zod output
(see products/alerts/backend/presentation/views/alert.py for the pattern).
- Plain
ViewSetmethods have@extend_schema(request=...)—
without it, drf-spectacular can't discover the request body
and the generated tool gets z.object({}) (zero parameters).
ModelViewSet with a serializer_class is fine; plain ViewSet with manual validation is not.
- Query parameters use
@validated_requestor@extend_schemawith a query serializer —
otherwise boolean and array query params may produce type mismatches in the generated code.
If a generated tool has an empty or wrong schema, the fix is almost always on the Django side,
not in the YAML config.
For a full audit checklist and before/after examples, use the improving-drf-endpoints skill.
When to add MCP tools
When a product exposes API endpoints that agents should be able to call.
MCP tools are atomic capabilities (list, get, create, update, delete) — not workflows.
If you're adding a new endpoint, check whether it should be agent-accessible.
If yes, add a YAML definition and generate the tool.
Tool design
Tools should be basic capabilities — atomic CRUD operations and simple actions.
Agents compose these primitives into higher-level workflows.
Good: "List feature flags", "Get experiment by ID", "Create a survey".
Bad: "Search for session recordings of an experiment" — bundles multiple concerns.
Tool naming constraints
Tool names and feature identifiers are validated at build time and in CI.
Violations fail the build.
Tool names
- Format: lowercase kebab-case — only
[a-z0-9-], no leading/trailing hyphens - Length: 52 characters or fewer
- Convention:
domain-action, e.g.cohorts-create,dashboard-get,feature-flags-list
Keep action verbs out of compact tool domains
The single-exec prompt builds its compact domain index with
ToolDomainExtractor. Large families can split at an intermediate segment.
Without action trimming, experiment-freeze-exposure can advertise the
redundant domain experiment-freeze instead of experiment.
Whenever you add or rename an action tool, check the