Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,19 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- name: Install uv
uses: astral-sh/setup-uv@v5

- name: Install project dependencies
run: npm install

- name: Run build with npm script
run: npm run build
run: uv sync

- name: Lint and format
run: npm run lint
run: |
uv run ruff format . --check
uv run ruff check .

- name: Test worker deployment
run: npm run deploy -- --dry-run
run: uv run pywrangler deploy --dry-run

- name: Run tests
run: npm run test
run: uv run pytest tests
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.venv/
.venv-pyodide/
.venv-workers/
.ruff_cache/
.pytest_cache/
node_modules/
package-lock.json
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
43 changes: 13 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,37 @@ This is an example of a Python Worker that uses the FastMCP package.
>[!NOTE]
>Due to the [size](https://developers.cloudflare.com/workers/platform/limits/#worker-size) of the Worker, this example can only be deployed if you're using the Workers Paid plan. Free plan users will encounter deployment errors because this Worker exceeds the 3MB size limit.

## Adding Packages

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.

### Vendoring Packages

First, install Python3.12 and pip for Python 3.12.

*Currently, other versions of Python will not work - use 3.12!*

Then set up your local pyodide virtual environment:
```console
npm run build
```

### Developing and Deploying
## Developing and Deploying

To develop your Worker run:

```console
npm run dev
uv run pywrangler dev
```

To deploy your Worker run:

```console
npm run deploy
uv run pywrangler deploy
```

### Testing
## Testing

To test run:

```console
npm run test
uv run pytest tests
```

### Linting and Formatting
## Linting and Formatting

This project uses Ruff for linting and formatting:

```console
npm run lint
uv ruff format . --check
uv ruff check .
```

### IDE Integration

To have good autocompletions in your IDE simply select .venv-pyodide/bin/python as your IDE's interpreter.
## IDE Integration

You should also install your dependencies for type hints.

```console
.venv-pyodide/bin/pip install -r requirements-dev.txt
```
To have good autocompletions in your IDE simply select .venv-workers/bin/python as your IDE's interpreter.
16 changes: 0 additions & 16 deletions package.json

This file was deleted.

27 changes: 21 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
[project]
name = "python-workers-mcp"
version = "0.1.0"
requires-python = "==3.12.*"
dependencies = ["mcp", "structlog"]

[dependency-groups]
dev = [
"workers-py@git+https://github.com/cloudflare/workers-py",
"pytest",
"requests",
"pytest-asyncio",
"ruff",
]

[tool.ruff]
target-version = "py312"
line-length = 100
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"F", # pyflakes
"B", # flake8-bugbear
"I", # isort
"E", # pycodestyle errors
"F", # pyflakes
"B", # flake8-bugbear
"I", # isort
"C4", # flake8-comprehensions
"UP", # pyupgrade
"N", # pep8-naming
"RUF", # ruff-specific rules
"N", # pep8-naming
"RUF", # ruff-specific rules
]
ignore = []

Expand Down
9 changes: 0 additions & 9 deletions requirements-dev.txt

This file was deleted.

5 changes: 0 additions & 5 deletions requirements-test.txt

This file was deleted.

43 changes: 0 additions & 43 deletions scripts/build.sh

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/lint.sh

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/test.sh

This file was deleted.

6 changes: 4 additions & 2 deletions src/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

def setup_server():
from mcp.server.fastmcp import FastMCP
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

from exceptions import HTTPException, http_exception

mcp = FastMCP("Demo", stateless_http=True)

@mcp.tool()
Expand All @@ -34,7 +34,9 @@ def echo_prompt(message: str) -> str:

app = mcp.streamable_http_app()
app.add_exception_handler(HTTPException, http_exception)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
app.add_middleware(
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
)
return mcp, app


Expand Down
Loading