Skip to content

Commit ce72188

Browse files
committed
completed mcp vabired basic server
1 parent fd78e75 commit ce72188

2 files changed

Lines changed: 67 additions & 50 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,31 @@ async def main():
159159
await client.cleanup()
160160

161161

162+
# Server code that fails, kept here for reference
163+
# @mcp.tool()
164+
# async def get_subreddit_info(query: str) -> Iterable[ReadResourceContents]:
165+
# async def get_subreddit_info(query: str) -> str:
166+
# """Answer the user query by accessing the subreddit_info from
167+
# get_subreddit resource. Use the reply_with_context prompt"""
168+
169+
# data = await mcp.read_resource("subreddit://info")
170+
# # making the prompt
171+
# prompt = mcp.get_prompt(
172+
# "reply_with_context",
173+
# arguments={"context": data.contents[0].text, "query": query},
174+
# )
175+
# returning the reply.
176+
# return prompt.messages[0].content.text
177+
# return data
178+
# return data[0].content
179+
180+
# @mcp.tool()
181+
# async def get_resources() -> str:
182+
# """Returns the list of resources available with you"""
183+
# resource_list = await mcp.list_resources()
184+
# res_list_str = ",".join([res.name for res in resource_list])
185+
# return f"Available resources with you are: {res_list_str}"
186+
162187
if __name__ == "__main__":
163188
import sys
164189

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

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
# "anthropic",
88
# ]
99
# ///
10-
from collections.abc import Iterable
11-
from mcp.server.fastmcp import FastMCP, Context
12-
from mcp.server.fastmcp.prompts import base
13-
from mcp.server.lowlevel.helper_types import ReadResourceContents
14-
from typing import List
10+
from mcp.server.fastmcp import FastMCP
1511
import praw
1612
from dotenv import load_dotenv
1713
import os
18-
from datetime import datetime, timedelta, timezone
1914
from anthropic import Anthropic
2015

2116
load_dotenv()
@@ -36,24 +31,6 @@
3631
mcp = FastMCP("mcp-vabired")
3732

3833

39-
@mcp.tool()
40-
def get_trending_posts(subreddit: str) -> str:
41-
"""Returns the treding hot posts in given subreddit.
42-
The topic, post body and upvotes are returned in text format"""
43-
trending_posts = ""
44-
# getting 10 posts that are in hot list
45-
subreddit = reddit.subreddit(subreddit)
46-
posts = getattr(subreddit, "hot")(limit=10)
47-
48-
for post in posts:
49-
# arranging them in the trending post variable
50-
trending_posts += f"Topic: {post.title}\n"
51-
trending_posts += f"score: {post.score}\n"
52-
trending_posts += f"url: {post.url}\n"
53-
# returns the post title, score and url in text form
54-
return trending_posts
55-
56-
5734
@mcp.prompt()
5835
def get_reply(comment: str) -> str:
5936
"""Prompt to get a suitable reply for a comment"""
@@ -66,33 +43,55 @@ def reply_with_context(context: str, query: str) -> str:
6643
return f"Review this {context} and answer the {query} precisely"
6744

6845

46+
# dynamic template resource
47+
@mcp.resource("reddit://search/{query}")
48+
def search_reddit(query: str) -> str:
49+
"""Searches the entire reddit and returns the post title, content, and score along with URL"""
50+
results = reddit.subreddit("all").search(query, sort="relevance", limit=10)
51+
posts = ""
52+
for post in results:
53+
posts += f"subreddit: {str(post.subreddit)} \n title: {post.title} \n comments: {post.num_comments} \n score: {post.score} \n url: {post.url}"
54+
return posts
55+
56+
57+
# Static Resource
6958
@mcp.resource("subreddit://info")
7059
def subreddit_info_static() -> str:
71-
"""Returns the details of SideProject & Webdev subreddit"""
72-
60+
"""Returns the details of SideProject & Webdev subreddit by reading a local file"""
7361
with open("subred_data.txt", "r") as f:
7462
name_desc_details = f.read()
7563
return name_desc_details
7664

7765

78-
# @mcp.tool()
79-
# async def get_subreddit_info(query: str) -> Iterable[ReadResourceContents]:
80-
# async def get_subreddit_info(query: str) -> str:
81-
# """Answer the user query by accessing the subreddit_info from
82-
# get_subreddit resource. Use the reply_with_context prompt"""
83-
84-
# data = await mcp.read_resource("subreddit://info")
85-
# # making the prompt
86-
# prompt = mcp.get_prompt(
87-
# "reply_with_context",
88-
# arguments={"context": data.contents[0].text, "query": query},
89-
# )
90-
# returning the reply.
91-
# return prompt.messages[0].content.text
92-
# return data
93-
# return data[0].content
66+
# Tool that uses a dynamic resource with templates
67+
@mcp.tool()
68+
async def search_for_me(query: str) -> str:
69+
"""Searches the entire reddit and returns the post title, content, and score along with URL"""
70+
resource_query = f"reddit://search/{query}"
71+
reddit_data = await mcp.read_resource(resource_query)
72+
return reddit_data[0].content
9473

9574

75+
# Tool that calls an API
76+
@mcp.tool()
77+
def get_trending_posts(subreddit: str) -> str:
78+
"""Returns the treding hot posts in given subreddit.
79+
The topic, post body and upvotes are returned in text format"""
80+
trending_posts = ""
81+
# getting 10 posts that are in hot list
82+
subreddit = reddit.subreddit(subreddit)
83+
posts = getattr(subreddit, "hot")(limit=10)
84+
85+
for post in posts:
86+
# arranging them in the trending post variable
87+
trending_posts += f"Topic: {post.title}\n"
88+
trending_posts += f"score: {post.score}\n"
89+
trending_posts += f"url: {post.url}\n"
90+
# returns the post title, score and url in text form
91+
return trending_posts
92+
93+
94+
# Tool that uses a static resource
9695
@mcp.tool()
9796
# async def get_subreddit_info(query: str) -> Iterable[ReadResourceContents]:
9897
async def get_subreddit_info(query: str) -> str:
@@ -109,14 +108,7 @@ async def get_subreddit_info(query: str) -> str:
109108
return prompt.messages[0].content.text
110109

111110

112-
# @mcp.tool()
113-
# async def get_resources() -> str:
114-
# """Returns the list of resources available with you"""
115-
# resource_list = await mcp.list_resources()
116-
# res_list_str = ",".join([res.name for res in resource_list])
117-
# return f"Available resources with you are: {res_list_str}"
118-
119-
111+
# Tool that uses LLM inside the server to get a reply.
120112
@mcp.tool()
121113
def reply_comment(comment_text: str) -> str:
122114
"""Returns a suitable polite reply for the comment"""

0 commit comments

Comments
 (0)