Skip to content

Commit a467523

Browse files
abrookinsclaude
andcommitted
Add --no-worker option to CLI commands for simplified development
- Add --no-worker flag to api and mcp CLI commands - Create HybridBackgroundTasks that uses either Docket or FastAPI background tasks - MCP stdio mode defaults to no-worker, SSE mode respects flag - Update quickstart documentation to use --no-worker for easier setup - Add comprehensive production deployment section with Docket guidance - Include 37 new tests covering all --no-worker functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent b775745 commit a467523

File tree

9 files changed

+710
-46
lines changed

9 files changed

+710
-46
lines changed

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ uv install --all-extras
2424
# Start Redis
2525
docker-compose up redis
2626

27-
# Start the server
28-
uv run agent-memory api
27+
# Start the server (development mode)
28+
uv run agent-memory api --no-worker
2929
```
3030

3131
### 2. Python SDK
@@ -62,11 +62,11 @@ results = await client.search_long_term_memory(
6262
### 3. MCP Integration
6363

6464
```bash
65-
# Start MCP server
65+
# Start MCP server (stdio mode - recommended for Claude Desktop)
6666
uv run agent-memory mcp
6767

68-
# Or with SSE mode
69-
uv run agent-memory mcp --mode sse --port 9000
68+
# Or with SSE mode (development mode)
69+
uv run agent-memory mcp --mode sse --port 9000 --no-worker
7070
```
7171

7272
## Documentation
@@ -121,11 +121,30 @@ docker-compose up
121121

122122
## Production Deployment
123123

124+
For production environments, use Docket workers for better reliability and scale:
125+
126+
```bash
127+
# Start the API server (production mode)
128+
uv run agent-memory api
129+
130+
# Start MCP server (production mode - SSE)
131+
uv run agent-memory mcp --mode sse --port 9000
132+
133+
# Start background workers (required for production)
134+
uv run agent-memory task-worker --concurrency 10
135+
```
136+
137+
**Production features:**
124138
- **Authentication**: OAuth2/JWT with multiple providers (Auth0, AWS Cognito, etc.)
125139
- **Redis**: Requires Redis with RediSearch module (RedisStack recommended)
126-
- **Scaling**: Supports Redis clustering and background task processing
140+
- **Background Processing**: Docket workers handle memory indexing, summarization, and compaction
141+
- **Scaling**: Supports Redis clustering and horizontal worker scaling
127142
- **Monitoring**: Structured logging and health checks included
128143

144+
**Development vs Production:**
145+
- **Development**: Use `--no-worker` flags for quick setup, tasks run inline
146+
- **Production**: Use separate worker processes for better performance and reliability
147+
129148
## License
130149

131150
Apache License 2.0 - see [LICENSE](LICENSE) file for details.

agent_memory_server/cli.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ async def run_migrations():
8282
@click.option("--port", default=settings.port, help="Port to run the server on")
8383
@click.option("--host", default="0.0.0.0", help="Host to run the server on")
8484
@click.option("--reload", is_flag=True, help="Enable auto-reload")
85-
def api(port: int, host: str, reload: bool):
85+
@click.option(
86+
"--no-worker", is_flag=True, help="Use FastAPI background tasks instead of Docket"
87+
)
88+
def api(port: int, host: str, reload: bool, no_worker: bool):
8689
"""Run the REST API server."""
8790
from agent_memory_server.main import on_start_logger
8891

8992
configure_logging()
93+
94+
# Set use_docket based on the --no-worker flag
95+
if no_worker:
96+
settings.use_docket = False
97+
9098
on_start_logger(port)
9199
uvicorn.run(
92100
"agent_memory_server.main:app",
@@ -104,7 +112,10 @@ def api(port: int, host: str, reload: bool):
104112
help="Run the MCP server in SSE or stdio mode",
105113
type=click.Choice(["stdio", "sse"]),
106114
)
107-
def mcp(port: int, mode: str):
115+
@click.option(
116+
"--no-worker", is_flag=True, help="Use FastAPI background tasks instead of Docket"
117+
)
118+
def mcp(port: int, mode: str, no_worker: bool):
108119
"""Run the MCP server."""
109120
import asyncio
110121

@@ -123,14 +134,19 @@ def mcp(port: int, mode: str):
123134
async def setup_and_run():
124135
# Redis setup is handled by the MCP app before it starts
125136

137+
# Set use_docket based on mode and --no-worker flag
138+
if mode == "stdio":
139+
# Don't run a task worker in stdio mode by default
140+
settings.use_docket = False
141+
elif no_worker:
142+
# Use --no-worker flag for SSE mode
143+
settings.use_docket = False
144+
126145
# Run the MCP server
127146
if mode == "sse":
128147
logger.info(f"Starting MCP server on port {port}\n")
129148
await mcp_app.run_sse_async()
130149
elif mode == "stdio":
131-
# Don't run a task worker in stdio mode.
132-
# TODO: Make configurable with a CLI flag?
133-
settings.use_docket = False
134150
await mcp_app.run_stdio_async()
135151
else:
136152
raise ValueError(f"Invalid mode: {mode}")

agent_memory_server/dependencies.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010
logger = get_logger(__name__)
1111

1212

13-
class DocketBackgroundTasks(BackgroundTasks):
14-
"""A BackgroundTasks implementation that uses Docket."""
13+
class HybridBackgroundTasks(BackgroundTasks):
14+
"""A BackgroundTasks implementation that can use either Docket or FastAPI background tasks."""
1515

1616
async def add_task(
1717
self, func: Callable[..., Any], *args: Any, **kwargs: Any
1818
) -> None:
19-
"""Run tasks either directly or through Docket"""
20-
from docket import Docket
21-
19+
"""Run tasks either directly, through Docket, or through FastAPI background tasks"""
2220
logger.info("Adding task to background tasks...")
2321

2422
if settings.use_docket:
2523
logger.info("Scheduling task through Docket")
24+
from docket import Docket
2625

2726
async with Docket(
2827
name=settings.docket_name,
@@ -31,15 +30,20 @@ async def add_task(
3130
# Schedule task through Docket
3231
await docket.add(func)(*args, **kwargs)
3332
else:
34-
logger.info("Running task directly")
35-
await func(*args, **kwargs)
33+
logger.info("Using FastAPI background tasks")
34+
# Use FastAPI's background tasks
35+
super().add_task(func, *args, **kwargs)
36+
37+
38+
# Backwards compatibility alias
39+
DocketBackgroundTasks = HybridBackgroundTasks
3640

3741

38-
def get_background_tasks() -> DocketBackgroundTasks:
42+
def get_background_tasks() -> HybridBackgroundTasks:
3943
"""
40-
Dependency function that returns a DocketBackgroundTasks instance.
44+
Dependency function that returns a HybridBackgroundTasks instance.
4145
4246
This is used by API endpoints to inject a consistent background tasks object.
4347
"""
4448
logger.info("Getting background tasks class")
45-
return DocketBackgroundTasks()
49+
return HybridBackgroundTasks()

docs/cli.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ agent-memory api [OPTIONS]
2727
- `--port INTEGER`: Port to run the server on. (Default: value from `settings.port`, usually 8000)
2828
- `--host TEXT`: Host to run the server on. (Default: "0.0.0.0")
2929
- `--reload`: Enable auto-reload for development.
30+
- `--no-worker`: Use FastAPI background tasks instead of Docket workers. Ideal for development and testing.
3031

31-
Example:
32+
**Examples:**
3233

3334
```bash
34-
agent-memory api --port 8080 --reload
35+
# Development mode (no separate worker needed)
36+
agent-memory api --port 8080 --reload --no-worker
37+
38+
# Production mode (requires separate worker process)
39+
agent-memory api --port 8080
3540
```
3641

3742
### `mcp`
@@ -45,20 +50,24 @@ agent-memory mcp [OPTIONS]
4550
**Options:**
4651

4752
- `--port INTEGER`: Port to run the MCP server on. (Default: value from `settings.mcp_port`, usually 9000)
48-
- `--sse`: Run the MCP server in Server-Sent Events (SSE) mode. If not provided, it runs in stdio mode.
53+
- `--mode [stdio|sse]`: Run the MCP server in stdio or SSE mode. (Default: stdio)
54+
- `--no-worker`: Use FastAPI background tasks instead of Docket workers. Ideal for development and testing.
4955

50-
Example (SSE mode):
56+
**Examples:**
5157

5258
```bash
53-
agent-memory mcp --port 9001 --sse
54-
```
59+
# Stdio mode (recommended for Claude Desktop) - automatically uses --no-worker
60+
agent-memory mcp
5561

56-
Example (stdio mode):
62+
# SSE mode for development (no separate worker needed)
63+
agent-memory mcp --mode sse --port 9001 --no-worker
5764

58-
```bash
59-
agent-memory mcp --port 9001
65+
# SSE mode for production (requires separate worker process)
66+
agent-memory mcp --mode sse --port 9001
6067
```
6168

69+
**Note:** Stdio mode automatically disables Docket workers as they're not needed when Claude Desktop manages the process lifecycle.
70+
6271
### `schedule-task`
6372

6473
Schedules a background task to be processed by a Docket worker.

docs/getting-started.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,34 @@ But you can also run these components via the CLI commands. Here's how you
2828
run the REST API server:
2929

3030
```bash
31+
# Development mode (no separate worker needed)
32+
uv run agent-memory api --no-worker
33+
34+
# Production mode (requires separate worker process)
3135
uv run agent-memory api
3236
```
3337

3438
Or the MCP server:
3539

3640
```bash
37-
uv run agent-memory mcp --mode <stdio|sse>
41+
# Stdio mode (recommended for Claude Desktop)
42+
uv run agent-memory mcp
43+
44+
# SSE mode for development
45+
uv run agent-memory mcp --mode sse --no-worker
46+
47+
# SSE mode for production
48+
uv run agent-memory mcp --mode sse
3849
```
3950

40-
Both servers require a worker to be running, which you can start like this:
51+
**For production deployments**, you'll need to run a separate worker process:
4152

4253
```bash
4354
uv run agent-memory task-worker
4455
```
4556

57+
**For development**, use the `--no-worker` flag to run tasks inline without needing a separate worker process.
58+
4659
**NOTE:** With uv, prefix the command with `uv`, e.g.: `uv run agent-memory --mode sse`. If you installed from source, you'll probably need to add `--directory` to tell uv where to find the code: `uv run --directory <path/to/checkout> run agent-memory --mode stdio`.
4760

4861
## Docker Compose

0 commit comments

Comments
 (0)