Skip to content

feat: provide the chat of the rag by MCP #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
466bb7f
add mcp server
MelvinKl May 5, 2025
c84f25b
Merge branch 'main' into feature/mcp
a-klos May 20, 2025
1fed3b8
chore: megre
a-klos Jun 16, 2025
6d5186b
Merge branch 'main' into feature/mcp
a-klos Jun 16, 2025
02a558f
feat: update MCP server configuration and dependencies, refactor chat…
a-klos Jun 16, 2025
1483c09
fix: update .gitignore to exclude all notebook files
a-klos Jun 16, 2025
dadf160
chore: update submodules to latest main
github-actions[bot] Jun 16, 2025
80f15c8
Update Tiltfile
a-klos Jun 16, 2025
4a5ba8f
Update mcp-server/Dockerfile
a-klos Jun 16, 2025
9542222
Update mcp-server/src/dependency_container.py
a-klos Jun 16, 2025
c6d304b
feat: update version and description in pyproject.toml; refactor tran…
a-klos Jun 16, 2025
4705662
Merge branch 'feature/mcp' of github.com:stackitcloud/rag-template in…
a-klos Jun 16, 2025
58f2201
feat: update flake8 exclusions, refactor chat handling in RagMcpServe…
a-klos Jun 16, 2025
2baa80e
chore: update submodules to latest main
github-actions[bot] Jun 16, 2025
f870b08
chore: update subproject commit in rag-infrastructure
a-klos Jun 17, 2025
2c923bc
chore: update submodules to latest main
github-actions[bot] Jun 17, 2025
6fe1dcb
feat: enhance MCP server with chat functionalities, update documentat…
a-klos Jun 20, 2025
bc3ea76
chore: update subproject commit in rag-infrastructure
a-klos Jun 20, 2025
ae9a5ac
Merge branch 'feature/mcp' of github.com:stackitcloud/rag-template in…
a-klos Jun 20, 2025
a6dfb55
chore: update submodules to latest main
github-actions[bot] Jun 20, 2025
3bd1d76
feat: add build and push step for mcp-server in Makefile
a-klos Jun 20, 2025
b4dba97
Merge branch 'feature/mcp' of github.com:stackitcloud/rag-template in…
a-klos Jun 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ frontend/*.log
*/.venv
*/.env
*/*.pyc


**/.notebooks
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ auth
node_modules/

**/.notebooks
**/.notebooks/*
**/todo*.md
**/mcp.json

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ build_and_push:
docker buildx build --platform linux/amd64 -t $(REGISTRY)/document-extractor:$(IMAGE_TAG) -f document-extractor/Dockerfile --push .
docker buildx build --platform linux/amd64 -t $(REGISTRY)/frontend:$(IMAGE_TAG) -f frontend/apps/chat-app/Dockerfile --push .
docker buildx build --platform linux/amd64 -t $(REGISTRY)/admin-frontend:$(IMAGE_TAG) -f frontend/apps/admin-app/Dockerfile --push .
docker buildx build --platform linux/amd64 -t $(REGISTRY)/mcp-server:$(IMAGE_TAG) -f mcp-server/Dockerfile --push .
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This repository contains the following components:
- [*rag-backend*](#111-rag-backend): The main component of the RAG.
- [*admin-backend*](#112-admin-backend): Manages user documents and confluence spaces, interacts with document-extractor and rag-backend.
- [*document-extractor*](#113-document-extractor): Extracts content from documents and Confluence spaces.
- [*mcp-server*](#114-mcp-server): Model Context Protocol server that provides MCP-compatible access to the RAG system.
- *frontend*: Frontend for both, chat and admin APIs.
- *rag-infrastructure*: Contains the helm-chart and other files related to infrastructure and deployment. Please consult [this README](https://github.com/stackitcloud/rag-infrastructure/blob/main/README.md) for further information.
- *rag-core-library*: Contains the API-libraries that are used to construct the backend-services in this repository. For further information, please consult [this README](https://github.com/stackitcloud/rag-core-library/blob/main/README.md).
Expand All @@ -68,6 +69,17 @@ The Document extractor is a component that is used to extract the content from t

All components are provided by the *extractor-api-lib*. For further information on endpoints and requirements, please consult [this README](https://github.com/stackitcloud/rag-core-library/blob/main/README.md#3-extractor-api-lib).

#### 1.1.4 MCP Server

The MCP Server is a Model Context Protocol (MCP) server that provides a bridge between MCP-compatible clients and the RAG backend. It enables AI assistants and other tools to interact with the RAG system through standardized MCP tools.

The MCP server runs as a sidecar container alongside the main RAG backend and exposes two main tools:

- `chat_simple`: Basic question-answering without conversation history
- `chat_with_history`: Advanced chat interface with conversation history and returns structured responses with `answer`, `finish_reason`, and `citations`.

For further information on configuration and usage, please consult the [MCP Server README](./mcp-server/README.md).

### 1.2 Requirements

> 📝 *Windows users*: make sure you use wsl for infrastructure setup & orchestration.
Expand Down
38 changes: 37 additions & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,37 @@ local_resource(
allow_parallel=True,
)

################################## build mcp image and do live update ##################################################
# NOTE: full image names should match the one in the helm chart values.yaml!
registry = "ghcr.io/stackitcloud/rag-template"
mcp_image_name = "rag-mcp"

mcp_context = "./mcp-server"
mcp_full_image_name = "%s/%s" % (registry, mcp_image_name)
docker_build(
mcp_full_image_name,
".",
build_args={
"dev": "1" if backend_debug else "0",
},
live_update=[
sync(mcp_context, "/app/mcp-server"),
],
dockerfile=mcp_context + "/Dockerfile",
)

# Add linter trigger
local_resource(
"MCP server linting",
create_linter_command(mcp_context, "back"),
labels=["linting"],
auto_init=False,
trigger_mode=TRIGGER_MODE_AUTO,
allow_parallel=True,
)

########################################################################################################################
################################## build admin backend image and do live update ##############################################
################################## build admin backend image and do live update ########################################
########################################################################################################################

# NOTE: full image names should match the one in the helm chart values.yaml!
Expand Down Expand Up @@ -313,6 +342,8 @@ value_override = [
"features.minio.enabled=true",
"shared.config.tls.enabled=false",
"shared.ssl=false",
"shared.config.basicAuth.enabled=true",
"features.mcp.enabled=true",
# ingress host names
"backend.ingress.host.name=rag.localhost",
# langfuse
Expand Down Expand Up @@ -398,6 +429,11 @@ k8s_resource(
31415,
container_port=31415,
name="Backend-Debugger",
),
port_forward(
9090,
container_port=8000,
name="MCP-Server",
)
],
labels=["backend"],
Expand Down
62 changes: 62 additions & 0 deletions mcp-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
FROM --platform=linux/amd64 python:3.11.7-bookworm AS build

ARG dev=0
ENV POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv
ENV POETRY_VERSION=1.8.3

WORKDIR /app

RUN DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential --no-install-recommends make && \
python3 -m venv "${POETRY_VIRTUALENVS_PATH}" \
&& $POETRY_VIRTUALENVS_PATH/bin/pip install "poetry==${POETRY_VERSION}"
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH"


WORKDIR /app/mcp-server
COPY mcp-server/pyproject.toml mcp-server/poetry.lock ./

RUN mkdir log && chmod 700 log
RUN touch /app/mcp-server/log/logfile.log && chmod 600 /app/mcp-server/log/logfile.log

RUN poetry config virtualenvs.create false &&\
if [ "$dev" = "1" ]; then \
poetry install --no-interaction --no-ansi --no-root --with dev; \
else \
poetry install --no-interaction --no-ansi --no-root; \
fi

FROM --platform=linux/amd64 python:3.11.7-bookworm
ARG dev=0

RUN adduser --disabled-password --gecos "" --uid 65532 nonroot

ENV POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
COPY --from=build /usr/local/bin/ /usr/local/bin/
COPY --from=build /usr/bin/make /usr/bin/make
COPY --from=build /usr/local/lib/ /usr/local/lib/

WORKDIR /app/mcp-server
COPY --chown=nonroot:nonroot mcp-server/src ./src
COPY --chown=nonroot:nonroot mcp-server/pyproject.toml mcp-server/poetry.lock ./
# cleanup
RUN apt-get clean autoclean
RUN apt-get autoremove --yes

RUN if [ "$dev" = "0" ]; then \
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
else \
echo "POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv" >> /etc/environment;\
export POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv;\
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
fi


USER nonroot
COPY --from=build --chown=nonroot:nonroot /app/mcp-server/log /app/mcp-server/log

ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"

CMD [ "poetry", "run", "python", "src/main.py" ]
106 changes: 106 additions & 0 deletions mcp-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# MCP Server

The MCP Server is a Model Context Protocol (MCP) server that provides a bridge between MCP-compatible clients and the RAG backend. It enables AI assistants and other tools to interact with the RAG system through standardized MCP tools.

## Features 🚀

- **Simple Chat Interface**: Basic question-answering without conversation history
- **Chat with History**: Conversational interface that maintains context across messages
- **Citation Support**: Returns source documents and metadata for transparency
- **Streamable HTTP Transport**: Uses HTTP-based transport for reliable communication
- **Configurable Settings**: Environment-based configuration for different deployment scenarios

## Architecture

The server consists of several key components:

- **RagMcpServer**: Main server class that handles MCP tool registration and request routing
- **Dependency Container**: Manages dependency injection for clean architecture
- **Settings**: Environment-based configuration management
- **RAG Backend Client**: Auto-generated OpenAPI client for backend communication

## Requirements

All required python libraries can be found in the [pyproject.toml](pyproject.toml) file.
The MCP server uses Poetry for dependency management and shares the base Dockerfile pattern with other services in the RAG template.

## Available Tools

The server exposes two main MCP tools for interacting with the RAG system:

### `chat_simple`

Simple question-answering interface that returns plain text responses.

**Parameters:**

- `session_id` (str): Unique identifier for the chat session
- `message` (str): The question or message to send to the RAG system

**Returns:**

- `str`: Plain text answer from the RAG system

### `chat_with_history`

Advanced chat interface that supports conversation history and returns structured responses with citations.

**Parameters:**

- `session_id` (str): Unique identifier for the chat session
- `message` (str): The current question or message
- `history` (list[dict], optional): Previous conversation history

**History Format:**
Each history item should be a dictionary with:

- `role`: Either "user" or "assistant"
- `message`: The message content

**Returns:**

- `dict`: Structured response containing:
- `answer`: The response text
- `finish_reason`: Why the response ended
- `citations`: List of source documents with content and metadata

## Configuration

The server supports configuration through environment variables with the following prefixes:

### MCP Settings (`MCP_` prefix)

- `MCP_HOST`: Server bind address (default: `0.0.0.0`)
- `MCP_PORT`: Server port (default: `8000`)
- `MCP_NAME`: Server name (default: `RAG MCP server`)

### Backend Settings (`BACKEND_` prefix)

- `BACKEND_BASE_PATH`: RAG backend URL (default: `http://127.0.0.1:8080`)

## Deployment

The MCP server is designed to be deployed alongside the main RAG backend as a sidecar container. A detailed explanation of the deployment can be found in the [main README](../README.md) and the [infrastructure README](../rag-infrastructure/README.md) of the project.

### Docker Support

The server includes Docker support for containerized deployment and is integrated into the main Tilt development workflow.

### Integration in RAG Template

The MCP server is automatically deployed when `backend.mcp.enabled=true` is set in the Helm values. It runs as a sidecar container alongside the main RAG backend, accessible via:

- **Port**: 8000 (configurable via `MCP_PORT`)
- **Endpoint**: `/mcp` path through the main ingress
- **Development**: Port-forwarded to 9090 in local Tilt setup

## Development

The MCP server is integrated into the main RAG template development workflow:

- **Tilt Integration**: Automatically built and deployed with live reload
- **Linting**: Included in the main linting pipeline
- **Testing**: Part of the overall test suite
- **Debugging**: Supports the same debugging workflow as other services

For detailed development setup instructions, see the [main README](../README.md).
7 changes: 7 additions & 0 deletions mcp-server/api-generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash


docker run --user $(id -u):$(id -g) --rm -v $PWD:/local openapitools/openapi-generator-cli@sha256:b35aee2d0f6ffadadcdad9d8fc3c46e8d48360c20b5731a5f47c809d51f67a04 generate -i /local/rag-core-library/rag-core-api/openapi.yaml -g python -o /local/mcp-server/src --additional-properties=generateSourceCodeOnly=True,packageName=rag_backend_client.openapi_client
cd ./mcp-server
black .
cd ..
Loading
Loading