Repository navigation
fix: prevent MCP guideline tools from returning homepage content - #8784
Conversation
🦋 Changeset detectedLatest commit: 1ec9763 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the fix preventing redirected homepage content from being presented as component guidance.
No open issues 🎉
What changed in this PR
Prevents Primer’s MCP guideline tools from presenting homepage content as component guidance when a requested page redirects elsewhere, addressing #8782.
Changes:
- Detect redirects away from the requested page in both guideline tools and return a no-guidelines response.
- Correct the usage-guidelines missing-page message and add redirect regression tests.
- Include a patch changeset for
@primer/mcp.
| File | Description |
|---|---|
packages/mcp/src/server.ts |
Adds shared redirect detection and corrects the usage-guidelines response. |
packages/mcp/src/server.test.ts |
Adds homepage-redirect regression coverage for both guideline tools. |
.changeset/mcp-guideline-redirects.md |
Records the fix for an @primer/mcp patch release. |
Review effort: Balanced
Ultroncn
left a comment
There was a problem hiding this comment.
Thanks for fixing this — the redirect check reads well, and I verified the new test really exercises it (new Response(body) defaults to ok: true, so the test would fail without the fix).
Two small notes:
-
The test only pins the redirect-to-homepage case. Since the helper's contract is "any different page", I left a suggestion below to also cover a redirect to a different non-homepage URL (the component page itself), so a future change that only special-cased
/couldn't silently pass. -
Not part of this PR, but the same pattern exists elsewhere in
server.ts:get_component_examples(/product/components/<slug>),get_pattern(/product/<scenario-patterns|ui-patterns>/<id>) andget_icon(/octicons/icon/<name>-<size>) fetch user-controlled primer.style URLs and only checkresponse.okbefore returning<main>content. If primer.style ever redirects one of those to the homepage with a 200, they'd return homepage content as docs. Might be worth applying this helper there in a follow-up — leaving that to you.
| it.each([ | ||
| { | ||
| tool: 'get_component_usage_guidelines', | ||
| expectedPath: '/product/components/card/guidelines', | ||
| expectedMessage: 'There are no usage guidelines for the `Card` component', | ||
| }, | ||
| { | ||
| tool: 'get_component_accessibility_guidelines', | ||
| expectedPath: '/product/components/card/accessibility', | ||
| expectedMessage: 'There are no accessibility guidelines for the `Card` component', | ||
| }, | ||
| ])('$tool does not return homepage content after a redirect', async ({tool, expectedPath, expectedMessage}) => { | ||
| const fetchMock = vi.mocked(fetch) | ||
| fetchMock.mockResolvedValue(responseWithFinalUrl('<main><h1>Primer homepage</h1></main>', 'https://primer.style/')) |
There was a problem hiding this comment.
Nice test — one small suggestion: the helper's contract is "any different page", but this only pins the redirect-to-homepage case. Adding a redirect to a different non-homepage URL (here, the component page itself) would also guard against a future change that only special-cased /:
| it.each([ | |
| { | |
| tool: 'get_component_usage_guidelines', | |
| expectedPath: '/product/components/card/guidelines', | |
| expectedMessage: 'There are no usage guidelines for the `Card` component', | |
| }, | |
| { | |
| tool: 'get_component_accessibility_guidelines', | |
| expectedPath: '/product/components/card/accessibility', | |
| expectedMessage: 'There are no accessibility guidelines for the `Card` component', | |
| }, | |
| ])('$tool does not return homepage content after a redirect', async ({tool, expectedPath, expectedMessage}) => { | |
| const fetchMock = vi.mocked(fetch) | |
| fetchMock.mockResolvedValue(responseWithFinalUrl('<main><h1>Primer homepage</h1></main>', 'https://primer.style/')) | |
| it.each([ | |
| { | |
| tool: 'get_component_usage_guidelines', | |
| expectedPath: '/product/components/card/guidelines', | |
| expectedMessage: 'There are no usage guidelines for the `Card` component', | |
| finalUrl: 'https://primer.style/', | |
| }, | |
| { | |
| tool: 'get_component_accessibility_guidelines', | |
| expectedPath: '/product/components/card/accessibility', | |
| expectedMessage: 'There are no accessibility guidelines for the `Card` component', | |
| finalUrl: 'https://primer.style/', | |
| }, | |
| { | |
| tool: 'get_component_accessibility_guidelines', | |
| expectedPath: '/product/components/card/accessibility', | |
| expectedMessage: 'There are no accessibility guidelines for the `Card` component', | |
| finalUrl: 'https://primer.style/product/components/card', | |
| }, | |
| ])('$tool does not return content from a different page after a redirect', async ({tool, expectedPath, expectedMessage, finalUrl}) => { | |
| const fetchMock = vi.mocked(fetch) | |
| fetchMock.mockResolvedValue(responseWithFinalUrl('<main><h1>Primer homepage</h1></main>', finalUrl)) |
When a component has no guidelines page, Primer’s redirect to the homepage can make the MCP tools present homepage content as component guidance. The tools now detect redirects away from the requested page and return a clear no-guidelines response.
Changelog
New
Changed
Removed
Rollout strategy
Testing & Reviewing
Review the redirect behavior for
Cardin both guideline tools and confirm components with published guidance still return it.