Skip to content

fix(web): count canonical MCP sources in analytics - #1651

Open
brendan-kellam wants to merge 3 commits into
mainfrom
fix/mcp-analytics-source
Open

fix(web): count canonical MCP sources in analytics#1651
brendan-kellam wants to merge 3 commits into
mainfrom
fix/mcp-analytics-source

Conversation

@brendan-kellam

@brendan-kellam brendan-kellam commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • classify both the canonical sourcebot-mcp-server source and legacy mcp source as MCP analytics activity
  • standardize ask_codebase on the canonical MCP source
  • keep MCP activity in the combined non-web and global active-user metrics
  • add regression coverage for MCP and API source classification

Regression

MCP analytics was introduced in #950 on March 4, 2026 using the mcp source. The shared MCP tool refactor in #1014 on March 22, 2026 changed tool audit metadata to sourcebot-mcp-server without updating the analytics query, which still required an exact mcp match.

Testing

  • yarn workspace @sourcebot/web eslint src/ee/features/analytics/actions.ts src/ee/features/analytics/actions.test.ts src/ee/features/mcp/constants.ts src/ee/features/mcp/server.ts
  • yarn workspace @sourcebot/web test src/app/api/(server)/ee/mcp/route.test.ts src/features/tools/adapters.test.ts src/ee/features/analytics/actions.test.ts --run
  • yarn workspace @sourcebot/web tsc --noEmit (blocked by existing generated .next/types errors in unrelated settings pages)

Note

Low Risk
Analytics classification and MCP audit metadata alignment only; no auth or data-mutation paths. SQL behavior change is limited to metric bucketing for existing audit rows.

Overview
Fixes EE analytics undercounting MCP usage after audit events started using the canonical sourcebot-mcp-server label while the dashboard still only counted legacy mcp.

The analytics SQL now derives a source_category (mcp, api, or sourcebot) in the core CTE and uses it for MCP/API/non-web metrics instead of scattered metadata->>'source' checks. MCP activity includes both sourcebot-mcp-server and legacy mcp.

MCP server labeling is centralized via MCP_SERVER_SOURCE / LEGACY_MCP_SERVER_SOURCE in constants.ts; ask_codebase and tool telemetry now emit the canonical source consistently.

Adds a Vitest regression asserting the raw analytics query binds both source values and filters on source_category. Changelog notes the fix for 5.1.12.

Reviewed by Cursor Bugbot for commit e0f376a. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Fixes MCP analytics so both the canonical sourcebot-mcp-server and legacy mcp audit sources are classified as MCP activity, preventing MCP usage from being undercounted after the shared MCP tool refactor.

  • Standardizes ask_codebase to use the canonical MCP source.
  • Adds regression coverage for MCP and API source classification.

Written for commit e0f376a. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Fixed analytics so MCP activity using the canonical source label is included correctly.
    • Preserved analytics tracking for activity using the legacy MCP source label.
    • Improved classification of MCP, API, and Sourcebot activity for more accurate usage metrics and active-user reporting.
  • Consistency

    • Standardized MCP activity source labeling across supported MCP interactions for more reliable analytics.

@github-actions

This comment has been minimized.

@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 3720a67b-865b-46cd-ad09-3279855d70e2

📥 Commits

Reviewing files that changed from the base of the PR and between 5ced124 and e0f376a.

📒 Files selected for processing (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


Walkthrough

The change centralizes MCP source labels and updates analytics classification to include canonical and legacy MCP activity. Tests verify the generated SQL filters and category mapping.

Changes

MCP analytics source classification

Layer / File(s) Summary
Centralize MCP source identifiers
packages/web/src/ee/features/mcp/constants.ts, packages/web/src/ee/features/mcp/server.ts
MCP source labels use shared constants. MCP server metadata, event capture, and askCodebase calls use the canonical source constant.
Classify MCP sources in analytics
packages/web/src/ee/features/analytics/actions.ts, packages/web/src/ee/features/analytics/actions.test.ts, CHANGELOG.md
Analytics derives source_category and classifies canonical and legacy MCP sources as mcp. Active-user and request filters use the derived category. Tests verify the generated SQL, and the changelog records the fix.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to e0f37

The supplied change context does not identify an actionable merge risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: counting canonical MCP sources in web analytics.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mcp-analytics-source

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found and verified against the latest diff

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/web/src/ee/features/analytics/actions.test.ts">

<violation number="1" location="packages/web/src/ee/features/analytics/actions.test.ts:52">
P3: This test asserts on reconstructed SQL string fragments ("THEN 'mcp'", aliases like `c.source_category`) and plain parameter membership instead of seeding audit rows and asserting the computed classification. Any behavior-preserving refactor (renaming the `source_category` alias, restructuring the CASE, or reformatting) fails the test spuriously, and no assertion verifies that a `sourcebot-mcp-server` row is actually classified as `mcp`. Seed rows with the canonical and legacy sources and assert on the returned `rows` values so the regression test stays meaningful if the query changes.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic


expect(parameters).toContain('sourcebot-mcp-server');
expect(parameters).toContain('mcp');
expect(query).toContain("THEN 'mcp'");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This test asserts on reconstructed SQL string fragments ("THEN 'mcp'", aliases like c.source_category) and plain parameter membership instead of seeding audit rows and asserting the computed classification. Any behavior-preserving refactor (renaming the source_category alias, restructuring the CASE, or reformatting) fails the test spuriously, and no assertion verifies that a sourcebot-mcp-server row is actually classified as mcp. Seed rows with the canonical and legacy sources and assert on the returned rows values so the regression test stays meaningful if the query changes.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/web/src/ee/features/analytics/actions.test.ts, line 52:

<comment>This test asserts on reconstructed SQL string fragments ("THEN 'mcp'", aliases like `c.source_category`) and plain parameter membership instead of seeding audit rows and asserting the computed classification. Any behavior-preserving refactor (renaming the `source_category` alias, restructuring the CASE, or reformatting) fails the test spuriously, and no assertion verifies that a `sourcebot-mcp-server` row is actually classified as `mcp`. Seed rows with the canonical and legacy sources and assert on the returned `rows` values so the regression test stays meaningful if the query changes.</comment>

<file context>
@@ -0,0 +1,57 @@
+
+        expect(parameters).toContain('sourcebot-mcp-server');
+        expect(parameters).toContain('mcp');
+        expect(query).toContain("THEN 'mcp'");
+        expect(query).toContain("WHERE c.source_category IN ('mcp', 'api')");
+        expect(query).toContain("WHERE c.source_category = 'mcp'");
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant