File size: 2,185 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { NextRequest, NextResponse } from "next/server";
import { getMcpAccessibilityConfig, setMcpAccessibilityConfig } from "@/lib/db/compression";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { mcpAccessibilityConfigSchema } from "@/shared/validation/compressionConfigSchemas";

// Read/update the mcpAccessibility engine config (compression/mcpAccessibility DB key) that the
// MCP server consumes on every tool call to trim oversized tool outputs. Kept as a dedicated
// sub-route (sibling of settings/compression) so the strict main settings schema stays focused
// and the #4206 numeric bounds become reachable from the dashboard.

export async function GET(request: NextRequest) {
  if (!(await isAuthenticated(request))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const config = await getMcpAccessibilityConfig();
    return NextResponse.json(config);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}

export async function PUT(request: NextRequest) {
  if (!(await isAuthenticated(request))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    let rawBody: unknown;
    try {
      rawBody = await request.json();
    } catch {
      return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
    }

    const validation = validateBody(mcpAccessibilityConfigSchema, rawBody);
    if (isValidationFailure(validation)) {
      return NextResponse.json({ error: validation.error }, { status: 400 });
    }

    // Partial-merge over the current config so toggling one field does not reset the others to
    // their defaults (setMcpAccessibilityConfig folds in DEFAULT + clampMcpAccessibilityConfig).
    const current = await getMcpAccessibilityConfig();
    await setMcpAccessibilityConfig({ ...current, ...validation.data });
    const config = await getMcpAccessibilityConfig();
    return NextResponse.json(config);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}