Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mcp-guideline-redirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/mcp': patch
---

Guidelines: Avoid returning homepage content when a component has no guidelines page.
32 changes: 32 additions & 0 deletions packages/mcp/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,38 @@ describe('MCP server', () => {
expect(fetchMock.mock.calls[0]?.[0].toString()).toBe(expectedUrl)
})

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/'))
Comment on lines +535 to +548

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 /:

Suggested change
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))


const result = await client.callTool({name: tool, arguments: {name: 'Card'}})

expect(fetchMock).toHaveBeenCalledWith(new URL(expectedPath, 'https://primer.style'))
expect(result.content).toContainEqual(
expect.objectContaining({
type: 'text',
text: expect.stringContaining(expectedMessage),
}),
)
expect(result.content).not.toContainEqual(
expect.objectContaining({
type: 'text',
text: expect.stringContaining('Primer homepage'),
}),
)
})

it.each(['ConfirmationDialog', 'confirmationdialog'])(
'retrieves ConfirmationDialog documentation for %s from its published URL',
async name => {
Expand Down
29 changes: 24 additions & 5 deletions packages/mcp/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const turndownService = new TurndownService()
// Load all tokens with guidelines from primitives
const allTokensWithGuidelines: TokenWithGuidelines[] = loadAllTokensWithGuidelines()

function hasRedirectedToDifferentPage(response: Response, url: URL): boolean {
if (!response.url) {
return false
}

const finalUrl = new URL(response.url)
return finalUrl.origin !== url.origin || finalUrl.pathname.replace(/\/$/, '') !== url.pathname.replace(/\/$/, '')
}

function createServer() {
const server = new McpServer({
name: 'Primer',
Expand Down Expand Up @@ -316,13 +325,18 @@ ${text}`,

const url = new URL(`/product/components/${match.slug}/guidelines`, 'https://primer.style')
const response = await fetch(url)
if (!response.ok) {
if ((response.status >= 400 && response.status < 500) || (response.status >= 300 && response.status < 400)) {
const redirected = hasRedirectedToDifferentPage(response, url)
if (!response.ok || redirected) {
if (
redirected ||
(response.status >= 400 && response.status < 500) ||
(response.status >= 300 && response.status < 400)
) {
return {
content: [
{
type: 'text',
text: `There are no accessibility guidelines for the \`${name}\` component in the @primer/react package.`,
text: `There are no usage guidelines for the \`${name}\` component in the @primer/react package.`,
},
],
}
Expand Down Expand Up @@ -389,8 +403,13 @@ ${text}`,

const url = new URL(`/product/components/${match.slug}/accessibility`, 'https://primer.style')
const response = await fetch(url)
if (!response.ok) {
if ((response.status >= 400 && response.status < 500) || (response.status >= 300 && response.status < 400)) {
const redirected = hasRedirectedToDifferentPage(response, url)
if (!response.ok || redirected) {
if (
redirected ||
(response.status >= 400 && response.status < 500) ||
(response.status >= 300 && response.status < 400)
) {
return {
content: [
{
Expand Down
Loading