Skip to content

Commit 0be578a

Browse files
committed
convert to absolute urls if needed
1 parent 639edf0 commit 0be578a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/mcp/src/docs-search/doc-fetch.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,42 @@ describe('DocFetchTool', () => {
120120
{ in: 'https://gradio.app/guides/x', out: 'https://www.gradio.app/guides/x' },
121121
{ in: 'https://www.gradio.app/guides/x', out: 'https://www.gradio.app/guides/x' },
122122
{ in: 'https://huggingface.co/docs/transformers', out: 'https://huggingface.co/docs/transformers' },
123+
{ in: '/docs/diffusers/index', out: 'https://huggingface.co/docs/diffusers/index' },
124+
{ in: './docs/diffusers/index', out: 'https://huggingface.co/docs/diffusers/index' },
123125
{ in: 'not a url', out: 'not a url' },
124126
];
125127
for (const c of cases) {
126128
expect(normalizeDocUrl(c.in)).toBe(c.out);
127129
}
128130
});
129131

132+
it('normalizes relative doc paths to the huggingface docs host', async () => {
133+
const fetchMock = stubFetch(() =>
134+
createMockResponse({
135+
content: '<h1>Title</h1><p>Body</p>',
136+
}),
137+
);
138+
139+
const result = await tool.fetch({ doc_url: '/docs/test' });
140+
expect(fetchMock).toHaveBeenCalledWith('https://huggingface.co/docs/test', {
141+
headers: { accept: 'text/markdown' },
142+
});
143+
expect(result).toContain('# Title');
144+
});
145+
146+
it('normalizes ./docs paths to the huggingface docs host', async () => {
147+
const fetchMock = stubFetch(() =>
148+
createMockResponse({
149+
content: '<h1>Another Title</h1><p>Body</p>',
150+
}),
151+
);
152+
153+
await tool.fetch({ doc_url: './docs/another' });
154+
expect(fetchMock).toHaveBeenCalledWith('https://huggingface.co/docs/another', {
155+
headers: { accept: 'text/markdown' },
156+
});
157+
});
158+
130159
it('should return subsequent chunks with offset', async () => {
131160
// Mock fetch to return the same long HTML
132161
const longHtml = '<h1>Long Document</h1>' + '<p>This is a very long sentence that will be repeated many times to create a document that exceeds the 7500 token limit for testing chunking functionality.</p>'.repeat(200);

packages/mcp/src/docs-search/doc-fetch.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,21 @@ export class DocFetchTool {
222222
*/
223223
export function normalizeDocUrl(input: string): string {
224224
try {
225-
const url = new URL(input);
225+
const trimmed = input.trim();
226+
if (trimmed.startsWith('/docs')) {
227+
return `https://huggingface.co${trimmed}`;
228+
}
229+
if (trimmed.startsWith('./docs')) {
230+
return `https://huggingface.co/${trimmed.slice(2)}`;
231+
}
232+
233+
const url = new URL(trimmed);
226234
const host = url.hostname.toLowerCase();
227235
if (host === 'gradio.app') {
228236
url.hostname = 'www.gradio.app';
229237
return url.toString();
230238
}
231-
return input;
239+
return trimmed;
232240
} catch {
233241
return input;
234242
}

0 commit comments

Comments
 (0)