testing-mcp-tools-locally
Skill721 starsChanged 59 days agoMITView on GitHub
Set up the local dev environment, seed data, and API keys to test the staff-only managed migrations MCP tools (managed-migrations-support-list, managed-migrations-support-get) end to end. Use when testing batch import support tooling, debugging MCP tool responses or discovery (tools not appearing), or verifying the support API before deploying. Covers the discovery gate: hidden scope, is_staff, user:read, and why wildcard keys and OAuth never work.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
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-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 1 is distinctive, and 9 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 34
If hogli wait fails on migrate-persons-db or migrate-behavioral-cohorts, those are optional separate databases — ignore them.
- Very specific: line 36
If it fails on migrate-postgres, check Docker port forwarding (see troubleshooting below).
- Very specific: line 55
If this fails with connection refused on port 5432, see troubleshooting below.
- Very specific: line 59
Use hogli dev:shell-plus to create BatchImport records in various states.
- Distinctive: line 146
Test with the MCP Inspector CLI:
- Very specific: line 177
Use the Hono server on port 3001 (see step 7), or set MCP_HONO_URL=http://localhost:3001 in .dev.vars.
- Very specific: line 199
Verify: nc -z 127.0.0.1 5432 should succeed.
- Very specific: line 204
Always pass a non-empty dict: secrets={'placeholder': 'true'}.
- Very specific: line 209
If it encounters a config validation error (e.g.
- Very specific: line 211
Stop the worker or seed with non-RUNNING statuses to prevent this.
≈ “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
Testing managed migrations MCP tools locally
Prerequisites
The dev environment must be running with Docker services healthy. The batch import support API and MCP tools require:
- A staff user (
is_staff = True) - A Personal API Key carrying both
batch_import_support:readanduser:read, explicitly - Postgres migrations applied (ClickHouse not required)
Why both scopes: the backend accepts batch_import_support:read alone,
but MCP tool discovery verifies staffness via /api/users/@me/ and hides the tools (fail-closed) when the key cannot make that call.
A * wildcard does not substitute for either — the discovery gate requires the hidden scope explicitly, and the backend's INTERNAL scope handling rejects wildcard keys outright.
For the production setup flow, see docs/support-mcp-tools.md.
1. Start the dev environment
hogli start -d
hogli waitIf hogli wait fails on migrate-persons-db or migrate-behavioral-cohorts,
those are optional separate databases — ignore them.
If it fails on migrate-postgres, check Docker port forwarding (see troubleshooting below).
2. Run Postgres migrations
hogli migrations:runClickHouse migration failures are fine — batch imports only need Postgres.
3. Verify DB connectivity from the Django shell
hogli dev:shell-plus -y -- -c "
from posthog.models import Team, User
print(Team.objects.first(), User.objects.first())
"If this fails with connection refused on port 5432, see troubleshooting below.
4. Seed batch import test data
Use hogli dev:shell-plus to create BatchImport records in various states.
The secrets field is an EncryptedJSONStringField — empty {} serializes to null
and violates the NOT NULL constraint; always pass a non-empty dict.
from products.managed_migrations.backend.models.batch_imports import BatchImport
BatchImport.objects.create(
team=team,
created_by_id=user.id,
status=BatchImport.Status.PAUSED,
import_config={
'source': {'type': 's3', 'bucket': 'test', 'region': 'us-east-1', 'prefix': 'data/'},
'data_format': {'type': 'json_lines', 'skip_blanks': True, 'content': {'type': 'mixpanel'}},
'sink': {'type': 'capture'},
},
secrets={'access_key': 'test', 'secret_key': 'test'},
state={'parts': [
{'key': 'part-1', 'current_offset': 50000, 'total_size': 50000},
{'key': 'part-2', 'current_offset': 10000, 'total_size': 50000},
{'key': 'part-3'},
]},
)See references/seed-data.md for a full seeding script covering all statuses.
Important: the local batch-import-worker process will pick up RUNNING records
and may modify their status (e.g. pausing them due to config validation errors).
To keep records stable for testing, either stop the worker or use COMPLETED/FAILED/PAUSED statuses.
5. Make your user staff and mint test keys
Mint fresh keys rather than editing scopes on an existing one — the MCP server caches a key's scopes per token, so edited scopes can serve stale results.
from posthog.models import User
from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.utils import generate_random_token_personal, hash_key_value
me = User.objects.first()
me.is_staff = True; me.save()
def mint(user, scopes):
token = generate_random_token_personal()
PersonalAPIKey.objects.create(user=user, label=str(scopes)[:40], secure_value=hash_key_value(token), scopes=scopes)
return token
print(mint(me, ["batch_import_support:read", "user:read"]))To test the negative cases of the discovery gate, also mint:
a ["*"] key (tools must NOT appear),
a ["batch_import_support:read"] key without user:read (tools must NOT appear — staff lookup fails closed),
and the full pair on a non-staff user (tools must NOT appear).
6. Test the API directly
# List all batch imports
curl -H "Authorization: Bearer <token>" \
http://localhost:8010/api/managed_migrations_support/ | jq
# Get detail for a specific import
curl -H "Authorization: Bearer <token>" \
http://localhost:8010/api/managed_migrations_support/<uuid>/ | jq7. Test via MCP
Run the Hono server, not pnpm run dev.
The wrangler worker (pnpm run dev, port 8787) proxies /mcp to production mcp.us.posthog.com unless MCP_HONO_URL is set,
so local keys get 401 Invalid API key.
The Hono server serves MCP directly against the local API:
cd services/mcp
cp .dev.vars.example .dev.vars # POSTHOG_API_BASE_URL=http://localhost:8010
pnpm run dev:hono # serves http://localhost:3001/mcpAuthenticate with the PAT as a Bearer header, never the OAuth flow. The hidden scope is structurally absent from OAuth — signing in through the inspector's OAuth login can never surface these tools.
The Hono server runs exec mode: tools/list returns a single exec tool,
and real tools are discovered and invoked through it.
Test with the MCP Inspector CLI:
# Discovery — should list both support tools for the staff key, none for the others
npx @modelcontextprotocol/inspector --cli http://localhost:3001/mcp \
--header "Authorization: Bearer <token>" \
--method tools/call --tool-name exec --tool-arg "command=search managed-migrations-support"
# Invocation — end-to-end through Django
npx @modelcontextprotocol/inspector --cli http://localhost:3001/mcp \
--header "Authorization: Bearer <token>" \
--method tools/call --tool-name exec --tool-arg "command=call managed-migrations-support-list {}"Expected discovery matrix:
| key | tools visible |
staff user, batch_import_support:read + user:read | both |
staff user, * only | none |
staff user, batch_import_support:read without user:read | none (staff lookup fails closed) |
| non-staff user, both scopes | none (and direct API calls 403) |
The interactive Inspector UI (http://localhost:6274) also works —
paste the PAT as the Bearer token in connection settings instead of using its OAuth login.
Troubleshooting
401 "Invalid API key" from localhost:8787
You're talking to the wrangler worker, which proxies /mcp to production — your local key is invalid there.
Use the Hono server on port 3001 (see step 7), or set MCP_HONO_URL=http://localhost:3001 in .dev.vars.
Tools don't appear for a key that should see them
Check, in order:
- The key carries
batch_import_support:readexplicitly —*does not match hidden scopes. - The key also carries
user:read(or*) — the discovery staff check reads/api/users/@me/and fails closed. - The key's user has
is_staff = True. - The key was minted with those scopes from the start — the MCP server caches scopes per token, so mint a fresh key instead of editing an existing one.
Port 5432 not reachable from host
The posthog-db-1 Docker container may have stale port mappings
(container created days ago without the current port binding config).
Fix by force-recreating:
docker compose -f docker-compose.dev.yml -f docker-compose.profiles.yml \
up -d --force-recreate dbVerify: nc -z 127.0.0.1 5432 should succeed.
secrets={} causes NOT NULL violation
EncryptedJSONStringField encrypts the value — an empty dict serializes to null.
Always pass a non-empty dict: secrets={'placeholder': 'true'}.
Batch import worker modifies seeded records
The local batch-import-worker process automatically claims RUNNING records.
If it encounters a config validation error (e.g. missing skip_blanks),
it will pause the import with a detailed Rust backtrace in status_message.
Stop the worker or seed with non-RUNNING statuses to prevent this.
The gates, end to end
A request passes through two independent layers:
- MCP discovery (presentation): a tool requiring an OAuth-hidden scope surfaces only when the key explicitly carries the scope AND
/api/users/@me/confirmsis_staff— otherwise it is hidden, fail-closed (services/mcp/src/lib/staff-only-tools.ts). - Django enforcement (the security boundary):
IsAuthenticated+IsStaffUser+APIScopePermissionwithscope_object = "INTERNAL"andbatch_import_support:read. Sessions need staffness only; PATs need staffness plus the explicit scope;*-only keys always 403.
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.

