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
17 changes: 15 additions & 2 deletions packages/mcp-core/src/internal/error-handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,24 @@ describe("formatErrorForUser", () => {
expect(await formatErrorForUser(error)).toBe(expected);
});

it("keeps the input error template for a 403 without a challenge", async () => {
it("says retrying won't help for a 403 without a challenge", async () => {
const result = await formatErrorForUser(createApiError("Forbidden", 403));

expect(result).toContain("**Input Error**");
expect(result).toContain("**Permission Denied**");
expect(result).toContain("API error (403): Forbidden");
expect(result).toContain("Retrying the same call will not help");
expect(result).not.toContain("**Input Error**");
expect(result).not.toContain("trying again");
});

it("finds a 403 without a challenge when a tool rewraps it as an input error", async () => {
const cause = new ApiPermissionError("Forbidden", "Forbidden", {});
const error = new UserInputError("API error (403): Forbidden", { cause });

const result = await formatErrorForUser(error);

expect(result).toContain("**Permission Denied**");
expect(result).not.toContain("**Input Error**");
});
});

Expand Down
34 changes: 30 additions & 4 deletions packages/mcp-core/src/internal/error-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,27 @@ export function isApiServerError(error: unknown): error is ApiServerError {
*/
export function findInsufficientScopeError(
error: unknown,
): ApiPermissionError | undefined {
return findPermissionError(error, (e) => e.isInsufficientScopeError());
}

/**
* A 403 that carries no scope challenge (the account lacks the role or project
* access), found on `error` or within the first few levels of its `cause`.
*/
export function findPermissionDeniedError(
error: unknown,
): ApiPermissionError | undefined {
return findPermissionError(error, (e) => !e.isInsufficientScopeError());
}

function findPermissionError(
error: unknown,
matches: (error: ApiPermissionError) => boolean,
): ApiPermissionError | undefined {
let current = error;
for (let depth = 0; depth < 5 && current instanceof Error; depth++) {
if (
current instanceof ApiPermissionError &&
current.isInsufficientScopeError()
) {
if (current instanceof ApiPermissionError && matches(current)) {
return current;
}
current = current.cause;
Expand Down Expand Up @@ -236,6 +250,18 @@ export async function formatErrorForUser(
].join("\n\n");
}

// A 403 without a scope challenge comes from the account's role or project
// access, not from the input, so the "try again" advice below would only
// trigger retries that cannot succeed.
const permissionDeniedError = findPermissionDeniedError(error);
if (permissionDeniedError) {
return [
"**Permission Denied**",
permissionDeniedError.toUserMessage(),
"This account does not have access to the requested resource. Retrying the same call will not help; ask an organization Owner or Manager for access, or use a different organization or project.",
].join("\n\n");
}

if (isUserInputError(error)) {
return [
"**Input Error**",
Expand Down
Loading