Skip to main content
Glama
TapanBhavsar

MCP Middleware Server

by TapanBhavsar
README.md
# MCP Middleware Server & Client

This project implements a FastMCP server with session-based memory using LangChain and Google Gemini, along with a client that demonstrates multi-server session compatibility.

## Features

- **Session-based Memory**: Each client session maintains its own conversation history.
- **Dynamic Authentication**: API keys are passed via headers and used to initialize session-specific LLMs.
- **Streamable HTTP**: Uses HTTP transport for robust session management.

## Setup Instructions

### 1. Prerequisites

- Python 3.10+
- A Google Gemini API Key

### 2. Installation

Clone the repository and install the dependencies:

```bash
pip install -r requirements.txt
```

### 3. Environment Configuration

Create a `.env` file based on the `.env.example`:

```bash
cp .env.example .env
```

Edit `.env` and add your `GOOGLE_API_KEY`.

## Usage

### Running the Server

Start the MCP server using the following command:

```bash
python server.py
```

By default, the server will run on `http://127.0.0.1:8000/mcp`.

### Running the Client

In a new terminal, run the client:

```bash
python client.py
```

## Session Compatibility Example

The server maintains state across multiple requests within the same session. You can verify this by following these steps in the client:

1. **Inform the AI of your name:**
   - **Input:** `HI my name is Tapan`
   - **AI Response:** `Hello Tapan! Nice to meet you. How can I help you today?`

2. **Verify the memory:**
   - **Input:** `what is my name?`
   - **AI Response:** `Your name is Tapan.`

This works because the `session_id` is tracked in the `_session_histories` dictionary on the server, ensuring that each user has a personalized and continuous conversation.

## Files

- `server.py`: The FastMCP server implementation with Auth middleware and session handling.
- `client.py`: A Python client using `MultiServerMCPClient` to interact with the server.
- `.env.example`: Template for environment variables.
- `requirements.txt`: Project dependencies.