MCP HubMCP Hub
3choff

mcp-chatbot

by: 3choff

A simple CLI chatbot that demonstrates the integration of the Model Context Protocol (MCP).

155created 05/12/2024
Visit
CLI
chatbot

📌Overview

Purpose:
To demonstrate and facilitate seamless integration of the Model Context Protocol (MCP) within a CLI chatbot, supporting flexible tool use and compatibility with multiple LLM providers.

Overview:
MCP Chatbot is a flexible example application that connects the power of the Model Context Protocol with conversational AI via a command-line interface. It is designed to work with any large language model (LLM) that adheres to OpenAI API standards. The architecture allows for dynamic integration and discovery of multiple tools across various MCP servers, making the chatbot highly adaptable and extensible while simplifying LLM and tool orchestration.

Key Features:

  • LLM Provider Flexibility:
    Compatible with any LLM supporting OpenAI API standards, allowing users to choose and switch between different providers seamlessly.

  • Dynamic Tool Integration:
    Automatically discovers and integrates tools declared by MCP servers at runtime, with descriptions dynamically included in the system prompt for maximum compatibility.

  • Server Configuration:
    Supports easy setup of multiple MCP servers using a straightforward JSON configuration, including support for environment variables and server-specific arguments.


MCP Chatbot

MCP Chatbot demonstrates integration of the Model Context Protocol (MCP) into a simple CLI chatbot. It showcases MCP's flexibility, supporting multiple tools via MCP servers, and works with any LLM provider that follows OpenAI API standards.

Key Features

  • LLM Provider Flexibility: Works with any LLM that follows OpenAI API standards.
  • Dynamic Tool Integration: Tools are declared in the system prompt for maximum compatibility.
  • Server Configuration: Supports multiple MCP servers with a simple JSON configuration.

Requirements

  • Python 3.10
  • python-dotenv
  • requests
  • mcp
  • uvicorn

Installation

  1. Clone the repository:

    git clone https://github.com/3choff/mcp-chatbot.git
    cd mcp-chatbot
    
  2. Install the dependencies:

    pip install -r requirements.txt
    
  3. Set up environment variables: Create a .env file and add your API key:

    LLM_API_KEY=your_api_key_here
    
  4. Configure servers: Create or edit servers_config.json:

    {
      "mcpServers": {
        "sqlite": {
          "command": "uvx",
          "args": ["mcp-server-sqlite", "--db-path", "./test.db"]
        },
        "puppeteer": {
          "command": "npx",
          "args": ["-y", "@modelcontextprotocol/server-puppeteer"]
        }
      }
    }
    

    You can also set environment variables for each server:

    {
      "mcpServers": {
        "server_name": {
          "command": "uvx",
          "args": ["mcp-server-name", "--additional-args"],
          "env": {
            "API_KEY": "your_api_key_here"
          }
        }
      }
    }
    

Usage

  1. Run the client:

    python main.py
    
  2. Interact with the assistant.

  3. Type quit or exit to end the session.

Architecture Overview

  • Tool Discovery: Tools are discovered from configured servers.
  • System Prompt: Tools are dynamically included in the system prompt.
  • Server Integration: Supports any MCP-compatible server.

Main Classes

  • Configuration: Manages environment variables and server configurations.
  • Server: Handles MCP server initialization and tool discovery.
  • Tool: Represents individual tools.
  • LLMClient: Manages communication with the LLM provider.
  • ChatSession: Orchestrates interaction between user, LLM, and tools.

Logic Flow

  1. Initialization

    • Load environment variables and server settings.
    • Initialize MCP servers and discover tools.
  2. Runtime

    • User input is received.
    • Input is sent to LLM with current tool context.
    • LLM determines response:
      • If a tool call → execute and return result.
      • If a direct answer → return response to user.
    • Tool results may be sent to LLM for further interpretation.
  3. Tool Integration

    • Tools are discovered dynamically.
    • Descriptions included in system prompt.
    • Execution via standardized MCP protocol.

Contributing

Feedback and contributions are welcome. Please create a new issue on the GitHub repository or submit a pull request.

License

This project is licensed under the MIT License.