MCP with RAG Demo

by thinklytics

252 downloads Not rated yet MIT license
GitHub

About

This demonstration project shows how to implement a Model Context Protocol (MCP) server with Retrieval-Augmented Generation (RAG) capabilities. The demo allows AI models to interact with a knowledge base, search for information, and add new documents.

Details

License
MIT license

Explore

- MCP server with tool and resource support
- RAG implementation (with fallback to in-memory storage)
- Client example for interacting with the MCP server
- Support for both SSE (HTTP) and stdio communication modes
- Simple prompt templates

Setting up with Highlight

This MCP is not yet compatible with Highlight’s one-click setup. However, you can still use it with Highlight by following these steps:

  1. Download and install Highlight from highlightai.com/download
  2. Navigate to the plugins tab and select "Add Custom Plugin"
  3. Configure the plugin with the settings below
    Plugin Name MCP with RAG Demo
    Command (node, npx, python, etc.)

    Please refer to the README for specific instructions on how to obtain API keys or other required environment variables.

  4. Enable "Start Automatically" if you want the plugin to start when Highlight launches

From the repository

- Python 3.8+
- pip (Python package manager)

source mcp-env/bin/activate # On Windows: mcp-env\Scripts\activate

The following environment variables can be set in the .env file:

- OPENAI_API_KEY: Your OpenAI API key (required)
- MCP_SERVER_URL: The URL of the MCP server (optional)
- DEBUG: Enable debug mode (optional)

Run the example script:

python openai_example.py

You can also specify options directly:

python openai_example.py --api-key your_api_key_here --server-url http://localhost:3000 --debug

echo

A simple tool that echoes back the provided message

add

A tool that adds two numbers together

add_document

Adds a document to the knowledge base

rag_search

Searches the knowledge base for information related to a query

list_documents

Lists all documents in the knowledge base

The following tools are available in this demo:

- echo: A simple tool that echoes back the provided message
- add: A tool that adds two numbers together
- add_document: Adds a document to the knowledge base
- rag_search: Searches the knowledge base for information related to a query
- list_documents: Lists all documents in the knowledge base

To add new tools, modify the server.py file:

```python
@mcp.tool()
def your_tool_name(param1: type, param2: type) -> return_type:
"""Tool description"""

Claude Desktop / Cursor

Paste into your MCP client config file to install this server.

{
    "mcpServers": {
        "mcp with rag demo": {
            "mcp-demo-thinklytics": {
                "command": "python",
                "args": [
                    "-m",
                    "venv",
                    "mcp-env"
                ]
            }
        }
    }
}

McpServers

{
    "mcp-demo-thinklytics": {
        "command": "python",
        "args": [
            "-m",
            "venv",
            "mcp-env"
        ]
    }
}

This demonstration project shows how to implement a Model Context Protocol (MCP) server with Retrieval-Augmented Generation (RAG) capabilities. The demo allows AI models to interact with a knowledge base, search for information, and add new documents.

Features

- MCP server with tool and resource support
- RAG implementation (with fallback to in-memory storage)
- Client example for interacting with the MCP server
- Support for both SSE (HTTP) and stdio communication modes
- Simple prompt templates

Prerequisites

- Python 3.8+
- pip (Python package manager)

Installation

1. Clone the repository:

git clone <repository-url>
cd mcp-demo

2. Create a virtual environment:

python -m venv mcp-env
source mcp-env/bin/activate  # On Windows: mcp-env\Scripts\activate

3. Install the required dependencies:

pip install -r requirements.txt

Project Structure

mcp-demo/
├── server.py           # Main MCP server implementation
├── client_example.py   # Example client to interact with the server
├── requirements.txt    # Project dependencies
├── sample_data.txt     # Sample data available as a resource
├── tools/
│   ├── __init__.py     # Package initialization
│   └── rag_tools.py    # RAG tools implementation
└── README.md           # This readme file

Running the Demo

Step 1: Start the MCP Server

You can run the MCP server in two different modes:

Option A: SSE (HTTP) Mode

This mode allows the server to accept connections over HTTP using Server-Sent Events (SSE):

python server.py --sse

By default, the server will listen on 0.0.0.0:8000. You can customize the host and port:

python server.py --sse --host 127.0.0.1 --port 9000
Option B: stdio Mode

This mode allows the server to communicate through standard input/output:

python server.py --stdio

Step 2: Run the Client Example

After the server is up and running, open a new terminal window (keeping the server running in the first one):

Connecting to an SSE Server

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.