Skip to content

feat(mcp): get_published_posts and get_published_post_stats tools - #1246

Draft
sweetmantech wants to merge 2 commits into
mainfrom
claude/project-thread-w4bdrf
Draft

sweetmantech wants to merge 2 commits into
mainfrom
claude/project-thread-w4bdrf

Conversation

@sweetmantech

@sweetmantech sweetmantech commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Before: an MCP agent could not list the posts Recoup published or read their LinkedIn numbers. GET /api/posts and GET /api/posts/{id}/stats existed only over REST, and get_artist_posts returns scraped posts, which carry no post id that stats can take.

After: two MCP tools mirror those routes. get_published_posts lists the credential account's posts with optional artist_account_id, platform, status, limit (max 50) and offset, in the REST { status, posts } envelope. get_published_post_stats takes a post id and returns the REST { status, stats } envelope: LinkedIn company-page posts only, credits checked before LinkedIn is called, $0.001 charged after a successful read. REST rejections come back as tool errors with the same message.

How:

  • lib/publishing/getPosts.ts and lib/publishing/stats/getPostStats.ts hold the reads the handlers used to do inline; both handlers now call them, with REST bodies and status codes unchanged. getPostStats returns ok, insufficient_credits or rejected (with the REST status and message). It gates with checkCreditsAvailable, and the handler builds the same 402 body ensureCreditsOrShortCircuit did, so the three handler test files now mock checkCreditsAvailable instead.
  • Tools in lib/mcp/tools/posts/, registered from lib/mcp/tools/index.ts. No account_id input; the account comes from resolveAccountId.
  • fullOAuthToolPolicy.ts (main's readOnly/destructive/openWorld shape): get_published_posts is read-only, not destructive, closed world; get_published_post_stats is read-only, not destructive, open world (LinkedIn via Composio) with a $0.001 notice. Both are pinned in the fullOAuthCatalog annotation table. tool-policy.json: mcp:read for the list, mcp:read + mcp:spend for stats (like search_web). Two rows in docs/mcp-oauth-tool-qualification.md. Delegated catalog 96 tools, 95 non-app (main is at 94/93).

Tests (red first): getPosts.test.ts, getPostStats.test.ts (each rejection, credit shortfall, LinkedIn 502, DB error), and register-tool tests for both tools. After merging main on Nov 18: full suite 1,531 files, 10,055 tests passed; eslint and prettier clean on changed files. Preview checks: see the results comment (run on the pre-merge commit; the tool code is unchanged by the merge).

Merge sequencing: docs#40 (contract) -> this PR -> docs follow-up pinning the operation inventory.

Tracking: recoupable/app#2399


Generated by Claude Code

Extract getPosts and getPostStats from the GET /api/posts and
GET /api/posts/{id}/stats handlers so REST and MCP share one read each.
Register both tools, classify them in the full OAuth policy, the OAuth
tool inventory and the qualification map.

Tracking: recoupable/app#2399
Contract: recoupable/docs#40

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
api Ready Ready Preview Nov 18, 2026 3:21am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Pre-merge checks: Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Solid & Clean Code Warning Summary: The PR adds getPostStats in lib/publishing/stats/getPostStats.ts. The function body is 62 lines (lines 48-109). The check says to look for functions longer than 20 lines and to avoid func… Split getPostStats (lib/publishing/stats/getPostStats.ts, lines 48-109, 62 lines) into single-purpose functions, each in its own file named after the function. Examples: a post loader, an access and state guard, a LinkedIn stats fetch, an…
Full details: Solid & Clean Code

Explanation

Summary: The PR adds getPostStats in lib/publishing/stats/getPostStats.ts. The function body is 62 lines (lines 48-109). The check says to look for functions longer than 20 lines and to avoid functions with multiple responsibilities. This function breaks both rules. Evidence: - getPostStats.ts is absent on main. The file and its function are new in this PR. The PR moved the logic out of getPostStatsHandler.ts into this function. The logic was not split. - The function does seven jobs in one body: 1. Loads the post row (lines 52-53). 2. Checks access (lines 55-59). 3. Checks publish state (lines 61-63). 4. Checks platform and organization rules (lines 64-70). 5. Parses the post URN and resolves the connection (lines 71-78). 6. Gates credits (lines 80-87). 7. Calls LinkedIn, handles its error, charges the account, and shapes the response (lines 89-108). It has about 9 early-return branches. It changes for any change in access, state, platform, credits, billing, or LinkedIn behavior. - File-function name matching passes. getPosts.ts exports getPosts, getPostStats.ts exports getPostStats, and the registerGetPublished*Tool.ts files match their exports. Minor DRY and KISS points (not the basis of the verdict): - getPublishedPostsSchema (registerGetPublishedPostsTool.ts, lines 16-35) repeats the field rules of getPostsQuerySchema. The tool then parses args again with that schema (line 66). The limit 1-50 rule appears in both places. - The resolveAccountId and error block repeats in both register files (lines 58-63 and 36-41). - The status union 403 | 404 | 409 | 422 | 502 repeats in GetPostStatsResult and reject. Constructive direction: split getPostStats into small single-purpose functions. Possible names are loadPublishedPost, assertPostStatsAllowed, and chargePostStatsRead. Keep getPostStats as a short orchestrator. Share the account-resolution block and the query schema.

Resolution

Split getPostStats (lib/publishing/stats/getPostStats.ts, lines 48-109, 62 lines) into single-purpose functions, each in its own file named after the function. Examples: a post loader, an access and state guard, a LinkedIn stats fetch, and the credit gate. Keep getPostStats as a short orchestrator under 20 lines. Optionally, reuse one query schema for the list tool and one account-resolution helper for both tool registrations.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Copy link
Copy Markdown
Contributor Author

Preview checks against api-oyprb749n-recoup.vercel.app, built from 39db510 (this PR's head). CI: compatibility, format, lint, ingestion pass; test was still running when checked.

Done-when Result
REST stats rejects a non-uuid id before auth (GET /api/posts/not-a-uuid/stats) Pass: 400 id must be a valid UUID, same as main
tools/list includes both tools Not run
get_published_posts with platform: "linkedin" lists a Tom Sarig LinkedIn clip post Not run
get_published_post_stats on that post matches REST and charges once Not run
Non-uuid id, unknown id, non-LinkedIn post, unauthenticated call return tool errors with no charge Not run

Why not run: the preview host is behind Vercel Authentication and only answers through a share link, which works for plain GETs only. The MCP checks need authenticated POSTs from a sandbox with a preview credential, and that setup was refused in this session. The happy path also needs a credential for the account that owns Tom's posts. Locally, the unit tests cover every tool-error path and the full suite passes (1496 files, 9593 tests).

Kept as draft; not merging.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

MCP preview checks against api-oyprb749n-recoup.vercel.app, built from 39db510 (this PR's head). I used a throwaway agent+ test account made on the preview, with API-key MCP auth (Authorization: Bearer). CI is green.

Done-when Result
tools/list includes both tools Pass: 85 tools, including get_published_posts and get_published_post_stats, with the expected input schemas and no account_id
Unauthenticated tools/list and tools/call Pass: 401 invalid_token / No authorization provided
get_published_posts with platform: "linkedin" Pass: {"status":"success","posts":[]}, identical to REST GET /api/posts?platform=linkedin (200). The test account owns no posts.
get_published_posts with limit: 51 Pass: input validation error, max 50
get_published_post_stats with a non-uuid id Pass: input validation error Invalid UUID (REST: 400 id must be a valid UUID)
get_published_post_stats with an unknown uuid Pass: {"success":false,"message":"Post not found"} (REST: 404 Post not found)
get_published_post_stats with a smuggled account_id Pass: ignored, still Post not found for this account
No charge on any failed read Pass: credits 300,000,000 before and after
Non-LinkedIn post returns the 422 message Not run: the test account has no published posts. Covered by unit tests
Happy path on a Tom Sarig LinkedIn post, matching REST and charging once Not run: needs the account that owns those posts. Planned as one $0.001 read on production after merge

Note: like the other tools, a domain error comes back as { success: false, message } text without isError: true, because getToolResultError works that way across the server. The docs in docs#40 describe it the same way.

Kept as draft; not merging.


Generated by Claude Code

Resolve fullOAuthToolPolicy and fullOAuthCatalog conflicts: adopt main's
readOnly/destructive/openWorld policy shape, classify get_published_posts
(closed world) and get_published_post_stats (LinkedIn, open world), and
raise the delegated catalog counts to 96 tools, 95 non-app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview — 2c05c18f Deployed Nov 18, 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.

2 participants