Skip to main content
Glama

analyze_sentiment

Analyze text sentiment for financial news using FinBERT to support trading decisions and portfolio management.

Instructions

Analyzes the sentiment of a given text using FinBERT on Modal (via Public Endpoint). Args: text: Text to analyze (e.g., news headline, article). Returns: Dictionary with polarity, confidence, and classification.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes

Implementation Reference

  • The core handler function for the 'analyze_sentiment' tool. It sends the input text to a FinBERT model hosted on Modal via HTTP POST request and processes the response to return sentiment polarity (-1 to 1), classification (POSITIVE/NEGATIVE/NEUTRAL), and confidence score.
    def analyze_sentiment(text: str) -> Dict[str, Any]: """ Analyzes the sentiment of a given text using FinBERT on Modal (via Public Endpoint). Args: text: Text to analyze (e.g., news headline, article). Returns: Dictionary with polarity, confidence, and classification. """ # Try Modal first try: # Check if URL is configured if "replace-me" in MODAL_ENDPOINT_URL: raise ValueError("Modal URL not configured") response = requests.post(MODAL_ENDPOINT_URL, json={"text": text}, timeout=15) response.raise_for_status() result = response.json() # FinBERT returns {'label': 'positive'/'negative'/'neutral', 'score': float} label = result['label'].upper() score = result['score'] # Map to polarity-like score for compatibility (-1 to 1) if label == "POSITIVE": polarity = score elif label == "NEGATIVE": polarity = -score else: polarity = 0.0 return { "text": text[:100] + "..." if len(text) > 100 else text, "polarity": round(polarity, 3), "subjectivity": 0.0, # FinBERT doesn't give subjectivity "classification": label, "model": "FinBERT (Modal Public)" } except Exception as e: logger.error(f"Modal FinBERT failed: {e}") return {"error": f"Error analyzing sentiment: {str(e)}"}
  • server.py:405-408 (registration)
    Registers the analyze_sentiment function as an MCP tool using the register_tools helper, which applies @mcp.tool() decorator. Part of the 'News & Sentiment' category registration.
    register_tools( [get_news, analyze_sentiment, get_symbol_sentiment], "News & Sentiment" )
  • Supporting tool 'get_symbol_sentiment' that uses analyze_sentiment on recent news headlines for a symbol to compute aggregate sentiment score.
    def get_symbol_sentiment(symbol: str) -> str: """ Fetches recent news for a symbol and calculates aggregate sentiment. Args: symbol: Ticker symbol. Returns: Aggregate sentiment analysis of recent news. """ try: ticker = yf.Ticker(symbol) news = ticker.news[:10] # Last 10 articles if not news: return f"No news found for {symbol}" sentiments = [] model_used = "Unknown" for item in news: title = item.get("title", "") if title: result = analyze_sentiment(title) if "polarity" in result: sentiments.append(result["polarity"]) model_used = result.get("model", "Unknown") if not sentiments: return f"No valid news titles for {symbol}" avg_polarity = sum(sentiments) / len(sentiments) if avg_polarity > 0.1: classification = "BULLISH" elif avg_polarity < -0.1: classification = "BEARISH" else: classification = "NEUTRAL" return (f"Sentiment Analysis for {symbol} ({len(sentiments)} articles):\n" f"Average Polarity: {avg_polarity:.3f}\n" f"Market Sentiment: {classification}\n" f"Model: {model_used}") except Exception as e: return f"Error analyzing sentiment for {symbol}: {str(e)}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/N-lia/MonteWalk'

If you have feedback or need assistance with the MCP directory API, please join our Discord server