Skip to content

feat(mcp): expose update_opportunity_status tool for workflow transitions - #110

Merged
gkhngyk merged 1 commit into
mainfrom
feat/mcp-update-opportunity-status
May 26, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feat/mcp-update-opportunity-status

Conversation

@gkhngyk

@gkhngyk gkhngyk commented May 26, 2026

Copy link
Copy Markdown
Contributor

Implements #67 to spec. Fourth and final content-opportunity MCP tool after #74 (list / get) and #109 (generate_brief), closing the read → analyse → act → mark-progress loop for MCP-driven content workflows.

What

  • New MCP tool: `update_opportunity_status(opportunity_id, status)` → `{ id, status, updated_at }`
  • New REST endpoint: `PATCH /api/mcp/content-opportunities/[id]/status` with body `{ "status": "..." }`
  • Status enum: `new` | `sent` | `in_progress` | `done` | `dismissed`

Auth + safety (per pattern from #109)

Ownership check first in the web data layer — same `brands!inner(organization_id)` join used by all the other content-opportunity functions:

```ts
const { data: ownership } = await supabaseAdmin
.from('content_opportunities')
.select('id, brands!inner(organization_id)')
.eq('id', opportunityId)
.eq('brands.organization_id', auth.organizationId)
.maybeSingle();
if (!ownership) return null;
```

Wrong-org or missing id returns null (→ 404) with no UPDATE issued — an attacker can't probe ids and can't flip rows they don't own. No external services, no LLM, so no upstream cost concern either.

Shared enum

The valid statuses live as a single exported tuple in `web/src/lib/mcp/data.ts`:

```ts
export const CONTENT_OPPORTUNITY_STATUSES = ['new', 'sent', 'in_progress', 'done', 'dismissed'] as const;
```

Both the MCP zod schema and the REST handler's manual validation read from this tuple, so the two surfaces can't drift — adding a new status later is a one-line change.

Files

File Change
`web/src/lib/mcp/data.ts` New `updateOpportunityStatusFor(auth, id, status)` + exported `CONTENT_OPPORTUNITY_STATUSES` + `ContentOpportunityStatus` type
`web/src/lib/mcp/server.ts` Registers `update_opportunity_status` with zod enum + per-status description so the model knows valid values without guessing
`web/src/app/api/mcp/content-opportunities/[id]/status/route.ts` PATCH handler sharing the same data function; invalid status returns 400 with the valid list

How verified

Local smoke test against running web + local Supabase, with an `ans_` API key and a demo content opportunity from `supabase/seed.sql`:

  • ✅ Valid update → 200 with new status, `content_opportunities.status` + `updated_at` refreshed
  • ✅ Invalid status (`"wat"`) → 400 with the valid list in the error message, no UPDATE issued
  • ✅ Wrong-org `opportunity_id` → 404, no UPDATE
  • ✅ Missing API key → 401 from `authenticateMcpRequest`
  • ✅ Missing / non-JSON body → 400
  • ✅ Dashboard Content Opportunities page picks up the change on next load
  • ✅ `yarn typecheck` + `yarn format:check` clean

Conflict note

This branches off `main`, not off #109's branch. If #109 lands first, this PR has trivial textual conflicts in `web/src/lib/mcp/data.ts` (both add new exports near the end) and `web/src/lib/mcp/server.ts` (both add a tool registration before the `return server` line). Either order works — the imports/tool registrations are additive and don't touch each other's code paths.

Out of scope (per #67)

  • Bulk status updates (single-row only)
  • Webhook firing on status changes (the dashboard's `/bulk/send-webhook` flow stays separate; this tool only flips the status field)
  • Audit-log / note on transitions (future enhancement)

Closes #67.

…ions

Closes the content-opportunity MCP loop (after #74 list/get and #109's
generate_brief) by letting MCP-driven flows move an opportunity between
new / sent / in_progress / done / dismissed without a dashboard round-trip.

Pattern follows #109: ownership-check-first in the web data layer via
the same brands!inner join. A wrong-org or missing id returns null
(→ 404) with no UPDATE issued, so an attacker can't probe ids and can't
flip rows they don't own. The shared CONTENT_OPPORTUNITY_STATUSES tuple
is reused by both the zod enum (MCP) and the REST handler's manual
validation, so the surfaces can't drift.

- web/src/lib/mcp/data.ts: updateOpportunityStatusFor(auth, id, status)
  + exported CONTENT_OPPORTUNITY_STATUSES tuple + ContentOpportunityStatus type
- web/src/lib/mcp/server.ts: registers update_opportunity_status with
  zod enum + per-status description so the model knows valid values
  without guessing
- web/src/app/api/mcp/content-opportunities/[id]/status/route.ts: PATCH
  handler sharing the same data function; manual enum validation
  returns 400 with the valid list

Closes #67.
@gkhngyk
gkhngyk force-pushed the feat/mcp-update-opportunity-status branch from e487acf to f030488 Compare May 26, 2026 14:41
@gkhngyk
gkhngyk merged commit c79b015 into main May 26, 2026
2 checks passed
@gkhngyk
gkhngyk deleted the feat/mcp-update-opportunity-status branch May 26, 2026 14:45
gkhngyk added a commit that referenced this pull request May 28, 2026
…) + REST endpoint (#116)

Adds the third A-group MCP read tool from the assistant-readiness list,
after the content-opportunity tools shipped in #74 / #109 / #110.

Combines two existing RPCs (competitor_aggregates and
share_of_voice_aggregates, both DB-side aggregated since #114) so the
tool answers "how do I compare to my competitors?" and "who's gaining
share of voice?" in a single round trip. Ownership-check first via
supabaseAdmin (wrong-org brand → null → 404, no RPC fire). Snapshot
shape — deltas are out of scope for V1 and consistent with the existing
get_visibility_summary / list_topics / list_prompts shape; a caller that
wants a delta issues a second call with an earlier window.

- web/src/lib/mcp/data.ts: getCompetitorComparisonFor(auth, params) —
  parallel RPC calls, computes brand + per-competitor averages
  (when-present semantics matching the UI), and per-(model_used,
  platform) SoV split. Uses the same competitor display-name fallback
  pattern as the existing competitor logic (name ?? competitor_id) so
  unnamed competitors don't collide.

- web/src/lib/mcp/server.ts: registers the get_competitor_comparison
  tool with a description that covers both questions the tool answers
  and the snapshot-vs-delta caveat.

- web/src/app/api/mcp/competitor-comparison/route.ts: GET handler
  sharing the same data fn; query params mirror the MCP tool args.

Verified end-to-end against the largest brand in the cloud project
(5054 prompt result appearances, 7 competitors, 10 distinct model/
platform combos) — returned shape matches the documented interface,
SoV totals + per-platform breakdown reconcile with the existing UI.

Closes #98.
gkhngyk added a commit that referenced this pull request May 31, 2026
Bundles the in-product Agent epic (#120, #121), the BYOK rollout on
cloud, the MCP toolkit expansion (#109, #110, #116, #117, #118), the
insights performance fix (#114), and the invite-accept flow rebuild
(#127, #129, #130) into a tagged release.

See CHANGELOG.md for the full notes.
gkhngyk added a commit that referenced this pull request May 31, 2026
Bundles the in-product Agent epic (#120, #121), the BYOK rollout on
cloud, the MCP toolkit expansion (#109, #110, #116, #117, #118), the
insights performance fix (#114), and the invite-accept flow rebuild
(#127, #129, #130) into a tagged release.

See CHANGELOG.md for the full notes.

This branch was successfully deployed

1 active deployment
Preview — f0304883 Deployed May 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(mcp): expose update_opportunity_status tool for workflow transitions

1 participant