Skip to content

Add Phase 4B: Pandora MCP server and safe MCP tools - #101

Merged
besfeng23 merged 1 commit into
mainfrom
codex/add-pandora-mcp-server-support
Jun 26, 2026
Merged

Add Phase 4B: Pandora MCP server and safe MCP tools#101
besfeng23 merged 1 commit into
mainfrom
codex/add-pandora-mcp-server-support

Conversation

@besfeng23

@besfeng23 besfeng23 commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Provide a thin, safe MCP adapter so Pandora Memory Engine can be connected from ChatGPT Custom Tools / MCP, exposing a small set of discoverable tools while reusing existing Phase 4A services.
  • Keep the system secure and phased: require explicit env gates, require bearer token auth, scope all operations to a server-side user id, and avoid enabling public reads/writes, model calls, embeddings, or semantic retrieval.

Description

  • Add a stateless Streamable HTTP MCP endpoint and compatibility aliases at /api/mcp, /api/mcp/sse, and /api/mcp/message that use the official @modelcontextprotocol/sdk transport in JSON-response mode for Vercel serverless.
  • Implement MCP bearer-token auth with constant-time comparison and required server-only DB key usage in lib/services/mcp-auth.ts, and use PANDORA_ENABLE_MCP, PANDORA_MCP_TOKEN, PANDORA_MCP_USER_ID, and PANDORA_MCP_DB_KEY to gate and scope access.
  • Register four safe tools implemented atop existing bridge/distillation helpers in lib/services/pandora-mcp-server.ts and lib/services/pandora-mcp-tools.ts: get_latest_context_pack, get_memory_context, capture_memory_event (gated by PANDORA_ENABLE_MCP_CAPTURE), and distill_context_pack (gated by PANDORA_ENABLE_MCP_DISTILLATION), and add best-effort audit logging in lib/services/pandora-mcp-audit.ts.
  • Add operator docs docs/phase-4b-pandora-mcp.md, unit tests tests/unit/pandora-mcp-auth.test.ts and tests/unit/pandora-mcp-tools.test.ts, and add @modelcontextprotocol/sdk runtime dependency to package.json.

Testing

  • Ran type checking with npm run typecheck and it completed without errors.
  • Ran linter with npm run lint; it passed but left existing pre-existing warnings in lib/db/core-repositories.ts and vitest.config.ts.
  • Ran unit tests with npm run test and all tests passed (76 test files, 454 tests).
  • Built the app with npm run build; build succeeded (warnings include the existing Supabase Edge runtime notice and Next.js export recognizer warnings for the re-exported routes).

Codex Task

Summary by CodeRabbit

  • New Features

    • Added MCP API routes for server and SSE-compatible access, enabling dynamic Node.js handling.
    • Introduced a new MCP-backed memory experience with tools to view recent context, capture events, and distill context packs.
    • Added setup and deployment documentation for the new MCP flow and endpoint configuration.
  • Bug Fixes

    • Added access checks and origin handling to improve request reliability and reduce unauthorized usage.
    • Improved request safety with consistent authentication and audit logging for MCP actions.

@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
memory Ready Ready Preview, Comment Jun 26, 2026 8:12pm

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Adds Pandora MCP authentication, tool handlers, MCP server wiring, Next.js route handling, and Phase 4B documentation for the MCP endpoint.

Changes

Pandora MCP Server

Layer / File(s) Summary
Auth principal resolution
lib/services/mcp-auth.ts, tests/unit/pandora-mcp-auth.test.ts
Bearer-token principal resolution returns structured success or error results, and helper checks expose capture and distillation feature gates.
MCP tool handlers and audit logging
lib/services/pandora-mcp-audit.ts, lib/services/pandora-mcp-tools.ts, tests/unit/pandora-mcp-tools.test.ts
The MCP tool module defines input schemas, reads context packs and memory context, captures events, distills packs, and writes audit log rows; tests cover feature gates, latest-pack selection, namespace validation, and item capping.
MCP server wiring
lib/services/pandora-mcp-server.ts, package.json
The MCP server registers four Zod-validated tools on pandora-memory and serializes tool results through the MCP SDK dependency.
MCP routes and docs
app/api/mcp/route.ts, app/api/mcp/message/route.ts, app/api/mcp/sse/route.ts, docs/phase-4b-pandora-mcp.md
The Next.js MCP route handles CORS, principal resolution, Supabase client creation, and streamable transport forwarding; the message and sse routes re-export the handlers, and the new Phase 4B doc page describes setup and endpoint usage.

Sequence Diagram(s)

Included in the hidden review stack artifact.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

I hopped through routes with twitchy ears,
And sniffed the logs through MCP spheres.
🐇 With packs and tools all neatly spun,
The moonlit bridge now hums as one.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding the Phase 4B Pandora MCP server and safe MCP tools.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/add-pandora-mcp-server-support

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cc074b6ca7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const input = memoryContextInputSchema.parse(rawInput);
const maxItems = Math.min(Math.max(input.max_items ?? 8, 1), 20);
const packs = await (client.from<MemoryContextPack>("memory_context_packs").select("*").eq("user_id", principal.userId).eq("namespace", input.namespace).eq("status", "active").order("created_at", { ascending: false }).limit(1) as unknown as Promise<{ data: MemoryContextPack[] | null; error: { message: string } | null }>);
const events = await (client.from<MemoryEvent>("memory_events").select("*").eq("user_id", principal.userId).eq("namespace", input.namespace).neq("status", "archived").order("created_at", { ascending: false }).limit(maxItems) as unknown as Promise<{ data: MemoryEvent[] | null; error: { message: string } | null }>);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude ignored events from MCP context

When an operator marks a memory event as ignored, this MCP query still returns it because it excludes only archived; the ignore endpoint leaves the row at status ignored, so get_memory_context can surface rejected facts and the distillation path below uses the same filter to fold them back into active context packs. This defeats the review/ignore workflow for any bearer-token MCP client once ignored events exist in the namespace.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant