Skip to content

Consolidate MCP metrics helpers and add regression tests#375

Merged
calvadev merged 1 commit intoshopstr-eng:mainfrom
YASH-ai-bit:fix/mcp-metrics
Apr 9, 2026
Merged

Consolidate MCP metrics helpers and add regression tests#375
calvadev merged 1 commit intoshopstr-eng:mainfrom
YASH-ai-bit:fix/mcp-metrics

Conversation

@YASH-ai-bit
Copy link
Copy Markdown
Contributor

@YASH-ai-bit YASH-ai-bit commented Apr 9, 2026

Summary

This PR consolidates the duplicate MCP metrics helpers by keeping utils/mcp/metrics.ts as the canonical implementation, turning utils/mcp/metric.ts into a compatibility re-export, and adding direct regression coverage for the metrics behavior.

What changed

  • kept utils/mcp/metrics.ts as the live MCP metrics implementation

  • updated utils/mcp/metrics.ts to track bounded request records in addition to the existing cumulative counters

  • preserved the current metrics fields and behavior already used by the MCP API routes

  • added compatible observability fields to getMetrics():

    • uptime.startedAt
    • uptime.durationSeconds
    • uptime.durationHuman
    • latency.unit
    • latency.sampleSize
    • latency.window
    • throughput.requestsPerMinute
    • throughput.recentRequests
  • replaced utils/mcp/metric.ts with a thin re-export of utils/mcp/metrics.ts so both paths share one state source

  • added utils/mcp/__tests__/metrics.test.ts

Why

There were two MCP metrics helper modules with overlapping responsibilities:

  • utils/mcp/metrics.ts
  • utils/mcp/metric.ts

The live MCP routes already import utils/mcp/metrics.ts, so this PR keeps that file as the canonical implementation and removes the risk of the two modules drifting or maintaining separate state.

The new tests add direct coverage for:

  • cumulative request counting
  • tool usage tracking
  • onboarding rate limiting
  • recent-window metrics behavior
  • compatibility through the metric.ts re-export

Testing

Ran:

  • npm test -- utils/mcp/__tests__/metrics.test.ts utils/mcp/__tests__/auth.test.ts utils/mcp/__tests__/request-proof-server.test.ts

Manual MCP smoke checks:

  • GET /api/mcp/status

  • POST /api/mcp/onboard

  • POST /api/mcp initialize without the required Accept header returns the expected 406

  • POST /api/mcp initialize with the required headers succeeds and returns a session id

Notes

  • This PR is intended to preserve existing MCP behavior. The current metrics.ts fields remain in place, and the extra metrics fields are additive only.

  • Before:

CREATE TABLE IF NOT EXISTS mcp_request_proofs (
  event_id TEXT PRIMARY KEY,
  pubkey TEXT NOT NULL,
  action TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (event_id)
)

This SQL syntax was throwing error in table creation while I was testing mcp/api, this issue has also been mentioned in #354.

  • After:
CREATE TABLE IF NOT EXISTS mcp_request_proofs (
  event_id TEXT NOT NULL,
  pubkey TEXT NOT NULL,
  action TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (event_id)
);

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 9, 2026

@YASH-ai-bit is attempting to deploy a commit to the shopstr-eng Team on Vercel.

A member of the Team first needs to authorize it.

@calvadev calvadev merged commit 0363761 into shopstr-eng:main Apr 9, 2026
1 check failed
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.

2 participants