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
16 changes: 4 additions & 12 deletions src/mcp/mcp-server-apps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ let apiBaseUrl: string;
const tempDirs: string[] = [];
const originalAllowPrivateNetwork = process.env.MCP4_SSRF_ALLOW_PRIVATE_NETWORK;

async function writeTempFile(name: string, content: string): Promise<string> {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'mcp-server-apps-'));
tempDirs.push(dir);
const filePath = path.join(dir, name);
await fs.writeFile(filePath, content, 'utf8');
return filePath;
}

beforeAll(async () => {
process.env.MCP4_SSRF_ALLOW_PRIVATE_NETWORK = 'true';
apiServer = createServer((req, res) => {
Expand Down Expand Up @@ -275,14 +267,14 @@ describe('MCPServer apps resources', () => {
},
});

expect(invalidReadResponse.error).toEqual({
expect(invalidReadResponse.error).toMatchObject({
code: -32602,
message: 'resources/read requires string parameter "uri"',
});
expect(invalidCompletionResponse.error).toEqual({
expect(invalidReadResponse.error.message).toMatch(/^Validation error: resources\/read requires string parameter "uri" \(correlation ID: .+\)$/);
expect(invalidCompletionResponse.error).toMatchObject({
code: -32602,
message: 'completion/complete requires a resource ref',
});
expect(invalidCompletionResponse.error.message).toMatch(/^Validation error: completion\/complete requires a resource ref \(correlation ID: .+\)$/);
});

it('propagates session context to fetch-backed resource and completion lookups', async () => {
Expand Down
20 changes: 13 additions & 7 deletions src/mcp/mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@ export class MCPServer {
// Generate correlation ID only on error (lazy)
const correlationId = generateCorrelationId();
this.logger.error('ListTools handler error', err as Error, { correlationId });
// Always return generic error to clients
throw new Error(`Internal error (correlation ID: ${correlationId})`);
// Return formatted error
throw new Error(this.formatErrorForClient(err, correlationId));
}
});

Expand All @@ -811,7 +811,7 @@ export class MCPServer {
} catch (err) {
const correlationId = generateCorrelationId();
this.logger.error('ListPrompts handler error', err as Error, { correlationId });
throw new Error(`Internal error (correlation ID: ${correlationId})`);
throw new Error(this.formatErrorForClient(err, correlationId));
}
});

Expand All @@ -832,7 +832,7 @@ export class MCPServer {
correlationId,
promptName: request.params.name,
});
throw new Error(`Internal error (correlation ID: ${correlationId})`);
throw new Error(this.formatErrorForClient(err, correlationId));
}
});

Expand Down Expand Up @@ -1851,6 +1851,8 @@ export class MCPServer {
result: promptResult,
};
} catch (error) {
const correlationId = generateCorrelationId();
this.logger.error('prompts/get handler error', error as Error, { correlationId });
let code = -32603;
if (error instanceof ValidationError) {
code = -32602;
Expand All @@ -1863,7 +1865,7 @@ export class MCPServer {
id: req.id,
error: {
code,
message: (error as Error).message,
message: this.formatErrorForClient(error, correlationId),
},
};
}
Expand Down Expand Up @@ -1901,12 +1903,14 @@ export class MCPServer {
result: await this.readResource(params.uri, sessionId, profileId),
};
} catch (error) {
const correlationId = generateCorrelationId();
this.logger.error('resources/read handler error', error as Error, { correlationId });
return {
jsonrpc: '2.0',
id: req.id,
error: {
code: error instanceof ValidationError ? -32602 : -32601,
message: (error as Error).message,
message: this.formatErrorForClient(error, correlationId),
},
};
}
Expand All @@ -1920,12 +1924,14 @@ export class MCPServer {
result: await this.completeResourceArgument(req as CompleteRequest, sessionId, profileId),
};
} catch (error) {
const correlationId = generateCorrelationId();
this.logger.error('completion/complete handler error', error as Error, { correlationId });
return {
jsonrpc: '2.0',
id: req.id,
error: {
code: error instanceof ValidationError ? -32602 : -32601,
message: (error as Error).message,
message: this.formatErrorForClient(error, correlationId),
},
};
}
Expand Down
Loading