File size: 1,544 Bytes
de6477a
 
 
 
 
 
 
 
 
0223f19
de6477a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
from typing import List
from mcp.server.fastmcp import FastMCP

# Load Sentiment Pipeline from HuggingFace
sentiment_pipeline = pipeline("sentiment-analysis")


# Create an MCP server
mcp = FastMCP("Second-MCP-Server",host="0.0.0.0")


#### Tool ####
# Tool to do sentiment analysis for a list of sentences
@mcp.tool()
def sentiment_analyzer(sentences: List[str]) -> List[dict]:
    """
    Analyzes the sentiment of a list of input sentences using a preloaded sentiment analysis pipeline.
    Args:
        sentences (List[str]): A list of input strings to be analyzed.
    Returns:
        List[dict]: A list of dictionaries, each containing:
            - 'text' (str): The original input sentence.
            - 'sentiment' (str): The predicted sentiment label (e.g., 'POSITIVE', 'NEGATIVE', etc.).
    
    Example:
        sentiment_analyzer(["I love this!", "This is terrible."])
        [{'text': 'I love this!', 'sentiment': 'POSITIVE'}, 
         {'text': 'This is terrible.', 'sentiment': 'NEGATIVE'}]
    """
    result = sentiment_pipeline(sentences)
    sentiments = []
    for i in range(len(sentences)):
        sentiments.append({'text': sentences[i], 
                           'sentiment': result[i]['label']})
    
    return sentiments


#### Prompt ####
@mcp.prompt()
def review_code(sentences: List[str]) -> str:
    return f"Analyze the sentiment of the following sentences:\n\n{sentences}"


if __name__ == "__main__":
    # Initialize and run the server
    mcp.run(transport='sse')