Skip to content

Commit fa425ea

Browse files
committed
got mcp vabired ready
1 parent d53769f commit fa425ea

5 files changed

Lines changed: 560 additions & 7 deletions

File tree

fw_ex/praw_spiked/mcp-reddit-server/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,30 @@ async def connect_to_server(self, server_script_path: str):
5959
# Need to get tools output if the server is up
6060
print("\nConnected to server with tools:", [tool.name for tool in tools])
6161

62+
# below code is used initially for testing the read_resource method
63+
# resource_test = await self.session.read_resource("subreddit://info")
64+
# print("Testing Resource in Client side:", resource_test)
65+
66+
# listing available prompts
67+
response = await self.session.list_prompts()
68+
prompts = response.prompts
69+
print("\nAvailable prompts:", [prompt.name for prompt in prompts])
70+
6271
async def process_query(self, query: str) -> str:
6372
"""Process a query using Claude and available tools"""
64-
messages = [{"role": "user", "content": query}]
65-
73+
# get the tools
6674
response = await self.session.list_tools()
75+
# get the resources
76+
avbl_data = await self.session.read_resource("subreddit://info")
77+
# use the resources in the prompt
78+
context_prompt = await self.session.get_prompt(
79+
"reply_with_context",
80+
arguments={"context": avbl_data.contents[0].text, "query": query},
81+
)
82+
query_with_context = context_prompt.messages[0].content
83+
print(query_with_context.text)
84+
# build it into the message list
85+
messages = [{"role": "user", "content": query_with_context.text}]
6786
available_tools = [
6887
{
6988
"name": tool.name,

fw_ex/praw_spiked/mcp-reddit-server/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.11"
77
dependencies = [
88
"anthropic>=0.49.0",
9+
"asyncpraw>=7.8.1",
910
"httpx>=0.28.1",
1011
"mcp[cli]>=1.6.0",
1112
"praw>=7.8.1",

fw_ex/praw_spiked/mcp-reddit-server/server.py

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
from dotenv import load_dotenv
55
import os
66
from datetime import datetime, timedelta, timezone
7+
from anthropic import Anthropic
78

89
load_dotenv()
10+
anthropic = Anthropic()
911

10-
# Creating a Reddit authorized instance
12+
# Creating a sync Reddit authorized instance
1113

1214
reddit = praw.Reddit(
1315
client_id=os.environ["AID"],
@@ -17,16 +19,17 @@
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()
2528
def 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+
42114
if __name__ == "__main__":
43-
print("Server Starts")
44115
mcp.run()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
subreddit:SideProject
2+
Name: SideProject
3+
Description:
4+
Welcome to rSideProject, a subreddit for sharing and receiving constructive feedback on side projects
5+
This is also a subreddit to get motivated and inspired to work on new projects, so please submit links to projects you find interesting.
6+
Submission Format
7+
When submitting a link to a project or startup, please use this format: [Project name] - [Short description]. For example, "Reddit - A web
8+
site for sharing and discussing links.
9+
Related Communities: r/startups, r/startups, r/Entrepreneur, r/Entrepreneur, r/design_critiques,r/learnprogramming,
10+
r/learnprogramming,r/coding,r/coding, r/gamer r/gamedev
11+
Tips: [Finding time for your side project](https://news.ycombinator.com/item?id=6107815)
12+
[Promoting your side project](https:/news.ycombinator.com/item?id=341288)
13+
[Resource list for startups]\n(https://news.ycombinator.comitem?id=5235137)
14+
[Places to post your side project]\n(https://github.com/mmccaff/PlacesToPostYourStartup)
15+
Subscribers: 336068
16+
Created: 1358396099.0
17+
spoilers_enabled: False
18+
over_18: False
19+
20+
reddit:webdev
21+
Name: webdev
22+
Description: Beginner question?
23+
[Try the FAQ first!](/r/webdev/wiki/faq) or the [WebDev Resources Post](/r/webdev/comments/1v7en8/webdev_resources/) then post
24+
in the Beginner Questions thread
25+
[Help fill out the wiki](/r/webdev/wiki/index)
26+
Posting Guidelines: No vague product support questions (like "why is this plugin not working" or "how do I set up X").
27+
For vague product support questions, please use communities relevant to that product for best results.
28+
Specific issues that follow rule 6 are allowed.\n\n2. Do not post memes, screenshots of bad design, or jokes.
29+
Check out /r/ProgrammerHumor/ for this type of content.\n\n3. Read and follow [reddiquette](http://www.reddit.com/wiki/reddiquette)
30+
no excessive self-promotion. Please refer to the Reddit 9:1 rule when considering posting self promoting materials
31+
4. We do not allow any commercial promotion or solicitation. Violations can result in a ban.
32+
5. Sharing your project, portfolio, or any other content that you want to either show off or request feedback on is limited to Showoff Saturday.
33+
If you post such content on any other day, it will be removed.
34+
6. If you are asking for assistance on a problem, you are required to provide
35+
Context of the problem: Research you have completed prior to requesting assistance
36+
Problem you are attempting to solve with high specificity\n\n7. General open ended career and getting started posts are only
37+
allowed in the pinned monthly getting started/careers thread. Specific assistance questions are allowed so long as
38+
they follow the required assistance post guidelines. \n\n Questions in violation of this rule will be removed or locked.
39+
Related Subreddits: r/web_design, r/WebDevBuddies, r/devops, r/javascript, r/learnjavascript (any JavaScript questions)
40+
r/webhosting (web hosting questions), r/WebdevTutorials, r/forhire, r/freelance (discussion related to freelancing)
41+
r/programmerhumor, r/cryptodevs
42+
(https://discord.gg/H9Jkc7p)
43+
Subscribers: 3005923
44+
Created: 1232857355.0
45+
spoilers_enabled: True
46+
over_18: False

0 commit comments

Comments
 (0)