Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions .github/workflows/token-cost.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ on:

permissions:
contents: read
pull-requests: write
checks: write

jobs:
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions design/mcp-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions docs/operations/token-cost-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
24 changes: 6 additions & 18 deletions packages/mcp-core/scripts/measure-ui-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,16 +18,13 @@ 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;
mime_type: string;
raw_bytes: number;
gzip_bytes: number;
brotli_bytes: number;
budget_bytes: number;
budget_percentage: number;
}

function parseArgs() {
Expand Down Expand Up @@ -85,10 +83,6 @@ async function readViews(): Promise<ViewStats[]> {
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 =
Expand All @@ -103,8 +97,6 @@ async function readViews(): Promise<ViewStats[]> {
brotli_bytes: brotliCompressSync(body, {
params: { [constants.BROTLI_PARAM_QUALITY]: 11 },
}).length,
budget_bytes: budget,
budget_percentage: Number(((body.length / budget) * 100).toFixed(1)),
});
}
}
Expand All @@ -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`;
}
Expand All @@ -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)}`,
);
Expand Down
Loading