Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/shared/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
if (isJSONRPCResultResponse(response)) {
resolver(response);
} else {
const error = new McpError(response.error.code, response.error.message, response.error.data);
const error = McpError.fromError(response.error.code, response.error.message, response.error.data);
resolver(error);
}
return;
Expand Down
17 changes: 15 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2314,20 +2314,33 @@ export class McpError extends Error {
this.name = 'McpError';
}

/**
* Strip the "MCP error NNNN: " prefix that McpError's constructor adds,
* so that fromError does not double-prefix when reconstructing an error
* whose message already carries the prefix from the wire.
*/
private static _stripErrorPrefix(message: string): string {
return message.replace(/^MCP error -?\d+: /, '');
}

/**
* Factory method to create the appropriate error type based on the error code and data
*/
static fromError(code: number, message: string, data?: unknown): McpError {
// Strip the "MCP error NNNN: " prefix if present, to avoid double-prefixing
// when the message comes from a serialized McpError on the wire.
const cleanMessage = McpError._stripErrorPrefix(message);

// Check for specific error types
if (code === ErrorCode.UrlElicitationRequired && data) {
const errorData = data as { elicitations?: unknown[] };
if (errorData.elicitations) {
return new UrlElicitationRequiredError(errorData.elicitations as ElicitRequestURLParams[], message);
return new UrlElicitationRequiredError(errorData.elicitations as ElicitRequestURLParams[], cleanMessage);
}
}

// Default to generic McpError
return new McpError(code, message, data);
return new McpError(code, cleanMessage, data);
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/shared/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5403,6 +5403,32 @@ describe('Error handling for missing resolvers', () => {
});
});

describe('McpError.fromError should not double-prefix', () => {
it('should not double-prefix when message already has MCP error prefix', () => {
// Simulate the message as it arrives from the wire (already prefixed by server's McpError constructor)
const prefixedMessage = 'MCP error -32601: Unknown tool: nope';
const error = McpError.fromError(ErrorCode.MethodNotFound, prefixedMessage);

// Should have exactly one prefix, not two
expect(error.message).toBe('MCP error -32601: Unknown tool: nope');
expect(error.message).not.toContain('MCP error -32601: MCP error');
});

it('should add prefix when message does not have it', () => {
// Direct usage without prefix (e.g., internal code creating an error)
const error = McpError.fromError(ErrorCode.InternalError, 'Something went wrong');

// Should have the prefix added
expect(error.message).toBe('MCP error -32603: Something went wrong');
});

it('should handle negative error codes correctly', () => {
const prefixedMessage = 'MCP error -32600: Invalid request';
const error = McpError.fromError(ErrorCode.InvalidRequest, prefixedMessage);
expect(error.message).toBe('MCP error -32600: Invalid request');
});
});

describe('Response and error message routing integration', () => {
it('should handle mixed response and error messages in queue', async () => {
await protocol.connect(transport);
Expand Down
Loading