Skip to content

Commit cb2b511

Browse files
committed
weather tol
1 parent 3775fdd commit cb2b511

20 files changed

Lines changed: 680 additions & 6 deletions

.github/workflows/mcp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717
DEFAULT_WORKSPACE: "${{github.workspace}}/LLM/MCP" #
1818
jobs:
1919
helloWorldDockerPackage:
20-
name: "Hello World Docker package"
20+
name: "MCP Hello World - Docker image build"
2121
runs-on: ubuntu-latest # https://github.com/actions/runner-images
2222
outputs:
2323
image_todays_tag: ${{ steps.set_var.outputs.TODAYS_DATE }}
@@ -161,7 +161,7 @@ jobs:
161161
echo " " >> $GITHUB_STEP_SUMMARY
162162
163163
helloWorldDockerPackageImageTest:
164-
name: "Hello World Docker image test"
164+
name: "MCP Hello World - Docker image test"
165165
runs-on: ubuntu-latest
166166
needs: helloWorldDockerPackage
167167
env:

LLM/MCP/1-hello-world/mcp.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ prereq(){
1010
}
1111
start(){
1212
printf "\n ---------------------------------------------------------------- "
13-
printf "\n Starting Hello World MCP Application ... "
13+
printf "\n Starting MCP: Hello World service using SSE transport ... "
1414
printf "\n ---------------------------------------------------------------- \n "
1515
uv run src/main.py &
1616
sleep 5
@@ -24,12 +24,16 @@ build(){
2424
}
2525
stop(){
2626
printf "\n ---------------------------------------------------------------- "
27-
printf "\n Stopping Hello World MCP Application ... "
27+
printf "\n Stopping MCP: Hello World service using SSE transport ... "
2828
printf "\n ---------------------------------------------------------------- \n "
2929

3030
# uv stop
31-
kill -9 $(lsof -ti tcp:8000)
32-
exit 0
31+
# kill -9 $(lsof -ti tcp:8000) &
32+
# Kill process on port 8000, skip if error (no process running)
33+
pid=$(lsof -ti tcp:8000 2>/dev/null)
34+
if [ -n "$pid" ]; then
35+
kill -9 "$pid" 2>/dev/null || true
36+
fi
3337
}
3438
test(){
3539
test-web
@@ -87,6 +91,7 @@ if [[ -n $arg ]] ; then
8791
;;
8892
STOP)
8993
stop
94+
exit 0
9095
;;
9196
*)
9297
echo "Invalid argument: $arg"

LLM/MCP/3-weather/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://hub.docker.com/_/python
2+
FROM python:3.12-slim
3+
4+
# Copy the uv binary from the astral-sh/uv image
5+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
6+
7+
RUN apt-get update && apt-get install -y curl git gnupg && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs && npm install -g npm@latest && apt-get clean && rm -rf /var/lib/apt/lists/*
8+
9+
# Copy the project into the image
10+
COPY ./ /app/
11+
12+
# Disable development dependencies
13+
ENV UV_NO_DEV=1
14+
15+
# Sync the project into a new environment, asserting the lockfile is up to date
16+
WORKDIR /app
17+
RUN uv lock && uv sync --frozen && uv pip install -e ".[dev]"
18+
19+
# # RUN tests
20+
# RUN uv run pytest tests/maintests.py -v
21+
22+
# Expose the port
23+
EXPOSE 8000
24+
25+
# # Health check
26+
# HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
27+
# CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
28+
29+
# Run the application
30+
# uv run src/main.py
31+
CMD ["uv", "run", "src/main.py"]

LLM/MCP/3-weather/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Hello World MCP Example
2+
3+
## Pre-requisites
4+
5+
- [uv](https://github.com/astral-sh/uv) (Fast Python package installer and resolver)
6+
7+
## Setup
8+
9+
Install dependencies:
10+
- ruff install info at https://docs.astral.sh/ruff/installation/
11+
```bash
12+
uv pip install -e ".[dev]" && uv sync
13+
```
14+
15+
## Running the Server
16+
17+
Run the MCP server (configured to use SSE transport):
18+
19+
```bash
20+
uv run src/main.py
21+
```
22+
23+
- The server will start on `http://127.0.0.1:8000`
24+
25+
## Testing
26+
27+
### In Browser: Validating the Server
28+
29+
- [http://127.0.0.1:8000](http://127.0.0.1:8000)
30+
![Inde Page](./images/indexPage.png)
31+
32+
- [http://127.0.0.1:8000?name=krishna](http://127.0.0.1:8000?name=krishna)
33+
![Inde Page with parms](./images/indexPageWithParms.png)
34+
35+
### Unit Testing
36+
37+
```bash
38+
uv run pytest tests/maintests.py -v
39+
```
40+
![Unit Tests](./images/unittests.png)
41+
42+
### Manual Testing via Curl (MCP Protocol)
43+
44+
To test the MCP protocol fully via `curl`, you need to simulate an MCP client. This involves two steps:
45+
46+
**1. Connect to SSE Stream**
47+
48+
Open a terminal and listen to the event stream:
49+
50+
```bash
51+
curl -N http://127.0.0.1:8000/sse
52+
```
53+
![SSE Stream](./images/sseStream.png)
54+
55+
*Keep this running.* You will see an `endpoint` event containing a URL (e.g., `/messages/?session_id=...`). Copy this URL.
56+
57+
**2. Send JSON-RPC Requests**
58+
59+
In a **new terminal**, use the URL you copied to send requests.
60+
61+
**Initialize:**
62+
63+
```bash
64+
curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \
65+
-H "Content-Type: application/json" \
66+
-d '{
67+
"jsonrpc": "2.0",
68+
"id": 1,
69+
"method": "initialize",
70+
"params": {
71+
"protocolVersion": "2024-11-05",
72+
"capabilities": {},
73+
"clientInfo": { "name": "curl-client", "version": "1.0" }
74+
}
75+
}'
76+
```
77+
![Messages Curl](./images/messages-curl.png)
78+
79+
![Messages Stream](./images/messages-stream.png)
80+
81+
82+
**List Tools:**
83+
84+
```bash
85+
curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \
86+
-H "Content-Type: application/json" \
87+
-d '{
88+
"jsonrpc": "2.0",
89+
"id": 2,
90+
"method": "tools/list"
91+
}'
92+
```
93+
![Tools list curl](./images/tools-list-curl.png)
94+
![Tools list resp](./images/tools-list-resp.png)
95+
96+
**Call Tool (index):**
97+
98+
```bash
99+
curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \
100+
-H "Content-Type: application/json" \
101+
-d '{
102+
"jsonrpc": "2.0",
103+
"id": 3,
104+
"method": "tools/call",
105+
"params": {
106+
"name": "index",
107+
"arguments": {}
108+
}
109+
}'
110+
```
111+
112+
![Tools call curl](./images/tools-call-index-req.png)
113+
![Tools call resp](./images/tools-call-index-resp.png)
114+
115+
**Call Tool (greeting):**
116+
117+
```bash
118+
curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \
119+
-H "Content-Type: application/json" \
120+
-d '{
121+
"jsonrpc": "2.0",
122+
"id": 4,
123+
"method": "tools/call",
124+
"params": {
125+
"name": "greeting",
126+
"arguments": { "name": "Krishna" }
127+
}
128+
}'
129+
```
130+
131+
![Tools call greeting curl](./images/tools-call-greeting-req.png)
132+
![Tools call greeting resp](./images/tools-call-greeting-resp.png)
133+
134+
### MCP Inspector
135+
136+
The [MCP Inspector](https://github.com/modelcontextprotocol/inspector) is a developer tool for testing and debugging MCP servers.
137+
138+
Run the inspector (this uses `npx` to run the inspector, which then runs your server in `stdio` mode):
139+
140+
```bash
141+
npx @modelcontextprotocol/inspector uv run src/main.py --transport stdio
142+
```
20.4 KB
Loading
29.6 KB
Loading
108 KB
Loading
129 KB
Loading
68.8 KB
Loading
107 KB
Loading

0 commit comments

Comments
 (0)