Skip to content

Commit 9369cfa

Browse files
authored
Merge pull request #313 from DefangLabs/mcp-with-ui-proxy
MCP Sample
2 parents 23bd272 + ceb4480 commit 9369cfa

29 files changed

+8979
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/typescript-node:22-bookworm
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: ANTHROPIC_API_KEY
24+
env:
25+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

samples/mcp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

samples/mcp/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# MCP
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-mcp-template%26template_owner%3DDefangSamples)
4+
5+
This is a sample of an MCP (Model Context Protocol) chatbot application built with Next.js, Python, and Anthropic Claude, deployed using Defang.
6+
7+
This example uses Docker's [`mcp/time`](https://hub.docker.com/r/mcp/time) image as a base for the MCP server (with MCP tools included), but it can be adapted to use any of the Docker [MCP images](https://hub.docker.com/u/mcp).
8+
9+
### How It Works
10+
11+
The [MCP client](https://modelcontextprotocol.io/quickstart/client) is written in Python and ran in a `venv` virtual environment. The MCP server is provided by the Docker `mcp/time` image. The MCP server communicates with the MCP client in a Quart app (i.e. Asynchronous Server Gateway Interface (ASGI) version of Flask) through the `stdio` transport method, as seen in `/mcp-server/main.py`. For more on MCP transport methods, see [here](https://modelcontextprotocol.io/docs/concepts/transports).
12+
13+
The UI (User Interface) and web server are built in Next.js (see `/ui/src/app`). The web server includes a forwarding action to connect to the MCP client.
14+
15+
Here's a breakdown of what happens when you interact with the UI:
16+
1. When a user submits a query to the chatbot, the browser sends a request to the Next.js web server.
17+
2. The Next.js web server will forward this request to the MCP client via a REST endpoint.
18+
3. The MCP client processes the request by interacting with the Anthropic (Claude) API and tools available through the MCP server.
19+
4. Once the response is generated, it is sent back to the Next.js web server and displayed to the user in the UI.
20+
21+
## Prerequisites
22+
23+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
24+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
25+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
26+
27+
## Development
28+
29+
To run the application locally, you can use the following command:
30+
31+
```bash
32+
docker compose up --build
33+
```
34+
35+
## Configuration
36+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
37+
38+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
39+
40+
### `ANTHROPIC_API_KEY`
41+
An API key for accessing the [Anthropic Claude API](https://docs.anthropic.com/en/api/getting-started).
42+
```bash
43+
defang config set ANTHROPIC_API_KEY
44+
```
45+
46+
## Deployment
47+
48+
> [!NOTE]
49+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
50+
51+
### Defang Playground
52+
53+
Deploy your application to the Defang Playground by opening up your terminal and typing:
54+
```bash
55+
defang compose up
56+
```
57+
58+
### BYOC
59+
60+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
61+
62+
---
63+
64+
Title: MCP
65+
66+
Short Description: An MCP (Model Context Protocol) chatbot assistant built with Next.js, Python, and Anthropic Claude.
67+
68+
Tags: MCP, Next.js, Python, Quart, Claude, AI, Anthropic, TypeScript, React, JavaScript
69+
70+
Languages: nodejs

samples/mcp/compose.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
services:
2+
ui:
3+
# uncomment to add your own domain
4+
# domainname: example.com
5+
build:
6+
context: ./ui
7+
dockerfile: Dockerfile
8+
ports:
9+
- target: 3000
10+
published: 3000
11+
mode: ingress
12+
deploy:
13+
resources:
14+
reservations:
15+
memory: 256M
16+
environment:
17+
- MCP_SERVER_URL=http://mcp-server:8000
18+
healthcheck:
19+
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
20+
21+
mcp-server:
22+
build:
23+
context: ./mcp-server
24+
dockerfile: Dockerfile
25+
ports:
26+
- target: 8000
27+
published: 8000
28+
mode: host
29+
environment:
30+
- ANTHROPIC_API_KEY
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
.env

samples/mcp/mcp-server/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
venv
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

samples/mcp/mcp-server/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM mcp/time:latest
2+
3+
WORKDIR /wrapper
4+
5+
COPY requirements.txt ./requirements.txt
6+
7+
# create a virtual environment
8+
RUN python3 -m venv venv && \
9+
# activate the virtual environment
10+
. venv/bin/activate && \
11+
pip3 install --upgrade pip && \
12+
pip3 install -r requirements.txt && \
13+
# deactivate the virtual environment
14+
deactivate
15+
16+
COPY . .
17+
18+
ENTRYPOINT ["./run.sh"]

0 commit comments

Comments
 (0)