reddit_post
Scrapes a specific Reddit post by URL to extract its content and metadata.
Instructions
Scrape a specific Reddit post
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | reddit post URL (eg. https://www.reddit.com/r/hometheater/comments/1jz9xk5/lg_ubk90_only_works_when_plugged_into_tv_via) |
Implementation Reference
- The RedditPostTool class that contains the handler logic: it registers a tool named 'reddit_post', accepts a Reddit post URL, calls the scraper API with target 'reddit_post', transforms the response by removing high-character-count fields (author_flair_richtext), and returns the result as text.
export class RedditPostTool extends Tool { toolset = TOOLSET.SOCIAL_MEDIA; private static FIELDS_WITH_HIGH_CHAR_COUNT = ['author_flair_richtext']; transformResponse = ({ data }: { data: object }) => { for (const fieldToRemove of RedditPostTool.FIELDS_WITH_HIGH_CHAR_COUNT) { data = removeKeyFromNestedObject({ obj: data, keyToRemove: fieldToRemove }); } return { data: JSON.stringify(data) }; }; register = ({ server, sapiClient, auth }: ToolRegistrationArgs) => { server.registerTool( 'reddit_post', { description: 'Scrape a specific Reddit post', inputSchema: { url: z .string() .describe( 'reddit post URL (eg. https://www.reddit.com/r/hometheater/comments/1jz9xk5/lg_ubk90_only_works_when_plugged_into_tv_via)' ), }, annotations: { readOnlyHint: true, openWorldHint: true, }, }, async (scrapingParams: ScrapingMCPParams, extra: ProgressExtra) => { const params = { ...scrapingParams, target: SCRAPER_API_TARGETS.REDDIT_POST, } satisfies ScraperAPIParams; const { data } = await sapiClient.scrape<object>({ auth, scrapingParams: params, extra }); const { data: text } = this.transformResponse({ data }); return { content: [ { type: 'text', text, }, ], }; } ); }; } - Input schema for the reddit_post tool: requires a 'url' string describing the Reddit post URL, with readOnlyHint and openWorldHint annotations.
{ description: 'Scrape a specific Reddit post', inputSchema: { url: z .string() .describe( 'reddit post URL (eg. https://www.reddit.com/r/hometheater/comments/1jz9xk5/lg_ubk90_only_works_when_plugged_into_tv_via)' ), }, annotations: { readOnlyHint: true, openWorldHint: true, }, }, - src/server/sapi-base-server.ts:113-117 (registration)Registration in the base server: RedditPostTool is instantiated in the allTools array (line 91) and registered via registerAllTools (line 113-117) or registerTools based on toolsets.
registerAllTools() { for (const tool of ScraperAPIBaseServer.allTools) { tool.register({ server: this.server, sapiClient: this.sapiClient, auth: this.auth }); } } - src/server/sapi-base-server.ts:99-117 (registration)Registration logic: if no toolsets specified, all tools including RedditPostTool are registered; otherwise tools are filtered by toolset (SOCIAL_MEDIA for RedditPostTool).
registerTools({ toolsets }: { toolsets: TOOLSET[] }) { if (toolsets.length === 0) { this.registerAllTools(); return; } for (const toolset of toolsets) { const tools = ScraperAPIBaseServer.allTools.filter(tool => tool.toolset === toolset); for (const tool of tools) { tool.register({ server: this.server, sapiClient: this.sapiClient, auth: this.auth }); } } } registerAllTools() { for (const tool of ScraperAPIBaseServer.allTools) { tool.register({ server: this.server, sapiClient: this.sapiClient, auth: this.auth }); } } - src/utils.ts:17-44 (helper)The removeKeyFromNestedObject utility used by RedditPostTool.transformResponse to strip the 'author_flair_richtext' field from the response data recursively.
export const removeKeyFromNestedObject = ({ obj, keyToRemove, }: { obj: object; keyToRemove: string; }): object => { if (typeof obj !== 'object' || obj === null) { return obj; } if (Array.isArray(obj)) { return obj.map(item => removeKeyFromNestedObject({ obj: item, keyToRemove })); } const record = obj as Record<string, unknown>; const newObj: Record<string, unknown> = {}; for (const key in record) { if (key === keyToRemove) { continue; } newObj[key] = removeKeyFromNestedObject({ obj: record[key] as object, keyToRemove }); } return newObj; };