Skip to content

Commit 020957c

Browse files
bm-claudeaiphernandezclaude
authored
feat: Multi-project support, OAuth authentication, and major improvements (#119)
Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9437a5f commit 020957c

File tree

169 files changed

+13405
-3447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+13405
-3447
lines changed

.env.oauth.example

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# OAuth Configuration for Basic Memory MCP Server
2+
# Copy this file to .env and update the values
3+
4+
# Enable OAuth authentication
5+
FASTMCP_AUTH_ENABLED=true
6+
7+
# OAuth provider type: basic, github, google, or supabase
8+
# - basic: Built-in OAuth provider with in-memory storage
9+
# - github: Integrate with GitHub OAuth
10+
# - google: Integrate with Google OAuth
11+
# - supabase: Integrate with Supabase Auth (recommended for production)
12+
FASTMCP_AUTH_PROVIDER=basic
13+
14+
# OAuth issuer URL (your MCP server URL)
15+
FASTMCP_AUTH_ISSUER_URL=http://localhost:8000
16+
17+
# Documentation URL for OAuth endpoints
18+
FASTMCP_AUTH_DOCS_URL=http://localhost:8000/docs/oauth
19+
20+
# Required scopes (comma-separated)
21+
# Examples: read,write,admin
22+
FASTMCP_AUTH_REQUIRED_SCOPES=read,write
23+
24+
# Secret key for JWT tokens (auto-generated if not set)
25+
# FASTMCP_AUTH_SECRET_KEY=your-secret-key-here
26+
27+
# Enable client registration endpoint
28+
FASTMCP_AUTH_CLIENT_REGISTRATION_ENABLED=true
29+
30+
# Enable token revocation endpoint
31+
FASTMCP_AUTH_REVOCATION_ENABLED=true
32+
33+
# Default scopes for new clients
34+
FASTMCP_AUTH_DEFAULT_SCOPES=read
35+
36+
# Valid scopes that can be requested
37+
FASTMCP_AUTH_VALID_SCOPES=read,write,admin
38+
39+
# Client secret expiry in seconds (optional)
40+
# FASTMCP_AUTH_CLIENT_SECRET_EXPIRY=86400
41+
42+
# GitHub OAuth settings (if using github provider)
43+
# GITHUB_CLIENT_ID=your-github-client-id
44+
# GITHUB_CLIENT_SECRET=your-github-client-secret
45+
46+
# Google OAuth settings (if using google provider)
47+
# GOOGLE_CLIENT_ID=your-google-client-id
48+
# GOOGLE_CLIENT_SECRET=your-google-client-secret
49+
50+
# Supabase settings (if using supabase provider)
51+
# SUPABASE_URL=https://your-project.supabase.co
52+
# SUPABASE_ANON_KEY=your-anon-key
53+
# SUPABASE_SERVICE_KEY=your-service-key # Optional, for admin operations
54+
# SUPABASE_JWT_SECRET=your-jwt-secret # Optional, for token validation
55+
# SUPABASE_ALLOWED_CLIENTS=client1,client2 # Comma-separated list of allowed client IDs

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ ENV/
5151

5252

5353
# claude action
54-
claude-output
54+
claude-output
55+
**/.claude/settings.local.json

AUTH.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# OAuth Quick Start
2+
3+
Basic Memory supports OAuth authentication for secure access control. For detailed documentation, see [OAuth Authentication Guide](docs/OAuth%20Authentication%20Guide.md).
4+
5+
## Quick Test with MCP Inspector
6+
7+
```bash
8+
# 1. Set a consistent secret key
9+
export FASTMCP_AUTH_SECRET_KEY="test-secret-key"
10+
11+
# 2. Start server with OAuth
12+
FASTMCP_AUTH_ENABLED=true basic-memory mcp --transport streamable-http
13+
14+
# 3. In another terminal, get a test token
15+
export FASTMCP_AUTH_SECRET_KEY="test-secret-key" # Same key!
16+
basic-memory auth test-auth
17+
18+
# 4. Copy the access token and use in MCP Inspector:
19+
# - Server URL: http://localhost:8000/mcp
20+
# - Transport: streamable-http
21+
# - Custom Headers:
22+
# Authorization: Bearer YOUR_ACCESS_TOKEN
23+
# Accept: application/json, text/event-stream
24+
```
25+
26+
## OAuth Endpoints
27+
28+
- `GET /authorize` - Authorization endpoint
29+
- `POST /token` - Token exchange endpoint
30+
- `GET /.well-known/oauth-authorization-server` - OAuth metadata
31+
32+
## Common Issues
33+
34+
1. **401 Unauthorized**: Make sure you're using the same secret key for both server and client
35+
2. **404 Not Found**: Use `/authorize` not `/auth/authorize`
36+
3. **Token Invalid**: Tokens don't persist across server restarts with basic provider
37+
38+
## Documentation
39+
40+
- [OAuth Authentication Guide](docs/OAuth%20Authentication%20Guide.md) - Complete setup guide
41+
- [Supabase OAuth Setup](docs/Supabase%20OAuth%20Setup.md) - Production deployment
42+
- [External OAuth Providers](docs/External%20OAuth%20Providers.md) - GitHub, Google integration

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ See the [README.md](README.md) file for a project overview.
6565
- Test database uses in-memory SQLite
6666
- Avoid creating mocks in tests in most circumstances.
6767
- Each test runs in a standalone environment with in memory SQLite and tmp_file directory
68+
- Do not use mocks in tests if possible. Tests run with an in memory sqlite db, so they are not needed. See fixtures in conftest.py
6869

6970
## BASIC MEMORY PRODUCT USAGE
7071

DB-REFACTOR.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# App-Level Database Refactoring
2+
3+
This document outlines the plan for migrating Basic Memory from per-project SQLite databases to a single app-level database that manages all knowledge data across projects.
4+
5+
## Goals
6+
7+
- Move to a single app-level SQLite database for all knowledge data
8+
- Deprecate per-project databases completely
9+
- Add project information to entities, observations, and relations
10+
- Simplify project switching and management
11+
- Enable better multi-project support for the Pro app
12+
- Prepare for cloud/GoHighLevel integration
13+
14+
## Architecture Changes
15+
16+
We're moving from:
17+
```
18+
~/.basic-memory/config.json (project list)
19+
~/basic-memory/[project-name]/.basic-memory/memory.db (one DB per project)
20+
```
21+
22+
To:
23+
```
24+
~/.basic-memory/config.json (project list) <- same
25+
~/.basic-memory/memory.db (app-level DB with project/entity/observation/search_index tables)
26+
~/basic-memory/[project-name]/.basic-memory/memory.db (project DBs deprecated) <- we are removing these
27+
```
28+
29+
## Implementation Tasks
30+
31+
### 1. Configuration Changes
32+
33+
- [x] Update config.py to use a single app database for all projects
34+
- [x] Add functions to get app database path for all operations
35+
- [x] Keep JSON-based config.json for project listing/paths
36+
- [x] Update project configuration loading to use app DB for all operations
37+
38+
39+
### 3. Project Model Implementation
40+
41+
- [x] Create Project SQLAlchemy model in models/project.py
42+
- [x] Define attributes: id, name, path, config, etc.
43+
- [x] Add proper indexes and constraints
44+
- [x] Add project_id foreign key to Entity, Observation, and Relation models
45+
- [x] Create migration script for updating schema with project relations
46+
- [x] Implement app DB initialization with project table
47+
48+
### 4. Repository Layer Updates
49+
50+
- [x] Create ProjectRepository for CRUD operations on Project model
51+
- [x] Update base Repository class to filter queries by project_id
52+
- [x] Update existing repositories to use project context automatically
53+
- [x] Implement query scoping to specific projects
54+
- [x] Add functions for project context management
55+
56+
### 5. Search Functionality Updates
57+
58+
- [x] Update search_index table to include project_id
59+
- [x] Modify search queries to filter by project_id
60+
- [x] Update FTS (Full Text Search) to be project-aware
61+
- [x] Add appropriate indices for efficient project-scoped searches
62+
- [x] Update search repository for project context
63+
64+
### 6. Service Layer Updates
65+
66+
- [x] Update ProjectService to manage projects in the database
67+
- [x] Add methods for project creation, deletion, updating
68+
- [x] Modify existing services to use project context
69+
- [x] Update initialization service for app DB setup
70+
- [x] ~~Implement project switching logic~~
71+
72+
### 7. Sync Service Updates
73+
74+
- [x] Modify background sync service to handle project context
75+
- [x] Update file watching to support multiple project directories
76+
- [x] Add project context to file sync events
77+
- [x] Update file path resolution to respect project boundaries
78+
- [x] Handle file change detection with project awareness
79+
80+
### 8. API Layer Updates
81+
82+
- [x] Update API endpoints to include project context
83+
- [x] Create new endpoints for project management
84+
- [x] Modify dependency injection to include project context
85+
- [x] Add request/response models for project operations
86+
- [x] ~~Implement middleware for project context handling~~
87+
- [x] Update error handling to include project information
88+
89+
### 9. MCP Tools Updates
90+
91+
- [x] Update MCP tools to include project context
92+
- [x] Add project selection capabilities to MCP server
93+
- [x] Update context building to respect project boundaries
94+
- [x] Update file operations to handle project paths correctly
95+
- [x] Add project-aware helper functions for MCP tools
96+
97+
### 10. CLI Updates
98+
99+
- [x] Update CLI commands to work with app DB
100+
- [x] Add or update project management commands
101+
- [x] Implement project switching via app DB
102+
- [x] Ensure CLI help text reflects new project structure
103+
- [x] ~~Add migration commands for existing projects~~
104+
- [x] Update project CLI commands to use the API with direct config fallback
105+
- [x] Added tests for CLI project commands
106+
107+
### 11. Performance Optimizations
108+
109+
- [x] Add proper indices for efficient project filtering
110+
- [x] Optimize queries for multi-project scenarios
111+
- [x] ~~Add query caching if needed~~
112+
- [x] Monitor and optimize performance bottlenecks
113+
114+
### 12. Testing Updates
115+
116+
- [x] Update test fixtures to support project context
117+
- [x] Add multi-project testing scenarios
118+
- [x] Create tests for migration processes
119+
- [ ] Test performance with larger multi-project datasets
120+
121+
### 13 Migrations
122+
123+
- [x] project table
124+
- [x] search project_id index
125+
- [x] project import/sync - during initialization
126+
127+
## Database Schema Changes
128+
129+
### New Project Table
130+
```sql
131+
CREATE TABLE project (
132+
id INTEGER PRIMARY KEY,
133+
name TEXT NOT NULL UNIQUE,
134+
description TEXT,
135+
path TEXT NOT NULL,
136+
config JSON,
137+
is_active BOOLEAN DEFAULT TRUE,
138+
is_default BOOLEAN DEFAULT FALSE,
139+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
140+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
141+
);
142+
```
143+
144+
### Modified Entity Table
145+
```sql
146+
ALTER TABLE entity ADD COLUMN project_id INTEGER REFERENCES project(id);
147+
CREATE INDEX ix_entity_project_id ON entity(project_id);
148+
```
149+
150+
### Modified Observation Table
151+
```sql
152+
-- No direct changes needed as observations are linked to entities which have project_id
153+
CREATE INDEX ix_observation_entity_project_id ON observation(entity_id, project_id);
154+
```
155+
156+
### Modified Relation Table
157+
```sql
158+
-- No direct changes needed as relations are linked to entities which have project_id
159+
CREATE INDEX ix_relation_from_project_id ON relation(from_id, project_id);
160+
CREATE INDEX ix_relation_to_project_id ON relation(to_id, project_id);
161+
```
162+
163+
## Migration Path
164+
165+
For existing projects, we'll:
166+
1. Create the project table in the app database
167+
2. For each project in config.json:
168+
a. Register the project in the project table
169+
b. Import all entities, observations, and relations from the project's DB
170+
c. Set the project_id on all imported records
171+
3. Validate that all data has been migrated correctly
172+
4. Keep config.json but use the database as the source of truth
173+
174+
## Testing
175+
176+
- [x] Test project creation, switching, deletion
177+
- [x] Test knowledge operations (entity, observation, relation) with project context
178+
- [x] Verify existing projects can be migrated successfully
179+
- [x] Test multi-project operations
180+
- [x] Test error cases (missing project, etc.)
181+
- [x] Test CLI commands with multiple projects
182+
- [x] Test CLI error handling for API failures
183+
- [x] Test CLI commands use only API, no config fallback
184+
185+
## Current Status
186+
187+
The app-level database refactoring is now complete! We have successfully:
188+
189+
1. Migrated from per-project SQLite databases to a single app-level database
190+
2. Added project context to all layers of the application (models, repositories, services, API)
191+
3. Implemented bidirectional synchronization between config.json and the database
192+
4. Updated all API endpoints to include project context
193+
5. Enhanced project management capabilities in both the API and CLI
194+
6. Added comprehensive test coverage for project operations
195+
7. Modified the directory router and all other routers to respect project boundaries
196+
197+
The only remaining task is to thoroughly test performance with larger multi-project datasets, which can be done as part of regular usage monitoring.
198+
199+
## CLI API Integration
200+
201+
The CLI commands have been updated to use the API endpoints for project management operations. This includes:
202+
203+
1. The `project list` command now fetches projects from the API
204+
2. The `project add` command creates projects through the API
205+
3. The `project remove` command removes projects through the API
206+
4. The `project default` command sets the default project through the API
207+
5. Added a new `project sync` command to synchronize projects between config and database
208+
6. The `project current` command now shows detailed project information from the API
209+
210+
This approach ensures that project operations performed through the CLI are synchronized with the database, maintaining consistency between the configuration file and the app-level database. Failed API requests result in a proper error message instructing the user to ensure the Basic Memory server is running, rather than falling back to direct config updates. This ensures that the database remains the single source of truth for project information.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ format:
4141

4242
# run inspector tool
4343
run-inspector:
44-
uv run mcp dev src/basic_memory/mcp/main.py
44+
npx @modelcontextprotocol/inspector
4545

4646
# Build app installer
4747
installer-mac:

0 commit comments

Comments
 (0)