A sophisticated LLM chatbot that uses Ollama for language model inference and MCP (Model Context Protocol) tools for enhanced functionality. All communication between components uses JSON format.
- Ollama Integration: Seamless integration with Ollama API for LLM inference
- MCP Tools: Extensible tool system with JSON-based definitions
- JSON Communication: All components communicate using JSON format
- Multiple Tools: Web search, calculator, file operations, and weather tools
- Conversation History: Maintains conversation context across interactions
- Health Monitoring: Built-in health checks for all components
- Error Handling: Robust error handling with JSON error responses
User Input → Chat Interface → Message Router → Ollama Client → LLM Response
↓
MCP Server ← Tool Definitions (JSON)
↓
Tool Execution → JSON Response
- Python 3.8+
- Ollama installed and running
- API keys for external services (optional)
-
Clone the repository:
git clone <repository-url> cd llm-chatbot
-
Install dependencies:
pip install -r requirements.txt
-
Install and start Ollama:
# Install Ollama (follow instructions at https://ollama.ai) # Start Ollama service ollama serve
-
Pull a model (optional, but recommended):
# You can use any available model. Some options: ollama pull gemma3n:e2b ollama pull llama2 ollama pull qwen2.5:7b -
Set up environment variables (optional):
cp .env.example .env # Edit .env with your API keys
Create a .env file in the project root:
# Ollama Configuration
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=gemma3n:e2b
# MCP Server Configuration
MCP_SERVER_PORT=8000
# API Keys (optional)
WEB_SEARCH_API_KEY=your_search_api_key
WEATHER_API_KEY=your_weather_api_keyTools are defined in config/tool_definitions.json. Each tool has:
- name: Unique identifier
- description: Human-readable description
- inputSchema: JSON Schema for parameters
- annotations: Metadata about tool behavior
python main.pypython main.py serverOnce the application is running, you can use these commands:
quitorexit: Exit the applicationclear: Clear conversation historyhealth: Check system healthhistory: Show conversation history
- Name:
web_search - Description: Search the web for current information
- Parameters:
query(required),max_results(optional)
- Name:
calculate - Description: Perform mathematical calculations
- Parameters:
expression(required)
- Name:
file_operations - Description: Read, write, or list files
- Parameters:
operation,path(required),content(for write)
- Name:
weather - Description: Get current weather information
- Parameters:
location(required),units(optional)
{
"model": "llama2",
"messages": [
{
"role": "user",
"content": "What's the weather in New York?"
}
],
"tools": [
{
"name": "weather",
"description": "Get current weather information",
"inputSchema": {
"type": "object",
"properties": {
"location": {"type": "string"},
"units": {"type": "string", "enum": ["metric", "imperial"]}
},
"required": ["location"]
}
}
]
}{
"name": "weather",
"arguments": {
"location": "New York",
"units": "metric"
}
}{
"content": [
{
"type": "text",
"text": "Weather for New York: 22°C, Partly Cloudy"
}
],
"isError": false
}llm-chatbot/
├── src/
│ ├── client/
│ │ ├── ollama_client.py # Ollama API client
│ │ └── chat_interface.py # User interface
│ ├── server/
│ │ ├── mcp_server.py # MCP server implementation
│ │ └── tool_handlers.py # Tool implementations
│ ├── utils/
│ │ └── message_router.py # Message routing logic
│ └── config/
│ └── settings.py # Configuration management
├── config/
│ └── tool_definitions.json # Tool definitions
├── requirements.txt
├── main.py
└── README.md
-
Define the tool in
config/tool_definitions.json:{ "name": "my_tool", "description": "Description of my tool", "inputSchema": { "type": "object", "properties": { "param1": {"type": "string"} }, "required": ["param1"] }, "annotations": { "title": "My Tool", "readOnlyHint": true } } -
Implement the tool handler in
src/server/tool_handlers.py:class MyTool(BaseTool): async def execute(self, arguments: Dict[str, Any]) -> Dict[str, Any]: # Your tool implementation return { "content": [{"type": "text", "text": "Result"}], "isError": False }
-
Register the tool in
MCPServer.initialize_tool_handlers():return { # ... existing tools "my_tool": MyTool() }
Run tests with pytest:
pytest tests/-
Check if Ollama is running:
curl http://localhost:11434/api/tags
-
Start Ollama:
ollama serve
-
Verify model availability:
ollama list
-
Check server health:
curl http://localhost:8000/health
-
View available tools:
curl http://localhost:8000/tools/list
If you see warnings about missing API keys:
- Set the required environment variables in
.env - Restart the application
- Some tools will work without API keys (with mock responses)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Ollama for the local LLM inference
- MCP Specification for the tool protocol
- FastAPI for the web framework