Skip to main content
Glama
crazyrabbitLTC

Twitter MCP Server

analyzeSentiment

Analyze emotional tone in tweets matching a search query to gauge public sentiment on topics, with options for sample size and keyword frequency analysis.

Instructions

Perform sentiment analysis on tweets matching a query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSearch query for sentiment analysis
sampleSizeNoNumber of tweets to analyze (default: 50)
includeKeywordsNoInclude keyword frequency analysis (default: true)

Implementation Reference

  • The core handler function for the analyzeSentiment tool. Searches for tweets using the query, applies keyword-based sentiment scoring with predefined positive/negative emoji-inclusive keywords, computes sentiment distribution, overall sentiment, confidence, keyword analysis, and returns formatted analytics.
    export const handleAnalyzeSentiment: SocialDataHandler<SentimentAnalysisArgs> = async (
        _client: any,
        { query, sampleSize = 50, includeKeywords = true }: SentimentAnalysisArgs
    ) => {
        try {
            const socialClient = getSocialDataClient();
            
            if (!socialClient) {
                return createMissingApiKeyResponse('Sentiment Analysis');
            }
            
            const result = await socialClient.searchTweets({
                query,
                maxResults: sampleSize
            });
    
            const tweets = result.data || [];
            
            if (tweets.length === 0) {
                return createSocialDataResponse(`No tweets found for sentiment analysis: ${query}`);
            }
    
            // Simple sentiment analysis based on keywords
            const positiveKeywords = ['good', 'great', 'awesome', 'love', 'amazing', 'excellent', 'perfect', 'happy', 'wonderful', '❤️', '😊', '👍', '🎉'];
            const negativeKeywords = ['bad', 'terrible', 'awful', 'hate', 'horrible', 'worst', 'disgusting', 'angry', 'sad', '😡', '😢', '👎', '💔'];
            
            let positive = 0, negative = 0, neutral = 0;
            const sentimentBreakdown = tweets.map((tweet: any) => {
                const text = tweet.text?.toLowerCase() || '';
                const positiveCount = positiveKeywords.filter(word => text.includes(word)).length;
                const negativeCount = negativeKeywords.filter(word => text.includes(word)).length;
                
                let sentiment: string;
                if (positiveCount > negativeCount) {
                    sentiment = 'positive';
                    positive++;
                } else if (negativeCount > positiveCount) {
                    sentiment = 'negative';
                    negative++;
                } else {
                    sentiment = 'neutral';
                    neutral++;
                }
                
                return {
                    tweet_id: tweet.id_str,
                    text: tweet.text?.substring(0, 150),
                    sentiment,
                    confidence: Math.max(positiveCount, negativeCount) > 0 ? 'medium' : 'low',
                    engagement: (tweet.favorite_count || 0) + (tweet.retweet_count || 0)
                };
            });
    
            let keywordAnalysis = {};
            if (includeKeywords) {
                const allText = tweets.map((t: any) => t.text).join(' ').toLowerCase();
                const words = allText.match(/\b\w+\b/g) || [];
                const wordFreq = words.reduce((freq: any, word: string) => {
                    if (word.length > 3) {
                        freq[word] = (freq[word] || 0) + 1;
                    }
                    return freq;
                }, {});
                
                keywordAnalysis = {
                    top_keywords: Object.entries(wordFreq)
                        .sort(([,a], [,b]) => (b as number) - (a as number))
                        .slice(0, 10)
                        .map(([word, count]) => ({ word, count })),
                    positive_indicators: positiveKeywords.filter(word => allText.includes(word)),
                    negative_indicators: negativeKeywords.filter(word => allText.includes(word))
                };
            }
    
            const sentimentAnalysis = {
                query,
                sample_size: tweets.length,
                analysis_date: new Date().toISOString(),
                sentiment_distribution: {
                    positive: {
                        count: positive,
                        percentage: Math.round((positive / tweets.length) * 100)
                    },
                    negative: {
                        count: negative,
                        percentage: Math.round((negative / tweets.length) * 100)
                    },
                    neutral: {
                        count: neutral,
                        percentage: Math.round((neutral / tweets.length) * 100)
                    }
                },
                overall_sentiment: positive > negative ? 'Positive' : negative > positive ? 'Negative' : 'Neutral',
                confidence_level: tweets.length > 30 ? 'High' : tweets.length > 10 ? 'Medium' : 'Low',
                keyword_analysis: keywordAnalysis,
                sample_tweets: {
                    most_positive: sentimentBreakdown.filter(t => t.sentiment === 'positive')
                        .sort((a, b) => b.engagement - a.engagement)[0],
                    most_negative: sentimentBreakdown.filter(t => t.sentiment === 'negative')
                        .sort((a, b) => b.engagement - a.engagement)[0]
                }
            };
    
            return createSocialDataResponse(
                formatAnalytics(sentimentAnalysis, `Sentiment Analysis: "${query}"`)
            );
        } catch (error) {
            throw new Error(formatSocialDataError(error as Error, 'sentiment analysis'));
        }
    };
  • Input schema definition for the analyzeSentiment tool, specifying query as required, optional sampleSize (10-200), and includeKeywords boolean.
    analyzeSentiment: {
        description: 'Perform sentiment analysis on tweets matching a query',
        inputSchema: {
            type: 'object',
            properties: {
                query: {
                    type: 'string',
                    description: 'Search query for sentiment analysis'
                },
                sampleSize: {
                    type: 'number',
                    description: 'Number of tweets to analyze (default: 50)',
                    minimum: 10,
                    maximum: 200
                },
                includeKeywords: {
                    type: 'boolean',
                    description: 'Include keyword frequency analysis (default: true)'
                }
            },
            required: ['query']
        }
    },
  • src/index.ts:494-497 (registration)
    Dispatches tool calls to the handleAnalyzeSentiment handler in the main MCP server request handler switch statement.
    case 'analyzeSentiment': {
        const args = request.params.arguments as any;
        response = await handleAnalyzeSentiment(client, args);
        break;
  • TypeScript interface defining the arguments for the sentiment analysis handler, matching the tool schema.
    interface SentimentAnalysisArgs {
        query: string;
        sampleSize?: number;
        includeKeywords?: boolean;
    }
  • src/index.ts:81-82 (registration)
    Imports the handleAnalyzeSentiment function (likely re-exported from handlers/socialdata/index.js) for use in the main server.
    handleAnalyzeSentiment,
    handleTrackVirality
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions sentiment analysis but fails to describe key traits like rate limits, authentication needs, output format, or whether it's a read-only or mutative operation. This leaves significant gaps for an agent to understand how to invoke it effectively.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without any unnecessary words or fluff. It is appropriately sized and front-loaded, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of sentiment analysis, lack of annotations, and no output schema, the description is incomplete. It doesn't explain what the output looks like (e.g., sentiment scores, aggregated results), behavioral constraints, or how it differs from similar tools, leaving the agent with insufficient context for effective use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters (query, sampleSize, includeKeywords) with descriptions and constraints. The description adds no additional meaning beyond what the schema provides, such as examples or usage notes, resulting in a baseline score of 3.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('perform sentiment analysis') and target resource ('tweets matching a query'), providing a specific verb+resource combination. However, it doesn't distinguish this tool from sibling tools like 'searchTweets' or 'advancedTweetSearch' that might also involve queries, missing explicit differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives such as 'searchTweets' or 'advancedTweetSearch', nor does it mention any prerequisites or exclusions. Usage is implied by the purpose but lacks explicit context for selection among siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/crazyrabbitLTC/mcp-twitter-server'

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