Skip to main content
Glama

search_posts

Search for posts in a specific subreddit using customizable parameters such as query, sort options, and time filters, returning detailed results for matching content.

Instructions

Search for posts within a subreddit.

Args:
    params: Search parameters including subreddit name, query, and filters

Returns:
    List of matching posts with their details

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Implementation Reference

  • The main handler function that implements the search_posts tool logic, performing subreddit search via Reddit API and returning formatted PostResult objects.
    @validate_call(validate_return=True)
    def search_posts(params: SearchPostsParams) -> List[PostResult]:
        """
        Search for posts within a subreddit.
    
        Args:
            params: Search parameters including subreddit name, query, and filters
    
        Returns:
            List of matching posts with their details
        """
        client = RedditClient.get_instance()
        subreddit = client.reddit.subreddit(params.subreddit_name)
    
        posts = subreddit.search(
            query=params.query,
            sort=params.sort,
            syntax=params.syntax,
            time_filter=params.time_filter,
        )
    
        return [
            PostResult(
                id=post.id,
                title=post.title,
                url=post.url,
                score=post.score,
                num_comments=post.num_comments,
                created_utc=format_utc_timestamp(post.created_utc),
            )
            for post in posts
        ]
  • Pydantic input schema defining parameters for the search_posts tool.
    class SearchPostsParams(BaseModel):
        """Parameters for searching posts within a subreddit"""
    
        subreddit_name: str = Field(description="Name of the subreddit to search in")
        query: str = Field(description="Search query string")
        sort: Literal["relevance", "hot", "top", "new", "comments"] = Field(
            default="relevance", description="How to sort the results"
        )
        syntax: Literal["cloudsearch", "lucene", "plain"] = Field(
            default="lucene", description="Query syntax to use"
        )
        time_filter: Literal["all", "year", "month", "week", "day", "hour"] = Field(
            default="all", description="Time period to limit results to"
        )
  • Pydantic output schema for individual post results returned by search_posts.
    class PostResult(BaseModel):
        """Reddit post search result"""
    
        id: str = Field(description="Unique identifier of the post")
        title: str = Field(description="Title of the post")
        url: str = Field(description="URL of the post")
        score: int = Field(description="Number of upvotes minus downvotes")
        num_comments: int = Field(description="Number of comments on the post")
        created_utc: str = Field(description="UTC timestamp when post was created")
  • Code block that iterates over the tools list and registers each tool (including search_posts) with the FastMCP server.
    for tool in tools:
        logger.info(f"Registering tool: {tool.__name__}")
        mcp.tool()(tool)
  • Central registry list of all MCP tools, including search_posts, which is imported and used for registration in server.py.
    tools = [
        get_submission,
        get_subreddit,
        get_comments_by_submission,
        get_comment_by_id,
        search_posts,
        search_subreddits,
    ]

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GridfireAI/reddit-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server