Commit 5922d220 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

adding dockerfile and example dockercompose

parent dca12699
Loading
Loading
Loading
Loading

Dockerfile

0 → 100644
+32 −0
Original line number Diff line number Diff line
# Build stage
FROM maven:3.9-eclipse-temurin-17 AS builder

WORKDIR /build

# Copy pom.xml and download dependencies
COPY pom.xml .
RUN mvn dependency:go-offline

# Copy source code
COPY src ./src

# Build application
RUN mvn clean package -DskipTests

# Runtime stage
FROM eclipse-temurin:17-jre-alpine

WORKDIR /app

# Copy built JAR from builder stage
COPY --from=builder /build/target/org.etsi.osl.mcp.backend-0.0.1-SNAPSHOT.jar app.jar

# Expose port
EXPOSE 11880

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:11880/actuator/health || exit 1

# Run application
ENTRYPOINT ["java", "-jar", "app.jar"]
+167 −56
Original line number Diff line number Diff line
# org.etsi.org.mcp.backend
# org.etsi.osl.mcp.backend

A Spring Boot backend service that integrates Large Language Models (LLMs) with the Model Context Protocol (MCP) for the OpenSlice platform. It provides REST and message-driven interfaces for AI-powered question answering with access to external tools and context via MCP servers.

## Features

## Getting started
- **REST API** - `/ask` endpoint for synchronous question processing
- **Message-Driven** - ActiveMQ queue listener for asynchronous question submission
- **MCP Integration** - Connects to MCP servers (e.g., OpenSlice) to provide tools and context to LLMs
- **OAuth2 Authentication** - Keycloak-based security for MCP server access
- **HTML Response Formatting** - Markdown to HTML conversion for web compatibility
- **Local LLM Support** - Ollama integration for running open-source models locally

To make it easy for you to get started with GitLab, here's a list of recommended next steps.
## Requirements

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
- Java 17+
- Maven 3.9+
- Ollama (for local LLM inference, default: `http://localhost:11434`)
- ActiveMQ broker (default: `tcp://localhost:61616`)
- Keycloak (for OAuth2, default: `http://keycloak:8080/auth/realms/openslice`)
- MCP Server (e.g., OpenSlice at `http://localhost:13015`)

## Add your files
## Quick Start

- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
### Build
```bash
mvn clean package
```

### Run Locally
```bash
mvn spring-boot:run
```
cd existing_repo
git remote add origin https://labs.etsi.org/rep/osl/code/org.etsi.org.mcp.backend.git
git branch -M main
git push -uf origin main

The application starts on port **11880**.

### Docker
```bash
docker build -t org.etsi.osl.mcp.backend:latest .
docker run -p 11880:11880 org.etsi.osl.mcp.backend:latest
```

## Integrate with your tools
### Docker Compose (Complete Stack)
The easiest way to run the entire application stack with all dependencies:

- [ ] [Set up project integrations](https://labs.etsi.org/rep/osl/code/org.etsi.org.mcp.backend/-/settings/integrations)
```bash
docker-compose up -d
```

## Collaborate with your team
This starts:
- Ollama LLM server (http://localhost:11434)
- OSL MCP Backend (http://localhost:11880)

- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
After services are running, pull an LLM model:
```bash
docker-compose exec ollama ollama pull gpt-oss:20b
```

## Test and Deploy
Check service status:
```bash
docker-compose ps
```

Use the built-in continuous integration in GitLab.
View logs:
```bash
docker-compose logs -f osl-mcp-backend
```

- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
Stop all services:
```bash
docker-compose down
```

***
See `docker-compose.yml` for configuration details and environment variables.

# Editing this README
## Usage

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
### REST API Example
```bash
curl -X POST http://localhost:11880/ask \
  -H "Content-Type: application/json" \
  -d '{"question": "What is the weather today?"}'
```

## Suggestions for a good README
Response:
```json
{
  "answer": "<html><body>...</body></html>"
}
```

Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
### ActiveMQ Integration
Send a message to the queue `agents/osl-mcp-backend` via ActiveMQ, and the response will be processed by the Camel route.

## Name
Choose a self-explaining name for your project.
## Configuration

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
Edit `src/main/resources/application.yaml` to customize:

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
- **Server port** - Change `server.port`
- **LLM model** - Adjust `spring.ai.ollama.chat.options.model`
- **MCP server URL** - Update `spring.ai.mcp.client.streamable-http.connections.openslice-server.url`
- **OAuth2 credentials** - Configure `spring.security.oauth2.client.registration.authserver`
- **ActiveMQ broker** - Modify `spring.activemq.brokerUrl`
- **Logging level** - Adjust `logging.level`

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Testing

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
```bash
mvn test
```

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
Run a single test:
```bash
mvn test -Dtest=ClassName#testMethodName
```

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Architecture

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
### High-Level Design

## Contributing
State if you are open to contributions and what your requirements are for accepting them.
The application follows a layered architecture with three main integration points:

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
1. **REST Layer** - `ChatController` provides HTTP endpoints for synchronous question processing
2. **Message Layer** - Apache Camel routes handle asynchronous message processing from ActiveMQ
3. **AI/Context Layer** - Spring AI ChatClient combines LLM capabilities with MCP tools and context

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
### Core Components

#### ChatController
- **Endpoint**: `POST /ask`
- **Function**: Accepts questions, invokes ChatClient, converts responses to HTML
- **Dependencies**: Spring AI ChatClient, MarkdownHelper
- **Methods**:
  - `ask()` - HTTP endpoint for REST clients
  - `simpleAsk()` - Internal method for Camel route integration

#### MCP Configuration
- Sets up OAuth2-secured synchronous HTTP connections to MCP servers
- Configures authentication with Keycloak for OAuth2 client credentials flow
- Manages MCP client customization with security context providers

#### Apache Camel Routes
- **Queue Listener**: `jms:queue:agents/[application-name]`
- **Message Flow**: ActiveMQ → Camel Route → ChatController.simpleAsk() → Response
- **Purpose**: Enables external systems to submit questions asynchronously

#### ActiveMQ Integration
- Provides connection pooling for JMS communication
- Routes incoming messages to the backend for processing
- Allows distributed systems to queue questions for batch processing

### Data Flow

```
REST Client                          External System
    |                                     |
    v                                     v
POST /ask                          ActiveMQ Queue
    |                                     |
    +-----------> ChatController <--------+
                      |
                      v
              Spring AI ChatClient
                      |
          +----------+----------+
          v                     v
      Ollama LLM            MCP Server
      (Language)            (Tools/Context)
          |                     |
          +----------+----------+
                      v
              HTML Response
                      |
          +----------+----------+
          v                     v
   REST Response         Queue Response
```

### Key Dependencies

- **Spring Boot 3.5.5** - Application framework
- **Spring AI 1.1.0** - LLM and MCP client integration
- **Apache Camel 4.0.0-RC2** - Message routing and orchestration
- **Spring Security** - OAuth2 authentication
- **CommonMark** - Markdown to HTML conversion

### External Services

- **Ollama** - Local LLM inference server
- **Keycloak** - OAuth2 authentication and authorization
- **ActiveMQ** - Message broker for async processing
- **MCP Server** - Provides tools and context to the LLM

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.

## License
For open source projects, say how it is licensed.

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
This project is part of OpenSlice by ETSI. See LICENSE file for details.

## References

- [OpenSlice](https://osl.etsi.org)
- [Spring AI Documentation](https://spring.io/projects/spring-ai)
- [Model Context Protocol](https://modelcontextprotocol.io)
- [Apache Camel](https://camel.apache.org)

docker-compose.yml

0 → 100644
+45 −0
Original line number Diff line number Diff line
version: '3.8'

services:

  # Ollama LLM Server
  ollama:
    image: ollama/ollama:latest
    container_name: osl-ollama
    ports:
      - "11434:11434"
    environment:
      OLLAMA_HOST: 0.0.0.0:11434
    volumes:
      - ollama_data:/root/.ollama
    # Note: You may need to pull a model after starting:
    # docker exec osl-ollama ollama pull gpt-oss:20b
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
      interval: 30s
      timeout: 10s
      retries: 3

  # OSL MCP Backend
  osl-mcp-backend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: osl-mcp-backend
    ports:
      - "11880:11880"
    environment:
      SPRING_AI_OLLAMA_BASE_URL: http://ollama:11434
      SPRING_AI_MCP_CLIENT_STREAMABLE_HTTP_CONNECTIONS_OPENSLICE_SERVER_URL: http://mcp-server:13015
      SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_AUTHSERVER_ISSUER_URI: http://keycloak:8080/auth/realms/openslice
      SPRING_ACTIVEMQ_BROKERURL: tcp://activemq:61616
      SPRING_ACTIVEMQ_USER: artemis
      SPRING_ACTIVEMQ_PASSWORD: artemis
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11880/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  ollama_data: