diff --git a/.github/workflows/token-cost.yml b/.github/workflows/token-cost.yml index 711299dd3..2864ef1d5 100644 --- a/.github/workflows/token-cost.yml +++ b/.github/workflows/token-cost.yml @@ -45,7 +45,6 @@ on: permissions: contents: read - pull-requests: write checks: write jobs: @@ -260,46 +259,6 @@ jobs: details_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` }); - - name: Comment on PR - uses: actions/github-script@v7 - # Skip for fork PRs (no write permissions). Posts on every run, since - # a view can change while the tool token count stays the same. - if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository - continue-on-error: true # Don't fail workflow if comment creation fails - with: - script: | - const fs = require('fs'); - const report = fs.readFileSync('packages/mcp-core/token-report.md', 'utf8'); - - // Find existing comment - const comments = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - }); - - const botComment = comments.data.find(comment => - comment.user.type === 'Bot' && - comment.body.includes('📊 MCP Server Token Cost Report') - ); - - // Update or create comment - if (botComment) { - await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: botComment.id, - body: report - }); - } else { - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - body: report - }); - } - - name: Upload token stats artifact uses: actions/upload-artifact@v4 if: always() diff --git a/design/mcp-apps.md b/design/mcp-apps.md index 96d25668a..58d1d8191 100644 --- a/design/mcp-apps.md +++ b/design/mcp-apps.md @@ -39,8 +39,8 @@ client gets the same answer as text. - A view is one self-contained HTML document with inline scripts and styles. Its declared CSP lists no origins, and it loads nothing from the network: no remote scripts, styles, fonts, or images, and no requests. -- Each view has a byte budget, and the build fails when the view exceeds - it. The chart's budget is 24 KiB. +- Each view has a hard byte limit, and the build fails when the view + exceeds it. The chart's limit is 24 KiB. - A view's `ui://` URI carries a version, and any change to the view's HTML ships under a new version, because hosts cache views by URI. - A view treats the tool result and every host message as untrusted. It diff --git a/docs/operations/token-cost-tracking.md b/docs/operations/token-cost-tracking.md index 182140682..40bda75a2 100644 --- a/docs/operations/token-cost-tracking.md +++ b/docs/operations/token-cost-tracking.md @@ -75,7 +75,8 @@ GitHub Actions workflow runs on every PR and push to main: **On Pull Requests:** -- 📝 **PR Comment:** Automatic comment with full report (updated on each push) +- ✅ **Check Run:** `Token Cost` check with the total, tool count, average, and + the change from main - 📊 **Job Summary:** Detailed per-tool breakdown in Actions tab - 📦 **Artifact:** `token-stats-{sha}.json` stored for 90 days @@ -99,12 +100,14 @@ pnpm --filter @sentry/mcp-core measure-ui # Display pnpm --filter @sentry/mcp-core measure-ui -- -o ui-stats.json # Write JSON ``` -Per view it reports raw, gzip (level 9), and brotli (quality 11) bytes, and raw -bytes as a percentage of the view's `budgetBytes` in `src/ui-resources.ts`. -The report adds an "MCP Apps views" section to the PR comment and job summary, -and `ui-stats.json` goes into the `token-stats-{sha}` artifact. The budget is -enforced by the view's generator, such as `generate-chart-app`, not by this -report; see [design/mcp-apps.md](../../design/mcp-apps.md). +Per view it reports raw, gzip (level 9), and brotli (quality 11) bytes. The +report adds an "MCP Apps views" section to the job summary, and `ui-stats.json` +goes into the `token-stats-{sha}` artifact. + +Each view's raw bytes have a hard limit, `budgetBytes` in +`src/ui-resources.ts`. The view's generator, such as `generate-chart-app`, +fails over it, and `ci:policy` runs that check in CI. This report only shows +the sizes; see [design/mcp-apps.md](../../design/mcp-apps.md). ## Understanding the Results diff --git a/packages/mcp-core/scripts/measure-ui-views.ts b/packages/mcp-core/scripts/measure-ui-views.ts index d074464fb..e2ee5356d 100644 --- a/packages/mcp-core/scripts/measure-ui-views.ts +++ b/packages/mcp-core/scripts/measure-ui-views.ts @@ -3,7 +3,8 @@ * * Builds the server with every skill, lists its resources, and reads each * `ui://` resource through `resources/read`, as a host does before it renders - * a view. Reports raw, gzip, and brotli bytes against the view's budget. + * a view. Reports raw, gzip, and brotli bytes. The view's generator enforces + * its byte limit, not this report. * * Usage: * pnpm run measure-ui # Display table @@ -17,7 +18,6 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; import { buildServer } from "../src/server.ts"; import { ALL_SKILLS } from "../src/skills.ts"; -import { UI_RESOURCES } from "../src/ui-resources.ts"; interface ViewStats { uri: string; @@ -25,8 +25,6 @@ interface ViewStats { raw_bytes: number; gzip_bytes: number; brotli_bytes: number; - budget_bytes: number; - budget_percentage: number; } function parseArgs() { @@ -85,10 +83,6 @@ async function readViews(): Promise { const { resources } = await client.listResources(); const views: ViewStats[] = []; for (const { uri } of resources.filter((r) => r.uri.startsWith("ui://"))) { - const budget = UI_RESOURCES.find((r) => r.uri === uri)?.budgetBytes; - if (budget === undefined) { - throw new Error(`${uri} is not in UI_RESOURCES, so it has no budget`); - } const { contents } = await client.readResource({ uri }); for (const content of contents) { const body = @@ -103,8 +97,6 @@ async function readViews(): Promise { brotli_bytes: brotliCompressSync(body, { params: { [constants.BROTLI_PARAM_QUALITY]: 11 }, }).length, - budget_bytes: budget, - budget_percentage: Number(((body.length / budget) * 100).toFixed(1)), }); } } @@ -129,18 +121,14 @@ function formatMarkdown(views: ViewStats[]): string { lines.push("_The server lists no `ui://` resources._"); } else { lines.push( - "| View | MIME type | Raw | Gzip | Brotli | Budget | % of budget |", - "|------|-----------|-----|------|--------|--------|-------------|", + "| View | MIME type | Raw | Gzip | Brotli |", + "|------|-----------|-----|------|--------|", ); for (const view of views) { lines.push( - `| \`${view.uri}\` | \`${view.mime_type}\` | ${bytes(view.raw_bytes)} | ${bytes(view.gzip_bytes)} | ${bytes(view.brotli_bytes)} | ${bytes(view.budget_bytes)} | ${view.budget_percentage}% |`, + `| \`${view.uri}\` | \`${view.mime_type}\` | ${bytes(view.raw_bytes)} | ${bytes(view.gzip_bytes)} | ${bytes(view.brotli_bytes)} |`, ); } - lines.push( - "", - "The budget applies to raw bytes, and the view's generator fails over it.", - ); } return `${lines.join("\n")}\n`; } @@ -151,7 +139,7 @@ function formatTable(views: ViewStats[]): string { lines.push( view.uri, ` MIME type: ${view.mime_type}`, - ` Raw: ${bytes(view.raw_bytes)} (${view.budget_percentage}% of ${bytes(view.budget_bytes)})`, + ` Raw: ${bytes(view.raw_bytes)}`, ` Gzip: ${bytes(view.gzip_bytes)}`, ` Brotli: ${bytes(view.brotli_bytes)}`, );