44from dotenv import load_dotenv
55import os
66from datetime import datetime , timedelta , timezone
7+ from anthropic import Anthropic
78
89load_dotenv ()
10+ anthropic = Anthropic ()
911
10- # Creating a Reddit authorized instance
12+ # Creating a sync Reddit authorized instance
1113
1214reddit = praw .Reddit (
1315 client_id = os .environ ["AID" ],
1719 password = os .environ ["APASS" ],
1820)
1921
22+
2023# Creating a MCP Server
21- mcp = FastMCP ("vabired" )
24+ mcp = FastMCP ("mcp- vabired" )
2225
2326
2427@mcp .tool ()
2528def get_trending_posts (subreddit : str ) -> str :
2629 """Returns the treding hot posts in given subreddit.
2730 The topic, post body and upvotes are returned in text format"""
2831 trending_posts = ""
29- # getting 5 posts that are in hot list
32+ # getting 10 posts that are in hot list
3033 subreddit = reddit .subreddit (subreddit )
3134 posts = getattr (subreddit , "hot" )(limit = 10 )
3235
@@ -35,10 +38,78 @@ def get_trending_posts(subreddit: str) -> str:
3538 trending_posts += f"Topic: { post .title } \n "
3639 trending_posts += f"score: { post .score } \n "
3740 trending_posts += f"url: { post .url } \n "
38-
41+ # returns the post title, score and url in text form
3942 return trending_posts
4043
4144
45+ @mcp .prompt ()
46+ def get_reply (comment : str ) -> str :
47+ """Prompt to get a suitable reply for a comment"""
48+ return f"Review this { comment } and provide a suitable polite response"
49+
50+
51+ @mcp .prompt ()
52+ def reply_with_context (context : str , query : str ) -> str :
53+ """Prompt to get the LLM to use the context and answer a query"""
54+ return f"Review this { context } and answer the { query } precisely"
55+
56+
57+ @mcp .resource ("subreddit://info" )
58+ def subreddit_info_static () -> str :
59+ """Returns the details of SideProject & Webdev subreddit"""
60+
61+ with open ("subred_data.txt" , "r" ) as f :
62+ name_desc_details = f .read ()
63+ return name_desc_details
64+
65+
66+ # @mcp.tool()
67+ # async def get_subreddit_info(query: str, ctx: Context) -> str:
68+ # """Answer the user query by accessing the subreddit_info from
69+ # get_subreddit resource. Use the reply_with_context prompt"""
70+
71+ # info = await ctx.read_resource("subreddit_info")
72+ # # making the prompt
73+ # prompt = mcp.get_prompt(
74+ # "reply_with_context", arguments={"context": info, "query": query}
75+ # )
76+ # # Building messages
77+ # messages = [
78+ # {
79+ # "role": "user",
80+ # "content": prompt,
81+ # }
82+ # ]
83+ # # Calling the model
84+ # response = anthropic.messages.create(
85+ # model="claude-3-5-haiku-20241022",
86+ # max_tokens=500,
87+ # messages=messages,
88+ # )
89+ # # returning the reply.
90+ # return response.content[0].text
91+
92+
93+ @mcp .tool ()
94+ def reply_comment (comment_text : str ) -> str :
95+ """Returns a suitable polite reply for the comment"""
96+ # Building the prompt using get_reply
97+ prompt = mcp .get_prompt ("get_reply" , arguments = {"comment" : comment_text })
98+ messages = [
99+ {
100+ "role" : "user" ,
101+ "content" : prompt ,
102+ },
103+ ]
104+ # calling the model with the prompt
105+ response = anthropic .messages .create (
106+ model = "claude-3-5-haiku-20241022" ,
107+ max_tokens = 500 ,
108+ messages = messages ,
109+ )
110+
111+ return response .content [0 ].text
112+
113+
42114if __name__ == "__main__" :
43- print ("Server Starts" )
44115 mcp .run ()
0 commit comments