Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/runtime/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export async function createClientContext(
await oauthSession?.close().catch(() => {});
throw primaryError;
}
// When OAuth is active the server already accepted a POST (returning 401),
// so it speaks Streamable HTTP. Falling back to SSE (GET) would fail with
// 405 Method Not Allowed or a fresh 401 that can never be resolved.
if (activeDefinition.auth === 'oauth') {
await oauthSession?.close().catch(() => {});
throw primaryError;
Comment on lines +169 to +171

Choose a reason for hiding this comment

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

P2 Badge Avoid skipping SSE fallback based on auth alone

activeDefinition.auth === 'oauth' is used here to skip SSE fallback for any primary error, but auth only describes the auth mechanism, not the transport (there’s no transport field in ServerDefinition). That means an OAuth‑protected server that only exposes SSE (e.g., a POST returning 405 or a network error on the streamable endpoint) will now fail permanently instead of falling back to SSE, even though SSE may be the only supported transport. Consider gating this on a confirmed streamable‑HTTP 401 (or another capability signal) rather than the auth flag alone.

Useful? React with 👍 / 👎.

}
if (primaryError instanceof Error) {
logger.info(`Falling back to SSE transport for '${activeDefinition.name}': ${primaryError.message}`);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/runtime-transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ describe('createClientContext (HTTP)', () => {
expect(clientConnect).toHaveBeenCalledTimes(2);
});

it('does not fall back to SSE when OAuth is active', async () => {
const definition: ServerDefinition = {
...stubHttpDefinition('https://example.com/mcp'),
auth: 'oauth',
};
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');
const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js');

vi.spyOn(Client.prototype, 'connect').mockImplementation(async (transport) => {
expect(transport).toBeInstanceOf(StreamableHTTPClientTransport);
throw new Error('connection failed');
});

await expect(
createClientContext(definition, logger, clientInfo, { maxOAuthAttempts: 0 })
).rejects.toThrow('connection failed');
});

it.skip('promotes ad-hoc HTTP servers to OAuth after unauthorized, then retries', async () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async () => {
return new Response(null, { status: 401, statusText: 'Unauthorized' });
Expand Down