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(mcp-analytics): let teams name their own harnesses (#124838)

Paul D'Ambra62b7efbaed14
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 1f01fc2122bd..0879551bde1e 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -315,6 +315,7 @@ rules:
|RelationshipProposal
|MCPAnalyticsSubmission
|MCPAuditEvent
+ |MCPCustomHarness
|MCPGatewayServer
|MCPIntentClusterSnapshot
|MCPIntentEmbeddingCache
@@ -823,6 +824,7 @@ rules:
|MetricsDashboardSuggestion
|RelationshipProposal
|MCPAuditEvent
+ |MCPCustomHarness
|MCPGatewayServer
|MCPIntentClusterSnapshot
|MCPIntentEmbeddingCache
diff --git a/docs/published/docs/mcp-analytics/harness-share.mdx b/docs/published/docs/mcp-analytics/harness-share.mdx
index a216df7658cd..7888676181f6 100644
--- a/docs/published/docs/mcp-analytics/harness-share.mdx
+++ b/docs/published/docs/mcp-analytics/harness-share.mdx
@@ -28,6 +28,12 @@ Remaining named harnesses use brand colors or stable palette colors.
PostHog assigns each call a harness from the client identity captured with it: the vendor header, the User-Agent, and the client name.
[How the harness label is resolved](/docs/mcp-analytics/events#how-the-harness-label-is-resolved) lists the order PostHog checks them in, and what to check if most calls land in **Other harnesses**.
+## Name your own harnesses
+
+To name an in-house harness, go to **Settings > Environment > MCP analytics > Custom harnesses**. Add a name and the text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name. Your custom names are checked before the built-in ones. When the text of two custom harnesses matches, the longer text wins, so `acme-agent` takes priority over `acme`. Each project can have up to 20.
+
+The custom name shows wherever harness names show, including the harness share card and the activity views. Harness names are cached, so a change can take a few minutes to appear.
+
## Harness labels in SQL
`properties.$virt_mcp_harness` returns the resolved harness label as a string on MCP events, and `NULL` on other events.
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 96c6760aa66b..a0f8b00abdb3 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -2685,9 +2685,9 @@ snapshots:
components-search--product-recents-and-starred--light:
hash: v1.k794b7964.b4341fc26c099d3e62aacbd6273249487b1a190273edae9c6e2d6448dbba5aff.IOJTS2PvP9IC17AmGQ4kVnbrTyYv4S-zOBFCyEheZYM
components-search--searching--dark:
- hash: v1.k794b7964.1fcd55397472722b2179ec09a87280387b3b85f412c4c59f4e13c51a36353ce5.L2vWd0VYf0qYBkJpsBLNgtBM6hZnHOS4PQ1Hb0quaAQ
+ hash: v1.k794b7964.baa3c7681dc8731116b48413c5c5327b6336a80e0f28fc38ecd0524d34a67f92.hPEQjTOsiG0z2z5WePcp_RuzvutOk1tbLGurRMkQO38
components-search--searching--light:
- hash: v1.k794b7964.0ee80593ce6cc44fcd6b762bd8075ed17797258d565ebedfec8bae3df2ee0739.okRC_hCeGJH0YYzy-c64u6U4MgUoy1AUcpn8bhnz09E
+ hash: v1.k794b7964.1bbad924a0ddf096e30d29f6c4ce1cb408447dfe63a2b1a5e660585cf3028bd0.MT6S3qF8y9gTTjtNvlBAWxUmRaua7PeuZD2_QMQ8O7w
components-sentencelist--full-sentence--dark:
hash: v1.k794b7964.7e8e5dd2e09d246cc3668f362f2391fe67618bd1143aa393832297f5dc30e558.yptEWKc5RAUY613Fwd5cQ0VIinMvpdoI6cuKgagAGxk
components-sentencelist--full-sentence--light:
@@ -14187,45 +14187,45 @@ snapshots:
scenes-app-mcp-analytics--activity--light:
hash: v1.k794b7964.c04cc27b140152a363fb6e789aabcb4b4453d4798ca8fe81c460be84b1284ab5.HzUuZj5kOugUnIrnPIlW9GVDNLORwJWXcs9Le_FM9Ow
scenes-app-mcp-analytics--activity-weekly-report-offer--dark:
- hash: v1.k794b7964.015d41d53e1e8a466f1298fbf4650bf693c8cf4a032881ef88e5ac26f27a4acf.Ap1TLeW53SN53WtDCXBqV7RKiqk8sSYJTWOa_JPqWTE
+ hash: v1.k794b7964.789d157cb1aaa7fe614fbdc0236843b864e7e914516f4315ce12f8339c22e46d.le1K0g8TxCmW9mdUUPibKLInFDQQtk8qiV7g2xVRZ8Y
scenes-app-mcp-analytics--activity-weekly-report-offer--light:
- hash: v1.k794b7964.c2a31ef509fca27a5b553a75be3b0513563a2120bceac7fe0fe5183628d536d0.3bg1-20HvCvb3wYelRjuOtNmaNBbvD8L3vyEC7c2MvM
+ hash: v1.k794b7964.84bea3c747f0653a85a61a1ebc609dfcdea73256f22ab444b7c6643598bf1054.7ywKk4orb_d1ZFonKuc9DOC3pgRpLh_d5Gkhf2bnBWw
scenes-app-mcp-analytics--activity-weekly-report-offer-failed--dark:
- hash: v1.k794b7964.9e931d7beb03f0369c1cfcff95292acd3afaeb1a6b7461644188041bf8a53405.q7_gUO4HHpIOGMhJQfNMTIQPKu9QBeOnjKrUMoN5EiM
+ hash: v1.k794b7964.e0733df2d22c4e393d1a5d61f75af02149e42c894f3ae030aaddf70027ed9031.a6eVLvHxsChKQvl_Jxi7gYLmbZTNnwU0vJesLu-Yl1k
scenes-app-mcp-analytics--activity-weekly-report-offer-failed--light:
- hash: v1.k794b7964.f87c7f7ed986ceeb889147d6995fbf0b09d7723c7623e2303d367d6ab292a5cc.oQ58OGokdXnymMViiDfdzR8S-K0NJhxesoPLcnmnBkQ
+ hash: v1.k794b7964.5633e98797141eb4e69c199f9cfac0b555b8bb7b79355c8050b76480884d6d3d.bXxZFp5KTOU-6nzuo54wDEwCdRmvPw8YoC077t0adZE
scenes-app-mcp-analytics--activity-weekly-report-offer-narrow--dark:
hash: v1.k794b7964.58f914c4e06a5dc7a4ff15d2d5d7275422eda120054ad8acd2022ac5c9e52740.MepuCgXQOk1feYUH4PNgdD5tvqoGu1lqAIDEyPA6PLY
scenes-app-mcp-analytics--activity-weekly-report-offer-narrow--light:
hash: v1.k794b7964.658fa4dc8524b276a16f818adb63791012f6d8868a7bec2e8f8214202b775a9b.axbru-R97XHtl3u-i8RhkXLPQx33wWyk0rADnPhcxu8
scenes-app-mcp-analytics--activity-weekly-report-offer-needs-consent--dark:
- hash: v1.k794b7964.83e185e1e11ee68625fd8d44f54097ff5567e32fb9a2d76f87b92f9aa5e6b4e9.pTLGthRIF3-gSVAuijDIo7ItzG_jTIwGQcikpAWhg08
+ hash: v1.k794b7964.1edcf7ec1509fb522d173c17207bcd0db18f3b1266595b9d5ec046771d276226.I5uYlfiELlLmP2zkUKyV8sAlWNabVHij5YTR7Ct0TqM
scenes-app-mcp-analytics--activity-weekly-report-offer-needs-consent--light:
- hash: v1.k794b7964.2daa5ec0a27c8907f8184097aaf3c126c314dde8c3fa8da8f8c37d26ade16848.L5-MBfozdbWrV8D23tlz_FW618NAzE5xiGvHtgFRTe8
+ hash: v1.k794b7964.3b6118de5cdbefab9a5f6f4da4e9b1a08256205e9759a7b2513ce6f910cef6f4.Zy--S0P7PsFtf0HUmt1TxJVjryb5Sai-r_w5RLDhjGU
scenes-app-mcp-analytics--dashboard--medium--dark:
- hash: v1.k794b7964.dd7d59b4a53412ddfa4d33b8752189a73ac5567ba08076d4421081564f7f182b.HVcCDSxVMKWB4PP8x3x9ffFtOwVpQlMSHQky7E23wjY
+ hash: v1.k794b7964.72b34dcc72acd1363067999df342b7c8227615bd56ea444bb94157fccb94525a.p3FQs0zUXHt1njxeUQnqtD0VTUeFyh6LhoVqr6-5V3A
scenes-app-mcp-analytics--dashboard--medium--light:
- hash: v1.k794b7964.8074f1d57a1364b26bfbd1a90775b2d40b7a967a1aa2f9f880c6c5670d38e0d3.ephjpwogAey_FREiUZPilJlN4n6o90lNyUvuAPgIzpA
+ hash: v1.k794b7964.ae68da6740384177f5ea69e2179a852e0d7504d0df1568db41da7106f4bd952b.IOvDYXWOuFwQusjjHHDXr3B7M92r97lqYLmb6owx4tA
scenes-app-mcp-analytics--dashboard--wide--dark:
- hash: v1.k794b7964.48f94778f0f08845dd257aa2d12ff1a12824702c3916ce436d3185e29947ae78.XWTwki8LReYnXoJamTiQQFn1nQg0zaX_4jPB0XGWgec
+ hash: v1.k794b7964.f79126af5a2cb50c1923338a9938cf4cdc96b83c6f86b1eadd98be45261d92aa.ToZxRvAj-XzCLuvi6PrUKQes8vrLTJSC_T0Tx-qzQsM
scenes-app-mcp-analytics--dashboard--wide--light:
- hash: v1.k794b7964.340966c0065f43f16f14c1851dccc95af6b2e893ed02093d4c2a22c59030d8d5.tgVoQgV0yZngM7l64DF2m_oPDp7dcKpFDDhmX9QqHNI
+ hash: v1.k794b7964.7a99a539411c23385d2ab1e0de4de21feb851976dcf301f7a1fb7a90fb3a57fb.lclXNkXG19LrcpaXMPogrc7AwHycJS8IrHwH1F1mvkU
scenes-app-mcp-analytics--dashboard-leaderboard-home--medium--dark:
- hash: v1.k794b7964.62ed4d36f3a4336c04ee7fd61f0bbbb0cbc473ab207e8160f21fbaf8a11547a3.AdEpafadet6zpyXEQZpgSwrXM_LmOxWkqhOFeLgw9r0
+ hash: v1.k794b7964.cc617d03a0676b9eab117c886f2fc514037e36f5bb563a8143b648029482d5c3.2MB3M9XRydrmy7DvX4Qy4ugJQ_J4XrHNcHH2T6uqhgw
scenes-app-mcp-analytics--dashboard-leaderboard-home--medium--light:
- hash: v1.k794b7964.2fac1d26fa23636dafef5b086afbed99864f47c4881496f57b1b4d74b2b56bb6.lCjpjz7Q5HfYQBxcz9l_Gb7e0lDS4rEUGBlb4THxMHU
+ hash: v1.k794b7964.8869199cec304c19185a270be1ce4d5b5581dd8b45830bb0a2636d03f5ffad37.0ycaCmVvU5r9xBX5dM4cc6vKY2U1BgR0VX_6tZUvU_E
scenes-app-mcp-analytics--dashboard-leaderboard-home--wide--dark:
- hash: v1.k794b7964.5448571cc722665f12483a3545f1a14932da9438ba02948743758f2efa9584b5.V-TxQ52GcdP-pKUxDQ5YWGPgP7c0xp7x-oiayABvd3w
+ hash: v1.k794b7964.ead69153326044d46b000996f3fe025612036c5c929e64635d890d42072ea4e2.8cifiIaJ6C4AQwYOaAP9ZgVkkACFMxH6neIVYKgc4Js
scenes-app-mcp-analytics--dashboard-leaderboard-home--wide--light:
- hash: v1.k794b7964.ae0a552da5dab344b6d2c577892a47e3ba7570c7d46b62addd9600cb3cf20422.yqPk6pti-p1W9G0d7xF20BvMcFXjpzEIrakYD9iJuMo
+ hash: v1.k794b7964.5c7c57637425377c33efd09d06857752150c6e31eb4dfa0253f17b2cf017ddd9.P8VrrTQkC0kHMLxjkLjTuDHzfwC3CeJsx7gjOOBvq9E
scenes-app-mcp-analytics--dashboard-narrow--dark:
- hash: v1.k794b7964.ad1f901d628a63d3ee7e37d765b8ad88bd40a9f117e98fb98bc083d54af2213e.i4plYiLDJthqGoGeC2YIFIOZBeQzTfZq_gJXCZGW3vs
+ hash: v1.k794b7964.ecb26dc86a6f09bfbbc13681cbc9fa007c4248e103f4a3b43e778eaf1f3b0de4.vmpwQjCd_B9zIyTYNvlY-aMSkfcbtOPBq-tTiuX-D68
scenes-app-mcp-analytics--dashboard-narrow--light:
- hash: v1.k794b7964.36ba141b42b99daff96b384b02c0ef7ce0109fdef489bed6e22d387886383828.-igCYOpf2GyR_HwHn6iAxfW0kOnMH_YSJKQj3gx_DxU
+ hash: v1.k794b7964.e24e4e9b42d54ca9148fdd679c54b32e29e9d0c4109559593c70bfd139448066.GbclMfGUhtceL1P2ZSjmwJ4p928-JR_dS2nRWBw8kII
scenes-app-mcp-analytics--dashboard-with-menu-bar--dark:
- hash: v1.k794b7964.18c750b7bf1048e67b1f40853e6901737da009c60e24ca0308f1dccdeadc7bf2.W-i3aLu0CUPi06k_iDnZ_KqcE2GMjKb_QGpF75NOb1U
+ hash: v1.k794b7964.144a6ed97c2ca61a084a813f45d866c1745fe3769039362a783d95d85af2708c.tEz48U3TwGvcUeb011Z12tD5aDRULZnw2t30BzXRfXI
scenes-app-mcp-analytics--dashboard-with-menu-bar--light:
- hash: v1.k794b7964.3e6469ebbcef8b7d2599c5c7360e49229a10ba1e9cae7fdde074109bbb03b267.iAMb6riQVQgxMSVzxXelhES4Hl5fZzzuVEHoriK9wnY
+ hash: v1.k794b7964.d1f6164b1a1ca1959ff87adb59e16eb6884aaca0490065d57766d8e15735c1c2.C_IND7UPVFZWoxbJ7vrIDbBVzUzssLzNmetEwJ9Mm-E
scenes-app-mcp-analytics--intent-clustering--dark:
hash: v1.k794b7964.9549b853f80d754b9d6ccf17ab144d24218143200c90df7af687e184e7de5329.Er1_HM8avmrqmulj2hCLnkp98-HeuRvF5ed3j-_jS2A
scenes-app-mcp-analytics--intent-clustering--light:
@@ -15575,13 +15575,13 @@ snapshots:
scenes-app-settings-authentication-domains--redesigned-needs-upgrade--light:
hash: v1.k794b7964.c30fe74ff364064355eba2a1458d90e1f206bba61c100c24ed7f0c6bd1da24a5.sYWeEL_3liphnWLFjVI7H5rEZyXuU8Z_nlxy0FsiytM
scenes-app-settings-environment--settings-environment-access-control--dark:
- hash: v1.k794b7964.11a7e4daaf98010d1e1b668ef86ffc4c574b8bacf7912f3c6065bf03ba53f255.q5A00gDxLEisVtSdGohgDuoLQHPfX044vKRINhpgDSY
+ hash: v1.k794b7964.871f7ea964a0959bbc8dde537c8bc492525be49de38a24eea41272cd53cfa7f8.VmBRzOky9SgrQdLyBgQBJp7ub48B3d8v-kr7gC8hQE4
scenes-app-settings-environment--settings-environment-access-control--light:
- hash: v1.k794b7964.c94bf0ed8c7c3ae76c6e610d0cbface61df0cea389c1907b0be714bf9a9e4c2e.k9sUTy4mUuET6uK05hnSU-D9i8VuMFWc7Vq3XvnnaDU
+ hash: v1.k794b7964.77a882b0fc251bd3cf170be462b5ca24b894d6b3476350de30d0db7754ee4d3f.90qlzpcTtsKiLqG7QEOyJnH_GU-Sk-3yefrrLYiAbqg
scenes-app-settings-environment--settings-environment-activity-logs--dark:
- hash: v1.k794b7964.7c5bd7fef968b879bbb953c5943bef56cbb314555dc3d6f47347b7eff172647b.2AOFhWVh9c53bJcrmw9qTt7skejZ4-gNLVxdgTkmiRY
+ hash: v1.k794b7964.fb95f116e0437a64089ad7cc32c67589f9b055daf0a789c2bf6249106138976e.BjW_iwQ2BkfSr3Sy3FLtQsbOkGPII6HK0S_M4XncKZw
scenes-app-settings-environment--settings-environment-activity-logs--light:
- hash: v1.k794b7964.a9ca734f3537a31b814b54683ddd49759609c7c98d7f72f25b7a38edf5aeef01.cgzCpvYHVqjstRmTtWWqqsEYrn2NLpJD0SRfwamLXQk
+ hash: v1.k794b7964.16aa748a5a2474ea930dd3232ac1e0253bc3a3d46ae556ab3c7a3b11d0ac79b6.JxbdVKG1vVI7diPtXMlYjbFOjDsrGaPPBAYIkpJjybM
scenes-app-settings-environment--settings-environment-agent-instructions--dark:
hash: v1.k794b7964.5225e6c2be31f23af61d600e99e2e3fa2c7f9ae26e60f58b8fe341274ceb849c.0llwfMe85dNkqPElcsxXR06WoXmodfKyEXn5s5TOWko
scenes-app-settings-environment--settings-environment-agent-instructions--light:
@@ -15599,9 +15599,9 @@ snapshots:
scenes-app-settings-environment--settings-environment-ai-subscriptions-codex-reauth-required--light:
hash: v1.k794b7964.931c6017abad7c503f7b89c06013d223ff5684313f7d23c1b91a495ab97a0028.EYFwROwpv-FFpD82a3ZP2nWhmQGMSwB2-4qalbUsTEU
scenes-app-settings-environment--settings-environment-autocapture--dark:
- hash: v1.k794b7964.12cfe0e7afe4dbf100d67b89a08f33efe5f6193e80202a39f0ab68046ee26695.OIw7LU6CTswvvxmvgeo-lScHtK2W4yc9cZplZurf7FM
+ hash: v1.k794b7964.56096a09bec1006fb0e4b5612885ce68772720d88fe37f80545b8e14132fc25b.7zYnO0IG486ncAsZOKOZV8hBKLMzOaz_KyTjro9ui4U
scenes-app-settings-environment--settings-environment-autocapture--light:
- hash: v1.k794b7964.4fb0ffc3582c0476355e435e2d694894f442ce553744a1b4365d0c7a70ed8edc.SX9rHBcxaCXjgW5QyFZBTWYxeF9JmTGbIx_J1G_FbYg
+ hash: v1.k794b7964.56db0fa80553816d40b77ce765f3ed25e87ff20425be2c2c6c0297488d103c56.asc3Fh9qnSt4RV8_MRVHzC64XJ7aMXkTuXUkXeWx6Tc
scenes-app-settings-environment--settings-environment-business-knowledge--dark:
hash: v1.k794b7964.228d2385f3647a8b0186fb62ccb6f06dddc05136160dbebedca6c0780b49a081.6k8tSnexmnS08sACFI71MiU6WtR4BoJxhGVzdUxO3cw
scenes-app-settings-environment--settings-environment-business-knowledge--light:
@@ -15675,29 +15675,33 @@ snapshots:
scenes-app-settings-environment--settings-environment-max--light:
hash: v1.k794b7964.ed58b5260eedc4d528b2cd81a64d946eb1d4892c8e93b01c91a3902519c0debc.uP05kHBTLY_TBNc4b6oWNZQNgk2zeJi9npKh-wZf3Jg
scenes-app-settings-environment--settings-environment-privacy--dark:
- hash: v1.k794b7964.08ef39817eef75bcee5d319868816fc780fd30479f63056f60df4168c74485b8.0PjvObNRzQspJMy3IhLMy2L0PswENoRCYdg3Jc3eiDQ
+ hash: v1.k794b7964.7f1c0beebf40a656fc7008edeb736973722bb1f807b6e6ece7d234f6bf98b069.1AXj62F0E52qdL3pYeS-UurjU-LNCHse0kNYHOdCU10
scenes-app-settings-environment--settings-environment-privacy--light:
- hash: v1.k794b7964.52575183df15ee111029b50d9a5699323a24f452953e188baf983fcec1128e1e._Xh6Gb8q1UZCF5MonVhd2fNxpHC8-_iep72Rt55Ep1U
+ hash: v1.k794b7964.d70fea52646386a21dc319221594c5e9c38eadf2d0287bdaeef45da80e61f20d.8Kr3LEAd9A2XfYqxU8iToVt_uyxck5Eq1j9R8xP5cL8
scenes-app-settings-environment--settings-environment-product-analytics--dark:
- hash: v1.k794b7964.c11e59ce895979abaf19782b6f124de1f2ce423d7bad8fa5dd1aaf97da8ff6a3.assJuIM4Wuv3nlrzqY9gEwAcZohCVDgSVPzUS4ph3G0
+ hash: v1.k794b7964.5a3e8186eccd79af0fa6e890552769159605910a496c38cfd587e2e089a7f1a8.8Uv_hGkVEDd2Dm_svLkOrWew7F0QVXkj0DmiWEnQHTI
scenes-app-settings-environment--settings-environment-product-analytics--light:
- hash: v1.k794b7964.d366e07baf77aefdb0d2ff313fd1c21ad318c9fd1e640e1fcef6994d92935067.1XXoPVysbEl2s8nUCvwOWw3RtAgkrv8XG1dJgOG9FGw
+ hash: v1.k794b7964.6e93a56f0a0044c8890183b394526e343728ef52a19c1978570aa9f21d5baf2e.PeEwAFTw2YIZQ0d81mQaILaS4DqcuYFiLnUpJmZKrkM
scenes-app-settings-environment--settings-environment-replay--dark:
- hash: v1.k794b7964.81aeaa4ffb47e8347ceccd2aecf68c51f1fbe9796fcd7fb75121dbe41e710ad2.kcBO_KCrWUs5KJM-V9drAfD9azOX95r-Nt2LN7VCuaM
+ hash: v1.k794b7964.23072058a8475921de34266eaed3fa6e1fe90fd92268fd29e6579df6c307d58d.30xm0MdMcOZ4dc_u2gZn1RehE5NPBWLX1zBnX3urXT0
scenes-app-settings-environment--settings-environment-replay--light:
- hash: v1.k794b7964.77cf5f75dff9d0a387db2a77475bee620b4e4ef54cd4b471e204019a12d3904d.tlLJ0tPlj2SLFLX1VXK3vJS0Na5wnzbns_Yy48EwUl0
+ hash: v1.k794b7964.edf975e83407a4ee20a81b8217041ec9689600ce01b2aac50860d37fcc674c1a.zmQy_laVu9NACTqQUC6OI65p_kXgmBOoWZqfHKgUwhs
scenes-app-settings-environment--settings-environment-revenue-analytics--dark:
- hash: v1.k794b7964.bd54bc88de82a6d7dc45d36637ee212bdaf57c7c4fa08702649a576bc3d1723e.U504F8ognlzGDVwrCR9zZ14MD4iHayKVhAUPASu3vmc
+ hash: v1.k794b7964.4ea09b55bbe15fec6cdcb8030bf347e4b816fd3935a27aeeed2348a21d152406.JUGIyde9-AVrSBZ6bEYn0RbTsai9Stwnqu0VjP9wBvM
scenes-app-settings-environment--settings-environment-revenue-analytics--light:
- hash: v1.k794b7964.50015df4eebbef1a3f3401ef1e8a394c13291862d8445b059d5cfb823fc3388d.xCxBvrCW04vjKpZK6oEzGrTKx_SeRXEGdDrRtxb3N2g
+ hash: v1.k794b7964.4485d1a4bec0c38273b87f389f024a95eb6da008b229c483c069e5a69f01086c.e60iO8T_BXdlCmtpJCRC4ezDYp3AUy-DyGUwNr_-yMw
scenes-app-settings-environment--settings-environment-surveys--dark:
- hash: v1.k794b7964.c8b19300bf2f67cafcc3d8704a414847efa93bb96eef2c18836c8d35a8fcaa38.TQkq9-XUY9PK8CHxoqb7yVPRZKhk4nHtYYUV9ogbtBY
+ hash: v1.k794b7964.d67715f4a7ac3a63220b7dca2699b0d15b5724a16207a3c08e0a1065689b0e74.NXo9vVTDQR4cz9kI-M01UbmxhCZxEXPmrnFOp0rOK1c
scenes-app-settings-environment--settings-environment-surveys--light:
- hash: v1.k794b7964.3e1899660265242e9b5f99a1bd57720e53d2bbda35f97bbf0d4cd9f39e0e318b.00C20HyQ9wlV-iE6LCI4Dh4o10DvW1cRSz2VeFa1pF4
+ hash: v1.k794b7964.e9827e5efbfff9b19d1b4f098f696ad6d296f6add9613ce8ffbe009cc6fded44.l7ZGtwkwtPW0kyb9a-szWxrK6JH4AmtiTJ_lYSUeu7s
scenes-app-settings-environment--settings-environment-web-analytics--dark:
- hash: v1.k794b7964.9d6e5a0e4df6abe463f890e8d1512cb88362799c647b049c33c71ef9287872df.xVLOD_xlKjbFfq-_S4scEZySJiXJWq2Bi4f-wShS03A
+ hash: v1.k794b7964.e0931458e04f6cb7d40aecaabddcb30a857eff42fdc4a74273e3795a0f4a02fa.PLdjJffI2Xgp1gYuq6PUcMg4IT6BhMz7Rh5PyrH6PVg
scenes-app-settings-environment--settings-environment-web-analytics--light:
- hash: v1.k794b7964.f943bb7f220b5641566bb172034d802c60a60e3170488aaf9be160be13460e80.qCNtLV2-xD30jkMOsMpr3TG43MtaRmOACCSUzeB8-iE
+ hash: v1.k794b7964.325010cf6189e34c2180eecbee28ed168fc787a7685572fbf22e0452d9a25e59.CwjnFv1AQ35ES1bCjZD4hTVI1thvG8vkTZcK8OcshK4
+ scenes-app-settings-environment--settings-mcp-analytics-custom-harnesses--dark:
+ hash: v1.k794b7964.df973fb99b893b28effeb6f733727e78b59de05b6127a7581276c173d7e2ef47.V1hQUYekUibTaCAYQFIYyXWD5GnEe52sD5iM27GzCF0
+ scenes-app-settings-environment--settings-mcp-analytics-custom-harnesses--light:
+ hash: v1.k794b7964.3875068aeaaae4362be669bb24ff49ff2711bbc6debd59936efe4775d5139b99.Fo9QIunWQUmiohIa68dwCJbKXmR5vHasn3LmC1q9Qic
scenes-app-settings-events-retention--organization-on-free-plan--dark:
hash: v1.k794b7964.b075e1de23700cf59e5e17cc0dbe15496d9e7ee89677ca11110b5507b17da120.ud-PQDmvtjOEehGDRhgCKwok3Fivo8yrPTs1kHt81bU
scenes-app-settings-events-retention--organization-on-free-plan--light:
@@ -15799,13 +15803,13 @@ snapshots:
scenes-app-settings-organization-member-offboarding--review-resources--light:
hash: v1.k794b7964.cda1cc2e98b958fb6272e514adbd5d9931ce274f0749671d2ac44ef91cfbc03e.MtNTKzbSL7vKj77Bg-fyrRVOxUqhDBwRLOeJ0u1ELGY
scenes-app-settings-project--settings-project-access-control--dark:
- hash: v1.k794b7964.7bd8563f354fecf26c68f067274cdb5dc1b53cd64be3e594ed71f3136a123653._ZK0SC33UPepw3ItmFTUWMtSsmOi7_AmXkz4haAPSR8
+ hash: v1.k794b7964.5d1ef2441bce805c8d5eedc46f18992b583f2ebd84a613a59689e0eb322c2625.HXeb5-tLBgpU9OhzEx4DSLMAFZtIpNQpf-s92H0WHJs
scenes-app-settings-project--settings-project-access-control--light:
- hash: v1.k794b7964.9d04cf96a600fd0b86fcfb9aa2b7d9ef265584f47a6e0742e7440bc5c46c8a17.H4Fbp713y3ts5npj26XcbGZGhRDF17tBvMcDFAz1klU
+ hash: v1.k794b7964.662d948c4dc0ca2b6c20581a32a0a6a60ebb822b5cc606f73f053eec7e19ff18.nEErzNWTisdPS3932xBarkW6JGyKGq9LmTCNZhgGrUI
scenes-app-settings-project--settings-project-autocapture--dark:
- hash: v1.k794b7964.feb61c2eb5cc0de7867a9910c1da3288df314b20224cf9796afdbb949df6cf9d.owV3jsLUa9gKohyJk_7VzvtgA4i5o8UT9dE8JPobSQc
+ hash: v1.k794b7964.56096a09bec1006fb0e4b5612885ce68772720d88fe37f80545b8e14132fc25b.LZTh51kr5B9XUFMvc2QwB0AxoNR7oDyjxuHgYzXQTVY
scenes-app-settings-project--settings-project-autocapture--light:
- hash: v1.k794b7964.4fb0ffc3582c0476355e435e2d694894f442ce553744a1b4365d0c7a70ed8edc.q4QWYWslcQV9tcGuTwJ7BOa1BYkqzBUJ4XgAchTpSQU
+ hash: v1.k794b7964.56db0fa80553816d40b77ce765f3ed25e87ff20425be2c2c6c0297488d103c56.K5eYRGxUeOvS4gCj0-InOkUG1Vn77t9E3C7M4ty47uE
scenes-app-settings-project--settings-project-customization--dark:
hash: v1.k794b7964.868400b8cf58b5ed3d8b9909da7e5ed44d0a4207b7491fb4d9d3a701509a06e2.M5fpwv39LahAoTBvtZc9WNavWVmbLylJ2NYiZq-A6xo
scenes-app-settings-project--settings-project-customization--light:
@@ -15843,17 +15847,17 @@ snapshots:
scenes-app-settings-project--settings-project-logs-read-only--light:
hash: v1.k794b7964.e234f3e555bcb083ca96f00005f91ccccf94941d371c71497162169077ec1022.jNkdKOK2KaEq2T2jksEoVtN5Nupa3FuJLTe-npm0RIM
scenes-app-settings-project--settings-project-product-analytics--dark:
- hash: v1.k794b7964.3e7f0b4280ea7a5cf384476e4aface586231eec249b8838fdc8c23d2296b8f3b.KR42Y7Ds4C-WrfeUR_WjNpuGeU275lvy20riTZz6JVI
+ hash: v1.k794b7964.529b8a14def21f75abb918da34abaf0a524a8720b3f9599a3eeb48671d07cff3.-JfMwFboJNDpq4FrmTbNszaP0fCugCUmGBuLNHHYxso
scenes-app-settings-project--settings-project-product-analytics--light:
- hash: v1.k794b7964.cc09c6c8c46f2dd1635982d3a647d3fca8965e41f9b6f3e9f997bd66cd57250b.7JkIJhualVcjPlJcrTluhqpVENR11XjMNhnzCcrBfbw
+ hash: v1.k794b7964.358161090967d5a58270dd749e6cdee2ad7cdd9466e468f65553da25bcff2cfc.tHrFn5rQ9KQUUGImXMgECrd4gIZ-9kbFxQ0ou8l8CBk
scenes-app-settings-project--settings-project-replay--dark:
- hash: v1.k794b7964.8ce54ec8a83fd3e878a1bb2d75ef68392487a26f1e0f7de9284561ad71ad8161.Mpfnk_sh9J8RWHJXffqyd_l2OPcxexT5utejEcTANFI
+ hash: v1.k794b7964.23072058a8475921de34266eaed3fa6e1fe90fd92268fd29e6579df6c307d58d.gw0d3spnFf9PQ-Xj1B8BCH3_o3i5xsYnvFSkS1pChkU
scenes-app-settings-project--settings-project-replay--light:
- hash: v1.k794b7964.1e27979c0921d28be67be9e1d0807417b19b698e1d4432b4a17c68446f21af73._SA_snmzrbAzrezT8jjn8wCMMJaoNLQ3M8C5BoW43BI
+ hash: v1.k794b7964.7240efb5fccffca8220ccaa97c7fa9ef1906ccaa383c6faa0f78ae749867cff1.ISbk1sPJYUjwH0fhLkkDAtu3JqUwNaogGUXqnm8lvSs
scenes-app-settings-project--settings-project-surveys--dark:
- hash: v1.k794b7964.522bbb84696447abda579888b4994a414b25d8df201fff0cecea357142b02bc6.A26LF9-unAiEaikwNbGw_tOmfUHstFEfuzrm4r7f3nU
+ hash: v1.k794b7964.ca0e0482b9545481080491615a78c9abbe4772832296692aab8c109d2f7c4416.T6MhSTS2muKOj9OCxii68YK1bVkCsHoT8yX8UrHDf5U
scenes-app-settings-project--settings-project-surveys--light:
- hash: v1.k794b7964.6aebd60842d2a0cf685c15f376e0eebb960e3d82d0158521a38e8a9c5e66d868.tqsr10pUadZwPukPMTdu-Td4FTjS-PbzQdqY5jEBrQE
+ hash: v1.k794b7964.52c30137e43554a5b10c1b8450e92b1b2b4d52c417aef5ae5b237f8574188ed4.XaAAahFfqjW4beeP5a4ZUS2i0m2YZz9shiRcsI6knOo
scenes-app-settings-settingsectionunavailable--admin-only--dark:
hash: v1.k794b7964.b6580b1a9c7735f0b03e9d5b58954169a1772b47bc2ae449f14771e1c5281a7b.Pp01b8BavwsYTSE4Mo3Q57hwN0X73qsh7JDbp7_DORo
scenes-app-settings-settingsectionunavailable--admin-only--light:
diff --git a/frontend/src/scenes/settings/SettingsMap.tsx b/frontend/src/scenes/settings/SettingsMap.tsx
index 4ca0e8b81fa3..9407116ba60a 100644
--- a/frontend/src/scenes/settings/SettingsMap.tsx
+++ b/frontend/src/scenes/settings/SettingsMap.tsx
@@ -9,6 +9,7 @@ import { Releases } from '@posthog/products-error-tracking/frontend/scenes/Error
import { SeverityRules } from '@posthog/products-error-tracking/frontend/scenes/ErrorTrackingConfigurationScene/severity_rules/SeverityRules'
import { SpikeDetectionSettings } from '@posthog/products-error-tracking/frontend/scenes/ErrorTrackingConfigurationScene/spike_detection/SpikeDetectionSettings'
import { SymbolSets } from '@posthog/products-error-tracking/frontend/scenes/ErrorTrackingConfigurationScene/symbol_sets/SymbolSets'
+import { McpCustomHarnessesSettings } from '@posthog/products-mcp-analytics/frontend/settings/McpCustomHarnessesSettings'
import { McpStoreSettings } from '@posthog/products-mcp-store/frontend/McpStoreSettings'
import { EventConfiguration } from '@posthog/products-revenue-analytics/frontend/settings/EventConfiguration'
import { ExternalDataSourceConfiguration } from '@posthog/products-revenue-analytics/frontend/settings/ExternalDataSourceConfiguration'
@@ -673,6 +674,22 @@ export const SETTINGS_MAP: SettingSection[] = [
},
],
},
+ {
+ level: 'environment',
+ id: 'mcp-analytics',
+ title: 'MCP analytics',
+ group: 'Products',
+ settings: [
+ {
+ id: 'mcp-analytics-custom-harnesses',
+ title: 'Custom harnesses',
+ description:
+ 'Name your own MCP clients. Calls from a client whose name or User-Agent contains the text you enter show under your name instead of Other harnesses. Harness names are cached, so a change can take a few minutes to show.',
+ component: <McpCustomHarnessesSettings />,
+ keywords: ['mcp', 'harness', 'client', 'user agent', 'other harnesses'],
+ },
+ ],
+ },
{
level: 'environment',
id: 'mcp-servers',
diff --git a/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx b/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
index 40e89c9162fe..8fc5a6129916 100644
--- a/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
+++ b/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
@@ -402,3 +402,29 @@ export const SettingsEnvironmentAgentInstructions: Story = {
}),
],
}
+
+export const SettingsMcpAnalyticsCustomHarnesses: Story = {
+ args: { sectionId: 'mcp-analytics' },
+ decorators: [
+ mswDecorator({
+ get: {
+ '/api/projects/:id/mcp_analytics/custom_harnesses/': [
+ {
+ id: '0193f2a1-0000-7000-8000-000000000001',
+ name: 'Acme agent',
+ match_pattern: 'acme-agent',
+ created_at: '2023-05-20T10:00:00Z',
+ updated_at: '2023-05-20T10:00:00Z',
+ },
+ {
+ id: '0193f2a1-0000-7000-8000-000000000002',
+ name: 'Support bot',
+ match_pattern: 'support-bot',
+ created_at: '2023-05-21T10:00:00Z',
+ updated_at: '2023-05-21T10:00:00Z',
+ },
+ ],
+ },
+ }),
+ ],
+}
diff --git a/frontend/src/scenes/settings/types.ts b/frontend/src/scenes/settings/types.ts
index 909fc0a0ff09..16c344e84c83 100644
--- a/frontend/src/scenes/settings/types.ts
+++ b/frontend/src/scenes/settings/types.ts
@@ -113,6 +113,7 @@ export type SettingSectionId =
| 'user-reminders'
| 'user-danger-zone'
// Standalone
+ | 'mcp-analytics'
| 'mcp-servers'
| 'posthog-mcp'
@@ -236,6 +237,7 @@ export type SettingId =
| 'logs-retention'
| 'logs-session-id-attribute-keys'
| 'marketing-settings'
+ | 'mcp-analytics-custom-harnesses'
| 'mcp-servers-manage'
| 'members'
| 'member-notifications'
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 61cab02fcee3..73c15ce5489e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -4383,12 +4383,18 @@ importers:
'@testing-library/dom':
specifier: '*'
version: 10.4.0
+ '@testing-library/user-event':
+ specifier: '*'
+ version: 14.6.1(@testing-library/[email protected])
'@types/react':
specifier: 18.3.27
version: 18.3.27
kea:
specifier: 'catalog:'
version: 4.0.0-pre.6(patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected])
+ kea-forms:
+ specifier: 'catalog:'
+ version: 3.2.0([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
kea-loaders:
specifier: 'catalog:'
version: 3.1.1([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
@@ -4408,6 +4414,9 @@ importers:
specifier: 9.5.3
version: 9.5.3([email protected])
devDependencies:
+ '@testing-library/react':
+ specifier: ^14.3.1
kea-test-utils:
specifier: 'catalog:'
version: 0.2.4([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
diff --git a/products/mcp_analytics/backend/custom_harnesses.py b/products/mcp_analytics/backend/custom_harnesses.py
new file mode 100644
index 000000000000..bec8c3ab956a
--- /dev/null
+++ b/products/mcp_analytics/backend/custom_harnesses.py
@@ -0,0 +1,45 @@
+from django.db import transaction
+from django.db.models.functions import Length
+
+from posthog.models.team.team import Team
+from posthog.utils import get_safe_cache, safe_cache_add, safe_cache_delete
+
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
+from products.mcp_analytics.backend.models import MCPCustomHarness
+
+# Short, because a read that races a save, or a failed delete after a save, leaves the old rules in the cache.
+CUSTOM_HARNESS_RULES_CACHE_TTL_SECONDS = 5 * 60
+
+RulePairs = tuple[tuple[str, str], ...]
+
+
+def _cache_key(team_id: int) -> str:
+ return f"mcp_analytics:custom_harness_rules:{team_id}"
+
+
+def _canonical_team_id(team: Team) -> int:
+ return team.parent_team_id or team.pk
+
+
+def _rule_pairs_from_db(team_id: int) -> RulePairs:
+ # The first matching rule wins, so the most specific pattern goes first.
+ return tuple(
+ MCPCustomHarness.objects.for_team(team_id, canonical=True)
+ .order_by(Length("match_pattern").desc(), "created_at")
+ .values_list("name", "match_pattern")
+ )
+
+
+def custom_harness_rules(team: Team) -> tuple[CustomHarnessRule, ...]:
+ team_id = _canonical_team_id(team)
+ pairs: RulePairs | None = get_safe_cache(_cache_key(team_id))
+ if pairs is None:
+ pairs = _rule_pairs_from_db(team_id)
+ safe_cache_add(_cache_key(team_id), pairs, CUSTOM_HARNESS_RULES_CACHE_TTL_SECONDS)
+ return tuple(CustomHarnessRule(name=name, pattern=pattern) for name, pattern in pairs)
+
+
+def invalidate_custom_harness_rules(team: Team) -> None:
+ # Deleting rather than writing means two concurrent saves cannot store their rules in the wrong order.
+ cache_key = _cache_key(_canonical_team_id(team))
+ transaction.on_commit(lambda: safe_cache_delete(cache_key))
diff --git a/products/mcp_analytics/backend/facade/api.py b/products/mcp_analytics/backend/facade/api.py
index 895d8ee1d202..e35d7a94a383 100644
--- a/products/mcp_analytics/backend/facade/api.py
+++ b/products/mcp_analytics/backend/facade/api.py
@@ -1,3 +1,4 @@
+from collections.abc import Sequence
from datetime import datetime
from django.db import transaction
@@ -12,13 +13,13 @@ from posthog.api.capture import capture_internal
from posthog.models.team.team import Team
from posthog.models.user import User
-from products.mcp_analytics.backend import logic
+from products.mcp_analytics.backend import custom_harnesses, logic
from products.mcp_analytics.backend.hogql_queries.base import (
EFFECTIVE_DESCRIPTION_SQL,
EFFECTIVE_TOOL_SQL,
NEW_SDK_SOURCE,
)
-from products.mcp_analytics.backend.mcp_harness import HARNESS_TOKEN_SQL, harness_label_sql
+from products.mcp_analytics.backend.mcp_harness import HARNESS_TOKEN_SQL, custom_arms_sql, harness_label_sql
from products.mcp_analytics.backend.models import MCPAnalyticsSubmission
from . import contracts
@@ -26,12 +27,37 @@ from .constants import MCP_MISSING_CAPABILITY_EVENT
from .enums import SubmissionKind
-def mcp_harness_virtual_field_expr() -> ast.Expr:
- # nosemgrep: hogql-fstring-audit (both SQL fragments are fixed server-side expressions)
- return parse_expr(
- f"if(startsWith(event, '$mcp_'), arrayMap(h -> {harness_label_sql('h')}, [{HARNESS_TOKEN_SQL}])[1], NULL)",
- cache_origin=CacheOrigin.BUILTIN,
- )
+def custom_harness_rules(team: Team) -> tuple[contracts.CustomHarnessRule, ...]:
+ return custom_harnesses.custom_harness_rules(team)
+
+
+def invalidate_custom_harness_rules(team: Team) -> None:
+ custom_harnesses.invalidate_custom_harness_rules(team)
+
+
+_VIRTUAL_FIELD_SQL = (
+ f"if(startsWith(event, '$mcp_'), arrayMap(h -> {harness_label_sql('h')}, [{HARNESS_TOKEN_SQL}])[1], NULL)"
+)
+
+
+def mcp_harness_virtual_field_expr(custom_rules: Sequence[contracts.CustomHarnessRule] = ()) -> ast.Expr:
+ # The fixed body is too long for the user parse cache, so only the short team rules parse as user input.
+ expr = parse_expr(_VIRTUAL_FIELD_SQL, cache_origin=CacheOrigin.BUILTIN)
+ if custom_rules:
+ # nosemgrep: hogql-fstring-audit (the rule fields are escaped and character-restricted)
+ rule_arms = parse_expr(f"multiIf({custom_arms_sql('h', custom_rules)}NULL)", cache_origin=CacheOrigin.USER)
+ assert isinstance(rule_arms, ast.Call)
+ _harness_label_call(expr).args[:0] = rule_arms.args[:-1]
+ return expr
+
+
+def _harness_label_call(virtual_field: ast.Expr) -> ast.Call:
+ assert isinstance(virtual_field, ast.Call)
+ label_access = virtual_field.args[1]
+ assert isinstance(label_access, ast.ArrayAccess) and isinstance(label_access.array, ast.Call)
+ label_lambda = label_access.array.args[0]
+ assert isinstance(label_lambda, ast.Lambda) and isinstance(label_lambda.expr, ast.Call)
+ return label_lambda.expr
def _to_submission(instance: MCPAnalyticsSubmission) -> contracts.Submission:
diff --git a/products/mcp_analytics/backend/facade/constants.py b/products/mcp_analytics/backend/facade/constants.py
index e404d082863f..f4ed24fff54c 100644
--- a/products/mcp_analytics/backend/facade/constants.py
+++ b/products/mcp_analytics/backend/facade/constants.py
@@ -1,3 +1,6 @@
from products.mcp_analytics.backend.constants import MCP_MISSING_CAPABILITY_EVENT
+from products.mcp_analytics.backend.mcp_harness import UNIDENTIFIED_HARNESS_LABEL
-__all__ = ["MCP_MISSING_CAPABILITY_EVENT"]
+MAX_CUSTOM_HARNESSES = 20
+
+__all__ = ["MAX_CUSTOM_HARNESSES", "MCP_MISSING_CAPABILITY_EVENT", "UNIDENTIFIED_HARNESS_LABEL"]
diff --git a/products/mcp_analytics/backend/facade/contracts.py b/products/mcp_analytics/backend/facade/contracts.py
index a8ab79742cce..cd934baa475f 100644
--- a/products/mcp_analytics/backend/facade/contracts.py
+++ b/products/mcp_analytics/backend/facade/contracts.py
@@ -15,6 +15,14 @@ class IntentGenerationUnavailable(RuntimeError):
"""
+@dataclass(frozen=True)
+class CustomHarnessRule:
+ """A team's own harness: calls whose client token contains `pattern` are labelled `name`."""
+
+ name: str
+ pattern: str
+
+
@dataclass(frozen=True)
class Submission:
id: UUID
diff --git a/products/mcp_analytics/backend/hogql_queries/base.py b/products/mcp_analytics/backend/hogql_queries/base.py
index 0b7fe7900321..ff4cd63b46fb 100644
--- a/products/mcp_analytics/backend/hogql_queries/base.py
+++ b/products/mcp_analytics/backend/hogql_queries/base.py
@@ -17,9 +17,11 @@ from posthog.hogql.errors import QueryError
from posthog.hogql.parser import parse_expr
from posthog.hogql.property import property_to_expr
+from posthog.hogql_queries.query_runner import AR, AnalyticsQueryRunner
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from products.access_control.backend.facade.user_access_control import UserAccessControl
+from products.mcp_analytics.backend.custom_harnesses import custom_harness_rules
if TYPE_CHECKING:
from posthog.schema import AnyPropertyFilterDiscriminated, DateRange, IntervalType
@@ -131,3 +133,14 @@ def mcp_query_date_range(
interval=interval,
now=datetime.now(team.timezone_info),
)
+
+
+class HarnessLabelQueryRunner(AnalyticsQueryRunner[AR]):
+ """A runner whose results carry harness labels, which depend on the team's custom harness rules."""
+
+ def get_cache_payload(self) -> dict:
+ payload = super().get_cache_payload()
+ # Without the rules in the key, a save keeps serving cached results with the old labels until they expire.
+ if rules := custom_harness_rules(self.team):
+ payload["mcp_custom_harness_rules"] = [[rule.name, rule.pattern] for rule in rules]
+ return payload
diff --git a/products/mcp_analytics/backend/hogql_queries/harness_breakdown.py b/products/mcp_analytics/backend/hogql_queries/harness_breakdown.py
index 346acd91c69b..64f2ee4327c0 100644
--- a/products/mcp_analytics/backend/hogql_queries/harness_breakdown.py
+++ b/products/mcp_analytics/backend/hogql_queries/harness_breakdown.py
@@ -13,13 +13,14 @@ from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
-from posthog.hogql_queries.query_runner import AnalyticsQueryRunner
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from products.mcp_analytics.backend import mcp_harness
from products.mcp_analytics.backend.constants import MCP_TOOL_CALL_EVENT
+from products.mcp_analytics.backend.custom_harnesses import custom_harness_rules
from products.mcp_analytics.backend.hogql_queries.base import (
CONVERSATION_ID_SQL,
+ HarnessLabelQueryRunner,
effective_tool_expr,
mcp_query_date_range,
mcp_source_expr,
@@ -31,7 +32,7 @@ if TYPE_CHECKING:
from posthog.models.user import User
-class MCPHarnessBreakdownQueryRunner(AnalyticsQueryRunner[MCPHarnessBreakdownQueryResponse]):
+class MCPHarnessBreakdownQueryRunner(HarnessLabelQueryRunner[MCPHarnessBreakdownQueryResponse]):
"""MCP tool-call activity grouped by the resolved client harness.
Powers the dashboard's harness tile and the `query-mcp-harness-breakdown` MCP
@@ -105,7 +106,7 @@ class MCPHarnessBreakdownQueryRunner(AnalyticsQueryRunner[MCPHarnessBreakdownQue
ORDER BY total_calls DESC
""",
placeholders={
- "label": parse_expr(mcp_harness.harness_label_sql("h")),
+ "label": parse_expr(mcp_harness.harness_label_sql("h", custom_harness_rules(self.team))),
"token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
"is_tool": effective_tool_expr(self.query.toolName),
"conversation_id": parse_expr(CONVERSATION_ID_SQL),
@@ -132,7 +133,7 @@ class MCPHarnessBreakdownQueryRunner(AnalyticsQueryRunner[MCPHarnessBreakdownQue
ORDER BY total_calls DESC
""",
placeholders={
- "label": parse_expr(mcp_harness.harness_label_sql("h")),
+ "label": parse_expr(mcp_harness.harness_label_sql("h", custom_harness_rules(self.team))),
"token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
"conversation_id": parse_expr(CONVERSATION_ID_SQL),
"where": self._where(),
diff --git a/products/mcp_analytics/backend/hogql_queries/missing_capabilities.py b/products/mcp_analytics/backend/hogql_queries/missing_capabilities.py
index 34cdf8761a9c..4c32e7f27439 100644
--- a/products/mcp_analytics/backend/hogql_queries/missing_capabilities.py
+++ b/products/mcp_analytics/backend/hogql_queries/missing_capabilities.py
@@ -23,13 +23,14 @@ from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
-from posthog.hogql_queries.query_runner import AnalyticsQueryRunner
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from products.mcp_analytics.backend import mcp_harness
from products.mcp_analytics.backend.constants import MCP_MISSING_CAPABILITY_EVENT
+from products.mcp_analytics.backend.custom_harnesses import custom_harness_rules
from products.mcp_analytics.backend.hogql_queries.base import (
CONVERSATION_ID_SQL,
+ HarnessLabelQueryRunner,
display_person_properties,
mcp_query_date_range,
shared_filter_exprs,
@@ -47,7 +48,7 @@ MAX_LIMIT = 500
_REPORT_TEXT = "toString(properties.$mcp_intent)"
-class MCPMissingCapabilitiesQueryRunner(AnalyticsQueryRunner[MCPMissingCapabilitiesQueryResponse]):
+class MCPMissingCapabilitiesQueryRunner(HarnessLabelQueryRunner[MCPMissingCapabilitiesQueryResponse]):
"""Chronological feed of the capabilities agents asked for and could not get.
Powers the "Missing capabilities" tab and the `query-mcp-missing-capabilities` MCP
@@ -134,7 +135,9 @@ class MCPMissingCapabilitiesQueryRunner(AnalyticsQueryRunner[MCPMissingCapabilit
LIMIT {limit} OFFSET {offset}
""",
placeholders={
- "harness_label": parse_expr(mcp_harness.harness_label_or_token_sql("h", "client_display")),
+ "harness_label": parse_expr(
+ mcp_harness.harness_label_or_token_sql("h", "client_display", custom_harness_rules(self.team))
+ ),
"token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
"display_name": parse_expr(mcp_harness.HARNESS_DISPLAY_NAME_SQL),
"report_text": parse_expr(_REPORT_TEXT),
diff --git a/products/mcp_analytics/backend/hogql_queries/tool_tables.py b/products/mcp_analytics/backend/hogql_queries/tool_tables.py
index f065aa05ecaf..036bf4e4c439 100644
--- a/products/mcp_analytics/backend/hogql_queries/tool_tables.py
+++ b/products/mcp_analytics/backend/hogql_queries/tool_tables.py
@@ -5,6 +5,7 @@ Both resolve the client harness to a customer label server-side via `mcp_harness
the dashboard donut — the frontend only maps a resolved label to its logo.
"""
+from collections.abc import Sequence
from functools import cached_property
from typing import TYPE_CHECKING
@@ -54,11 +55,14 @@ from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from products.mcp_analytics.backend import mcp_harness
from products.mcp_analytics.backend.constants import MCP_TOOL_CALL_EVENT
+from products.mcp_analytics.backend.custom_harnesses import custom_harness_rules
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
from products.mcp_analytics.backend.hogql_queries.base import (
CONVERSATION_ID_SQL,
EFFECTIVE_DESCRIPTION_SQL,
EFFECTIVE_TOOL_SQL,
NEW_SDK_SOURCE,
+ HarnessLabelQueryRunner,
display_person_properties,
effective_tool_expr,
mcp_query_date_range,
@@ -76,10 +80,13 @@ if TYPE_CHECKING:
# pipeline resolves descriptions identically to the Descriptions table here.
_EFFECTIVE_DESCRIPTION = EFFECTIVE_DESCRIPTION_SQL
+
# A per-row distinct-and-sorted list of resolved harness labels, collected from the
# token computed in the inner subquery. groupArray evaluates the label per row, so the
# token only has to be materialized once as `h`.
-_HARNESS_LABELS_AGG = f"arraySort(arrayDistinct(groupArray({mcp_harness.harness_label_sql('h')})))"
+def _harness_labels_agg(custom_rules: Sequence[CustomHarnessRule]) -> str:
+ return f"arraySort(arrayDistinct(groupArray({mcp_harness.harness_label_sql('h', custom_rules)})))"
+
# Raw failure-bucket parts of an errored $mcp_tool_call: the SDK stamps a semantic bucket
# ($mcp_error_type: internal, validation, api_4xx, api_5xx, permission, timeout, rate_limited,
@@ -133,7 +140,7 @@ def _tool_call_where(
return ast.And(exprs=exprs)
-class MCPToolTopUsersQueryRunner(AnalyticsQueryRunner[MCPToolTopUsersQueryResponse]):
+class MCPToolTopUsersQueryRunner(HarnessLabelQueryRunner[MCPToolTopUsersQueryResponse]):
query: MCPToolTopUsersQuery
cached_response: CachedMCPToolTopUsersQueryResponse
@@ -175,7 +182,7 @@ class MCPToolTopUsersQueryRunner(AnalyticsQueryRunner[MCPToolTopUsersQueryRespon
LIMIT 5
""",
placeholders={
- "_HARNESS_LABELS_AGG": parse_expr(_HARNESS_LABELS_AGG),
+ "_HARNESS_LABELS_AGG": parse_expr(_harness_labels_agg(custom_harness_rules(self.team))),
"token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
"where": self._where(),
},
@@ -219,7 +226,7 @@ class MCPToolTopUsersQueryRunner(AnalyticsQueryRunner[MCPToolTopUsersQueryRespon
)
-class MCPToolFailuresQueryRunner(AnalyticsQueryRunner[MCPToolFailuresQueryResponse]):
+class MCPToolFailuresQueryRunner(HarnessLabelQueryRunner[MCPToolFailuresQueryResponse]):
query: MCPToolFailuresQuery
cached_response: CachedMCPToolFailuresQueryResponse
@@ -266,7 +273,7 @@ class MCPToolFailuresQueryRunner(AnalyticsQueryRunner[MCPToolFailuresQueryRespon
LIMIT 20
""",
placeholders={
- "_HARNESS_LABELS_AGG": parse_expr(_HARNESS_LABELS_AGG),
+ "_HARNESS_LABELS_AGG": parse_expr(_harness_labels_agg(custom_harness_rules(self.team))),
"_COMPOSED_FAILURE_LABEL": parse_expr(_COMPOSED_FAILURE_LABEL),
"_RAW_ERROR_TYPE": parse_expr(_RAW_ERROR_TYPE),
"_RAW_ERROR_STATUS": parse_expr(_RAW_ERROR_STATUS),
@@ -315,7 +322,7 @@ class MCPToolFailuresQueryRunner(AnalyticsQueryRunner[MCPToolFailuresQueryRespon
_ERROR_MESSAGE = "substring(coalesce(toString(properties.$mcp_error_message), ''), 1, 2048)"
-class MCPToolFailureOccurrencesQueryRunner(AnalyticsQueryRunner[MCPToolFailureOccurrencesQueryResponse]):
+class MCPToolFailureOccurrencesQueryRunner(HarnessLabelQueryRunner[MCPToolFailureOccurrencesQueryResponse]):
query: MCPToolFailureOccurrencesQuery
cached_response: CachedMCPToolFailureOccurrencesQueryResponse
@@ -381,7 +388,7 @@ class MCPToolFailureOccurrencesQueryRunner(AnalyticsQueryRunner[MCPToolFailureOc
LIMIT 50
""",
placeholders={
- "harness_label": parse_expr(mcp_harness.harness_label_sql("h")),
+ "harness_label": parse_expr(mcp_harness.harness_label_sql("h", custom_harness_rules(self.team))),
"_CONVERSATION_ID": parse_expr(CONVERSATION_ID_SQL),
"_ERROR_MESSAGE": parse_expr(_ERROR_MESSAGE),
"_RAW_ERROR_STATUS": parse_expr(_RAW_ERROR_STATUS),
@@ -666,7 +673,7 @@ class MCPToolDescriptionsQueryRunner(AnalyticsQueryRunner[MCPToolDescriptionsQue
)
-class MCPToolSampleIntentsQueryRunner(AnalyticsQueryRunner[MCPToolSampleIntentsQueryResponse]):
+class MCPToolSampleIntentsQueryRunner(HarnessLabelQueryRunner[MCPToolSampleIntentsQueryResponse]):
query: MCPToolSampleIntentsQuery
cached_response: CachedMCPToolSampleIntentsQueryResponse
@@ -707,7 +714,7 @@ class MCPToolSampleIntentsQueryRunner(AnalyticsQueryRunner[MCPToolSampleIntentsQ
LIMIT 5
""",
placeholders={
- "harness_label": parse_expr(mcp_harness.harness_label_sql("h")),
+ "harness_label": parse_expr(mcp_harness.harness_label_sql("h", custom_harness_rules(self.team))),
"token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
"where": where,
},
diff --git a/products/mcp_analytics/backend/logic.py b/products/mcp_analytics/backend/logic.py
index 4480d4e0ac04..5148604f3351 100644
--- a/products/mcp_analytics/backend/logic.py
+++ b/products/mcp_analytics/backend/logic.py
@@ -1,6 +1,7 @@
import json
import hashlib
import dataclasses
+from collections.abc import Sequence
from datetime import datetime, timedelta
from typing import Any
@@ -37,7 +38,9 @@ from products.mcp_analytics.backend.constants import (
MCP_MISSING_CAPABILITY_EVENT,
MCP_TOOL_CALL_EVENT,
)
+from products.mcp_analytics.backend.custom_harnesses import custom_harness_rules
from products.mcp_analytics.backend.facade import contracts, enums
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
from products.mcp_analytics.backend.hogql_queries.base import shared_filter_exprs
from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPIntentClusterSnapshot, MCPSession
@@ -534,7 +537,9 @@ ACTIVITY_TOP_TOOLS_LIMIT = 5
ACTIVITY_CLIENTS_LIMIT = 6
ACTIVITY_RECENT_CALLS_LIMIT = 20
-_ACTIVITY_STATS_SQL = f"""
+
+def _activity_stats_sql(custom_rules: Sequence[CustomHarnessRule]) -> str:
+ return f"""
SELECT
count() AS total_calls,
uniq(tool) AS distinct_tools,
@@ -544,7 +549,7 @@ SELECT
-- mean Codex), which the Clients card folds into one row, so counting tokens would
-- put "2 clients" next to a one-row card. The raw `$mcp_client_name` is absent from
-- every non-initialize call and cannot be counted here at all.
- uniqIf({mcp_harness.harness_label_or_token_sql("h")}, h != '') AS distinct_clients,
+ uniqIf({mcp_harness.harness_label_or_token_sql("h", custom_rules=custom_rules)}, h != '') AS distinct_clients,
countIf(has_intent) AS calls_with_intent,
countIf(is_error) AS error_calls
FROM (
@@ -561,6 +566,7 @@ FROM (
)
"""
+
_ACTIVITY_MISSING_CAPABILITY_SQL = """
SELECT count()
FROM events
@@ -581,6 +587,7 @@ ORDER BY calls DESC
LIMIT {limit}
"""
+
# Resolved through the canonical classifier rather than the raw `$mcp_client_name`,
# which is absent on any call that isn't the session's `initialize` — reading it alone
# left the large majority of calls unattributed and lumped whole clients under one
@@ -589,9 +596,10 @@ LIMIT {limit}
# (differing case, or a name carrying mcp-remote's proxy signature) into a single row.
# Unrecognized clients are named verbatim: this is a ranked top-N list, so a
# self-reported name is more use than collapsing it into "Other".
-_ACTIVITY_CLIENTS_SQL = f"""
+def _activity_clients_sql(custom_rules: Sequence[CustomHarnessRule]) -> str:
+ return f"""
SELECT
- {mcp_harness.harness_label_or_token_sql("h")} AS client,
+ {mcp_harness.harness_label_or_token_sql("h", custom_rules=custom_rules)} AS client,
count() AS calls
FROM (
SELECT
@@ -606,7 +614,9 @@ ORDER BY calls DESC
LIMIT {{limit}}
"""
-_ACTIVITY_RECENT_CALLS_SQL = f"""
+
+def _activity_recent_calls_sql(custom_rules: Sequence[CustomHarnessRule]) -> str:
+ return f"""
SELECT
timestamp,
tool,
@@ -615,7 +625,7 @@ SELECT
error_raw,
duration_ms,
-- Resolved, not raw: the live feed showed a blank caller on most rows otherwise.
- {mcp_harness.harness_label_or_token_sql("h")} AS client_name
+ {mcp_harness.harness_label_or_token_sql("h", custom_rules=custom_rules)} AS client_name
FROM (
SELECT
timestamp,
@@ -797,6 +807,7 @@ def _query_activity_overview(
# Building the database costs more Python time than most of these queries spend in
# ClickHouse, and the threads share the GIL, so build it once for all five.
database = _shared_activity_database(team, user)
+ rules = custom_harness_rules(team)
def run_query(spec: tuple[str, str, object]) -> list[Any]:
assert isinstance(spec[2], dict)
@@ -808,7 +819,7 @@ def _query_activity_overview(
run_query,
[
(
- _ACTIVITY_STATS_SQL,
+ _activity_stats_sql(rules),
"mcp_analytics_activity_stats",
{
"tool_call_event": tool_call_event,
@@ -834,7 +845,7 @@ def _query_activity_overview(
},
),
(
- _ACTIVITY_CLIENTS_SQL,
+ _activity_clients_sql(rules),
"mcp_analytics_activity_clients",
{
"tool_call_event": tool_call_event,
@@ -843,7 +854,7 @@ def _query_activity_overview(
},
),
(
- _ACTIVITY_RECENT_CALLS_SQL,
+ _activity_recent_calls_sql(rules),
"mcp_analytics_activity_recent_calls",
{
"tool_call_event": tool_call_event,
diff --git a/products/mcp_analytics/backend/mcp_harness.py b/products/mcp_analytics/backend/mcp_harness.py
index e60d669d8791..84222ba226e7 100644
--- a/products/mcp_analytics/backend/mcp_harness.py
+++ b/products/mcp_analytics/backend/mcp_harness.py
@@ -35,6 +35,12 @@ the token into the `multiIf`. `token_col` is always a SQL identifier the caller
controls, never request input.
"""
+from collections.abc import Sequence
+
+from posthog.hogql.escape_sql import escape_hogql_string
+
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
+
# Leading product token of the User-Agent, e.g. "claude-code" from "claude-code/2.1.x (cli)".
_UA_PRODUCT = "extract(toString(properties.$mcp_client_user_agent), '^([^/]+)')"
@@ -102,11 +108,12 @@ UNIDENTIFIED_HARNESS_LABEL = "Unidentified client"
_MAX_TOKEN_LABEL_LENGTH = 200
-def harness_label_sql(token_col: str = "h") -> str:
+def harness_label_sql(token_col: str = "h", custom_rules: Sequence[CustomHarnessRule] = ()) -> str:
"""Bucket a normalized harness token column into a customer label.
Every unrecognized token collapses into "Other", so the result is always one of
- `HARNESS_LABELS`. Callers that aggregate labels into a per-row array need that
+ `HARNESS_LABELS` or a team's custom rule names. A custom rule is tried before the built-in
+ buckets, so a team can also rename a built-in client. Callers that aggregate labels into a per-row array need that
bound; callers that rank a top-N list should prefer `harness_label_or_token_sql`,
which names an unrecognized client instead of discarding it.
@@ -115,10 +122,12 @@ def harness_label_sql(token_col: str = "h") -> str:
Surface-specific entries are listed before the generic prefix matches so
`find`-style first-match precedence matches the frontend registry order.
"""
- return _label_multi_if(token_col, "'Other'")
+ return _label_multi_if(token_col, "'Other'", custom_rules)
-def harness_label_or_token_sql(token_col: str = "h", display_col: str = "client_display") -> str:
+def harness_label_or_token_sql(
+ token_col: str = "h", display_col: str = "client_display", custom_rules: Sequence[CustomHarnessRule] = ()
+) -> str:
"""Same bucketing as `harness_label_sql`, but names unrecognized clients verbatim.
An unrecognized token is not noise — it is the client's own self-reported name
@@ -137,10 +146,18 @@ def harness_label_or_token_sql(token_col: str = "h", display_col: str = "client_
fallback = (
f"if({token_col} = '', '{UNIDENTIFIED_HARNESS_LABEL}', substring({display_col}, 1, {_MAX_TOKEN_LABEL_LENGTH}))"
)
- return _label_multi_if(token_col, fallback)
+ return _label_multi_if(token_col, fallback, custom_rules)
+
+
+def custom_arms_sql(token_col: str, custom_rules: Sequence[CustomHarnessRule]) -> str:
+ # Rule fields are team-supplied. They are escaped here and also restricted to a safe character set on save.
+ return "".join(
+ f"position({token_col}, {escape_hogql_string(rule.pattern.lower())}) > 0, {escape_hogql_string(rule.name)},\n "
+ for rule in custom_rules
+ )
-def _label_multi_if(token_col: str, fallback_sql: str) -> str:
+def _label_multi_if(token_col: str, fallback_sql: str, custom_rules: Sequence[CustomHarnessRule] = ()) -> str:
"""The shared bucketing `multiIf`, parameterized by its final (else) arm.
`token_col` is interpolated into SQL, so it must be a bare identifier — never
@@ -149,7 +166,7 @@ def _label_multi_if(token_col: str, fallback_sql: str) -> str:
if not token_col.isidentifier():
raise ValueError(f"token_col must be a SQL identifier, got {token_col!r}")
return f"""multiIf(
- {token_col} = 'claude-code claude-desktop', 'Claude Desktop',
+ {custom_arms_sql(token_col, custom_rules)}{token_col} = 'claude-code claude-desktop', 'Claude Desktop',
{token_col} = 'claude-code claude-vscode', 'Claude Code (VS Code)',
startsWith({token_col}, 'claude-code sdk'), 'Claude Agent SDK',
startsWith({token_col}, 'claude-code'), 'Claude Code',
diff --git a/products/mcp_analytics/backend/migrations/0012_mcpcustomharness.py b/products/mcp_analytics/backend/migrations/0012_mcpcustomharness.py
new file mode 100644
index 000000000000..1e581d0ffeab
--- /dev/null
+++ b/products/mcp_analytics/backend/migrations/0012_mcpcustomharness.py
@@ -0,0 +1,76 @@
+# Generated by Django 5.2.18 on 2026-10-28 13:31
+
+import django.core.validators
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("mcp_analytics", "0011_alter_mcpsession_session_id"),
+ ("posthog", "1414_remove_filesystem_rows_for_deleted_insights"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="MCPCustomHarness",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "name",
+ models.CharField(
+ max_length=100,
+ validators=[
+ django.core.validators.RegexValidator(
+ "^[\\w .+&()/-]+$",
+ "Use letters, numbers, spaces and . + & ( ) / - only.",
+ )
+ ],
+ ),
+ ),
+ (
+ "match_pattern",
+ models.CharField(
+ max_length=100,
+ validators=[
+ django.core.validators.RegexValidator(
+ "^[a-z0-9._ /-]+$",
+ "Use lowercase letters, numbers, spaces and . _ / - only.",
+ )
+ ],
+ ),
+ ),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_mcp_custom_harness",
+ "ordering": ["created_at"],
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("team", "match_pattern"),
+ name="unique_mcp_custom_harness_pattern",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/products/mcp_analytics/backend/migrations/max_migration.txt b/products/mcp_analytics/backend/migrations/max_migration.txt
index 905bc9273f09..2a715cb0ba8a 100644
--- a/products/mcp_analytics/backend/migrations/max_migration.txt
+++ b/products/mcp_analytics/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0011_alter_mcpsession_session_id
+0012_mcpcustomharness
diff --git a/products/mcp_analytics/backend/models.py b/products/mcp_analytics/backend/models.py
index 0dbae64f3740..9b12b8950998 100644
--- a/products/mcp_analytics/backend/models.py
+++ b/products/mcp_analytics/backend/models.py
@@ -1,5 +1,6 @@
"""Django models for mcp_analytics."""
+from django.core.validators import RegexValidator
from django.db import models
from posthog.models.scoping.root_mixin import TeamScopedRootMixin
@@ -125,3 +126,26 @@ class MCPSession(UUIDModel, TeamScopedRootMixin):
constraints = [
models.UniqueConstraint(fields=["team", "session_id"], name="unique_mcp_session"),
]
+
+
+class MCPCustomHarness(UUIDModel, TeamScopedRootMixin):
+ # A database constraint to posthog_team would lock that hot table when the table is created.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")
+ name = models.CharField(
+ max_length=100,
+ validators=[RegexValidator(r"^[\w .+&()/-]+$", "Use letters, numbers, spaces and . + & ( ) / - only.")],
+ )
+ match_pattern = models.CharField(
+ max_length=100,
+ validators=[RegexValidator(r"^[a-z0-9._ /-]+$", "Use lowercase letters, numbers, spaces and . _ / - only.")],
+ )
+
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ db_table = "posthog_mcp_custom_harness"
+ ordering = ["created_at"]
+ constraints = [
+ models.UniqueConstraint(fields=["team", "match_pattern"], name="unique_mcp_custom_harness_pattern"),
+ ]
diff --git a/products/mcp_analytics/backend/presentation/serializers.py b/products/mcp_analytics/backend/presentation/serializers.py
index 2493e4fefcac..6a28017493c1 100644
--- a/products/mcp_analytics/backend/presentation/serializers.py
+++ b/products/mcp_analytics/backend/presentation/serializers.py
@@ -2,6 +2,9 @@ import json
from datetime import datetime
from typing import Any
+from django.core.exceptions import ValidationError as DjangoValidationError
+from django.db import IntegrityError, transaction
+
import pydantic
from rest_framework import serializers
@@ -12,7 +15,8 @@ from posthog.schema import (
SessionPropertyFilter,
)
-from products.mcp_analytics.backend.models import MCPAnalyticsSubmission
+from products.mcp_analytics.backend.facade.constants import MAX_CUSTOM_HARNESSES, UNIDENTIFIED_HARNESS_LABEL
+from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPCustomHarness
_PROPERTY_FILTERS_ADAPTER: pydantic.TypeAdapter[list[AnyPropertyFilterDiscriminated]] = pydantic.TypeAdapter(
list[AnyPropertyFilterDiscriminated]
@@ -863,3 +867,64 @@ class MCPMissingCapabilityCreateSerializer(MCPAnalyticsSubmissionContextSerializ
default=True,
help_text="Whether the missing capability blocked the user's progress.",
)
+
+
+DUPLICATE_MATCH_PATTERN_ERROR = "A custom harness already uses this pattern."
+
+
+class MCPCustomHarnessSerializer(serializers.ModelSerializer):
+ match_pattern = serializers.CharField(
+ max_length=100,
+ help_text=(
+ "Text to look for in the client name or User-Agent product that the harness reports. "
+ "Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent/1.2`."
+ ),
+ )
+
+ class Meta:
+ model = MCPCustomHarness
+ fields = ["id", "name", "match_pattern", "created_at", "updated_at"]
+ read_only_fields = ["id", "created_at", "updated_at"]
+ extra_kwargs = {"name": {"help_text": "Name to show for calls from this harness."}}
+
+ def validate_name(self, value: str) -> str:
+ # The dashboard gives these two labels their own meaning, so a team rule must not produce them.
+ if value.strip().lower() in {"other", UNIDENTIFIED_HARNESS_LABEL.lower()}:
+ raise serializers.ValidationError(f"{value.strip()} is a built-in label. Choose another name.")
+ return value
+
+ def validate_match_pattern(self, value: str) -> str:
+ pattern = value.strip().lower()
+ try:
+ MCPCustomHarness._meta.get_field("match_pattern").run_validators(pattern)
+ except DjangoValidationError as error:
+ raise serializers.ValidationError(error.messages)
+ return pattern
+
+ def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
+ team_id = self.context["get_team"]().id
+ others = MCPCustomHarness.objects.for_team(team_id)
+ if self.instance is not None:
+ others = others.exclude(pk=self.instance.pk)
+ if "match_pattern" in attrs and others.filter(match_pattern=attrs["match_pattern"]).exists():
+ raise serializers.ValidationError({"match_pattern": DUPLICATE_MATCH_PATTERN_ERROR})
+ if self.instance is None and others.count() >= MAX_CUSTOM_HARNESSES:
+ raise serializers.ValidationError(f"A project can have at most {MAX_CUSTOM_HARNESSES} custom harnesses.")
+ return attrs
+
+ def create(self, validated_data: dict[str, Any]) -> MCPCustomHarness:
+ # validate() already checked for a duplicate pattern, but a concurrent request can still
+ # win the race and insert first. The atomic block keeps the IntegrityError from poisoning
+ # an outer transaction, so the view can still return a normal 400.
+ try:
+ with transaction.atomic():
+ return MCPCustomHarness.objects.create(team_id=self.context["get_team"]().id, **validated_data)
+ except IntegrityError:
+ raise serializers.ValidationError({"match_pattern": DUPLICATE_MATCH_PATTERN_ERROR})
+
+ def update(self, instance: MCPCustomHarness, validated_data: dict[str, Any]) -> MCPCustomHarness:
+ try:
+ with transaction.atomic():
+ return super().update(instance, validated_data)
+ except IntegrityError:
+ raise serializers.ValidationError({"match_pattern": DUPLICATE_MATCH_PATTERN_ERROR})
diff --git a/products/mcp_analytics/backend/presentation/urls.py b/products/mcp_analytics/backend/presentation/urls.py
index 768f72aad919..33fea3eaa84b 100644
--- a/products/mcp_analytics/backend/presentation/urls.py
+++ b/products/mcp_analytics/backend/presentation/urls.py
@@ -2,11 +2,18 @@
from rest_framework.routers import DefaultRouter
-from .views import MCPFeedbackViewSet, MCPIntentClusterViewSet, MCPMissingCapabilityViewSet, MCPSessionViewSet
+from .views import (
+ MCPCustomHarnessViewSet,
+ MCPFeedbackViewSet,
+ MCPIntentClusterViewSet,
+ MCPMissingCapabilityViewSet,
+ MCPSessionViewSet,
+)
router = DefaultRouter()
router.register(r"feedback", MCPFeedbackViewSet, basename="mcp-analytics-feedback")
router.register(r"missing_capabilities", MCPMissingCapabilityViewSet, basename="mcp-analytics-missing-capability")
router.register(r"sessions", MCPSessionViewSet, basename="mcp-analytics-sessions")
router.register(r"intent_clusters", MCPIntentClusterViewSet, basename="mcp-analytics-intent-clusters")
+router.register(r"custom_harnesses", MCPCustomHarnessViewSet, basename="mcp-analytics-custom-harnesses")
urlpatterns = router.urls
diff --git a/products/mcp_analytics/backend/presentation/views.py b/products/mcp_analytics/backend/presentation/views.py
index 506a19876668..41326b2ecea7 100644
--- a/products/mcp_analytics/backend/presentation/views.py
+++ b/products/mcp_analytics/backend/presentation/views.py
@@ -10,6 +10,7 @@ from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
+from rest_framework.serializers import BaseSerializer
from posthog.api.mixins import ValidatedRequest, validated_request
from posthog.api.pagination import BoundedLimitOffsetPagination, page_link, previous_offset
@@ -20,7 +21,7 @@ from posthog.permissions import PostHogFeatureFlagPermission
from products.mcp_analytics.backend import logic
from products.mcp_analytics.backend.facade import api, contracts, enums
-from products.mcp_analytics.backend.models import MCPAnalyticsSubmission
+from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPCustomHarness
from .serializers import (
MCP_SESSION_LIST_DEFAULT_LIMIT,
@@ -28,6 +29,7 @@ from .serializers import (
MCPActivityOverviewQuerySerializer,
MCPActivityOverviewSerializer,
MCPAnalyticsSubmissionSerializer,
+ MCPCustomHarnessSerializer,
MCPFeedbackCreateSerializer,
MCPIntentClusterSnapshotSerializer,
MCPIntentDigestSerializer,
@@ -423,3 +425,33 @@ class MCPMissingCapabilityViewSet(BaseMCPAnalyticsSubmissionViewSet):
)
def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
return self._list_response(request, enums.SubmissionKind.MISSING_CAPABILITY)
+
+
+class MCPCustomHarnessViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
+ """A team's own harness names, shown in place of "Other" in MCP analytics."""
+
+ serializer_class = MCPCustomHarnessSerializer
+ scope_object = "mcp_analytics"
+ queryset = MCPCustomHarness.objects.unscoped()
+ pagination_class = None
+
+ def safely_get_queryset(self, queryset: QuerySet[MCPCustomHarness]) -> QuerySet[MCPCustomHarness]:
+ # Rows save under the parent team (`TeamScopedRootMixin.save()`), so a child environment filters on it too.
+ return queryset.filter(team_id=self.team.parent_team_id or self.team.pk)
+
+ def _should_skip_parents_filter(self) -> bool:
+ # The default parent-lookup filter adds the raw URL team id, which hides the rows a child environment
+ # saved under its parent. safely_get_queryset already scopes every read.
+ return True
+
+ def perform_create(self, serializer: BaseSerializer[Any]) -> None:
+ super().perform_create(serializer)
+ api.invalidate_custom_harness_rules(self.team)
+
+ def perform_update(self, serializer: BaseSerializer[Any]) -> None:
+ super().perform_update(serializer)
+ api.invalidate_custom_harness_rules(self.team)
+
+ def perform_destroy(self, instance: MCPCustomHarness) -> None:
+ super().perform_destroy(instance)
+ api.invalidate_custom_harness_rules(self.team)
diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.py
index a4fbfa1226ef..7a387c79f7e6 100644
--- a/products/mcp_analytics/backend/tests/test_api.py
+++ b/products/mcp_analytics/backend/tests/test_api.py
@@ -25,7 +25,7 @@ from products.access_control.backend.models.property_access_control import Prope
from products.access_control.backend.property_access_control import PropertyAccessLevel
from products.mcp_analytics.backend import intent_generation, logic, mcp_harness
from products.mcp_analytics.backend.facade import api, contracts, enums
-from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPSession
+from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPCustomHarness, MCPSession
from products.mcp_analytics.backend.tests import _MCPAnalyticsTeamScopedTestMixin
INTERNAL_CLIENT = "internal-bot"
@@ -1051,11 +1051,16 @@ class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixin
{"$mcp_oauth_client_name": "cursor-vscode"},
mcp_harness.UNIDENTIFIED_HARNESS_LABEL,
),
+ ("custom_rule_on_client_name", {"$mcp_client_name": "acme-agent"}, "Acme Agent"),
+ ("custom_rule_on_user_agent_only", {"$mcp_client_user_agent": "acme-agent/2.0 (node)"}, "Acme Agent"),
+ ("custom_rule_beats_built_in", {"$mcp_client_name": "cursor-internal"}, "Our Cursor"),
]
)
def test_attributes_clients_from_every_identity_signal(
self, _name: str, properties: dict[str, Any], expected: str
) -> None:
+ MCPCustomHarness.objects.create(team=self.team, name="Acme Agent", match_pattern="acme-agent")
+ MCPCustomHarness.objects.create(team=self.team, name="Our Cursor", match_pattern="cursor-internal")
self._emit_call(properties, count=2)
overview = api.get_activity_overview(self.team)
diff --git a/products/mcp_analytics/backend/tests/test_harness_breakdown.py b/products/mcp_analytics/backend/tests/test_harness_breakdown.py
index 94cf37ce8b1f..bf9660a1c25e 100644
--- a/products/mcp_analytics/backend/tests/test_harness_breakdown.py
+++ b/products/mcp_analytics/backend/tests/test_harness_breakdown.py
@@ -3,6 +3,7 @@ from datetime import UTC, datetime, timedelta
from typing import Any
from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
+from unittest.mock import patch
from parameterized import parameterized
@@ -15,13 +16,18 @@ from posthog.schema import (
PropertyOperator,
)
+from posthog.hogql import parser
from posthog.hogql.parser import parse_expr
from posthog.hogql.query import execute_hogql_query
+from posthog.hogql.visitor import clear_locations
from posthog.hogql_queries.events_query_runner import EventsQueryRunner
from products.mcp_analytics.backend import mcp_harness
+from products.mcp_analytics.backend.facade.api import mcp_harness_virtual_field_expr
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
from products.mcp_analytics.backend.hogql_queries.harness_breakdown import MCPHarnessBreakdownQueryRunner
+from products.mcp_analytics.backend.models import MCPCustomHarness
from products.mcp_analytics.backend.tests import _MCPAnalyticsTeamScopedTestMixin
@@ -48,6 +54,35 @@ def test_harness_expressions_are_parseable_hogql(_name: str, sql: str) -> None:
parse_expr(sql)
+def test_custom_rules_come_before_built_in_buckets_and_are_escaped() -> None:
+ rule = CustomHarnessRule(name="O'Brien \\ Agent", pattern="Acme'Agent")
+
+ for sql in (
+ mcp_harness.harness_label_sql("h", [rule]),
+ mcp_harness.harness_label_or_token_sql("h", custom_rules=[rule]),
+ ):
+ parse_expr(sql)
+ assert sql.index("O\\'Brien") < sql.index("'Claude Code',")
+
+
+def test_virtual_field_with_rules_matches_a_full_parse_and_reuses_the_parse_cache() -> None:
+ rules = (CustomHarnessRule(name="Acme Agent", pattern="acme-agent"),)
+ full_parse = parse_expr(
+ "if(startsWith(event, '$mcp_'), arrayMap(h -> {label}, [{token}])[1], NULL)",
+ placeholders={
+ "label": parse_expr(mcp_harness.harness_label_sql("h", rules)),
+ "token": parse_expr(mcp_harness.HARNESS_TOKEN_SQL),
+ },
+ )
+ mcp_harness_virtual_field_expr(rules)
+
+ with patch.object(parser, "_invoke_parser", wraps=parser._invoke_parser) as invoke_parser:
+ expr = mcp_harness_virtual_field_expr(rules)
+
+ assert invoke_parser.call_count == 0
+ assert clear_locations(expr) == clear_locations(full_parse)
+
+
def test_label_or_token_keeps_the_bounded_label_set_reachable() -> None:
# The verbatim fallback must only replace the "Other" arm — if it shadowed a real
# branch, recognized clients would start rendering as raw tokens.
@@ -104,6 +139,17 @@ class TestMCPHarnessBreakdownQueryRunner(_MCPAnalyticsTeamScopedTestMixin, Click
self.assertEqual(response.column_formats, ["mcp_harness", None])
self.assertEqual(response.results, [("OpenAI Codex", 2), ("Claude Code", 1)])
+ def test_custom_harness_names_calls_in_the_breakdown(self) -> None:
+ MCPCustomHarness.objects.create(team=self.team, name="Acme Agent", match_pattern="acme-agent")
+ MCPCustomHarness.objects.create(team=self.team, name="Our Cursor", match_pattern="cursor-internal")
+ self._emit(properties={"$mcp_client_name": "Acme-Agent/1.2"})
+ self._emit(properties={"$mcp_client_user_agent": "acme-agent/2.0 (node)"}, distinct_id="d2")
+ self._emit(properties={"$mcp_client_name": "cursor-internal"}, distinct_id="d3")
+ self._emit(properties={"$mcp_client_name": "something-else"}, distinct_id="d4")
+ flush_persons_and_events()
+
+ self.assertEqual(set(self._breakdown()), {"Acme Agent", "Other", "Our Cursor"})
+
def test_virtual_harness_property_is_empty_on_other_events(self) -> None:
_create_event(team=self.team, event="$pageview", distinct_id="d1", properties={"$mcp_client_name": "codex"})
diff --git a/products/mcp_analytics/backend/tests/test_missing_capabilities.py b/products/mcp_analytics/backend/tests/test_missing_capabilities.py
index 136088a20cbf..6f07df8c1afd 100644
--- a/products/mcp_analytics/backend/tests/test_missing_capabilities.py
+++ b/products/mcp_analytics/backend/tests/test_missing_capabilities.py
@@ -9,6 +9,7 @@ from parameterized import parameterized
from posthog.schema import DateRange, MCPMissingCapabilitiesItem, MCPMissingCapabilitiesQuery
from products.mcp_analytics.backend.hogql_queries.missing_capabilities import MCPMissingCapabilitiesQueryRunner
+from products.mcp_analytics.backend.models import MCPCustomHarness
from products.mcp_analytics.backend.tests import _MCPAnalyticsTeamScopedTestMixin
@@ -101,9 +102,11 @@ class TestMCPMissingCapabilitiesQueryRunner(_MCPAnalyticsTeamScopedTestMixin, Cl
assert [row.intent for row in self._run(filterTestAccounts=filter_test_accounts)] == expected
- def test_labels_unidentified_and_self_reported_clients(self) -> None:
+ def test_labels_unidentified_self_reported_and_custom_named_clients(self) -> None:
+ MCPCustomHarness.objects.create(team=self.team, name="Acme Agent", match_pattern="acme-agent")
self._emit(intent="no client identity", client_name=None)
self._emit(intent="self-reported client", client_name="some-inhouse-agent")
+ self._emit(intent="custom named client", client_name="acme-agent")
flush_persons_and_events()
harness_by_intent = {row.intent: row.harness for row in self._run()}
@@ -111,6 +114,7 @@ class TestMCPMissingCapabilitiesQueryRunner(_MCPAnalyticsTeamScopedTestMixin, Cl
assert harness_by_intent == {
"no client identity": "Unidentified client",
"self-reported client": "some-inhouse-agent",
+ "custom named client": "Acme Agent",
}
def test_pages_equal_timestamps_deterministically(self) -> None:
diff --git a/products/mcp_analytics/backend/tests/test_presentation.py b/products/mcp_analytics/backend/tests/test_presentation.py
index 72c64fd2e4c8..83f2bf9ad7ba 100644
--- a/products/mcp_analytics/backend/tests/test_presentation.py
+++ b/products/mcp_analytics/backend/tests/test_presentation.py
@@ -5,12 +5,15 @@ from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event
from unittest.mock import AsyncMock, MagicMock, patch
from django.core.cache import cache
+from django.db import IntegrityError
from django.test import SimpleTestCase
from parameterized import parameterized
from rest_framework import serializers, status
from temporalio.exceptions import WorkflowAlreadyStartedError
+from posthog.schema import MCPHarnessBreakdownQuery
+
from posthog.models import Organization, Team
from posthog.models.utils import uuid7
from posthog.temporal.mcp_analytics.intent_clustering.constants import (
@@ -20,7 +23,15 @@ from posthog.temporal.mcp_analytics.intent_clustering.constants import (
)
from products.mcp_analytics.backend import intent_generation
-from products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPIntentClusterSnapshot, MCPSession
+from products.mcp_analytics.backend.facade.api import custom_harness_rules
+from products.mcp_analytics.backend.facade.contracts import CustomHarnessRule
+from products.mcp_analytics.backend.hogql_queries.harness_breakdown import MCPHarnessBreakdownQueryRunner
+from products.mcp_analytics.backend.models import (
+ MCPAnalyticsSubmission,
+ MCPCustomHarness,
+ MCPIntentClusterSnapshot,
+ MCPSession,
+)
from products.mcp_analytics.backend.presentation.serializers import (
MCP_SESSION_LIST_DEFAULT_LIMIT,
MCP_SESSION_LIST_MAX_LIMIT,
@@ -631,6 +642,144 @@ class TestMCPAnalyticsPresentation(_MCPAnalyticsTeamScopedTestMixin, APIBaseTest
assert data["next"] is not None
+class TestMCPCustomHarnessEndpoint(_MCPAnalyticsTeamScopedTestMixin, APIBaseTest):
+ def _url(self, suffix: str = "") -> str:
+ return f"/api/environments/{self.team.id}/mcp_analytics/custom_harnesses/{suffix}"
+
+ def test_create_lowercases_the_pattern_and_lists_it(self) -> None:
+ response = self.client.post(self._url(), {"name": "Acme Agent", "match_pattern": " Acme-Agent "}, format="json")
+
+ assert response.status_code == status.HTTP_201_CREATED
+ assert response.json()["match_pattern"] == "acme-agent"
+ listed = self.client.get(self._url()).json()
+ assert [(row["name"], row["match_pattern"]) for row in listed] == [("Acme Agent", "acme-agent")]
+
+ @parameterized.expand(
+ [
+ ("quote_in_name", {"name": "Bad'name", "match_pattern": "ok"}),
+ ("brace_in_name", {"name": "Bad{name}", "match_pattern": "ok"}),
+ ("quote_in_pattern", {"name": "Ok", "match_pattern": "bad'pattern"}),
+ ("empty_pattern", {"name": "Ok", "match_pattern": " "}),
+ ("reserved_other", {"name": "other", "match_pattern": "ok"}),
+ ("reserved_unidentified", {"name": "Unidentified client", "match_pattern": "ok"}),
+ ]
+ )
+ def test_create_rejects_unsafe_values(self, _name: str, payload: dict[str, str]) -> None:
+ response = self.client.post(self._url(), payload, format="json")
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+
+ def test_create_rejects_duplicate_pattern_and_enforces_the_limit(self) -> None:
+ self.client.post(self._url(), {"name": "One", "match_pattern": "one"}, format="json")
+ duplicate = self.client.post(self._url(), {"name": "Again", "match_pattern": "ONE"}, format="json")
+ assert duplicate.status_code == status.HTTP_400_BAD_REQUEST
+
+ MCPCustomHarness.objects.bulk_create(
+ [MCPCustomHarness(team=self.team, name=f"H{i}", match_pattern=f"h{i}") for i in range(19)]
+ )
+ over_limit = self.client.post(self._url(), {"name": "Extra", "match_pattern": "extra"}, format="json")
+ assert over_limit.status_code == status.HTTP_400_BAD_REQUEST
+
+ def test_a_redis_outage_falls_back_to_postgres_without_reporting_an_exception(self) -> None:
+ MCPCustomHarness.objects.create(team=self.team, name="One", match_pattern="one")
+ outage = Exception("redis is down")
+
+ with (
+ patch("posthoganalytics.api_key", "phc_test"),
+ patch("posthoganalytics.capture_exception") as capture_exception,
+ ):
+ with patch.object(cache, "get", side_effect=outage), patch.object(cache, "add", side_effect=outage):
+ rules = custom_harness_rules(self.team)
+ with self.captureOnCommitCallbacks() as on_commit_callbacks:
+ response = self.client.post(self._url(), {"name": "Two", "match_pattern": "two"}, format="json")
+ with patch.object(cache, "delete", side_effect=outage):
+ for callback in on_commit_callbacks:
+ callback()
+
+ assert rules == (CustomHarnessRule(name="One", pattern="one"),)
+ assert response.status_code == status.HTTP_201_CREATED
+ capture_exception.assert_not_called()
+
+ def test_a_save_drops_rules_another_writer_cached_before_it_committed(self) -> None:
+ self.client.post(self._url(), {"name": "One", "match_pattern": "one"}, format="json")
+
+ with self.captureOnCommitCallbacks(execute=True):
+ self.client.post(self._url(), {"name": "Two", "match_pattern": "two"}, format="json")
+ cache.set(f"mcp_analytics:custom_harness_rules:{self.team.pk}", (("One", "one"),))
+
+ assert custom_harness_rules(self.team) == (
+ CustomHarnessRule(name="One", pattern="one"),
+ CustomHarnessRule(name="Two", pattern="two"),
+ )
+
+ def test_create_returns_a_400_when_the_unique_constraint_wins_a_race(self) -> None:
+ with patch(
+ "products.mcp_analytics.backend.models.MCPCustomHarness.objects.create",
+ side_effect=IntegrityError("duplicate key"),
+ ):
+ response = self.client.post(self._url(), {"name": "One", "match_pattern": "one"}, format="json")
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert response.json()["attr"] == "match_pattern"
+
+ def _breakdown_cache_key(self) -> str:
+ return MCPHarnessBreakdownQueryRunner(query=MCPHarnessBreakdownQuery(), team=self.team).get_cache_key()
+
+ def test_create_update_and_delete_refresh_the_cached_rules_and_query_cache_keys(self) -> None:
+ assert custom_harness_rules(self.team) == ()
+ cache_keys = [self._breakdown_cache_key()]
+
+ with self.captureOnCommitCallbacks(execute=True):
+ created = self.client.post(self._url(), {"name": "One", "match_pattern": "one"}, format="json").json()
+ assert custom_harness_rules(self.team) == (CustomHarnessRule(name="One", pattern="one"),)
+ with self.assertNumQueries(0):
+ assert custom_harness_rules(self.team) == (CustomHarnessRule(name="One", pattern="one"),)
+ cache_keys.append(self._breakdown_cache_key())
+
+ with self.captureOnCommitCallbacks(execute=True):
+ updated = self.client.patch(self._url(f"{created['id']}/"), {"name": "Uno"}, format="json")
+ assert updated.status_code == status.HTTP_200_OK
+ assert updated.json()["name"] == "Uno"
+ assert custom_harness_rules(self.team) == (CustomHarnessRule(name="Uno", pattern="one"),)
+ cache_keys.append(self._breakdown_cache_key())
+
+ with self.captureOnCommitCallbacks(execute=True):
+ deleted = self.client.delete(self._url(f"{created['id']}/"))
+ assert deleted.status_code == status.HTTP_204_NO_CONTENT
+ assert self.client.get(self._url()).json() == []
+ assert custom_harness_rules(self.team) == ()
+ cache_keys.append(self._breakdown_cache_key())
+
+ assert len(set(cache_keys[:3])) == 3
+ assert cache_keys[3] == cache_keys[0]
+
+ def test_a_longer_pattern_matches_before_a_shorter_one_it_contains(self) -> None:
+ self.client.post(self._url(), {"name": "Acme", "match_pattern": "acme"}, format="json")
+ self.client.post(self._url(), {"name": "Acme agent", "match_pattern": "acme-agent"}, format="json")
+
+ assert custom_harness_rules(self.team) == (
+ CustomHarnessRule(name="Acme agent", pattern="acme-agent"),
+ CustomHarnessRule(name="Acme", pattern="acme"),
+ )
+
+ def test_other_teams_harnesses_are_not_listed(self) -> None:
+ other_team = Team.objects.create(organization=self.organization, name="other-team")
+ MCPCustomHarness.objects.create(team=other_team, name="Elsewhere", match_pattern="elsewhere")
+
+ assert self.client.get(self._url()).json() == []
+
+ def test_child_environment_lists_and_deletes_the_rules_it_creates(self) -> None:
+ child = Team.objects.create(
+ organization=self.organization, project=self.team.project, parent_team=self.team, name="Child"
+ )
+ child_url = f"/api/environments/{child.id}/mcp_analytics/custom_harnesses/"
+
+ created = self.client.post(child_url, {"name": "Acme", "match_pattern": "acme"}, format="json").json()
+
+ assert [row["name"] for row in self.client.get(child_url).json()] == ["Acme"]
+ assert self.client.delete(f"{child_url}{created['id']}/").status_code == status.HTTP_204_NO_CONTENT
+
+
class TestMCPSessionIntentEndpoint(_MCPAnalyticsTeamScopedTestMixin, APIBaseTest):
def _url(self, session_id: str) -> str:
return f"/api/environments/{self.team.id}/mcp_analytics/sessions/{session_id}/generate_intent/"
diff --git a/products/mcp_analytics/backend/tests/test_tool_tables.py b/products/mcp_analytics/backend/tests/test_tool_tables.py
index 4c2c3686fdc1..45b28227bcf6 100644
--- a/products/mcp_analytics/backend/tests/test_tool_tables.py
+++ b/products/mcp_analytics/backend/tests/test_tool_tables.py
@@ -34,6 +34,7 @@ from products.mcp_analytics.backend.hogql_queries.tool_tables import (
MCPToolStatsQueryRunner,
MCPToolTopUsersQueryRunner,
)
+from products.mcp_analytics.backend.models import MCPCustomHarness
from products.mcp_analytics.backend.tests import _MCPAnalyticsTeamScopedTestMixin
NEW_SDK_SOURCE = "posthog_mcp_analytics"
@@ -82,9 +83,11 @@ class TestMCPToolTopUsersQueryRunner(_MCPAnalyticsTeamScopedTestMixin, Clickhous
("sdk_mcp_remote_stripped", "claude-ai (via mcp-remote 0.1.37)", ["Claude.ai"]),
("sdk_cursor", "cursor-vscode", ["Cursor"]),
("sdk_unknown_to_other", "totally-unknown-thing", ["Other"]),
+ ("custom_rule_beats_built_in", "cursor-internal", ["Our Cursor"]),
]
)
def test_resolves_harness_labels(self, _name: str, client_name: str, expected: list[str]) -> None:
+ MCPCustomHarness.objects.create(team=self.team, name="Our Cursor", match_pattern="cursor-internal")
self._emit(client_name=client_name)
flush_persons_and_events()
diff --git a/products/mcp_analytics/frontend/MCPAnalyticsDashboard.stories.tsx b/products/mcp_analytics/frontend/MCPAnalyticsDashboard.stories.tsx
index 67c83498cda1..69bca4f9b319 100644
--- a/products/mcp_analytics/frontend/MCPAnalyticsDashboard.stories.tsx
+++ b/products/mcp_analytics/frontend/MCPAnalyticsDashboard.stories.tsx
@@ -85,6 +85,8 @@ const SESSION_RESULTS = [
const HARNESS_RESULTS = [
{ harness: 'Claude Code', total_calls: 6200, errors: 240, error_rate_pct: 3.9, sessions: 820 },
{ harness: 'Cursor', total_calls: 2100, errors: 96, error_rate_pct: 4.6, sessions: 410 },
+ // A team-defined custom harness name. It has no brand mark, so it shows the generic one.
+ { harness: 'Acme agent', total_calls: 1500, errors: 30, error_rate_pct: 2.0, sessions: 260 },
{ harness: 'OpenAI Codex', total_calls: 980, errors: 71, error_rate_pct: 7.2, sessions: 180 },
{ harness: 'Claude.ai', total_calls: 760, errors: 22, error_rate_pct: 2.9, sessions: 240 },
{ harness: 'VS Code', total_calls: 540, errors: 12, error_rate_pct: 2.2, sessions: 120 },
diff --git a/products/mcp_analytics/frontend/generated/api.schemas.ts b/products/mcp_analytics/frontend/generated/api.schemas.ts
index e40efd791117..96611cb24547 100644
--- a/products/mcp_analytics/frontend/generated/api.schemas.ts
+++ b/products/mcp_analytics/frontend/generated/api.schemas.ts
@@ -7,6 +7,40 @@
* PostHog API - generated
* OpenAPI spec version: 1.0.0
*/
+export interface MCPCustomHarnessApi {
+ readonly id: string
+ /**
+ * Name to show for calls from this harness.
+ * @maxLength 100
+ * @pattern ^[\w .+&()/-]+$
+ */
+ name: string
+ /**
+ * Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent/1.2`.
+ * @maxLength 100
+ */
+ match_pattern: string
+ readonly created_at: string
+ readonly updated_at: string
+}
+
+export interface PatchedMCPCustomHarnessApi {
+ readonly id?: string
+ /**
+ * Name to show for calls from this harness.
+ * @maxLength 100
+ * @pattern ^[\w .+&()/-]+$
+ */
+ name?: string
+ /**
+ * Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent/1.2`.
+ * @maxLength 100
+ */
+ match_pattern?: string
+ readonly created_at?: string
+ readonly updated_at?: string
+}
+
/**
* * `feedback` - Feedback
* * `missing_capability` - Missing capability
diff --git a/products/mcp_analytics/frontend/generated/api.ts b/products/mcp_analytics/frontend/generated/api.ts
index d0617b934726..e081352d96b8 100644
--- a/products/mcp_analytics/frontend/generated/api.ts
+++ b/products/mcp_analytics/frontend/generated/api.ts
@@ -11,6 +11,7 @@ import { apiMutator } from '../../../../frontend/src/lib/api-orval-mutator'
import type {
MCPActivityOverviewApi,
MCPAnalyticsSubmissionApi,
+ MCPCustomHarnessApi,
MCPFeedbackCreateApi,
MCPIntentClusterSnapshotApi,
MCPIntentDigestApi,
@@ -26,8 +27,141 @@ import type {
PaginatedMCPAnalyticsSubmissionListApi,
PaginatedMCPSessionListApi,
PaginatedMCPToolCallListApi,
+ PatchedMCPCustomHarnessApi,
} from './api.schemas'
+// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497
+type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? A : B
+
+type WritableKeys<T> = {
+ [P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P>
+}[keyof T]
+
+type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
+type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never
+
+type Writable<T> = Pick<T, WritableKeys<T>>
+type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
+ ? {
+ [P in keyof Writable<T>]: T[P] extends object ? NonReadonly<NonNullable<T[P]>> : T[P]
+ }
+ : DistributeReadOnlyOverUnions<T>
+
+export const getMcpAnalyticsCustomHarnessesListUrl = (projectId: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesList = async (
+ projectId: string,
+ options?: RequestInit
+): Promise<MCPCustomHarnessApi[]> => {
+ return apiMutator<MCPCustomHarnessApi[]>(getMcpAnalyticsCustomHarnessesListUrl(projectId), {
+ ...options,
+ method: 'GET',
+ })
+}
+
+export const getMcpAnalyticsCustomHarnessesCreateUrl = (projectId: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesCreate = async (
+ projectId: string,
+ mCPCustomHarnessApi: NonReadonly<MCPCustomHarnessApi>,
+ options?: RequestInit
+): Promise<MCPCustomHarnessApi> => {
+ return apiMutator<MCPCustomHarnessApi>(getMcpAnalyticsCustomHarnessesCreateUrl(projectId), {
+ ...options,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(mCPCustomHarnessApi),
+ })
+}
+
+export const getMcpAnalyticsCustomHarnessesRetrieveUrl = (projectId: string, id: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/${id}/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesRetrieve = async (
+ projectId: string,
+ id: string,
+ options?: RequestInit
+): Promise<MCPCustomHarnessApi> => {
+ return apiMutator<MCPCustomHarnessApi>(getMcpAnalyticsCustomHarnessesRetrieveUrl(projectId, id), {
+ ...options,
+ method: 'GET',
+ })
+}
+
+export const getMcpAnalyticsCustomHarnessesUpdateUrl = (projectId: string, id: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/${id}/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesUpdate = async (
+ projectId: string,
+ id: string,
+ mCPCustomHarnessApi: NonReadonly<MCPCustomHarnessApi>,
+ options?: RequestInit
+): Promise<MCPCustomHarnessApi> => {
+ return apiMutator<MCPCustomHarnessApi>(getMcpAnalyticsCustomHarnessesUpdateUrl(projectId, id), {
+ ...options,
+ method: 'PUT',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(mCPCustomHarnessApi),
+ })
+}
+
+export const getMcpAnalyticsCustomHarnessesPartialUpdateUrl = (projectId: string, id: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/${id}/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesPartialUpdate = async (
+ projectId: string,
+ id: string,
+ patchedMCPCustomHarnessApi?: NonReadonly<PatchedMCPCustomHarnessApi>,
+ options?: RequestInit
+): Promise<MCPCustomHarnessApi> => {
+ return apiMutator<MCPCustomHarnessApi>(getMcpAnalyticsCustomHarnessesPartialUpdateUrl(projectId, id), {
+ ...options,
+ method: 'PATCH',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(patchedMCPCustomHarnessApi),
+ })
+}
+
+export const getMcpAnalyticsCustomHarnessesDestroyUrl = (projectId: string, id: string) => {
+ return `/api/projects/${projectId}/mcp_analytics/custom_harnesses/${id}/`
+}
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesDestroy = async (
+ projectId: string,
+ id: string,
+ options?: RequestInit
+): Promise<void> => {
+ return apiMutator<void>(getMcpAnalyticsCustomHarnessesDestroyUrl(projectId, id), {
+ ...options,
+ method: 'DELETE',
+ })
+}
+
export const getMcpAnalyticsFeedbackListUrl = (projectId: string, params?: McpAnalyticsFeedbackListParams) => {
const normalizedParams = new URLSearchParams()
diff --git a/products/mcp_analytics/frontend/generated/api.zod.ts b/products/mcp_analytics/frontend/generated/api.zod.ts
index c2d1ee677772..ee6d4785a213 100644
--- a/products/mcp_analytics/frontend/generated/api.zod.ts
+++ b/products/mcp_analytics/frontend/generated/api.zod.ts
@@ -9,6 +9,74 @@
*/
import * as zod from 'zod'
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesCreateBodyNameMax = 100
+
+export const mcpAnalyticsCustomHarnessesCreateBodyNameRegExp = new RegExp('^[\\w .+&()\/-]+$')
+export const mcpAnalyticsCustomHarnessesCreateBodyMatchPatternMax = 100
+
+export const McpAnalyticsCustomHarnessesCreateBody = /* @__PURE__ */ zod.object({
+ name: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesCreateBodyNameMax)
+ .regex(mcpAnalyticsCustomHarnessesCreateBodyNameRegExp)
+ .describe('Name to show for calls from this harness.'),
+ match_pattern: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesCreateBodyMatchPatternMax)
+ .describe(
+ 'Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent\/1.2`.'
+ ),
+})
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesUpdateBodyNameMax = 100
+
+export const mcpAnalyticsCustomHarnessesUpdateBodyNameRegExp = new RegExp('^[\\w .+&()\/-]+$')
+export const mcpAnalyticsCustomHarnessesUpdateBodyMatchPatternMax = 100
+
+export const McpAnalyticsCustomHarnessesUpdateBody = /* @__PURE__ */ zod.object({
+ name: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesUpdateBodyNameMax)
+ .regex(mcpAnalyticsCustomHarnessesUpdateBodyNameRegExp)
+ .describe('Name to show for calls from this harness.'),
+ match_pattern: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesUpdateBodyMatchPatternMax)
+ .describe(
+ 'Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent\/1.2`.'
+ ),
+})
+
+/**
+ * A team's own harness names, shown in place of "Other" in MCP analytics.
+ */
+export const mcpAnalyticsCustomHarnessesPartialUpdateBodyNameMax = 100
+
+export const mcpAnalyticsCustomHarnessesPartialUpdateBodyNameRegExp = new RegExp('^[\\w .+&()\/-]+$')
+export const mcpAnalyticsCustomHarnessesPartialUpdateBodyMatchPatternMax = 100
+
+export const McpAnalyticsCustomHarnessesPartialUpdateBody = /* @__PURE__ */ zod.object({
+ name: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesPartialUpdateBodyNameMax)
+ .regex(mcpAnalyticsCustomHarnessesPartialUpdateBodyNameRegExp)
+ .optional()
+ .describe('Name to show for calls from this harness.'),
+ match_pattern: zod
+ .string()
+ .max(mcpAnalyticsCustomHarnessesPartialUpdateBodyMatchPatternMax)
+ .optional()
+ .describe(
+ 'Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent\/1.2`.'
+ ),
+})
+
/**
* Create a new MCP feedback submission for the current project.
*/
diff --git a/products/mcp_analytics/frontend/settings/McpCustomHarnessForm.tsx b/products/mcp_analytics/frontend/settings/McpCustomHarnessForm.tsx
new file mode 100644
index 000000000000..b2beaf3520c3
--- /dev/null
+++ b/products/mcp_analytics/frontend/settings/McpCustomHarnessForm.tsx
@@ -0,0 +1,49 @@
+import { useActions, useValues } from 'kea'
+import { Form } from 'kea-forms'
+
+import { LemonButton, LemonInput } from '@posthog/lemon-ui'
+
+import { LemonField } from 'lib/lemon-ui/LemonField'
+
+import { mcpCustomHarnessesLogic } from './mcpCustomHarnessesLogic'
+
+export function McpCustomHarnessForm(): JSX.Element {
+ const { isCustomHarnessFormSubmitting, saveInProgressReason } = useValues(mcpCustomHarnessesLogic)
+ const { cancelEditingHarness } = useActions(mcpCustomHarnessesLogic)
+
+ return (
+ <Form
+ logic={mcpCustomHarnessesLogic}
+ formKey="customHarnessForm"
+ enableFormOnSubmit
+ className="flex flex-col gap-2 border rounded p-2 bg-surface-primary"
+ >
+ <div className="flex flex-wrap items-start gap-2">
+ <LemonField name="name" label="Name" className="flex-1 min-w-60">
+ <LemonInput autoFocus placeholder="Acme agent" maxLength={100} />
+ </LemonField>
+ <LemonField
+ name="match_pattern"
+ label="Client name contains"
+ help="Not case sensitive. Matches the client name or User-Agent product the harness reports."
+ className="flex-1 min-w-60"
+ >
+ <LemonInput placeholder="acme-agent" maxLength={100} />
+ </LemonField>
+ </div>
+ <div className="flex justify-end gap-2">
+ <LemonButton type="secondary" onClick={cancelEditingHarness} disabledReason={saveInProgressReason}>
+ Cancel
+ </LemonButton>
+ <LemonButton
+ type="primary"
+ htmlType="submit"
+ loading={isCustomHarnessFormSubmitting}
+ data-attr="mcp-custom-harness-save"
+ >
+ Save
+ </LemonButton>
+ </div>
+ </Form>
+ )
+}
diff --git a/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.test.tsx b/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.test.tsx
new file mode 100644
index 000000000000..34213bfe5463
--- /dev/null
+++ b/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.test.tsx
@@ -0,0 +1,103 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { Provider } from 'kea'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import type { MCPCustomHarnessApi } from '../generated/api.schemas'
+import { McpCustomHarnessesSettings } from './McpCustomHarnessesSettings'
+
+const HARNESSES_URL = '/api/projects/:team_id/mcp_analytics/custom_harnesses/'
+
+describe('McpCustomHarnessesSettings', () => {
+ let stored: MCPCustomHarnessApi[]
+
+ beforeEach(() => {
+ stored = []
+ useMocks({
+ get: { [HARNESSES_URL]: () => [200, stored] },
+ post: {
+ [HARNESSES_URL]: async ({ request }) => {
+ const body = (await request.json()) as { name: string; match_pattern: string }
+ const created = {
+ ...body,
+ id: '0193f2a1-0000-7000-8000-000000000001',
+ created_at: '2026-01-01T00:00:00Z',
+ updated_at: '2026-01-01T00:00:00Z',
+ }
+ stored = [...stored, created]
+ return [201, created]
+ },
+ },
+ patch: {
+ [`${HARNESSES_URL}:id/`]: async ({ params, request }) => {
+ const body = (await request.json()) as Partial<MCPCustomHarnessApi>
+ stored = stored.map((h) => (h.id === params.id ? { ...h, ...body } : h))
+ return [200, stored.find((h) => h.id === params.id)]
+ },
+ },
+ })
+ initKeaTests()
+ })
+
+ afterEach(() => {
+ cleanup()
+ })
+
+ it('adds a harness inline, then edits it in place', async () => {
+ render(
+ <Provider>
+ <McpCustomHarnessesSettings />
+ </Provider>
+ )
+ await userEvent.click(await screen.findByText('Add custom harness'))
+ await userEvent.type(screen.getByPlaceholderText('Acme agent'), 'Beta bot')
+ await userEvent.type(screen.getByPlaceholderText('acme-agent'), 'beta-bot')
+ await userEvent.click(screen.getByText('Save'))
+
+ expect(await screen.findByText('Beta bot')).toBeInTheDocument()
+ expect(screen.queryByPlaceholderText('Acme agent')).not.toBeInTheDocument()
+
+ await userEvent.click(screen.getByLabelText('Edit Beta bot'))
+ const nameInput = screen.getByDisplayValue('Beta bot')
+ await userEvent.clear(nameInput)
+ await userEvent.type(nameInput, 'Beta assistant')
+ await userEvent.click(screen.getByText('Save'))
+
+ expect(await screen.findByText('Beta assistant')).toBeInTheDocument()
+ await waitFor(() => expect(screen.queryByText('Beta bot')).not.toBeInTheDocument())
+ })
+
+ it('blocks opening another form while a save is pending', async () => {
+ stored = [
+ { id: 'a', name: 'Alpha', match_pattern: 'alpha', created_at: '', updated_at: '' },
+ { id: 'b', name: 'Beta', match_pattern: 'beta', created_at: '', updated_at: '' },
+ ] as MCPCustomHarnessApi[]
+ let finishSave: () => void = () => {}
+ useMocks({
+ patch: {
+ [`${HARNESSES_URL}:id/`]: () =>
+ new Promise((resolve) => {
+ finishSave = () => resolve([200, stored[0]])
+ }),
+ },
+ })
+ render(
+ <Provider>
+ <McpCustomHarnessesSettings />
+ </Provider>
+ )
+ await userEvent.click(await screen.findByLabelText('Edit Alpha'))
+ await userEvent.click(screen.getByText('Save'))
+
+ await waitFor(() => expect(screen.getByLabelText('Edit Beta')).toHaveAttribute('aria-disabled', 'true'))
+ expect(screen.getByText('Add custom harness').closest('button')).toHaveAttribute('aria-disabled', 'true')
+ expect(screen.getByText('Cancel').closest('button')).toHaveAttribute('aria-disabled', 'true')
+
+ finishSave()
+ await waitFor(() => expect(screen.getByLabelText('Edit Beta')).toHaveAttribute('aria-disabled', 'false'))
+ })
+})
diff --git a/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.tsx b/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.tsx
new file mode 100644
index 000000000000..61685d20eb03
--- /dev/null
+++ b/products/mcp_analytics/frontend/settings/McpCustomHarnessesSettings.tsx
@@ -0,0 +1,88 @@
+import { useActions, useValues } from 'kea'
+
+import { IconPencil, IconPlus, IconTrash } from '@posthog/icons'
+import { LemonButton, LemonDialog, LemonSkeleton } from '@posthog/lemon-ui'
+
+import { mcpCustomHarnessesLogic } from './mcpCustomHarnessesLogic'
+import { McpCustomHarnessForm } from './McpCustomHarnessForm'
+
+export function McpCustomHarnessesSettings(): JSX.Element {
+ const { customHarnesses, customHarnessesLoading, editingHarnessId, deletingHarnessId, saveInProgressReason } =
+ useValues(mcpCustomHarnessesLogic)
+ const { startAddingHarness, startEditingHarness, deleteCustomHarness } = useActions(mcpCustomHarnessesLogic)
+
+ return (
+ <div className="flex flex-col gap-2" data-attr="mcp-custom-harnesses">
+ {editingHarnessId === 'new' ? (
+ <McpCustomHarnessForm />
+ ) : (
+ <LemonButton
+ className="w-full"
+ type="secondary"
+ icon={<IconPlus />}
+ onClick={startAddingHarness}
+ disabledReason={saveInProgressReason}
+ data-attr="mcp-custom-harness-add"
+ >
+ Add custom harness
+ </LemonButton>
+ )}
+
+ {customHarnesses === null && customHarnessesLoading ? (
+ <LemonSkeleton className="h-10" repeat={2} />
+ ) : customHarnesses?.length === 0 && editingHarnessId !== 'new' ? (
+ <p className="text-secondary m-0">
+ No custom harnesses yet. Add one to name calls from your own clients.
+ </p>
+ ) : (
+ customHarnesses?.map((harness) =>
+ editingHarnessId === harness.id ? (
+ <McpCustomHarnessForm key={harness.id} />
+ ) : (
+ <div
+ key={harness.id}
+ className="border rounded flex items-center gap-2 p-2 pl-4 bg-surface-primary"
+ >
+ <span className="flex-1 min-w-0 truncate font-semibold" title={harness.name}>
+ {harness.name}
+ </span>
+ <span className="flex-1 min-w-0 truncate text-secondary" title={harness.match_pattern}>
+ Matches <code>{harness.match_pattern}</code>
+ </span>
+ <div className="flex gap-1 shrink-0">
+ <LemonButton
+ icon={<IconPencil />}
+ tooltip="Edit"
+ aria-label={`Edit ${harness.name}`}
+ onClick={() => startEditingHarness(harness)}
+ disabledReason={saveInProgressReason}
+ data-attr="mcp-custom-harness-edit"
+ />
+ <LemonButton
+ icon={<IconTrash />}
+ tooltip="Delete"
+ aria-label={`Delete ${harness.name}`}
+ loading={deletingHarnessId === harness.id}
+ onClick={() =>
+ LemonDialog.open({
+ title: `Delete ${harness.name}?`,
+ description:
+ 'Calls from this client go back to the built-in harness names.',
+ primaryButton: {
+ status: 'danger',
+ children: 'Delete',
+ onClick: () => deleteCustomHarness(harness.id),
+ },
+ secondaryButton: { children: 'Cancel' },
+ })
+ }
+ data-attr="mcp-custom-harness-delete"
+ />
+ </div>
+ </div>
+ )
+ )
+ )}
+ </div>
+ )
+}
diff --git a/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.test.ts b/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.test.ts
new file mode 100644
index 000000000000..8096b9cc791f
--- /dev/null
+++ b/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.test.ts
@@ -0,0 +1,163 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import type { MCPCustomHarnessApi } from '../generated/api.schemas'
+import { mcpCustomHarnessesLogic } from './mcpCustomHarnessesLogic'
+
+const HARNESSES_URL = '/api/projects/:team_id/mcp_analytics/custom_harnesses/'
+
+const harness = (id: string, name: string, match_pattern: string): MCPCustomHarnessApi => ({
+ id,
+ name,
+ match_pattern,
+ created_at: '2026-01-01T00:00:00Z',
+ updated_at: '2026-01-01T00:00:00Z',
+})
+
+describe('mcpCustomHarnessesLogic', () => {
+ let stored: MCPCustomHarnessApi[]
+ let posted: unknown[]
+ let patched: { id: string; body: unknown }[]
+ let deleted: string[]
+ let rejectCreate: boolean
+ let releaseDelete: () => void
+ let deleteReleased: Promise<void>
+
+ beforeEach(() => {
+ stored = [harness('a', 'Acme agent', 'acme-agent')]
+ posted = []
+ patched = []
+ deleted = []
+ rejectCreate = false
+ deleteReleased = new Promise<void>((resolve) => {
+ releaseDelete = resolve
+ })
+ useMocks({
+ get: { [HARNESSES_URL]: () => [200, stored] },
+ post: {
+ [HARNESSES_URL]: async ({ request }) => {
+ const body = (await request.json()) as { name: string; match_pattern: string }
+ posted.push(body)
+ if (rejectCreate) {
+ return [400, { detail: 'A custom harness already uses this pattern.' }]
+ }
+ const created = harness(`id-${posted.length}`, body.name, body.match_pattern)
+ stored = [...stored, created]
+ return [201, created]
+ },
+ },
+ patch: {
+ [`${HARNESSES_URL}:id/`]: async ({ params, request }) => {
+ const body = (await request.json()) as Partial<MCPCustomHarnessApi>
+ patched.push({ id: params.id as string, body })
+ stored = stored.map((h) => (h.id === params.id ? { ...h, ...body } : h))
+ return [200, stored.find((h) => h.id === params.id)]
+ },
+ },
+ delete: {
+ [`${HARNESSES_URL}:id/`]: async ({ params }) => {
+ await deleteReleased
+ deleted.push(params.id as string)
+ stored = stored.filter((h) => h.id !== params.id)
+ return [204]
+ },
+ },
+ })
+ initKeaTests()
+ jest.spyOn(lemonToast, 'error').mockImplementation(() => 'toast')
+ })
+
+ const mounted = async (): Promise<ReturnType<typeof mcpCustomHarnessesLogic.build>> => {
+ const logic = mcpCustomHarnessesLogic()
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadCustomHarnessesSuccess'])
+ return logic
+ }
+
+ it('loads the project harnesses on mount', async () => {
+ const logic = await mounted()
+ expect(logic.values.customHarnesses).toEqual(stored)
+ })
+
+ it.each([
+ [{ name: '', match_pattern: 'acme' }, { name: 'Enter a name' }],
+ [{ name: ' ', match_pattern: 'acme' }, { name: 'Enter a name' }],
+ [{ name: 'Acme', match_pattern: '' }, { match_pattern: 'Enter the text to match' }],
+ ])('does not send %j and shows %j', async (values, errors) => {
+ const logic = await mounted()
+ logic.actions.startAddingHarness()
+ logic.actions.setCustomHarnessFormValues(values)
+ logic.actions.submitCustomHarnessForm()
+ await expectLogic(logic).toDispatchActions(['submitCustomHarnessFormFailure'])
+ expect(logic.values.customHarnessFormValidationErrors).toMatchObject(errors)
+ expect(posted).toEqual([])
+ })
+
+ it('saves a trimmed name, closes the form and shows the new harness', async () => {
+ const logic = await mounted()
+ logic.actions.startAddingHarness()
+ logic.actions.setCustomHarnessFormValues({ name: ' Beta bot ', match_pattern: 'beta-bot' })
+ logic.actions.submitCustomHarnessForm()
+ await expectLogic(logic).toDispatchActions(['submitCustomHarnessFormSuccess', 'loadCustomHarnessesSuccess'])
+
+ expect(posted).toEqual([{ name: 'Beta bot', match_pattern: 'beta-bot' }])
+ expect(logic.values.editingHarnessId).toBeNull()
+ expect(logic.values.customHarnesses?.map((h) => h.name)).toEqual(['Acme agent', 'Beta bot'])
+ })
+
+ it('edits a harness in place with the values it already has', async () => {
+ const logic = await mounted()
+ logic.actions.startEditingHarness(stored[0])
+ expect(logic.values.editingHarnessId).toBe('a')
+ expect(logic.values.customHarnessForm).toEqual({ name: 'Acme agent', match_pattern: 'acme-agent' })
+
+ logic.actions.setCustomHarnessFormValues({ name: 'Acme assistant' })
+ logic.actions.submitCustomHarnessForm()
+ await expectLogic(logic).toDispatchActions(['submitCustomHarnessFormSuccess', 'loadCustomHarnessesSuccess'])
+
+ expect(posted).toEqual([])
+ expect(patched).toEqual([{ id: 'a', body: { name: 'Acme assistant', match_pattern: 'acme-agent' } }])
+ expect(logic.values.editingHarnessId).toBeNull()
+ expect(logic.values.customHarnesses?.map((h) => h.name)).toEqual(['Acme assistant'])
+ })
+
+ it('cancelling an edit clears the form so the next add starts empty', async () => {
+ const logic = await mounted()
+ logic.actions.startEditingHarness(stored[0])
+ logic.actions.cancelEditingHarness()
+ logic.actions.startAddingHarness()
+
+ expect(logic.values.editingHarnessId).toBe('new')
+ expect(logic.values.customHarnessForm).toEqual({ name: '', match_pattern: '' })
+ })
+
+ it('keeps what the person typed and shows the server reason when a save fails', async () => {
+ rejectCreate = true
+ const logic = await mounted()
+ logic.actions.startAddingHarness()
+ logic.actions.setCustomHarnessFormValues({ name: 'Acme copy', match_pattern: 'acme-agent' })
+ logic.actions.submitCustomHarnessForm()
+ await expectLogic(logic).toDispatchActions(['submitCustomHarnessFormFailure'])
+
+ expect(logic.values.editingHarnessId).toBe('new')
+ expect(logic.values.customHarnessForm).toEqual({ name: 'Acme copy', match_pattern: 'acme-agent' })
+ expect(lemonToast.error).toHaveBeenCalledWith('A custom harness already uses this pattern.')
+ })
+
+ it('marks a harness as deleting until the request ends, then reloads the list', async () => {
+ const logic = await mounted()
+ logic.actions.deleteCustomHarness('a')
+ expect(logic.values.deletingHarnessId).toBe('a')
+
+ releaseDelete()
+ await expectLogic(logic).toDispatchActions(['deleteCustomHarnessFinished', 'loadCustomHarnessesSuccess'])
+
+ expect(deleted).toEqual(['a'])
+ expect(logic.values.deletingHarnessId).toBeNull()
+ expect(logic.values.customHarnesses).toEqual([])
+ })
+})
diff --git a/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.ts b/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.ts
new file mode 100644
index 000000000000..99c4eb2c7d2a
--- /dev/null
+++ b/products/mcp_analytics/frontend/settings/mcpCustomHarnessesLogic.ts
@@ -0,0 +1,230 @@
+import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
+import { forms } from 'kea-forms'
+import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
+import { loaders } from 'kea-loaders'
+import posthog from 'posthog-js'
+
+import { lemonToast } from '@posthog/lemon-ui'
+
+import { ApiConfig } from 'lib/api'
+
+import {
+ mcpAnalyticsCustomHarnessesCreate,
+ mcpAnalyticsCustomHarnessesDestroy,
+ mcpAnalyticsCustomHarnessesList,
+ mcpAnalyticsCustomHarnessesPartialUpdate,
+} from '../generated/api'
+import type { MCPCustomHarnessApi } from '../generated/api.schemas'
+
+const projectId = (): string => String(ApiConfig.getCurrentProjectId())
+
+export interface CustomHarnessFormValues {
+ name: string
+ match_pattern: string
+}
+
+const EMPTY_FORM: CustomHarnessFormValues = { name: '', match_pattern: '' }
+
+/** The id of the harness being edited, `'new'` while adding one, or null when no form is open. */
+export type EditingHarnessId = string | 'new' | null
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface mcpCustomHarnessesLogicValues {
+ customHarnessForm: CustomHarnessFormValues
+ customHarnessFormAllErrors: Record<string, any>
+ customHarnessFormChanged: boolean
+ customHarnessFormErrors: DeepPartialMap<CustomHarnessFormValues, ValidationErrorType>
+ customHarnessFormHasErrors: boolean
+ customHarnessFormManualErrors: Record<string, any>
+ customHarnessFormTouched: boolean
+ customHarnessFormTouches: Record<string, boolean>
+ customHarnessFormValidationErrors: DeepPartialMap<CustomHarnessFormValues, ValidationErrorType>
+ customHarnesses: MCPCustomHarnessApi[] | null
+ customHarnessesLoading: boolean
+ deletingHarnessId: string | null
+ editingHarnessId: EditingHarnessId
+ isCustomHarnessFormSubmitting: boolean
+ isCustomHarnessFormValid: boolean
+ saveInProgressReason: string | null
+ showCustomHarnessFormErrors: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface mcpCustomHarnessesLogicActions {
+ cancelEditingHarness: () => {
+ value: true
+ }
+ deleteCustomHarness: (id: string) => {
+ id: string
+ }
+ deleteCustomHarnessFinished: () => {
+ value: true
+ }
+ loadCustomHarnesses: () => any
+ loadCustomHarnessesFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadCustomHarnessesSuccess: (
+ customHarnesses: MCPCustomHarnessApi[],
+ payload?: any
+ ) => {
+ customHarnesses: MCPCustomHarnessApi[]
+ payload?: any
+ }
+ resetCustomHarnessForm: (values?: CustomHarnessFormValues) => {
+ values?: CustomHarnessFormValues
+ }
+ setCustomHarnessFormManualErrors: (errors: Record<string, any>) => {
+ errors: Record<string, any>
+ }
+ setCustomHarnessFormValue: (
+ key: FieldName,
+ value: any
+ ) => {
+ name: FieldName
+ value: any
+ }
+ setCustomHarnessFormValues: (values: DeepPartial<CustomHarnessFormValues>) => {
+ values: DeepPartial<CustomHarnessFormValues>
+ }
+ startAddingHarness: () => {
+ value: true
+ }
+ startEditingHarness: (harness: MCPCustomHarnessApi) => {
+ harness: MCPCustomHarnessApi
+ }
+ submitCustomHarnessForm: () => {
+ value: boolean
+ }
+ submitCustomHarnessFormFailure: (
+ error: Error,
+ errors: Record<string, any>
+ ) => {
+ error: Error
+ errors: Record<string, any>
+ }
+ submitCustomHarnessFormRequest: (customHarnessForm: CustomHarnessFormValues) => {
+ customHarnessForm: CustomHarnessFormValues
+ }
+ submitCustomHarnessFormSuccess: (customHarnessForm: CustomHarnessFormValues) => {
+ customHarnessForm: CustomHarnessFormValues
+ }
+ touchCustomHarnessFormField: (key: string) => {
+ key: string
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface mcpCustomHarnessesLogicMeta {
+ __keaTypeGenInternalSelectorTypes: {
+ saveInProgressReason: (isCustomHarnessFormSubmitting: boolean) => string | null
+ }
+}
+
+export type mcpCustomHarnessesLogicType = MakeLogicType<
+ mcpCustomHarnessesLogicValues,
+ mcpCustomHarnessesLogicActions,
+ Record<string, any>,
+ mcpCustomHarnessesLogicMeta
+>
+
+export const mcpCustomHarnessesLogic = kea<mcpCustomHarnessesLogicType>([
+ path(['products', 'mcp_analytics', 'frontend', 'settings', 'mcpCustomHarnessesLogic']),
+ actions({
+ startAddingHarness: true,
+ startEditingHarness: (harness: MCPCustomHarnessApi) => ({ harness }),
+ cancelEditingHarness: true,
+ deleteCustomHarness: (id: string) => ({ id }),
+ deleteCustomHarnessFinished: true,
+ }),
+ reducers({
+ editingHarnessId: [
+ null as EditingHarnessId,
+ {
+ startAddingHarness: () => 'new',
+ startEditingHarness: (_, { harness }) => harness.id,
+ cancelEditingHarness: () => null,
+ submitCustomHarnessFormSuccess: () => null,
+ },
+ ],
+ deletingHarnessId: [
+ null as string | null,
+ {
+ deleteCustomHarness: (_, { id }) => id,
+ deleteCustomHarnessFinished: () => null,
+ },
+ ],
+ }),
+ loaders({
+ customHarnesses: [
+ null as MCPCustomHarnessApi[] | null,
+ {
+ loadCustomHarnesses: async () => await mcpAnalyticsCustomHarnessesList(projectId()),
+ },
+ ],
+ }),
+ forms(({ actions, values }) => ({
+ customHarnessForm: {
+ defaults: EMPTY_FORM,
+ errors: ({ name, match_pattern }) => ({
+ name: name.trim() ? undefined : 'Enter a name',
+ match_pattern: match_pattern.trim() ? undefined : 'Enter the text to match',
+ }),
+ submit: async ({ name, match_pattern }) => {
+ const harness = { name: name.trim(), match_pattern }
+ const editingId = values.editingHarnessId
+ try {
+ if (editingId && editingId !== 'new') {
+ await mcpAnalyticsCustomHarnessesPartialUpdate(projectId(), editingId, harness)
+ } else {
+ await mcpAnalyticsCustomHarnessesCreate(projectId(), harness)
+ }
+ } catch (error) {
+ lemonToast.error(
+ (error as { detail?: string })?.detail ||
+ 'Could not save the custom harness. Check the name and text, then try again.'
+ )
+ throw error
+ }
+ posthog.capture(editingId === 'new' ? 'mcp custom harness created' : 'mcp custom harness updated')
+ actions.loadCustomHarnesses()
+ },
+ },
+ })),
+ selectors({
+ saveInProgressReason: [
+ (s) => [s.isCustomHarnessFormSubmitting],
+ (isSubmitting: boolean): string | null => (isSubmitting ? 'Wait for the save to finish' : null),
+ ],
+ }),
+ listeners(({ actions }) => ({
+ startAddingHarness: () => {
+ actions.resetCustomHarnessForm(EMPTY_FORM)
+ },
+ startEditingHarness: ({ harness }) => {
+ actions.resetCustomHarnessForm({ name: harness.name, match_pattern: harness.match_pattern })
+ },
+ cancelEditingHarness: () => {
+ actions.resetCustomHarnessForm(EMPTY_FORM)
+ },
+ deleteCustomHarness: async ({ id }) => {
+ try {
+ await mcpAnalyticsCustomHarnessesDestroy(projectId(), id)
+ } catch {
+ lemonToast.error('Could not delete the custom harness. Try again.')
+ return
+ } finally {
+ actions.deleteCustomHarnessFinished()
+ }
+ posthog.capture('mcp custom harness deleted')
+ actions.loadCustomHarnesses()
+ },
+ })),
+ afterMount(({ actions }) => {
+ actions.loadCustomHarnesses()
+ }),
+])
diff --git a/products/mcp_analytics/package.json b/products/mcp_analytics/package.json
index adb5bd6a19c1..d3f17a7af36c 100644
--- a/products/mcp_analytics/package.json
+++ b/products/mcp_analytics/package.json
@@ -10,6 +10,7 @@
"@posthog/quill-components": "workspace:*",
"@posthog/quill-primitives": "workspace:*",
"kea": "catalog:",
+ "kea-forms": "catalog:",
"kea-loaders": "catalog:",
"kea-router": "catalog:",
"kea-subscriptions": "catalog:",
@@ -17,6 +18,7 @@
"react-intersection-observer": "9.5.3"
},
"devDependencies": {
+ "@testing-library/react": "^14.3.1",
"kea-test-utils": "catalog:"
},
"peerDependencies": {
@@ -24,6 +26,7 @@
"@posthog/icons": "catalog:",
"@storybook/react": "catalog:",
"@testing-library/dom": "*",
+ "@testing-library/user-event": "*",
"@types/react": "*",
"react": "*"
}
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index afe31ebdc17c..87fea7e563ba 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -63876,6 +63876,23 @@ export namespace Schemas {
readonly count: number;
}
+ export interface MCPCustomHarness {
+ readonly id: string;
+ /**
+ * Name to show for calls from this harness.
+ * @maxLength 100
+ * @pattern ^[\w .+&()/-]+$
+ */
+ name: string;
+ /**
+ * Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent/1.2`.
+ * @maxLength 100
+ */
+ match_pattern: string;
+ readonly created_at: string;
+ readonly updated_at: string;
+ }
+
/**
* Score breakdown so an agent can explain its choice: fit, liveness, trust, and whether real usage signal contributed.
*/
@@ -84540,6 +84557,23 @@ export namespace Schemas {
readonly updated_at?: string | null;
}
+ export interface PatchedMCPCustomHarness {
+ readonly id?: string;
+ /**
+ * Name to show for calls from this harness.
+ * @maxLength 100
+ * @pattern ^[\w .+&()/-]+$
+ */
+ name?: string;
+ /**
+ * Text to look for in the client name or User-Agent product that the harness reports. Matching ignores case and finds the text anywhere in the name, so `acme-agent` matches `Acme-Agent/1.2`.
+ * @maxLength 100
+ */
+ match_pattern?: string;
+ readonly created_at?: string;
+ readonly updated_at?: string;
+ }
+
export interface PatchedMCPGatewayServerUpdate {
/**
* Display name shown across the gateway.