Skip to content

Commit 2b0e037

Browse files
authored
feat: provide the chat of the rag by MCP (#27)
This pull request introduces a new component, the MCP Server, to the RAG system. The MCP Server facilitates interaction between MCP-compatible clients and the RAG backend, providing features like simple chat interfaces, conversation history, and citation support. The changes include updates to documentation, build configurations, and the addition of new files and dependencies for the MCP Server. ### MCP Server Introduction and Features * **New Component Documentation**: Added a detailed description of the MCP Server in `README.md` and created a dedicated `mcp-server/README.md` to explain its features, architecture, configuration, and deployment. [[1]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R50) [[2]](diffhunk://#diff-de8068a21895dfaa62ca644f5c42338393947c7b760c8862c7ec34a2abb29bf1R1-R106) * **Dockerfile for MCP Server**: Added `mcp-server/Dockerfile` to define the containerized environment for the MCP Server, including dependency installation and runtime setup. ### Build and Deployment Configuration * **Tiltfile Updates**: Integrated the MCP Server into the development workflow with live updates, linting, and port-forwarding configurations in `Tiltfile`. Enabled the MCP feature in Helm values. [[1]](diffhunk://#diff-c2ee8653e1d6b85f0aadf87cd438a9250806c052877248442be4d434cbc52425R180-R210) [[2]](diffhunk://#diff-c2ee8653e1d6b85f0aadf87cd438a9250806c052877248442be4d434cbc52425R345-R346) [[3]](diffhunk://#diff-c2ee8653e1d6b85f0aadf87cd438a9250806c052877248442be4d434cbc52425R432-R436) * **Makefile Update**: Added a build command for the MCP Server to the `Makefile`. ### Codebase Additions * **Dependency Injection**: Introduced `mcp-server/src/dependency_container.py` for managing application dependencies using `dependency-injector`. * **Main Entry Point**: Added `mcp-server/src/main.py` to initialize and run the MCP Server. * **OpenAPI Client**: Generated OpenAPI client files under `mcp-server/src/rag_backend_client/openapi_client/` for backend communication. [[1]](diffhunk://#diff-08494b7fe5b52ed37ca18f372ed78c8e17b6a13fcea8c33ee860af361fb7b0ffR1-R44) [[2]](diffhunk://#diff-36e13e6a9f7a6f22ba251ad8f60039b94d194a20ce9c3f6f249feed038d61ea2R1-R4) [[3]](diffhunk://#diff-e9ff3d7051093df73131df1a7e1201884e1ef2d0d6673573e59acaf32f4e1d7cR1-R20) [[4]](diffhunk://#diff-c7187b1f33a4416b9df23604777a695bf0d52aba4dbc2dcbecbf834cf446fceeR1-R30) ### Supporting Files and Dependencies * **Project Configuration**: Added `mcp-server/pyproject.toml` to define dependencies and development tools for the MCP Server. * **OpenAPI Generator Ignore**: Added `.openapi-generator-ignore` to exclude generated files from being overwritten. * **API Generator Script**: Added `mcp-server/api-generator.sh` to automate the generation of OpenAPI client code. ### Miscellaneous * **Dockerignore Update**: Updated `.dockerignore` to exclude unnecessary files for the MCP Server.
1 parent 02036cf commit 2b0e037

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+7911
-3
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ frontend/*.log
1818
*/.venv
1919
*/.env
2020
*/*.pyc
21+
22+
23+
**/.notebooks

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ auth
1212
node_modules/
1313

1414
**/.notebooks
15+
**/.notebooks/*
1516
**/todo*.md
17+
**/mcp.json
1618

1719
# Byte-compiled / optimized / DLL files
1820
__pycache__/

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ build_and_push:
2727
docker buildx build --platform linux/amd64 -t $(REGISTRY)/document-extractor:$(IMAGE_TAG) -f document-extractor/Dockerfile --push .
2828
docker buildx build --platform linux/amd64 -t $(REGISTRY)/frontend:$(IMAGE_TAG) -f frontend/apps/chat-app/Dockerfile --push .
2929
docker buildx build --platform linux/amd64 -t $(REGISTRY)/admin-frontend:$(IMAGE_TAG) -f frontend/apps/admin-app/Dockerfile --push .
30+
docker buildx build --platform linux/amd64 -t $(REGISTRY)/mcp-server:$(IMAGE_TAG) -f mcp-server/Dockerfile --push .

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This repository contains the following components:
4747
- [*rag-backend*](#111-rag-backend): The main component of the RAG.
4848
- [*admin-backend*](#112-admin-backend): Manages user documents and confluence spaces, interacts with document-extractor and rag-backend.
4949
- [*document-extractor*](#113-document-extractor): Extracts content from documents and Confluence spaces.
50+
- [*mcp-server*](#114-mcp-server): Model Context Protocol server that provides MCP-compatible access to the RAG system.
5051
- *frontend*: Frontend for both, chat and admin APIs.
5152
- *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.
5253
- *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).
@@ -68,6 +69,17 @@ The Document extractor is a component that is used to extract the content from t
6869

6970
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).
7071

72+
#### 1.1.4 MCP Server
73+
74+
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.
75+
76+
The MCP server runs as a sidecar container alongside the main RAG backend and exposes two main tools:
77+
78+
- `chat_simple`: Basic question-answering without conversation history
79+
- `chat_with_history`: Advanced chat interface with conversation history and returns structured responses with `answer`, `finish_reason`, and `citations`.
80+
81+
For further information on configuration and usage, please consult the [MCP Server README](./mcp-server/README.md).
82+
7183
### 1.2 Requirements
7284

7385
> 📝 *Windows users*: make sure you use wsl for infrastructure setup & orchestration.

Tiltfile

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,37 @@ local_resource(
177177
allow_parallel=True,
178178
)
179179

180+
################################## build mcp image and do live update ##################################################
181+
# NOTE: full image names should match the one in the helm chart values.yaml!
182+
registry = "ghcr.io/stackitcloud/rag-template"
183+
mcp_image_name = "rag-mcp"
184+
185+
mcp_context = "./mcp-server"
186+
mcp_full_image_name = "%s/%s" % (registry, mcp_image_name)
187+
docker_build(
188+
mcp_full_image_name,
189+
".",
190+
build_args={
191+
"dev": "1" if backend_debug else "0",
192+
},
193+
live_update=[
194+
sync(mcp_context, "/app/mcp-server"),
195+
],
196+
dockerfile=mcp_context + "/Dockerfile",
197+
)
198+
199+
# Add linter trigger
200+
local_resource(
201+
"MCP server linting",
202+
create_linter_command(mcp_context, "back"),
203+
labels=["linting"],
204+
auto_init=False,
205+
trigger_mode=TRIGGER_MODE_AUTO,
206+
allow_parallel=True,
207+
)
208+
180209
########################################################################################################################
181-
################################## build admin backend image and do live update ##############################################
210+
################################## build admin backend image and do live update ########################################
182211
########################################################################################################################
183212

184213
# NOTE: full image names should match the one in the helm chart values.yaml!
@@ -313,6 +342,8 @@ value_override = [
313342
"features.minio.enabled=true",
314343
"shared.config.tls.enabled=false",
315344
"shared.ssl=false",
345+
"shared.config.basicAuth.enabled=true",
346+
"features.mcp.enabled=true",
316347
# ingress host names
317348
"backend.ingress.host.name=rag.localhost",
318349
# langfuse
@@ -398,6 +429,11 @@ k8s_resource(
398429
31415,
399430
container_port=31415,
400431
name="Backend-Debugger",
432+
),
433+
port_forward(
434+
9090,
435+
container_port=8000,
436+
name="MCP-Server",
401437
)
402438
],
403439
labels=["backend"],

mcp-server/Dockerfile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
FROM --platform=linux/amd64 python:3.11.7-bookworm AS build
2+
3+
ARG dev=0
4+
ENV POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv
5+
ENV POETRY_VERSION=1.8.3
6+
7+
WORKDIR /app
8+
9+
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
10+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential --no-install-recommends make && \
11+
python3 -m venv "${POETRY_VIRTUALENVS_PATH}" \
12+
&& $POETRY_VIRTUALENVS_PATH/bin/pip install "poetry==${POETRY_VERSION}"
13+
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH"
14+
15+
16+
WORKDIR /app/mcp-server
17+
COPY mcp-server/pyproject.toml mcp-server/poetry.lock ./
18+
19+
RUN mkdir log && chmod 700 log
20+
RUN touch /app/mcp-server/log/logfile.log && chmod 600 /app/mcp-server/log/logfile.log
21+
22+
RUN poetry config virtualenvs.create false &&\
23+
if [ "$dev" = "1" ]; then \
24+
poetry install --no-interaction --no-ansi --no-root --with dev; \
25+
else \
26+
poetry install --no-interaction --no-ansi --no-root; \
27+
fi
28+
29+
FROM --platform=linux/amd64 python:3.11.7-bookworm
30+
ARG dev=0
31+
32+
RUN adduser --disabled-password --gecos "" --uid 65532 nonroot
33+
34+
ENV POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv
35+
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
36+
COPY --from=build /usr/local/bin/ /usr/local/bin/
37+
COPY --from=build /usr/bin/make /usr/bin/make
38+
COPY --from=build /usr/local/lib/ /usr/local/lib/
39+
40+
WORKDIR /app/mcp-server
41+
COPY --chown=nonroot:nonroot mcp-server/src ./src
42+
COPY --chown=nonroot:nonroot mcp-server/pyproject.toml mcp-server/poetry.lock ./
43+
# cleanup
44+
RUN apt-get clean autoclean
45+
RUN apt-get autoremove --yes
46+
47+
RUN if [ "$dev" = "0" ]; then \
48+
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
49+
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
50+
else \
51+
echo "POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv" >> /etc/environment;\
52+
export POETRY_VIRTUALENVS_PATH=/app/mcp-server/.venv;\
53+
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
54+
fi
55+
56+
57+
USER nonroot
58+
COPY --from=build --chown=nonroot:nonroot /app/mcp-server/log /app/mcp-server/log
59+
60+
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"
61+
62+
CMD [ "poetry", "run", "python", "src/main.py" ]

mcp-server/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# MCP Server
2+
3+
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.
4+
5+
## Features 🚀
6+
7+
- **Simple Chat Interface**: Basic question-answering without conversation history
8+
- **Chat with History**: Conversational interface that maintains context across messages
9+
- **Citation Support**: Returns source documents and metadata for transparency
10+
- **Streamable HTTP Transport**: Uses HTTP-based transport for reliable communication
11+
- **Configurable Settings**: Environment-based configuration for different deployment scenarios
12+
13+
## Architecture
14+
15+
The server consists of several key components:
16+
17+
- **RagMcpServer**: Main server class that handles MCP tool registration and request routing
18+
- **Dependency Container**: Manages dependency injection for clean architecture
19+
- **Settings**: Environment-based configuration management
20+
- **RAG Backend Client**: Auto-generated OpenAPI client for backend communication
21+
22+
## Requirements
23+
24+
All required python libraries can be found in the [pyproject.toml](pyproject.toml) file.
25+
The MCP server uses Poetry for dependency management and shares the base Dockerfile pattern with other services in the RAG template.
26+
27+
## Available Tools
28+
29+
The server exposes two main MCP tools for interacting with the RAG system:
30+
31+
### `chat_simple`
32+
33+
Simple question-answering interface that returns plain text responses.
34+
35+
**Parameters:**
36+
37+
- `session_id` (str): Unique identifier for the chat session
38+
- `message` (str): The question or message to send to the RAG system
39+
40+
**Returns:**
41+
42+
- `str`: Plain text answer from the RAG system
43+
44+
### `chat_with_history`
45+
46+
Advanced chat interface that supports conversation history and returns structured responses with citations.
47+
48+
**Parameters:**
49+
50+
- `session_id` (str): Unique identifier for the chat session
51+
- `message` (str): The current question or message
52+
- `history` (list[dict], optional): Previous conversation history
53+
54+
**History Format:**
55+
Each history item should be a dictionary with:
56+
57+
- `role`: Either "user" or "assistant"
58+
- `message`: The message content
59+
60+
**Returns:**
61+
62+
- `dict`: Structured response containing:
63+
- `answer`: The response text
64+
- `finish_reason`: Why the response ended
65+
- `citations`: List of source documents with content and metadata
66+
67+
## Configuration
68+
69+
The server supports configuration through environment variables with the following prefixes:
70+
71+
### MCP Settings (`MCP_` prefix)
72+
73+
- `MCP_HOST`: Server bind address (default: `0.0.0.0`)
74+
- `MCP_PORT`: Server port (default: `8000`)
75+
- `MCP_NAME`: Server name (default: `RAG MCP server`)
76+
77+
### Backend Settings (`BACKEND_` prefix)
78+
79+
- `BACKEND_BASE_PATH`: RAG backend URL (default: `http://127.0.0.1:8080`)
80+
81+
## Deployment
82+
83+
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.
84+
85+
### Docker Support
86+
87+
The server includes Docker support for containerized deployment and is integrated into the main Tilt development workflow.
88+
89+
### Integration in RAG Template
90+
91+
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:
92+
93+
- **Port**: 8000 (configurable via `MCP_PORT`)
94+
- **Endpoint**: `/mcp` path through the main ingress
95+
- **Development**: Port-forwarded to 9090 in local Tilt setup
96+
97+
## Development
98+
99+
The MCP server is integrated into the main RAG template development workflow:
100+
101+
- **Tilt Integration**: Automatically built and deployed with live reload
102+
- **Linting**: Included in the main linting pipeline
103+
- **Testing**: Part of the overall test suite
104+
- **Debugging**: Supports the same debugging workflow as other services
105+
106+
For detailed development setup instructions, see the [main README](../README.md).

mcp-server/api-generator.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
4+
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
5+
cd ./mcp-server
6+
black .
7+
cd ..

0 commit comments

Comments
 (0)