mastra-ai/mastra · error · MastraError

MCP_CLIENT_REDIRECT_MISSING_LOCATION

MCP_CLIENT_REDIRECT_MISSING_LOCATION

Error message

Received a ${response.status} redirect with no Location header from "${currentUrl.host}" while following redirects under the allowedHosts policy.

What it means

When fetchFollowingAllowedRedirects (the allowedHosts-aware fetch used by the MCP client) receives a 3xx redirect response, it requires a Location header to know where to follow. If the server sends a redirect status with no Location header, this MastraError (MCP_CLIENT_REDIRECT_MISSING_LOCATION, THIRD_PARTY) is thrown.

Source

Thrown at packages/mcp/src/client/url-policy.ts:205

  fetchImpl: (url: string | URL, init?: RequestInit) => Promise<Response>,
  url: string | URL,
  init: RequestInit | undefined,
  allowedHosts: readonly string[],
): Promise<Response> {
  let currentUrl = assertHostAllowed(url, allowedHosts);
  let method = init?.method ?? 'GET';
  let body = init?.body;
  const headers = new Headers(init?.headers);

  for (let redirectsFollowed = 0; ; redirectsFollowed++) {
    const response = await fetchImpl(currentUrl, { ...init, method, body, headers, redirect: 'manual' });
    if (!REDIRECT_STATUS_CODES.has(response.status)) {
      return response;
    }

    const location = response.headers.get('location');
    if (!location) {
      throw new MastraError({
        id: 'MCP_CLIENT_REDIRECT_MISSING_LOCATION',
        domain: ErrorDomain.MCP,
        category: ErrorCategory.THIRD_PARTY,
        text: `Received a ${response.status} redirect with no Location header from "${currentUrl.host}" while following redirects under the allowedHosts policy.`,
      });
    }
    if (redirectsFollowed >= MAX_REDIRECT_HOPS) {
      throw new MastraError({
        id: 'MCP_CLIENT_TOO_MANY_REDIRECTS',
        domain: ErrorDomain.MCP,
        category: ErrorCategory.THIRD_PARTY,
        // Interpolate only the host, not the full URL: a URL's path/query could
        // contain substrings (e.g. "sessionId") that isReconnectableMCPError
        // matches, which would misclassify this terminal failure as transient.
        text: `Exceeded the maximum of ${MAX_REDIRECT_HOPS} redirect hops while requesting host "${currentUrl.host}" under the allowedHosts policy.`,
      });
    }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Inspect the raw response with curl -v and fix the server/proxy to include a Location header on redirects
  2. Bypass the broken intermediary or correct its redirect configuration
  3. Point the client directly at the final destination URL so no redirect occurs
  4. If the redirect is legitimate, verify headers are not being stripped by middleware

Example fix

// before (server side, bad)
res.writeHead(302); res.end();
// after
res.writeHead(302, { Location: 'https://target.example.com/mcp' }); res.end();
Defensive patterns

Strategy: retry

Validate before calling

const res = await fetch(url, { method: 'HEAD', redirect: 'manual' });
if (REDIRECT_STATUS_CODES.has(res.status) && !res.headers.get('location')) {
  throw new Error(`Upstream ${res.status} at ${url.host} lacks Location header; fix server/proxy before MCP calls`);
}

Type guard

null

Try / catch

try {
  await client.tools();
} catch (e) {
  if (e instanceof MastraError && e.id === 'MCP_CLIENT_REDIRECT_MISSING_LOCATION') {
    console.error('Intermediary sent a redirect without Location; bypass proxy or fix gateway config');
  } else throw e;
}

Prevention

When it happens

Trigger: A remote MCP server (or intermediary proxy) returns 301/302/303/307/308 without a Location header while the client uses an allowedHosts URL policy.

Common situations: Misconfigured reverse proxies or gateways behind the MCP server; broken load balancer health redirects; corporate proxies that strip Location headers.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/e5c18010c6907654. Report an issue: GitHub.