Skip to content

Commit 716eb1f

Browse files
committed
FastMCP working
1 parent e817d75 commit 716eb1f

File tree

15 files changed

+418
-544
lines changed

15 files changed

+418
-544
lines changed

.github/workflows/python-ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Python CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
lint-and-format:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Set up Python 3.12
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.12'
19+
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install ruff
24+
25+
- name: Lint with Ruff
26+
run: |
27+
ruff check .
28+
ruff format --check .
29+

.github/workflows/test.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Test Deploy CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
vendoring:
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, macos-latest] # , windows-latest
16+
runs-on: ${{ matrix.os }}
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: '3.12'
26+
27+
- name: Install Node.js
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: 22
31+
32+
- name: Install Wrangler
33+
run: npm install -g wrangler
34+
35+
- name: Run vendoring (Linux/macOS)
36+
if: runner.os != 'Windows'
37+
run: |
38+
python3.12 -m venv .venv
39+
source .venv/bin/activate
40+
.venv/bin/pip install pyodide-build
41+
.venv/bin/pyodide venv .venv-pyodide
42+
.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt
43+
44+
# Windows is not supported by pyodide.
45+
# - name: Run vendoring (Windows)
46+
# if: runner.os == 'Windows'
47+
# run: |
48+
# python -m venv .venv
49+
# .\.venv\Scripts\Activate.ps1
50+
# .venv\Scripts\pip install pyodide-build
51+
# .venv\Scripts\pyodide venv .venv-pyodide
52+
# .venv-pyodide\Scripts\pip install -t src/vendor -r vendor.txt
53+
54+
- name: Test worker deployment
55+
run: wrangler deploy --dry-run
56+
57+
- name: Run tests (Linux/macOS)
58+
if: runner.os != 'Windows'
59+
run: |
60+
source .venv/bin/activate
61+
pip install -r requirements-test.txt
62+
pytest tests

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.venv/
2+
.venv-pyodide/
3+
.pytest_cache/
24
node_modules/
5+
__pycache__/
36
src/vendor/
4-
.vscode/
7+
.vscode/
8+
.wrangler/

README.md

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
# Vendoring Packages: FastAPI + Jinja2 Example
1+
# Python Workers: FastMCP Example
22

3-
*Note: You must have Python Packages enabled on your account for built-in packages to work. Request Access to our Closed Beta using [This Form](https://forms.gle/FcjjhV3YtPyjRPaL8)*
4-
5-
This is an example of a Python Worker that uses a built-in package (FastAPI) with a vendored package (Jinja2).
3+
This is an example of a Python Worker that uses the FastMCP package.
64

75
## Adding Packages
86

9-
Built-in packages can be selected from [this list](https://developers.cloudflare.com/workers/languages/python/packages/#supported-packages) and added to your `requirements.txt` file. These can be used with no other explicit install step.
10-
117
Vendored packages are added to your source files and need to be installed in a special manner. The Python Workers team plans to make this process automatic in the future, but for now, manual steps need to be taken.
128

139
### Vendoring Packages
1410

15-
[//]: # (NOTE: when updating the instructions below, be sure to also update the vendoring.yml CI workflow)
16-
1711
First, install Python3.12 and pip for Python 3.12.
1812

1913
*Currently, other versions of Python will not work - use 3.12!*
@@ -30,36 +24,32 @@ Within our virtual environment, install the pyodide CLI:
3024
.venv/bin/pyodide venv .venv-pyodide
3125
```
3226

33-
Next, add packages to your vendor.txt file. Here we'll add jinja2
34-
```
35-
jinja2
36-
```
37-
38-
Lastly, add these packages to your source files at `src/vendor`. For any additional packages, re-run this command.
27+
Lastly, download the vendored packages. For any additional packages, re-run this command.
3928
```console
4029
.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt
4130
```
4231

43-
### Using Vendored packages
32+
### Developing and Deploying
4433

45-
In your wrangler.toml, make the vendor directory available:
34+
To develop your Worker, run `npx wrangler@latest dev`.
4635

47-
```toml
48-
[[rules]]
49-
globs = ["vendor/**"]
50-
type = "Data"
51-
fallthrough = true
52-
```
36+
To deploy your Worker, run `npx wrangler@latest deploy`.
5337

54-
Now, you can import and use the packages:
38+
### Testing
5539

56-
```python
57-
import jinja2
58-
# ... etc ...
40+
To test run:
41+
```console
42+
source .venv/bin/activate
43+
pip install -r requirements-test.txt
44+
pytest tests
5945
```
6046

61-
### Developing and Deploying
47+
### Linting and Formatting
6248

63-
To develop your Worker, run `npx wrangler@latest dev`.
49+
This project uses Ruff for linting and formatting:
6450

65-
To deploy your Worker, run `npx wrangler@latest deploy`.
51+
```console
52+
pip install ruff
53+
ruff check . # Run linting
54+
ruff format . # Format code
55+
```

pyproject.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[tool.ruff]
2+
target-version = "py312"
3+
line-length = 100
4+
[tool.ruff.lint]
5+
select = [
6+
"E", # pycodestyle errors
7+
"F", # pyflakes
8+
"B", # flake8-bugbear
9+
"I", # isort
10+
"C4", # flake8-comprehensions
11+
"UP", # pyupgrade
12+
"N", # pep8-naming
13+
"RUF", # ruff-specific rules
14+
]
15+
ignore = []
16+
17+
[tool.ruff.lint.isort]
18+
known-first-party = ["src"]
19+
20+
[tool.ruff.format]
21+
quote-style = "double"
22+
indent-style = "space"
23+
line-ending = "auto"
24+
skip-magic-trailing-comma = false

requirements-test.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest
2+
requests
3+
mcp
4+
pytest-asyncio
5+
ruff

0 commit comments

Comments
 (0)