import '../shared/base.css'; import { App } from '@modelcontextprotocol/ext-apps'; interface UsageData { period: string; startDate: string; endDate: string; totals: { requests: number; promptTokens: number; completionTokens: number; errors: number; estimatedCostUSD?: number; }; usage: Record< string, Record< string, { requests: number; promptTokens: number; completionTokens: number; errors: number; } > >; costByProvider?: Record; } const periodLabels: Record = { today: 'Today', '7d': 'Last 7 Days', '30d': 'Last 30 Days', all: 'All Time', }; const app = new App({ name: 'UsageStats', version: '1.0.0' }, {}); app.ontoolresult = (params) => { const container = document.getElementById('app')!; if (params.isError) { container.innerHTML = `
Tool execution failed
`; return; } const content = params.content; if (!content || !Array.isArray(content) || content.length < 2) { container.innerHTML = `
No structured data received
`; return; } try { const data: UsageData = JSON.parse((content[1] as { type: string; text: string }).text); render(data); } catch { container.innerHTML = `
Failed to parse usage data
`; } }; function render(data: UsageData) { const container = document.getElementById('app')!; const periodLabel = periodLabels[data.period] || data.period; const totalTokens = data.totals.promptTokens + data.totals.completionTokens; let html = `
`; html += `

Usage Statistics

`; html += `
${esc(periodLabel)}
`; html += `
${esc(data.startDate)} to ${esc(data.endDate)}
`; html += `
`; // Summary cards html += `
`; html += summaryCard('Requests', fmt(data.totals.requests), 'req'); html += summaryCard('Total Tokens', fmt(totalTokens), 'tok'); html += summaryCard('Errors', fmt(data.totals.errors), data.totals.errors > 0 ? 'err' : 'ok'); if (data.totals.estimatedCostUSD !== undefined) { html += summaryCard('Est. Cost', '$' + formatCost(data.totals.estimatedCostUSD), 'cost'); } html += `
`; // Provider breakdown const providers = Object.keys(data.usage); if (providers.length > 0) { // Token distribution bar const maxTokens = Math.max( ...providers.map((p) => { let t = 0; for (const m of Object.values(data.usage[p])) t += m.promptTokens + m.completionTokens; return t; }), 1 ); html += `

By Provider

`; for (const provider of providers) { const models = data.usage[provider]; let providerTokens = 0; let providerRequests = 0; for (const m of Object.values(models)) { providerTokens += m.promptTokens + m.completionTokens; providerRequests += m.requests; } const pct = (providerTokens / maxTokens) * 100; const cost = data.costByProvider?.[provider]; html += `
`; html += ``; html += `${esc(provider)}`; html += `${fmt(providerRequests)} req · ${fmt(providerTokens)} tokens`; if (cost !== undefined) html += ` · $${formatCost(cost)}`; html += ``; html += `
`; // Model detail table html += ``; html += ``; html += ``; for (const [model, stats] of Object.entries(models)) { html += ``; html += ``; html += ``; html += ``; html += ``; html += ``; html += ``; } html += `
ModelRequestsPromptCompletionErrors
${esc(model)}${fmt(stats.requests)}${fmt(stats.promptTokens)}${fmt(stats.completionTokens)}${stats.errors > 0 ? `${fmt(stats.errors)}` : '0'}
`; } html += `
`; } else { html += `
No usage data for this period.
`; } container.innerHTML = html; } function summaryCard(label: string, value: string, kind: string) { return `
${value}
${label}
`; } function fmt(n: number): string { return n.toLocaleString(); } function formatCost(cost: number): string { if (cost < 0.01) return cost.toFixed(6); if (cost < 1) return cost.toFixed(4); return cost.toFixed(2); } function esc(s: string): string { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; } app.connect().catch(console.error);