feat: consolidate remote MCP servers to use url in config (#13762)

This commit is contained in:
Jack Wotherspoon
2025-12-02 20:01:33 -05:00
committed by GitHub
parent 145fb246a6
commit bdbbe9232d
9 changed files with 801 additions and 271 deletions

View File

@@ -0,0 +1,42 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { isAuthenticationError, UnauthorizedError } from './errors.js';
describe('isAuthenticationError', () => {
it('should detect error with code: 401 property (MCP SDK style)', () => {
const error = { code: 401, message: 'Unauthorized' };
expect(isAuthenticationError(error)).toBe(true);
});
it('should detect UnauthorizedError instance', () => {
const error = new UnauthorizedError('Authentication required');
expect(isAuthenticationError(error)).toBe(true);
});
it('should return false for 404 errors', () => {
const error = { code: 404, message: 'Not Found' };
expect(isAuthenticationError(error)).toBe(false);
});
it('should handle null and undefined gracefully', () => {
expect(isAuthenticationError(null)).toBe(false);
expect(isAuthenticationError(undefined)).toBe(false);
});
it('should handle non-error objects', () => {
expect(isAuthenticationError('string error')).toBe(false);
expect(isAuthenticationError(123)).toBe(false);
expect(isAuthenticationError({})).toBe(false);
});
it('should detect 401 in various message formats', () => {
expect(isAuthenticationError(new Error('401 Unauthorized'))).toBe(true);
expect(isAuthenticationError(new Error('HTTP 401'))).toBe(true);
expect(isAuthenticationError(new Error('Status code: 401'))).toBe(true);
});
});

View File

@@ -117,3 +117,42 @@ function parseResponseData(error: GaxiosError): ResponseData {
}
return error.response?.data as ResponseData;
}
/**
* Checks if an error is a 401 authentication error.
* Uses structured error properties from MCP SDK errors.
*
* @param error The error to check
* @returns true if this is a 401/authentication error
*/
export function isAuthenticationError(error: unknown): boolean {
// Check for MCP SDK errors with code property
// (SseError and StreamableHTTPError both have numeric 'code' property)
if (error && typeof error === 'object' && 'code' in error) {
const errorCode = (error as { code: unknown }).code;
if (errorCode === 401) {
return true;
}
}
// Check for UnauthorizedError class (from MCP SDK or our own)
if (
error instanceof Error &&
error.constructor.name === 'UnauthorizedError'
) {
return true;
}
if (error instanceof UnauthorizedError) {
return true;
}
// Fallback: Check for MCP SDK's plain Error messages with HTTP 401
// The SDK sometimes throws: new Error(`Error POSTing to endpoint (HTTP 401): ...`)
const message = getErrorMessage(error);
if (message.includes('401')) {
return true;
}
return false;
}