-
Notifications
You must be signed in to change notification settings - Fork 69
Add MCP server example #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47e4180
Add MCP server example
ryanking13 4d63826
Add deploy to cloudflare button
ryanking13 b6f6531
More readme
ryanking13 4d099f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7065a31
typo
ryanking13 38734ea
rename
ryanking13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,6 @@ pylock.toml | |
| .venv-workers | ||
| .pytest_cache | ||
| .ruff_cache | ||
|
|
||
| **/.DS_Store | ||
| **/default.profraw | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # MCP Server with D1 | ||
|
|
||
| [](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/python-workers-examples/tree/main/mcp-server) | ||
|
|
||
| This example shows how to create a simple [Model Context Protocol](https://modelcontextprotocol.io/) server. | ||
| It uses the official [Python MCP SDK](https://py.sdk.modelcontextprotocol.io/) and a D1 database. | ||
|
|
||
| It implements 3 tools, as a simple incident management system: | ||
|
|
||
| - `open_incident(title, severity)` | ||
| - `add_update(id, note)` | ||
| - `list_open_incidents()` | ||
|
|
||
| This is based on the stateless MCP protocol version `2026-07-28`. | ||
|
|
||
| ## Development | ||
|
|
||
| Install [uv](https://docs.astral.sh/uv/getting-started/installation/), then | ||
| initialize local D1 and start the Worker: | ||
|
|
||
| ```sh | ||
| uv run pywrangler d1 migrations apply mcp-incidents --local | ||
| uv run pywrangler dev | ||
| ``` | ||
|
|
||
| Use modern JSON-RPC requests with the protocol header. For example, discover | ||
| the server and list its tools: | ||
|
|
||
| ```sh | ||
| curl -X POST http://localhost:8787/mcp \ | ||
| -H 'Content-Type: application/json' \ | ||
| -H 'Accept: application/json, text/event-stream' \ | ||
| -H 'MCP-Protocol-Version: 2026-07-28' \ | ||
| -H 'Mcp-Method: server/discover' \ | ||
| --data '{"jsonrpc":"2.0","id":1,"method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}' | ||
|
|
||
| curl -X POST http://localhost:8787/mcp \ | ||
| -H 'Content-Type: application/json' \ | ||
| -H 'Accept: application/json, text/event-stream' \ | ||
| -H 'MCP-Protocol-Version: 2026-07-28' \ | ||
| -H 'Mcp-Method: tools/call' \ | ||
| -H 'Mcp-Name: open_incident' \ | ||
| --data '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}},"name":"open_incident","arguments":{"title":"API latency","severity":"high"}}}' | ||
| ``` | ||
|
|
||
| You can also use your favorite MCP client to test the server. For example, if you are using VS Code, add the following to your settings: | ||
|
|
||
| ```json | ||
| { | ||
| "servers": { | ||
| "incident-server": { | ||
| "type": "http", | ||
| "url": "http://localhost:8787/mcp" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| and your MCP client should be able to discover and use the server. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| CREATE TABLE incidents ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| title TEXT NOT NULL, | ||
| severity TEXT NOT NULL, | ||
| status TEXT NOT NULL DEFAULT 'open', | ||
| created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
||
| CREATE TABLE incident_updates ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| incident_id INTEGER NOT NULL REFERENCES incidents(id) ON DELETE CASCADE, | ||
| note TEXT NOT NULL, | ||
| created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "name": "mcp-server", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "deploy": "uv run pywrangler deploy", | ||
| "dev": "uv run pywrangler dev", | ||
| "start": "uv run pywrangler dev" | ||
| }, | ||
| "devDependencies": { | ||
| "wrangler": "^4.114.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [project] | ||
| name = "mcp-server" | ||
| version = "0.0.0" | ||
| description = "An MCP incident server using Python Workers and D1" | ||
| readme = "README.md" | ||
| requires-python = ">=3.14" | ||
| dependencies = [ | ||
| "mcp==2.2.0" | ||
| ] | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "workers-py", | ||
| "workers-runtime-sdk" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| from urllib.parse import urlparse | ||
|
|
||
| from workers import Response, WorkerEntrypoint, asgi | ||
|
|
||
|
|
||
| def build_app(env): | ||
| from mcp.server import MCPServer | ||
|
|
||
| server = MCPServer("Incident server") | ||
|
|
||
| @server.tool() | ||
| async def open_incident(title: str, severity: str) -> dict: | ||
| """Open an incident with a title and severity, then return the new incident.""" | ||
| if len(title) > 200 or len(severity) > 32: | ||
| return {"error": "title or severity is too long"} | ||
|
|
||
| row = ( | ||
| await env.DB.prepare( | ||
| """ | ||
| INSERT INTO incidents (title, severity) | ||
| VALUES (?, ?) | ||
| RETURNING id, title, severity, status, created_at, updated_at | ||
| """ | ||
| ) | ||
| .bind(title, severity) | ||
| .first() | ||
| ) | ||
| return { | ||
| "id": row.id, | ||
| "title": row.title, | ||
| "severity": row.severity, | ||
| "status": row.status, | ||
| "created_at": row.created_at, | ||
| "updated_at": row.updated_at, | ||
| } | ||
|
|
||
| @server.tool() | ||
| async def add_update(id: int, note: str) -> dict: | ||
| """Add a note to an open incident identified by id and return the update.""" | ||
| if len(note) > 4000: | ||
| return {"error": "note is too long", "id": id} | ||
|
|
||
| incident = ( | ||
| await env.DB.prepare("SELECT id FROM incidents WHERE id = ?") | ||
| .bind(id) | ||
| .first() | ||
| ) | ||
| if incident is None: | ||
| return {"error": "incident not found", "id": id} | ||
|
|
||
| results = await env.DB.batch( | ||
| [ | ||
| env.DB.prepare( | ||
| """ | ||
| INSERT INTO incident_updates (incident_id, note) | ||
| VALUES (?, ?) | ||
| RETURNING id, incident_id, note, created_at | ||
| """ | ||
| ).bind(id, note), | ||
| env.DB.prepare( | ||
| "UPDATE incidents SET updated_at = CURRENT_TIMESTAMP WHERE id = ?" | ||
| ).bind(id), | ||
| ] | ||
| ) | ||
| update = results[0].results[0] | ||
| return { | ||
| "id": update.id, | ||
| "incident_id": update.incident_id, | ||
| "note": update.note, | ||
| "created_at": update.created_at, | ||
| } | ||
|
|
||
| @server.tool() | ||
| async def list_open_incidents() -> list[dict]: | ||
| """List open incidents in newest-first order, including their chronological updates.""" | ||
| rows = await env.DB.prepare( | ||
| """ | ||
| SELECT | ||
| i.id AS id, | ||
| i.title AS title, | ||
| i.severity AS severity, | ||
| i.status AS status, | ||
| i.created_at AS created_at, | ||
| i.updated_at AS updated_at, | ||
| u.id AS update_id, | ||
| u.incident_id AS update_incident_id, | ||
| u.note AS update_note, | ||
| u.created_at AS update_created_at | ||
| FROM incidents i | ||
| LEFT JOIN incident_updates u ON u.incident_id = i.id | ||
| WHERE i.status = 'open' | ||
| ORDER BY i.created_at DESC, i.id DESC, u.created_at ASC, u.id ASC | ||
| """ | ||
| ).all() | ||
|
|
||
| incidents_by_id = {} | ||
| result = [] | ||
| for row in rows.results: | ||
| incident = incidents_by_id.get(row.id) | ||
| if incident is None: | ||
| incident = { | ||
| "id": row.id, | ||
| "title": row.title, | ||
| "severity": row.severity, | ||
| "status": row.status, | ||
| "created_at": row.created_at, | ||
| "updated_at": row.updated_at, | ||
| "updates": [], | ||
| } | ||
| incidents_by_id[row.id] = incident | ||
| result.append(incident) | ||
| if row.update_id is not None: | ||
| incident["updates"].append( | ||
| { | ||
| "id": row.update_id, | ||
| "incident_id": row.update_incident_id, | ||
| "note": row.update_note, | ||
| "created_at": row.update_created_at, | ||
| } | ||
| ) | ||
| return result | ||
|
|
||
| app = server.streamable_http_app( | ||
| streamable_http_path="/mcp", | ||
| stateless_http=True, | ||
| ) | ||
|
|
||
| return app | ||
|
|
||
|
|
||
| class Default(WorkerEntrypoint): | ||
| def __init__(self, ctx, env): | ||
| super().__init__(ctx, env) | ||
| self.app = build_app(env) | ||
|
|
||
| async def fetch(self, request): | ||
| path = urlparse(request.url).path | ||
| if path == "/mcp" and request.method == "POST": | ||
| return await asgi.fetch(self.app, request, self.env) | ||
|
|
||
| return Response("Not found", status=404) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "mcp-server", | ||
| "main": "src/entry.py", | ||
| "compatibility_date": "2026-09-07", | ||
| "compatibility_flags": [ | ||
| "python_workers", | ||
| "python_workers_314" | ||
| ], | ||
| "d1_databases": [ | ||
| { | ||
| "binding": "DB", | ||
| "database_name": "mcp-incidents", | ||
| "database_id": "00000000-0000-0000-0000-000000000000", // Replace with your D1 database UUID. | ||
| "migrations_dir": "./migrations" | ||
| } | ||
| ], | ||
| "observability": { | ||
| "enabled": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.