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
14 changes: 12 additions & 2 deletions src/routes/api/mcp/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export const POST: RequestHandler = async (event) => {

try {
const { request } = event;
const body = await request.json();
let body: any;
try {
body = await request.json();
} catch {
return json({ error: 'Invalid or missing JSON body' }, { status: 400 });
}
const { instanceId, mcpId, config, npmPackage, defaultArgs, envVars, command, args } = body as {
instanceId: string;
mcpId?: string;
Expand Down Expand Up @@ -150,7 +155,12 @@ export const DELETE: RequestHandler = async (event) => {

try {
const { request } = event;
const body = await request.json();
let body: any;
try {
body = await request.json();
} catch {
return json({ error: 'Invalid or missing JSON body' }, { status: 400 });
}
const { instanceId } = body as { instanceId: string };

if (!instanceId) {
Expand Down
7 changes: 6 additions & 1 deletion src/routes/api/mcp/call/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export const POST: RequestHandler = async (event) => {

try {
const { request } = event;
const body = await request.json();
let body: any;
try {
body = await request.json();
} catch {
return json({ error: 'Invalid or missing JSON body' }, { status: 400 });
}
const { instanceId, toolName, args, requestId } = body as {
instanceId: string;
toolName: string;
Expand Down
32 changes: 32 additions & 0 deletions src/routes/api/mcp/mcp.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,36 @@ describe('/api/mcp', () => {
expect(body.authority.reasonCodes).toContain('native_governor_required');
expect(connectMCP).not.toHaveBeenCalled();
});

it('returns 400 for empty request body instead of 500', async () => {
const response = await POST({
request: new Request('http://127.0.0.1/api/mcp', {
method: 'POST',
headers: { 'content-type': 'application/json', 'x-api-key': 'mcp-test-secret' },
body: ''
}),
url: new URL('http://127.0.0.1/api/mcp'),
getClientAddress: () => '127.0.0.1'
} as never);

expect(response.status).toBe(400);
expect(await response.json()).toMatchObject({ error: expect.stringContaining('JSON body') });
expect(connectMCP).not.toHaveBeenCalled();
});

it('returns 400 for malformed JSON body instead of 500', async () => {
const response = await POST({
request: new Request('http://127.0.0.1/api/mcp', {
method: 'POST',
headers: { 'content-type': 'application/json', 'x-api-key': 'mcp-test-secret' },
body: '{not valid json'
}),
url: new URL('http://127.0.0.1/api/mcp'),
getClientAddress: () => '127.0.0.1'
} as never);

expect(response.status).toBe(400);
expect(await response.json()).toMatchObject({ error: expect.stringContaining('JSON body') });
expect(connectMCP).not.toHaveBeenCalled();
});
});