Skip to content

Commit 1bb2b41

Browse files
authored
docs: update samples and readme for current version (#38)
1 parent 61ecac1 commit 1bb2b41

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

samples/README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,36 @@ See [the documentation](https://mcp-auth.dev/docs) for the full guide.
66

77
## Prerequisites
88

9+
### Virtual environment setup
10+
11+
First, navigate to the project root directory and set up a virtual environment:
12+
13+
```bash
14+
# Navigate to the project root directory (one level up from samples)
15+
cd ..
16+
17+
# Create a new virtual environment using uv
18+
uv venv
19+
20+
# Activate the virtual environment (optional when using 'uv run')
21+
source .venv/bin/activate
22+
```
23+
924
### Install dependencies
1025

11-
First, install the required dependencies:
26+
Install the required dependencies using uv:
1227

1328
```bash
14-
# Install production dependencies
15-
pip install -e .
29+
# Make sure you are in the project root directory (where pyproject.toml is located)
30+
# Install the project in development mode
31+
uv pip install -e .
1632

1733
# Install development dependencies (optional, for development and testing)
18-
pip install -e ".[dev]"
34+
uv pip install -e ".[dev]"
35+
36+
# Alternative: Traditional pip method (after activating virtual environment)
37+
# pip install -e .
38+
# pip install -e ".[dev]"
1939
```
2040

2141
### Environment setup
@@ -27,7 +47,7 @@ Set up the required environment variable:
2747
export MCP_AUTH_ISSUER=<your_auth_issuer_url>
2848
```
2949

30-
## Directory Structure
50+
## Directory structure
3151

3252
- `current/`: Latest sample implementations (MCP server as resource server)
3353
- `v0_1_1/`: Legacy sample implementations (MCP server as authorization server)
@@ -48,8 +68,8 @@ To run the Todo Manager server:
4868
# Make sure you are in the samples directory first
4969
cd samples
5070

51-
# Start the Todo Manager server
52-
uvicorn current.todo-manager.server:app --host 0.0.0.0 --port 3001
71+
# Start the Todo Manager server using uv
72+
uv run uvicorn current.todo-manager.server:app --host 127.0.0.1 --port 3001
5373
```
5474

5575
## Legacy examples (v0.1.1)
@@ -63,12 +83,13 @@ A simple server that demonstrates basic authentication. It provides a single too
6383
- `whoami`: Returns the authenticated user's information
6484

6585
To run the WhoAmI server:
86+
6687
```bash
6788
# Make sure you are in the samples directory first
6889
cd samples
6990

70-
# Start the WhoAmI server
71-
uvicorn v0_1_1.whoami:app --host 0.0.0.0 --port 3001
91+
# Start the WhoAmI server using uv
92+
uv run uvicorn v0_1_1.whoami:app --host 127.0.0.1 --port 3001
7293
```
7394

7495
### Todo Manager MCP server (legacy)
@@ -80,10 +101,11 @@ Legacy version of the todo manager that acts as both authorization and resource
80101
- `delete-todo`: Delete a todo (requires `delete:todos` scope for others' todos)
81102

82103
To run the legacy Todo Manager server:
104+
83105
```bash
84106
# Make sure you are in the samples directory first
85107
cd samples
86108

87-
# Start the legacy Todo Manager server
88-
uvicorn v0_1_1.todo-manager.server:app --host 0.0.0.0 --port 3001
109+
# Start the legacy Todo Manager server using uv
110+
uv run uvicorn v0_1_1.todo-manager.server:app --host 127.0.0.1 --port 3001
89111
```

samples/current/todo-manager/server.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"""
1313

1414
import os
15+
import contextlib
16+
1517
from typing import Any, List, Optional
1618
from mcp.server.fastmcp import FastMCP
1719
from starlette.applications import Starlette
@@ -29,7 +31,7 @@
2931
from .service import TodoService
3032

3133
# Initialize the FastMCP server
32-
mcp = FastMCP("Todo Manager")
34+
mcp = FastMCP(name="Todo Manager", stateless_http=True)
3335

3436
# Initialize the todo service
3537
todo_service = TodoService()
@@ -44,7 +46,7 @@
4446
)
4547

4648
auth_server_config = fetch_server_config(auth_issuer, AuthServerType.OIDC)
47-
resource_id = "http://localhost:3001"
49+
resource_id = "http://localhost:3001/mcp"
4850
mcp_auth = MCPAuth(
4951
protected_resources=[
5052
ResourceServerConfig(
@@ -136,12 +138,19 @@ def delete_todo(id: str) -> dict[str, Any]:
136138
else:
137139
return {"error": "Failed to delete todo"}
138140

141+
@contextlib.asynccontextmanager
142+
async def lifespan(app: Starlette):
143+
async with contextlib.AsyncExitStack() as stack:
144+
await stack.enter_async_context(mcp.session_manager.run())
145+
yield
146+
139147
# Create the middleware and app
140148
bearer_auth = Middleware(mcp_auth.bearer_auth_middleware('jwt', resource=resource_id))
141149
app = Starlette(
142150
routes=[
143151
# Protect the MCP server with the Bearer auth middleware
144152
*mcp_auth.resource_metadata_router().routes,
145-
Mount("/", app=mcp.sse_app(), middleware=[bearer_auth]),
153+
Mount("/", app=mcp.streamable_http_app(), middleware=[bearer_auth]),
146154
],
155+
lifespan=lifespan,
147156
)

0 commit comments

Comments
 (0)