An open API service indexing awesome lists of open source software.

https://github.com/amirhosseinazami1373/mcp-sentiment-agent

This project is my first attempt in making a MCP based agent for simple task of sentimemnt analysis.
https://github.com/amirhosseinazami1373/mcp-sentiment-agent

deepseek-r1 llm mcp sentiment-analysis smolagents

Last synced: 3 months ago
JSON representation

This project is my first attempt in making a MCP based agent for simple task of sentimemnt analysis.

Awesome Lists containing this project

README

          

# ๐Ÿง  MCP Sentiment Agent

A **fully local AI agent** powered by `smolagents`, running a local LLM (DeepSeek-R1 via Ollama), and calling Python tools through the [Machine-Centric Protocol (MCP)](https://huggingface.co/learn/mcp-course/en/). This agent analyzes sentiment from natural language inputs and returns structured results, all with zero cloud dependencies.

---

## ๐Ÿš€ Features

- ๐Ÿค– **LLM-powered agent** using `ToolCallingAgent` from [smolagents](https://github.com/huggingface/smolagents)
- ๐Ÿ“Š **Sentiment analysis tool** exposed through Gradio with MCP server
- ๐Ÿ’ฌ **TextBlob** for analyzing polarity and subjectivity
- โšก **DeepSeek-R1 8B** running locally via [Ollama](https://ollama.com)
- ๐Ÿ”Œ **Fully local**: no OpenAI, no external API calls
- ๐Ÿ› ๏ธ **Modular design**: easily add new tools to your agent

---

## ๐Ÿ“ Project Structure

```
mcp-sentiment-agent/
โ”œโ”€โ”€ app.py # Gradio MCP tool server exposing `sentiment()`
โ”œโ”€โ”€ tiny_agent.py # Tool-calling agent using local DeepSeek-R1
โ”œโ”€โ”€ requirements.txt # Python dependencies
โ”œโ”€โ”€ README.md # This file
โ””โ”€โ”€ .venv/ # (Optional) Python virtual environment
```

---

## ๐Ÿงช How It Works

### ๐Ÿงพ User prompt:

> "Analyze the sentiment of: 'I love this project!'"

### ๐Ÿง  Reasoning steps:

1. The agent sends the prompt to the LLM.
2. The LLM decides to use the `sentiment()` tool.
3. `smolagents` calls the MCP tool server running in `app.py`.
4. `TextBlob` analyzes polarity and subjectivity.
5. The agent returns a final, summarized response.

---

## โš™๏ธ How to Run This Project Locally

### ๐Ÿ”ง 1. Clone the repo

```bash
git clone https://github.com/amirhosseinazami1373/mcp-sentiment-agent.git
cd mcp-sentiment-agent
```

### ๐Ÿ 2. Set up Python environment

```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```

### ๐Ÿ“ฆ 3. Install NLTK corpora for TextBlob (first time only)

```bash
python -m textblob.download_corpora
```

---

### ๐Ÿง  4. Start Ollama and pull the model

If not already done:

```bash
ollama pull deepseek-r1:8b
```

Start the model:

```bash
ollama run deepseek-r1:8b
```

### ๐ŸŒ 5. Start the tool server (in another terminal)

```bash
python app.py
```

This exposes your tool at:
`http://localhost:7860/gradio_api/mcp/schema`

### ๐Ÿ” 6. Run the agent

```bash
python tiny_agent.py
```

---

## ๐Ÿงฉ Output Example

```
Calling tool: 'sentiment' with arguments: {'text': "'I love this!'"}
Observations: Sentiment(polarity=0.625, subjectivity=0.6)

Final answer: The sentiment of the text is positive with a polarity score of 0.625 and a high degree of subjectivity.
```

---

## ๐Ÿง  Technologies Used

| Component | Description |
|------------------|-------------------------------------------------|
| `smolagents` | Lightweight multi-step tool-calling framework |
| `TextBlob` | Sentiment analysis tool |
| `Gradio` + `MCP` | Exposes Python functions as structured tools |
| `Ollama` | Runs local LLMs (e.g., DeepSeek-R1, LLaMA3) |
| `LiteLLM` | Unified LLM client used by smolagents |

---

## ๐Ÿ”„ Extend the Agent

Want to build more powerful agents?

- Add tools to `app.py` (e.g., math, web search, summarizer)
- Use different local models (e.g., `llama3`, `mistral`)
- Deploy the tool server on Hugging Face Spaces
- Create an interactive chat UI with Gradio

---

## ๐Ÿ“š References

- [Hugging Face MCP Course](https://huggingface.co/learn/mcp-course/en/)
- [smolagents GitHub](https://github.com/huggingface/smolagents)
- [Ollama](https://ollama.com/)
- [TextBlob Documentation](https://textblob.readthedocs.io/en/dev/)

---