Skip to content

feat(mcp): expose get_ai_traffic tool and REST endpoint - #148

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
Pallavikumarimdb:feature/mcp-ai-traffic
Jun 3, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
Pallavikumarimdb:feature/mcp-ai-traffic

Conversation

@Pallavikumarimdb

Copy link
Copy Markdown
Contributor

Overview

Exposes AI-referral traffic analytics to MCP-compatible AI assistants and external clients. Implements a new MCP tool get_ai_traffic alongside a matching REST endpoint, allowing clients to query real visits arriving from AI engines (like ChatGPT, Claude, and Perplexity) with segmented platform, landing page, and geographic breakdowns.

** Relaxed the UUID Validator**

MCP tools only need identifier matching and ownership checks. Strict RFC-4122 version/variant validation provides little practical value here because IDs are never generated by users; they are retrieved from the database via list_brands, list_topics, and similar tools.

Relaxing validation prevents false negatives for existing UUID-shaped identifiers - such as the seeded local development brand 33333333-3333-3333-3333-333333333333 - while still rejecting malformed input. To do this, replaced .uuid() with a relaxed UUID regex validator (relaxedUuid) across all tool schemas. This verifies the hyphenated hexadecimal structure (8-4-4-4-12) without strict RFC 4122 rules.

Key Changes

  • web/src/lib/mcp/data.ts: Added getAiTrafficFor data fetcher and aggregator with tenant-ownership checks.
  • web/src/lib/mcp/server.ts: Registered the get_ai_traffic tool and refactored schemas to utilize the relaxed UUID validator.
  • web/src/app/api/mcp/ai-traffic/route.ts: Created the matching REST API GET endpoint.

Closes: #99

@github-actions github-actions 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.

Welcome to Ansvisor, and thank you for your first PR! A maintainer will be by soon to review.

In the meantime, two things that make review faster:

  • A short description of why the change matters, not just what it does — most of our PRs link a Fixes #... to the issue that explains the why.
  • A quick local test note if there's anything worth eyeballing in the browser.

If you'd like to keep an eye on where Ansvisor goes next, a ⭐ on the repo helps others discover it (and Watch sends you a ping when related issues land). Either way — looking forward to your next one.

@gkhngyk

gkhngyk commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR, @Pallavikumarimdb — strong shape, ownership check + date expansion are exactly the patterns the existing tools use, and the rationale for relaxedUuid (seeded local-dev brand 33333333-… failing strict RFC 4122) is fair.

CI's web — format, lint & typecheck is failing only on a Prettier format check — no lint or typecheck errors. Two files need their formatting auto-fixed:

  • web/src/lib/mcp/data.ts
  • web/src/lib/mcp/server.ts

Quick fix:

cd web
yarn format
git add -u
git commit -m "chore: apply prettier formatting"
git push

Once that's pushed, CI re-runs (it'll need a maintainer to click Approve and run again on the first attempt — fork PR convention). After it's green I'll approve and merge.

One small nit you can roll into the same push if you'd like — purely a consistency thing, not blocking:

In getAiTrafficFor, the AiTrafficParams uses from / to, but the other data functions in the same file use dateFrom / dateTo. Renaming for consistency keeps grep / IDE refactor tools happy. Totally optional.

@Pallavikumarimdb

Copy link
Copy Markdown
Contributor Author

Thanks for the PR, @Pallavikumarimdb — strong shape, ownership check + date expansion are exactly the patterns the existing tools use, and the rationale for relaxedUuid (seeded local-dev brand 33333333-… failing strict RFC 4122) is fair.

CI's web — format, lint & typecheck is failing only on a Prettier format check — no lint or typecheck errors. Two files need their formatting auto-fixed:

* `web/src/lib/mcp/data.ts`

* `web/src/lib/mcp/server.ts`

Quick fix:

cd web
yarn format
git add -u
git commit -m "chore: apply prettier formatting"
git push

Once that's pushed, CI re-runs (it'll need a maintainer to click Approve and run again on the first attempt — fork PR convention). After it's green I'll approve and merge.

One small nit you can roll into the same push if you'd like — purely a consistency thing, not blocking:

In getAiTrafficFor, the AiTrafficParams uses from / to, but the other data functions in the same file use dateFrom / dateTo. Renaming for consistency keeps grep / IDE refactor tools happy. Totally optional.

I have made the required changes and run the yarn format. Let me know if there are any further changes or improvements you'd like me to make.

@gkhngyk

gkhngyk commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Thanks @Pallavikumarimdb — the rename + most of the format fixes landed, but CI still flags web/src/lib/mcp/data.ts. The remaining diff is tiny — two identical blocks (lines 724–729 and 1158–1163) where the chained .split().map().filter() inside a ternary needs an extra indent level. Prettier 3.8.x wants the chain to align deeper than the : null branch so the structure stays readable.

Current:

const p_models = params.model
  ? params.model
    .split(',')
    .map((s) => s.trim())
    .filter(Boolean)
  : null;

Expected:

const p_models = params.model
  ? params.model
      .split(',')
      .map((s) => s.trim())
      .filter(Boolean)
  : null;

(Both occurrences need the same fix.)

If yarn format from inside web/ isn't producing this, the most likely cause is a Prettier version mismatch — the repo pins prettier@^3.8.3. A quick `cd web && yarn install && yarn format` from a clean install should resolve it. If you'd rather just patch the two indents by hand that's also fine; CI re-runs on push.

@Pallavikumarimdb

Copy link
Copy Markdown
Contributor Author

Thanks @Pallavikumarimdb — the rename + most of the format fixes landed, but CI still flags web/src/lib/mcp/data.ts. The remaining diff is tiny — two identical blocks (lines 724–729 and 1158–1163) where the chained .split().map().filter() inside a ternary needs an extra indent level. Prettier 3.8.x wants the chain to align deeper than the : null branch so the structure stays readable.

Current:

const p_models = params.model
  ? params.model
    .split(',')
    .map((s) => s.trim())
    .filter(Boolean)
  : null;

Expected:

const p_models = params.model
  ? params.model
      .split(',')
      .map((s) => s.trim())
      .filter(Boolean)
  : null;

(Both occurrences need the same fix.)

If yarn format from inside web/ isn't producing this, the most likely cause is a Prettier version mismatch — the repo pins prettier@^3.8.3. A quick cd web && yarn install && yarn format from a clean install should resolve it. If you'd rather just patch the two indents by hand that's also fine; CI re-runs on push.

Done 👍

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Format check is finally green and the changes look good — relaxed UUID validator, ownership check + date-expansion patterns match the existing tools, and the new REST endpoint mirrors the MCP tool registration cleanly. Thanks for sticking with the prettier round-trip @Pallavikumarimdb.

@gkhngyk
gkhngyk merged commit 4b7e607 into ansvisor:main Jun 3, 2026
1 check passed
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.

feat(mcp): expose get_ai_traffic tool (AI-referral analytics) + REST endpoint

2 participants