implementing-mcp-tools
Skill721 starsChanged 58 days agoMITView on GitHub
Guide for exposing PostHog product endpoints as MCP tools. Use when creating new or updating API endpoints, adding MCP tool definitions, scaffolding YAML configs, or writing serializers with good descriptions. Covers the full pipeline from Django serializer to generated TypeScript tool handler.
2 other projects use a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
- phughesmcr/deno-mcp-templatetheir own version, first seen Mar 28, 2026
Other versions2 projects use a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 1
- phughesmcr/deno-mcp-template★ 33same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- common/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- rust/CLAUDE.mdCLAUDE.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- autoresolving-pr-conflictsSkill
- building-product-empty-statesSkill
- clickhouse-migrationsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- ingestion-pipeline-doctor-nodejsSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
22 rules. About 0 are common, 8 are distinctive, and 14 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
14
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 8
Read the full guide at docs/published/handbook/engineering/ai/implementing-mcp-tools.md.
- Distinctive: line 34
Read the type system guide for the full picture.
- Distinctive: line 36
Before scaffolding YAML, verify:
- Very specific: line 41
Use ListField(child=serializers.CharField()) instead of bare ListField(), and @extend_schema_field(PydanticModel) on JSONField subclasses …
- Very specific: line 51
If a generated tool has an empty or wrong schema, the fix is almost always on the Django side, not in the YAML config.
- Distinctive: line 57
When a product exposes API endpoints that agents should be able to call.
- Distinctive: line 60
If you're adding a new endpoint, check whether it should be agent-accessible.
- Distinctive: line 61
If yes, add a YAML definition and generate the tool.
- Very specific: line 96
Add operation verbs such as freeze, publish, or emit.
- Very specific: line 103
Convention: should match the product folder name, e.g.
- Very specific: line 179
Add feature_flag to any tool (standard or query wrapper) to gate its exposure on a PostHog feature flag evaluated at MCP init time for the …
- Very specific: line 197
Or gate it: Add feature_flag with feature_flag_behavior: enable and turn the flag on once the route is serving.
- Very specific: line 199
Keep the old name on a rename: Leave the previous tool name in the YAML, pointing at the same operation, until the new name has deployed …
- Very specific: line 220
Keep the redirect entry until the old name stops receiving traffic.
- Very specific: line 246
Use help_text on serializer fields — it becomes the OpenAPI description.
- Very specific: line 247
Use param_overrides in YAML to override generated descriptions with imperative instructions.
- Distinctive: line 249
Avoid jargon that an LLM wouldn't understand without context.
- Very specific: line 253
Every list/get endpoint should have a corresponding HogQL system table in posthog/hogql/database/schema/system.py.
- Very specific: line 259
Use mcp_version: 1 on read/list YAML tools when a system table covers the same data — v2 agents use SQL instead.
- Very specific: line 262
When adding a system table, also add a model reference file (models-<domain>.md) in …
- Distinctive: line 268
v1 (legacy): all CRUD tools exposed, for clients without skill support.
- Very specific: line 269
v2 (SQL-first): read/list tools replaced by HogQL, create/update/delete tools kept.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Implementing MCP tools
Read the full guide at docs/published/handbook/engineering/ai/implementing-mcp-tools.md.
Quick workflow
# 1. Scaffold a starter YAML with all operations disabled.
# --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. Configure the YAML — enable tools, add scopes, annotations, descriptions
# Place in products/<product>/mcp/*.yaml (preferred) or services/mcp/definitions/*.yaml
# 3. 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/
# 4. Generate handlers and schemas
hogli build:openapiBefore 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. UseListField(child=serializers.CharField())instead of bareListField(), and@extend_schema_field(PydanticModel)onJSONFieldsubclasses to get typed Zod output (seeproducts/alerts/backend/presentation/views/alert.pyfor the pattern). - Plain
ViewSetmethods have@extend_schema(request=...)— without it, drf-spectacular can't discover the request body and the generated tool getsz.object({})(zero parameters).ModelViewSetwith aserializer_classis fine; plainViewSetwith 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
TRAILING_ACTIONS set in the
same change. If a rendered domain can end in an operation verb that is not
already present, add the verb. Cover it in
services/mcp/tests/unit/instructions.test.ts. This applies even when the verb
is not the final segment of the full tool name.
Add operation verbs such as freeze, publish, or emit. Do not add resource
or capability nouns such as config, logs, stats, or schedule merely to
make the prompt shorter; those remain useful discovery domains.
Feature identifiers
- Format: lowercase snake*case — only
[a-z0-9*], must start with a letter - Convention: should match the product folder name, e.g.
error_tracking,feature_flags
Why 52 characters?
MCP clients enforce different limits on tool names. The 52-char limit is the safe zone that works across all known clients:
| Client | Limit | Notes |
| MCP spec (draft) | 1–128 chars, [A-Za-z0-9_\-.] | Official recommendation, not enforced |
| Claude Code | 64 chars | Hard limit; prefixes tool names with mcp____ |
| Cursor | 60 chars combined | server_name + tool_name; tools over this are silently filtered |
| OpenAI API | ^[a-zA-Z0-9_-]+$, 64 chars | No dots allowed |
With the server name "posthog" (7 chars) plus a separator, tool names must stay at or below 52 characters to fit within Cursor's 60-char combined limit.
CI enforcement
pnpm --filter=@posthog/mcp lint-tool-names— validates length and pattern for YAML and JSON definitions- A vitest test validates all runtime
TOOL_MAPandGENERATED_TOOL_MAPentries
YAML definitions
YAML files configure which operations are exposed as MCP tools. See existing definitions for patterns:
products/<product>/mcp/*.yaml— preferred, keeps config close to the codeservices/mcp/definitions/*.yaml— fallback for functionality without a product folder
The build pipeline discovers YAML files from both paths.
Key fields
category: Human readable name
feature: snake_case_name # should match the product folder name (used for runtime filtering)
url_prefix: /path # frontend app route, used for enrich_url links
tools:
your-tool-name: # kebab-case
operation: operationId_from_openapi
enabled: true
scopes:
- your_product:read
annotations:
readOnly: true
destructive: false
idempotent: true
# Optional:
mcp_version: 1 # 2 for create/update/delete ops, 1 for read/list if available via HogQL
title: List things
description: >
Human-friendly description for the LLM.
list: true
enrich_url: '{id}'
param_overrides:
name:
description: Custom description for the LLM
response: # filter response fields (applied per-item on list endpoints)
include: [id, key, name] # keep only these fields (dot-path wildcards supported)
exclude: [filters.groups.*.properties] # remove these fields
# include and exclude are mutually exclusive
selectable: true # add optional `fields` param so the agent picks a subset of `include` per call
# (constrained to the allowlist); omit `fields` to return the full set. Requires `include`.
strip_nulls: true # remove keys whose value is `null`, applied after include/exclude
# Use it on tools that echo a nested serializer schema, where the unset optional fields
# dominate the payload. Rejected with `list: true`, where per-row null removal makes the
# TOON table larger. Use `exclude` to drop the fields on a list tool instead.
feature_flag: my-flag-key # gate this tool behind a PostHog feature flag
feature_flag_behavior: enable # 'enable' (default) or 'disable'Unknown keys are rejected at build time (Zod .strict()).
Gating tools with feature flags
Add feature_flag to any tool (standard or query wrapper) to gate its exposure on a PostHog feature flag evaluated at MCP init time for the current user.
feature_flag_behavior: enable(default) — tool is shown only when the flag is on. Use for rolling out new tools.feature_flag_behavior: disable— tool is hidden when the flag is on. Use for sunsetting old tools.
Reusing the same flag key with both behaviors performs an atomic swap: flag on → new tool visible, old tool hidden; flag off → old tool visible, new tool hidden. Useful for A/B testing tool variations.
Flags are evaluated in parallel at init via evaluateFeatureFlags. If a flag can't be evaluated (service error, missing flag), enable-gated tools are excluded and disable-gated tools are included — fail-closed for new tools, fail-open for existing ones.
Enabling or renaming a tool
The MCP server and Django deploy separately. A tool that reaches clients before its route lands returns 404 on every call until the Django deploy catches up. That hits a whole agent fleet at once.
- Land the route first. Ship the endpoint, then enable the tool in a later change. A tool with
enabled: truein the same commit as a brand-new route is live in clients as soon as the MCP server deploys. - Or gate it. Add
feature_flagwithfeature_flag_behavior: enableand turn the flag on once the route is serving. - Keep the old name on a rename. Leave the previous tool name in the YAML, pointing at the same
operation, until the new name has deployed everywhere. Sunset it with
feature_flag_behavior: disableon the same flag key, which swaps the two atomically.
Deprecating a tool
A rename or a removal has two stages. Do not stop after the first one.
- Alias, while callers migrate. Register the old name as a thin wrapper that calls the current
handler and adds a
_deprecation_noticeto the response. The call still succeeds, and the agent learns the new name. Seeservices/mcp/src/tools/skills/deprecatedAliases.ts, spread intoTOOL_MAPinservices/mcp/src/tools/index.ts. Use an alias only when the replacement accepts the same arguments. - Redirect, when you delete the alias. In the same change, add the old name to
DEPRECATED_TOOL_REDIRECTSinservices/mcp/src/tools/exec.ts. The call then fails with adeprecated_toolerror that names the replacement, instead of the genericUnknown tool: "...". State any argument changes in the text — see theself-driving-inbox-getentry.
Go directly to stage 2 when the replacement is not a drop-in. An alias that quietly drops renamed parameters is worse than a call that fails.
Keep the redirect entry until the old name stops receiving traffic. isRecordableToolName records
$mcp_exec_target_tool only for a name the server owns: a live tool, or a
DEPRECATED_TOOL_REDIRECTS key. If you delete the entry too early, the remaining calls become
unattributable in MCP analytics, and you can no longer tell whether anything still uses the old name.
A tool that a feature flag removes is a different case. It keeps its definition, declares
superseded_by in the YAML, and flagGatedToolMessage answers the call.
Syncing after endpoint changes
pnpm --filter=@posthog/mcp run scaffold-yaml -- --sync-allIdempotent and non-destructive — adds new operations as enabled: false, removes stale ones.
Serializer descriptions
Descriptions flow through the entire pipeline:
Django serializer field → OpenAPI spec → Zod schema → MCP tool descriptionThese descriptions are what agents read to understand tool parameters.
- Use
help_texton serializer fields — it becomes the OpenAPI description. - Use
param_overridesin YAML to override generated descriptions with imperative instructions. - Be specific about formats, constraints, and valid values.
- Avoid jargon that an LLM wouldn't understand without context.
HogQL system tables
Every list/get endpoint should have a corresponding HogQL system table
in posthog/hogql/database/schema/system.py.
This lets agents query data via SQL in v2 of the MCP.
Each system table must include a team_id column for data isolation.
Use mcp_version: 1 on read/list YAML tools when a system table covers the same data —
v2 agents use SQL instead.
When adding a system table, also add a model reference file
(models-<domain>.md) in products/posthog_ai/skills/querying-posthog-data/references/
and register it in products/posthog_ai/skills/querying-posthog-data/SKILL.md under Data Schema.
Two MCP versions
- v1 (legacy): all CRUD tools exposed, for clients without skill support.
- v2 (SQL-first): read/list tools replaced by HogQL, create/update/delete tools kept. For coding agents.
Control per-tool availability with mcp_version: 1/2 in the YAML definition.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

