Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lib/mcp/oauth/__tests__/registerFullOAuthTools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { describe, it, expect, vi } from "vitest";
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerFullOAuthTools } from "../registerFullOAuthTools";
import { registerGetArtistSocialsTool } from "../../tools/artistSocials/registerGetArtistSocialsTool";

const socials = vi.hoisted(() => {
process.env.PRIVY_PROJECT_SECRET = "test";
return vi.fn();
});
vi.mock("@/lib/auth/validateAuthContext", () => ({ validateAuthContext: vi.fn() }));
vi.mock("@/lib/artist/getArtistSocials", () => ({ getArtistSocials: socials }));
vi.mock("@/lib/supabase/serverClient", () => ({ default: {} }));

const operation = vi.fn(async (args: unknown) => ({
content: [{ type: "text" as const, text: JSON.stringify(args) }],
Expand Down Expand Up @@ -50,8 +59,8 @@ vi.mock("../../tools", () => ({
{ inputSchema: z.object({ active: z.boolean() }) },
operation,
);
registerGetArtistSocialsTool(server);
for (const name of [
"get_artist_socials",
"artist_deep_research",
"spotify_deep_research",
"get_catalog_playcount_history",
Expand Down Expand Up @@ -120,6 +129,37 @@ describe("full OAuth registry", () => {
expect(registered.has("spotify_deep_research")).toBe(false);
expect(registered.has("ingestion")).toBe(false);
});
it("reads artist socials through the real tool registration with OAuth-bound access", async () => {
const { registered, prepare } = setup();
const tool = registered.get("get_artist_socials")!;
const artist_account_id = "artist-listed-for-owner";
const profile = {
status: "success",
socials: [{ username: "saved-profile", follower_count: 42 }],
pagination: { page: 1, limit: 20, total_count: 1, total_pages: 1 },
};
socials.mockResolvedValue(profile);

const result = await tool.run({ artist_account_id }, extra);
expect(result.isError).toBeFalsy();
const expectedArgs = { artist_account_id, page: 1, limit: 20 };
expect(prepare).toHaveBeenCalledExactlyOnceWith("get_artist_socials", expectedArgs, "owner");
expect(socials).toHaveBeenCalledExactlyOnceWith(expectedArgs);
expect(JSON.parse(result.content[0].text)).toEqual(profile);

socials.mockClear();
const custom = { artist_account_id, page: 2, limit: 5, history: 14 };
expect((await tool.run(custom, extra)).isError).toBeFalsy();
expect(socials).toHaveBeenCalledExactlyOnceWith(custom);

socials.mockClear();
prepare.mockRejectedValueOnce(new Error("Artist access denied"));
expect((await tool.run({ artist_account_id: "outside-workspace" }, extra)).isError).toBe(true);
expect(
(await tool.run({ artist_account_id, account_id: "different-owner" }, extra)).isError,
).toBe(true);
expect(socials).not.toHaveBeenCalled();
});
it.each([
undefined,
{ ...access, scopes: ["mcp:read", "mcp:write"] },
Expand Down
7 changes: 6 additions & 1 deletion lib/mcp/oauth/fullOAuthToolPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export const fullOAuthToolPolicy: Record<string, ToolPolicy> = {
get_artist_posts: { readOnly: true, destructive: false, openWorld: false },
get_scraper_results: { readOnly: true, destructive: false, openWorld: false },
scrape_artist_socials: { readOnly: false, destructive: true, openWorld: true },
get_artist_socials: { readOnly: true, destructive: false, openWorld: false },
get_artist_socials: {
readOnly: true,
destructive: false,
openWorld: false,
notice: "Use the artist account_id returned by list_artists as artist_account_id.",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Custom agent: Enforce Clear Code Style and Maintainability Practices

This policy file is 239 lines, well above the rule’s 100-line limit, and this entry expands it further. Split the policy inventory into smaller modules.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/mcp/oauth/fullOAuthToolPolicy.ts, line 47:

<comment>This policy file is 239 lines, well above the rule’s 100-line limit, and this entry expands it further. Split the policy inventory into smaller modules.</comment>

<file context>
@@ -40,7 +40,12 @@ export const fullOAuthToolPolicy: Record<string, ToolPolicy> = {
+    readOnly: true,
+    destructive: false,
+    openWorld: false,
+    notice: "Use the artist account_id returned by list_artists as artist_account_id.",
+  },
   artist_deep_research: { readOnly: true, destructive: false, openWorld: false },
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Custom agent: Flag AI Slop and Fabricated Changes

list_artists returns each artist as { id, name }, not with an account_id field. Tell callers to use the returned id as artist_account_id.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/mcp/oauth/fullOAuthToolPolicy.ts, line 47:

<comment>`list_artists` returns each artist as `{ id, name }`, not with an `account_id` field. Tell callers to use the returned `id` as `artist_account_id`.</comment>

<file context>
@@ -40,7 +40,12 @@ export const fullOAuthToolPolicy: Record<string, ToolPolicy> = {
+    readOnly: true,
+    destructive: false,
+    openWorld: false,
+    notice: "Use the artist account_id returned by list_artists as artist_account_id.",
+  },
   artist_deep_research: { readOnly: true, destructive: false, openWorld: false },
</file context>
Suggested change
notice: "Use the artist account_id returned by list_artists as artist_account_id.",
+ notice: "Use the artist id returned by list_artists as artist_account_id.",

},
artist_deep_research: { readOnly: true, destructive: false, openWorld: false },
spotify_deep_research: { readOnly: true, destructive: false, openWorld: false },
get_spotify_album: { readOnly: true, destructive: false, openWorld: true },
Expand Down
14 changes: 10 additions & 4 deletions lib/mcp/oauth/registerFullOAuthTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export function registerFullOAuthTools(
) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Custom agent: Enforce Clear Code Style and Maintainability Practices

This module is 283 lines, well over Rule 3’s 100-line limit, and combines schema normalization, policy selection, authorization, and tool registration. Split these responsibilities into focused modules under the limit.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/mcp/oauth/registerFullOAuthTools.ts, line 110:

<comment>This module is 283 lines, well over Rule 3’s 100-line limit, and combines schema normalization, policy selection, authorization, and tool registration. Split these responsibilities into focused modules under the limit.</comment>

<file context>
@@ -102,13 +106,15 @@ export function registerFullOAuthTools(
-    const originalObject =
-      config.inputSchema instanceof z.ZodObject ? config.inputSchema : undefined;
+    // The MCP SDK accepts raw Zod shapes. The OAuth adapter also parses them itself.
+    const registeredSchema =
+      config.inputSchema instanceof z.ZodType ? config.inputSchema : z.object(config.inputSchema);
+    const originalObject = registeredSchema instanceof z.ZodObject ? registeredSchema : undefined;
</file context>

const register = (
name: string,
config: { description?: string; inputSchema: z.ZodType; annotations?: Record<string, unknown> },
config: {
description?: string;
inputSchema: z.ZodType | z.ZodRawShape;
annotations?: Record<string, unknown>;
},
handler: (args: Args, extra: Extra) => Promise<CallToolResult>,
operation?: {
name: string;
Expand Down Expand Up @@ -102,13 +106,15 @@ export function registerFullOAuthTools(
}
: fullOAuthToolPolicy[name];
if (!policy) throw new Error(`Missing delegated policy for ${name}`);
const originalObject =
config.inputSchema instanceof z.ZodObject ? config.inputSchema : undefined;
// The MCP SDK accepts raw Zod shapes. The OAuth adapter also parses them itself.
const registeredSchema =
config.inputSchema instanceof z.ZodType ? config.inputSchema : z.object(config.inputSchema);
const originalObject = registeredSchema instanceof z.ZodObject ? registeredSchema : undefined;
// External agents need not create a Recoup chat merely to send an email.
const sourceSchema =
name === "send_email" && originalObject
? originalObject.extend({ room_id: originalObject.shape.room_id.optional() })
: config.inputSchema;
: registeredSchema;
const object = sourceSchema instanceof z.ZodObject ? sourceSchema : undefined;
const hasOwner = !!object && "account_id" in object.shape;
const inputSchema = object
Expand Down
Loading