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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, test } from 'bun:test';
import { normalizeMcpUrl } from '../server-builder.ts';

describe('normalizeMcpUrl', () => {
test('removes trailing slashes from the URL pathname', () => {
expect(normalizeMcpUrl('https://api.example.com/mcp/')).toBe(
'https://api.example.com/mcp',
);
expect(normalizeMcpUrl('https://api.example.com/mcp///')).toBe(
'https://api.example.com/mcp',
);
});

test('preserves trailing slashes inside query parameters', () => {
expect(normalizeMcpUrl('https://api.example.com/mcp?key=/')).toBe(
'https://api.example.com/mcp?key=/',
);
expect(normalizeMcpUrl('https://api.example.com/mcp/?key=value/')).toBe(
'https://api.example.com/mcp?key=value/',
);
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] The tests cover ? and # independently but not together. Adding a combined case locks in the invariant that url.search(/[?#]/) correctly handles ? before # per RFC 3986 ordering, and prevents a regression if someone rewrites the split logic.

Suggested change
expect(normalizeMcpUrl('https://api.example.com/mcp/?key=value/')).toBe(
'https://api.example.com/mcp?key=value/',
);
});
test('preserves trailing slashes when both query and fragment are present', () => {
expect(normalizeMcpUrl('https://api.example.com/mcp/?key=val#/route/')).toBe(
'https://api.example.com/mcp?key=val#/route/',
);

— qwen3.7-max via Qwen Code /review

test('preserves trailing slashes inside URL fragments', () => {
expect(normalizeMcpUrl('https://api.example.com/mcp#/')).toBe(
'https://api.example.com/mcp#/',
);
expect(normalizeMcpUrl('https://api.example.com/mcp/#/route/')).toBe(
'https://api.example.com/mcp#/route/',
);
});

test('keeps legacy fallback behavior for non-parseable URLs', () => {
expect(normalizeMcpUrl('not a url///')).toBe('not a url');
});
});
12 changes: 10 additions & 2 deletions packages/desktop/packages/shared/src/sources/server-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,19 @@ export class SourceServerBuilder {

/**
* Normalize MCP URL to standard format
* - Removes trailing slashes
* - Removes trailing slashes from the path portion only
* - Preserves query strings and fragments unchanged
* - Preserves the user-configured path as-is (no /mcp suffix appended)
*/
export function normalizeMcpUrl(url: string): string {
return url.replace(/\/+$/, '');
const suffixIndex = url.search(/[?#]/);
if (suffixIndex === -1) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] Consider adding a brief comment explaining why the URL constructor is intentionally avoided here. A future maintainer might see the manual search(/[?#]/) + slice() parsing and refactor to new URL(), which would subtly change behavior (host lowercasing, percent-encoding normalization, throwing on unparseable inputs like 'not a url///').

Suggested change
if (suffixIndex === -1) {
// Intentionally avoids URL constructor: preserves original URL verbatim and handles non-parseable strings without throwing.
const suffixIndex = url.search(/[?#]/);

— qwen3.7-max via Qwen Code /review

return url.replace(/\/+$/, '');
}

return (
url.slice(0, suffixIndex).replace(/\/+$/, '') + url.slice(suffixIndex)
);
}

// Singleton instance
Expand Down
Loading