Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(mcp): expose structured response detail from BadRequestException in handleToolError

Yudhi Armyndharis218bf5da4eba
diff --git a/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts b/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
index 79f983adf..2deb09d44 100644
--- a/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
+++ b/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
@@ -213,3 +213,52 @@ describe('FIX 6: handleToolError sanitises non-HTTP errors', () => {
expect(payload.message).toBe('Missing API key');
});
});
+
+// ---------------------------------------------------------------------------
+// FIX 7: BadRequestException with string-array detail must expose the array,
+// not the generic 'Bad Request Exception' string
+// ---------------------------------------------------------------------------
+
+describe('FIX 7: handleToolError exposes structured BadRequestException detail', () => {
+ it('BadRequestException with string[] detail returns the array, not the generic string', () => {
+ const err = new BadRequestException(['sessionId: too small', 'limit: max 100']);
+ const result = handleToolError(err);
+ const content = (result.content[0] as { type: string; text: string }).text;
+ const payload = JSON.parse(content) as { success: boolean; name: string; message: unknown };
+
+ expect(payload.success).toBe(false);
+ expect(payload.message).not.toBe('Bad Request Exception');
+ expect(Array.isArray(payload.message)).toBe(true);
+ expect(payload.message).toContain('sessionId: too small');
+ expect(payload.message).toContain('limit: max 100');
+ });
+
+ it('BadRequestException with a plain string stays as that string', () => {
+ const err = new BadRequestException('Session x not found');
+ const result = handleToolError(err);
+ const content = (result.content[0] as { type: string; text: string }).text;
+ const payload = JSON.parse(content) as { message: unknown };
+ expect(payload.message).toBe('Session x not found');
+ });
+
+ it('NotFoundException with a plain string stays as that string', () => {
+ const err = new BadRequestException('Session x not found');
+ const result = handleToolError(err);
+ const content = (result.content[0] as { type: string; text: string }).text;
+ const payload = JSON.parse(content) as { message: unknown };
+ expect(payload.message).toBe('Session x not found');
+ });
+
+ it('plain Error still yields Internal error (no leakage)', () => {
+ const result = handleToolError(new Error('db secret leaked'));
+ const content = (result.content[0] as { type: string; text: string }).text;
+ const payload = JSON.parse(content) as { message: unknown };
+ expect(payload.message).toBe('Internal error');
+ });
+
+ it('result is marked isError=true', () => {
+ const err = new BadRequestException(['sessionId: too small']);
+ const result = handleToolError(err);
+ expect(result.isError).toBe(true);
+ });
+});
diff --git a/src/modules/mcp/tool-result.ts b/src/modules/mcp/tool-result.ts
index 7bda4cfad..43c910310 100644
--- a/src/modules/mcp/tool-result.ts
+++ b/src/modules/mcp/tool-result.ts
@@ -45,7 +45,12 @@ export function jsonToolResult(data: object, isError = false): CallToolResult {
export function handleToolError(error: unknown): CallToolResult {
if (error instanceof HttpException) {
logger.error(error.message, error.stack);
- return jsonToolResult({ success: false, name: error.name, message: error.message }, true);
+ const res = error.getResponse();
+ const message =
+ typeof res === 'object' && res !== null && 'message' in res
+ ? (res as Record<string, unknown>)['message']
+ : error.message;
+ return jsonToolResult({ success: false, name: error.name, message }, true);
}
if (error instanceof Error) {
logger.error(error.message, error.stack);