MCP Chatbot
以下摘录自该仓库的 README(片段) · 在 GitHub 读完整 README →
This chatbot example demonstrates how to integrate the Model Context Protocol (MCP) into a simple CLI chatbot. The implementation showcases MCP's flexibility by supporting multiple tools through MCP servers and is compatible with any LLM provider that follows OpenAI API standards.
If you find this project helpful, don’t forget to ⭐ star the repository or buy me a ☕ coffee.
Key Features
- **LLM Provider Flexibility**: Works with any LLM that follows OpenAI API standards (tested with Llama 3.2 90b on Groq and GPT-4o mini on GitHub Marketplace).
- **Dynamic Tool Integration**: Tools are declared in the system prompt, ensuring maximum compatibility across different LLMs.
- **Server Configuration**: Supports multiple MCP servers through a simple JSON configuration file like the Claude Desktop App.
Requirements
- Python 3.10
- python-dotenv
- requests
- mcp
- uvicorn
Installation
1. **Clone the repository:**
2. **Install the dependencies:**
3. **Set up environment variables:**
Create a .env file in the root directory and add your API key:
4. **Configure servers:**
The servers_config.json follows the same structure as Claude Desktop, allowing for easy integration of multiple servers.
Here's an example:
Environment variables are supported as well. Pass them as you would with the Claude Desktop App.
Example:
Usage
1. **Run the client:**
2. **Interact with the assistant:**
The assistant will automatically detect available tools and can respond to queries based on the tools provided by the configured servers.
3. **Exit the session:**
Type quit or exit to end the session.
Architecture
- **Tool Discovery**: Tools are automatically discovered from configured servers.
- **System Prompt**: Tools are dynamically included in the system prompt, allowing the LLM to understand available capabilities.
- **Server Integration**: Supports any MCP-compatible server, tested with various server implementations including Uvicorn and Node.js.
Class Structure
- **Configuration**: Manages environment variables and server configurations
- **Server**: Handles MCP server initialization, tool discovery, and execution
- **Tool**: Represents individual tools with their properties and formatting
- **LLMClient**: Manages communication with the LLM provider
- **ChatSession**: Orchestrates the interaction between user, LLM, and tools
Logic Flow
1. **Initialization**:
- Configuration loads environment variables and server settings
- Servers are initialized with their respective tools
- Tools are discovered and formatted for LLM understanding
2. **Runtime Flow**:
- User input is received
- Input is sent to LLM with context of available tools