Unverified Commit a29b4fa5 authored by Jude Gao's avatar Jude Gao Committed by GitHub

[MCP] Fix: Do not dedup by URL for MCP get_page_metadata (#84564)

The `get_page_metadata` MCP tool was incorrectly grouping browser
sessions by URL instead of counting each session individually. When
multiple browser tabs were open to the same URL, the tool would report
only 1 session instead of the actual number of sessions.
parent c0cd530a
...@@ -59,16 +59,17 @@ export function registerGetPageMetadataTool( ...@@ -59,16 +59,17 @@ export function registerGetPageMetadataTool(
} }
} }
const metadataByUrl = new Map<string, PageMetadata>() const sessionMetadata: Array<{ url: string; metadata: PageMetadata }> =
[]
for (const response of responses) { for (const response of responses) {
if (response.data) { if (response.data) {
// TODO: Add other metadata for the current page render here. Currently, we only have segment trie data. // TODO: Add other metadata for the current page render here. Currently, we only have segment trie data.
const pageMetadata = convertSegmentTrieToPageMetadata(response.data) const pageMetadata = convertSegmentTrieToPageMetadata(response.data)
metadataByUrl.set(response.url, pageMetadata) sessionMetadata.push({ url: response.url, metadata: pageMetadata })
} }
} }
if (metadataByUrl.size === 0) { if (sessionMetadata.length === 0) {
return { return {
content: [ content: [
{ {
...@@ -79,7 +80,7 @@ export function registerGetPageMetadataTool( ...@@ -79,7 +80,7 @@ export function registerGetPageMetadataTool(
} }
} }
const output = formatPageMetadata(metadataByUrl) const output = formatPageMetadata(sessionMetadata)
return { return {
content: [ content: [
...@@ -145,10 +146,12 @@ function convertSegmentTrieToPageMetadata(data: SegmentTrieData): PageMetadata { ...@@ -145,10 +146,12 @@ function convertSegmentTrieToPageMetadata(data: SegmentTrieData): PageMetadata {
} }
} }
function formatPageMetadata(metadataByUrl: Map<string, PageMetadata>): string { function formatPageMetadata(
let output = `# Page metadata from ${metadataByUrl.size} browser session(s)\n\n` sessionMetadata: Array<{ url: string; metadata: PageMetadata }>
): string {
let output = `# Page metadata from ${sessionMetadata.length} browser session(s)\n\n`
for (const [url, metadata] of metadataByUrl) { for (const { url, metadata } of sessionMetadata) {
let displayUrl = url let displayUrl = url
try { try {
const urlObj = new URL(url) const urlObj = new URL(url)
......
...@@ -180,6 +180,33 @@ describe('mcp-server get_page_metadata tool', () => { ...@@ -180,6 +180,33 @@ describe('mcp-server get_page_metadata tool', () => {
await session2.close() await session2.close()
} }
}) })
it('should count multiple browser tabs with the same URL separately', async () => {
await new Promise((resolve) => setTimeout(resolve, 500))
const session1 = await launchStandaloneSession(next.url, '/')
const session2 = await launchStandaloneSession(next.url, '/')
try {
await new Promise((resolve) => setTimeout(resolve, 1000))
let metadata: string = ''
await retry(async () => {
const sessionId = 'test-same-url-' + Date.now()
metadata = await callGetPageMetadata(next.url, sessionId)
const rootSessions = (metadata.match(/## Session: \/(?!\w)/g) || [])
.length
expect(rootSessions).toBeGreaterThanOrEqual(2)
})
const rootSessions = (metadata.match(/## Session: \/(?!\w)/g) || [])
.length
expect(rootSessions).toBeGreaterThanOrEqual(2)
} finally {
await session1.close()
await session2.close()
}
})
}) })
describe('pages router', () => { describe('pages router', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment