File size: 2,480 Bytes
6d8aa95
 
4facc97
107cced
 
 
 
 
0ae4701
4facc97
 
107cced
 
 
 
 
 
 
 
 
4facc97
107cced
 
 
 
 
 
 
 
 
 
0ae4701
 
 
 
 
 
4facc97
 
 
0ae4701
4facc97
 
 
0ae4701
4facc97
 
0ae4701
4facc97
 
 
107cced
6d8aa95
a28b1b4
 
 
 
 
 
 
 
 
 
 
4facc97
 
 
 
 
a28b1b4
4facc97
107cced
4facc97
a28b1b4
 
4facc97
 
 
0ae4701
 
107cced
6d8aa95
107cced
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''RSS MCP server demonstration client app.'''

import os
import logging
from pathlib import Path
from logging.handlers import RotatingFileHandler

import gradio as gr
import client.interface as interface
from client.mcp_client import MCPClientWrapper
from client.anthropic_bridge import AnthropicBridge

# Make sure log directory exists
Path('logs').mkdir(parents=True, exist_ok=True)

# Set-up logger
logger = logging.getLogger()

logging.basicConfig(
    handlers=[RotatingFileHandler(
        'logs/rss_client.log',
        maxBytes=100000,
        backupCount=10,
        mode='w'
    )],
    level=logging.INFO,
    format='%(levelname)s - %(name)s - %(message)s'
)

logger = logging.getLogger(__name__)

RSS_CLIENT = MCPClientWrapper(
    'https://agents-mcp-hackathon-rss-mcp-server.hf.space/gradio_api/mcp/sse'
)

BRIDGE = AnthropicBridge(
    RSS_CLIENT,
    api_key=os.environ['ANTHROPIC_API_KEY']
)

async def send_message(message: str, chat_history: list) -> str:
    '''Submits user message to agent'''

    function_logger = logging.getLogger(__name__ + '.submit_input')
    function_logger.info('Submitting user message: %s', message)

    chat_history.append({"role": "user", "content": message})
    chat_history = await interface.agent_input(BRIDGE, chat_history)

    return '', chat_history


with gr.Blocks(title='MCP RSS client') as demo:
    gr.Markdown('# Agentic RSS reader')
    gr.Markdown("""
        Uses sister Space 
        [RSS feed reader](https://huggingface.co/spaces/Agents-MCP-Hackathon/rss-mcp-server) 
        via MCP. Click 'Connect to MCP server' to get started. Check out the
        [main project repo on GitHub](https://github.com/gperdrizet/MCP-hackathon/tree/main).
        Both Spaces by [George Perdrizet](https://www.linkedin.com/in/gperdrizet/).
    """)

    connect_btn = gr.Button('Connect to MCP server')
    status = gr.Textbox(label='MCP server tool dump', interactive=False, lines=4)

    chatbot = gr.Chatbot(
        value=[],
        height=500,
        type='messages',
        show_copy_button=True
    )

    msg = gr.Textbox(
        label='Ask about content or articles on a site or platform',
        placeholder='Is there anything new on Hacker News?',
        scale=4
    )

    connect_btn.click(RSS_CLIENT.list_tools, outputs=status) # pylint: disable=no-member
    msg.submit(send_message, [msg, chatbot], [msg, chatbot]) # pylint: disable=no-member

if __name__ == '__main__':

    demo.launch(debug=True)