Skip to main content
Glama

Reddit MCP Tool

reddit_client.cpython-310.pyc4.93 kB
o 2f�h��@s>dZddlmZmZmZddlZddlmZGdd�d�ZdS)zReddit API client wrapper.�)�Any�Dict�ListN�)� RedditConfigc@s�eZdZdZdefdd�Zedd��Z   dd ed ed e d edede e ee ff dd�Z dede ee ffdd�Zd ede ee ffdd�Zdd ed e de e ee ffdd�Z   dd ed e d edede e ee ff dd�ZdS)� RedditClientz!Reddit API client for MCP server.�configcCs||_d|_dS)z6Initialize Reddit client with read-only configuration.N)r�_reddit)�selfr�r �:/Users/lige/Desktop/reddit-mcp/reddit_mcp/reddit_client.py�__init__ s zRedditClient.__init__cCs.|jdurtj|jj|jj|jjd�|_|jS)z*Lazy initialize AsyncPRAW Reddit instance.N)� client_id� client_secret� user_agent)r � asyncprawZRedditrrrr)r r r r �reddits �zRedditClient.reddit� � relevance�all�subreddit_name�query�limit�sort� time_filter�returnc �s��z_|j�|�IdH}g}|j||||d�}|2zD3dHW} | j| j| jr*t| j�nd| j| j| j d| j ��| j | j t | j�dkrJ| jdd�dn| j| j| jt| j�d� } |�| �q6|WStyx} z td|�d t| �����d} ~ ww) z Search for posts in a subreddit.N�rrr� [deleted]�https://reddit.com���...� �id�title�author�score� upvote_ratio�url� permalink� created_utc� num_comments�selftext�is_self�domain� subredditzError searching posts in r/�: �rr.�searchr"r#r$�strr%r&r'r(r)r*�lenr+r,r-�append� Exception) r rrrrrr.�posts�search_results� submission� post_data�er r r � search_postss@� � $� ���zRedditClient.search_posts�post_idc �s��zY|jj|d�IdH}id|j�d|j�d|jrt|j�nd�d|j�d|j�d |j�d d |j ���d |j �d |j �d|j �d|j �d|j�dt|j��d|j�d|j�d|j�WStyr}z td|�dt|�����d}~ww)z/Get detailed information about a specific post.)r"Nr"r#r$rr%r&r'r(rr)r*r+r,r-r.Z flair_text�locked�stickiedzError getting post details for r/)rr8r"r#r$r2r%r&r'r(r)r*r+r,r-r.Zlink_flair_textr=r>r5)r r<r8r:r r r �get_post_detailsIsP������� �� � � � � ������zRedditClient.get_post_detailsc �s��z2|j�|�IdH}|j|jt|j�dkr|jdd�dn|j|j|j|j|j |j d|j��d� WSt yK}z t d|�dt |�����d}~ww)z"Get information about a subreddit.Nrr zhttps://reddit.com/r/) �namer#� description� subscribers�active_user_countr)�over18�public_descriptionr'z#Error getting subreddit info for r/r/) rr.� display_namer#r3rArBrCr)rDrEr5r2)r rr.r:r r r �get_subreddit_infohs"�$ � ��zRedditClient.get_subreddit_infoc�s��zZ|j�|�IdH}g}|j|d�2zD3dHW}|j|j|jr%t|j�nd|j|j|j d|j ��|j |j t |j�dkrE|jdd�dn|j|j|jt|j�d� }|�|�q6|WStys}z td|�d t|�����d}~ww) zGet hot posts from a subreddit.N)rrr��r r!zError getting hot posts from r/r/)rr.�hotr"r#r$r2r%r&r'r(r)r*r3r+r,r-r4r5)r rrr.r6r8r9r:r r r � get_hot_posts|s4� $� ���zRedditClient.get_hot_postsc �s��z_|j�d�IdH}g}|j||||d�}|2zD3dHW}|j|j|jr*t|j�nd|j|j|j d|j ��|j |j t |j�dkrJ|jdd�dn|j|j|jt|j�d� } |�| �q6|WStyx} z td |�d t| �����d} ~ ww) z9Search for posts across all of Reddit (site-wide search).rNrrrrr r!z&Error searching all Reddit for query 'z': r0) r rrrrZ all_subredditr6r7r8r9r:r r r �search_all_reddit�s@�� $� ���zRedditClient.search_all_redditN)rrr)r)�__name__� __module__� __qualname__�__doc__rr �propertyrr2�intrrrr;r?rGrJrKr r r r r sL ������ �,$ ������r) rO�typingrrrrrrrr r r r �<module>s  

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/GeLi2001/reddit-mcp'

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