Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
import { NextResponse } from 'next/server';
import { z } from 'zod';
import { authenticateMcpRequest } from '@/lib/mcp-auth';
import { demoBrandRefusal } from '@/lib/mcp/demo-gate';
import { suggestContentChangesFor } from '@/lib/mcp/content';
import { contentUuid, suggestionsShape } from '@/lib/mcp/content-schemas';
/**
* POST /api/mcp/content-drafts/[id]/suggestions — REST mirror of the
* `suggest_content_changes` MCP tool. Body: { suggestions: [...] }
*/
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
const auth = await authenticateMcpRequest(req);
if (auth instanceof NextResponse) return auth;
const { id } = await params;
const parsed = z.object(suggestionsShape).safeParse(await req.json().catch(() => null));
if (!contentUuid.safeParse(id).success || !parsed.success) {
return NextResponse.json({ error: 'Invalid request' }, { status: 400 });
}
try {
// Nothing written on the demo brand's sample data, as the tool refuses it.
const refused = await demoBrandRefusal('suggest_content_changes', auth.organizationId, {
draft_id: id,
});
if (refused) return refused;
const result = await suggestContentChangesFor(auth, id, parsed.data.suggestions);
if (result === 'published') {
return NextResponse.json(
{ error: 'The draft is published; suggest on a new draft' },
{ status: 409 },
);
}
if (result === 'open_suggestions') {
return NextResponse.json(
{
error:
'A person still has open suggestions on this draft to accept or reject; suggest again once the draft lists none',
},
{ status: 409 },
);
}
if (result === 'writing') {
return NextResponse.json(
{ error: 'The Content Agent is writing this draft; suggest again once it has finished' },
{ status: 409 },
);
}
if (!result) return NextResponse.json({ error: 'Draft not found' }, { status: 404 });
return NextResponse.json(result, { status: result.kept ? 201 : 200 });
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : 'Failed' },
{ status: 500 },
);
}
}