Python Workers: FastMCP Example

by davila7

327 downloads
Not rated
GitHub

Description

# Python Workers: FastMCP Example This is an example of a Python Worker that uses the FastMCP package. [![Deploy to Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/python-workers-m…

About

# Python Workers: FastMCP Example This is an example of a Python Worker that uses the FastMCP package. [![Deploy to Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/python-workers-mcp) >[!NOTE] >Due to the…

Details

Author
davila7
Downloads
327
Categories
Developer Tools

- Example of a Python Worker using FastMCP
- Supports vendored packages for Worker compatibility
- Requires Python 3.12 for package management
- Deployment limited to Workers Paid plan
- Includes testing with pytest and linting with Ruff

To develop, run npx wrangler@latest dev. To deploy, run npx wrangler@latest deploy. Before deployment, packages must be vendored using Python 3.12 with a special process described in the README.

Python Workers: FastMCP Example

This is an example of a Python Worker that uses the FastMCP package.

Deploy to Workers

>[!NOTE]
>Due to the 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 create a virtual environment and activate it from your shell:

python3.12 -m venv .venv
source .venv/bin/activate

Within our virtual environment, install the pyodide CLI:

.venv/bin/pip install pyodide-build
.venv/bin/pyodide venv .venv-pyodide

Lastly, download the vendored packages. For any additional packages, re-run this command.

.venv-pyodide/bin/pip install -t src/vendor -r vendor.txt

Developing and Deploying

To develop your Worker, run npx wrangler@latest dev.

To deploy your Worker, run npx wrangler@latest deploy.

Testing

To test run:

source .venv/bin/activate
pip install -r requirements-test.txt
pytest tests

Linting and Formatting

This project uses Ruff for linting and formatting:

pip install ruff
ruff check .  # Run linting
ruff format .  # Format code
No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.