MCPcopy Index your code
hub / github.com/3choff/mcp-chatbot

github.com/3choff/mcp-chatbot @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
23 symbols 59 edges 1 files 19 documented · 83%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

MCP Chatbot

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:

bash git clone https://github.com/3choff/mcp-chatbot.git cd mcp-chatbot

  1. Install the dependencies:

bash pip install -r requirements.txt

  1. Set up environment variables:

Create a .env file in the root directory and add your API key:

plaintext LLM_API_KEY=your_api_key_here

  1. Configure servers:

The servers_config.json follows the same structure as Claude Desktop, allowing for easy integration of multiple servers. Here's an example:

json { "mcpServers": { "sqlite": { "command": "uvx", "args": ["mcp-server-sqlite", "--db-path", "./test.db"] }, "puppeteer": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-puppeteer"] } } } Environment variables are supported as well. Pass them as you would with the Claude Desktop App.

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

Usage

  1. Run the client:

bash python main.py

  1. 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.

  1. 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

flowchart TD
    A[Start] --> B[Load Configuration]
    B --> C[Initialize Servers]
    C --> D[Discover Tools]
    D --> E[Format Tools for LLM]
    E --> F[Wait for User Input]

    F --> G{User Input}
    G --> H[Send Input to LLM]
    H --> I{LLM Decision}
    I -->|Tool Call| J[Execute Tool]
    I -->|Direct Response| K[Return Response to User]

    J --> L[Return Tool Result]
    L --> M[Send Result to LLM]
    M --> N[LLM Interprets Result]
    N --> O[Present Final Response to User]

    K --> O
    O --> F
  1. Initialization:
  2. Configuration loads environment variables and server settings
  3. Servers are initialized with their respective tools
  4. Tools are discovered and formatted for LLM understanding

  5. Runtime Flow:

  6. User input is received
  7. Input is sent to LLM with context of available tools
  8. LLM response is parsed:
    • If it's a tool call → execute tool and return result
    • If it's a direct response → return to user
  9. Tool results are sent back to LLM for interpretation
  10. Final response is presented to user

  11. Tool Integration:

  12. Tools are dynamically discovered from MCP servers
  13. Tool descriptions are automatically included in system prompt
  14. Tool execution is handled through standardized MCP protocol

Contributing

Feedback and contributions are welcome. If you encounter any issues or have suggestions for improvements, please create a new issue on the GitHub repository.

If you'd like to contribute to the development of the project, feel free to submit a pull request with your changes.

License

This project is licensed under the MIT License.

Core symbols most depended-on inside this repo

list_tools
called by 2
main.py
cleanup
called by 2
main.py
get_response
called by 2
main.py
cleanup_servers
called by 2
main.py
load_env
called by 1
main.py
load_config
called by 1
main.py
initialize
called by 1
main.py
execute_tool
called by 1
main.py

Shape

Method 17
Class 5
Function 1

Languages

Python100%

Modules by API surface

main.py23 symbols

For agents

$ claude mcp add mcp-chatbot \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact