Skip to content

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.

fix(mcp): read a draft's live page at the address it was published at, as the Live page tab does (#1548 review) (#1554)

Cihan224e5af57162
diff --git a/web/src/app/api/mcp/content-drafts/[id]/impact/route.ts b/web/src/app/api/mcp/content-drafts/[id]/impact/route.ts
index 35778dc8e..d5b526004 100644
--- a/web/src/app/api/mcp/content-drafts/[id]/impact/route.ts
+++ b/web/src/app/api/mcp/content-drafts/[id]/impact/route.ts
@@ -22,10 +22,7 @@ export async function GET(req: Request, { params }: { params: Promise<{ id: stri
try {
const impact = await getContentImpactFor(auth, id, days.data);
if (impact === 'no_page') {
- return NextResponse.json(
- { error: 'This draft is a new page, not live yet' },
- { status: 409 },
- );
+ return NextResponse.json({ error: 'This draft has no live address yet' }, { status: 409 });
}
if (!impact) return NextResponse.json({ error: 'Draft not found' }, { status: 404 });
return NextResponse.json(impact);
diff --git a/web/src/lib/mcp/content.test.ts b/web/src/lib/mcp/content.test.ts
index 74c33e54e..6c6f41804 100644
--- a/web/src/lib/mcp/content.test.ts
+++ b/web/src/lib/mcp/content.test.ts
@@ -12,6 +12,7 @@ const db = vi.hoisted(() => ({
rows: {} as Record<string, unknown>,
rpc: {} as Record<string, unknown>,
rpcCalls: [] as [string, unknown][],
+ selects: [] as [string, string][],
writes: [] as { table: string; op: string; value: unknown }[],
filters: [] as [string, string, unknown][],
}));
@@ -25,7 +26,7 @@ vi.mock('@/lib/supabase/admin', () => ({
from: (table: string) => {
const read = () => ({ data: db.rows[table] ?? null, error: null });
const chain: Record<string, unknown> = {
- select: () => chain,
+ select: (columns?: string) => (db.selects.push([table, columns ?? '']), chain),
eq: (col: string, v: unknown) => (db.filters.push([table, col, v]), chain),
in: () => chain,
is: (col: string, v: unknown) => (db.filters.push([table, col, v]), chain),
@@ -76,6 +77,7 @@ const draftRow = (over = {}) => ({
page_type: 'feature_page',
author: 'user',
source_url: 'https://acme.example/forecasting',
+ published_url: null,
body_html: `<h1>Pipeline forecasting</h1><p>${QUOTED}</p><p>Our forecasting is very accurate.</p>`,
action_id: 'act-1',
prompt_ids: [],
@@ -88,6 +90,7 @@ beforeEach(() => {
db.rows = {};
db.rpc = {};
db.rpcCalls = [];
+ db.selects = [];
db.writes = [];
db.filters = [];
brandInOrg.mockReset().mockResolvedValue(true);
@@ -363,4 +366,28 @@ describe('getContentImpactFor', () => {
await expect(getContentImpactFor(auth, 'd1', 30)).resolves.toBe('no_page');
expect(db.rpcCalls).toEqual([]);
});
+
+ it('reads a new page at the address it was published at, as the Live page tab does (#1548 review)', async () => {
+ db.rows.content_drafts = draftRow({
+ method: 'create',
+ source_url: null,
+ published_url: 'https://acme.example/new-guide',
+ });
+ await getContentImpactFor(auth, 'd1', 30);
+ // The draft is read with its published address.
+ expect(db.selects.find(([table]) => table === 'content_drafts')?.[1]).toContain(
+ 'published_url',
+ );
+ expect(db.rpcCalls).toEqual([
+ [
+ 'content_page_impact',
+ { p_brand_id: expect.any(String), p_page: 'https://acme.example/new-guide', p_days: 30 },
+ ],
+ ]);
+ // An optimized page that went live elsewhere: its published address too.
+ db.rpcCalls.length = 0;
+ db.rows.content_drafts = draftRow({ published_url: 'https://acme.example/moved' });
+ await getContentImpactFor(auth, 'd1', 30);
+ expect(db.rpcCalls[0][1]).toMatchObject({ p_page: 'https://acme.example/moved' });
+ });
});
diff --git a/web/src/lib/mcp/content.ts b/web/src/lib/mcp/content.ts
index 700eddbcc..1cd0f0516 100644
--- a/web/src/lib/mcp/content.ts
+++ b/web/src/lib/mcp/content.ts
@@ -38,7 +38,7 @@ import {
const LIST_LIMIT = 100;
const DRAFT_COLUMNS =
- 'id, brand_id, title, status, method, page_type, author, source_url, body_html, action_id, prompt_ids, updated_at, agent_state, agent_state_at, brands!inner(organization_id, site_type, industry, shopping_mode_enabled), actions(action_no)';
+ 'id, brand_id, title, status, method, page_type, author, source_url, published_url, body_html, action_id, prompt_ids, updated_at, agent_state, agent_state_at, brands!inner(organization_id, site_type, industry, shopping_mode_enabled), actions(action_no)';
interface DraftRow {
id: string;
@@ -49,6 +49,8 @@ interface DraftRow {
page_type: string | null;
author: string;
source_url: string | null;
+ /** Where the page went live (Send to CMS or marked published, 00262), as the Live page tab reads it. */
+ published_url: string | null;
body_html: string;
action_id: string | null;
prompt_ids: string[] | null;
@@ -370,7 +372,8 @@ export async function createContentDraftFor(
/**
* The draft's page in AI answers, AI visits and Google over the period
- * (CE-016, 00209), as the Impact tab reads it. 'no_page' for a draft that
+ * (CE-016, 00209), as the Live page tab reads it: the address it was
+ * published at, else the page it optimizes. 'no_page' for a draft that
* isn't of a live page yet; null outside the organization.
*/
export async function getContentImpactFor(
@@ -380,12 +383,13 @@ export async function getContentImpactFor(
): Promise<Record<string, unknown> | 'no_page' | null> {
const draft = await draftFor(auth, draftId);
if (!draft) return null;
- if (!draft.source_url) return 'no_page';
+ const page = draft.published_url ?? draft.source_url;
+ if (!page) return 'no_page';
if (!(IMPACT_PERIODS as readonly number[]).includes(days))
throw new Error('days must be 7, 30, 90 or 365');
const { data, error } = await supabaseAdmin.rpc('content_page_impact', {
p_brand_id: draft.brand_id,
- p_page: draft.source_url,
+ p_page: page,
p_days: days,
});
if (error) throw new Error(error.message);
diff --git a/web/src/lib/mcp/server.ts b/web/src/lib/mcp/server.ts
index e1d162ec9..e4fe45ea7 100644
--- a/web/src/lib/mcp/server.ts
+++ b/web/src/lib/mcp/server.ts
@@ -1636,7 +1636,7 @@ export function createMcpServer(auth: McpAuthContext): McpServer {
const impact = await getContentImpactFor(auth, args.draft_id, args.days);
if (impact === 'no_page') {
return {
- content: [{ type: 'text' as const, text: 'This draft is a new page, not live yet' }],
+ content: [{ type: 'text' as const, text: 'This draft has no live address yet' }],
isError: true,
};
}