get_subreddit_new_posts
Retrieve recent posts from any Reddit community by specifying the subreddit name and desired post count.
Instructions
Get new posts from a specific subreddit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| subreddit_name | Yes | Name of the subreddit (e.g. 'Python', 'news') | |
| limit | No | Number of posts to return (default: 10) |
Implementation Reference
- src/mcp_server_reddit/server.py:152-157 (handler)The handler function that fetches the newest posts from the specified subreddit using the redditwarp client. It iterates over new posts, builds Post model instances using the _build_post helper, and returns a list of Post objects.def get_subreddit_new_posts(self, subreddit_name: str, limit: int = 10) -> list[Post]: """Get new posts from a specific subreddit""" posts = [] for subm in self.client.p.subreddit.pull.new(subreddit_name, limit): posts.append(self._build_post(subm)) return posts
- Pydantic model defining the structure of a Post returned by the tool, including fields like id, title, author, score, subreddit, url, timestamp, comment count, post type, and content.class Post(BaseModel): id: str title: str author: str score: int subreddit: str url: str created_at: str comment_count: int post_type: PostType content: str | None
- JSON schema defining the tool's input parameters: subreddit_name (required string) and optional limit (integer 1-100, default 10). Used for MCP tool validation.inputSchema={ "type": "object", "properties": { "subreddit_name": { "type": "string", "description": "Name of the subreddit (e.g. 'Python', 'news')", }, "limit": { "type": "integer", "description": "Number of posts to return (default: 10)", "default": 10, "minimum": 1, "maximum": 100 } }, "required": ["subreddit_name"]
- src/mcp_server_reddit/server.py:253-273 (registration)Registers the tool with MCP server via list_tools(), specifying name, description, and input schema.Tool( name=RedditTools.GET_SUBREDDIT_NEW_POSTS.value, description="Get new posts from a specific subreddit", inputSchema={ "type": "object", "properties": { "subreddit_name": { "type": "string", "description": "Name of the subreddit (e.g. 'Python', 'news')", }, "limit": { "type": "integer", "description": "Number of posts to return (default: 10)", "default": 10, "minimum": 1, "maximum": 100 } }, "required": ["subreddit_name"] } ),
- src/mcp_server_reddit/server.py:397-402 (registration)Dispatches tool calls in the call_tool handler by matching the tool name and invoking the corresponding RedditServer method with parsed arguments.case RedditTools.GET_SUBREDDIT_NEW_POSTS.value: subreddit_name = arguments.get("subreddit_name") if not subreddit_name: raise ValueError("Missing required argument: subreddit_name") limit = arguments.get("limit", 10) result = reddit_server.get_subreddit_new_posts(subreddit_name, limit)
- Helper function used by the handler to construct Post models from raw redditwarp submission objects, calling other helpers for post_type and content.def _build_post(self, submission) -> Post: """Helper method to build Post object from submission""" return Post( id=submission.id36, title=submission.title, author=submission.author_display_name or '[deleted]', score=submission.score, subreddit=submission.subreddit.name, url=submission.permalink, created_at=submission.created_at.astimezone().isoformat(), comment_count=submission.comment_count, post_type=self._get_post_type(submission), content=self._get_post_content(submission) )