|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
4 | | -echo "==> Initializing Letta database schema..." |
| 4 | +# Start Letta server in the background. Letta handles its own schema |
| 5 | +# creation and seeds the default org/user on first boot — we don't need |
| 6 | +# to create tables or insert rows manually. |
| 7 | +echo "==> Starting Letta server..." |
| 8 | +letta server --host 0.0.0.0 --port 8283 --ade & |
| 9 | +LETTA_PID=$! |
5 | 10 |
|
6 | | -# Wait for PostgreSQL to be ready |
7 | | -until python3 -c "import psycopg2; psycopg2.connect('$LETTA_PG_URI')" 2>/dev/null; do |
8 | | - echo "Waiting for PostgreSQL..." |
9 | | - sleep 2 |
| 11 | +# Wait for Letta to become healthy (it creates the schema + default org |
| 12 | +# during startup, so the /v1/health endpoint only succeeds after that). |
| 13 | +echo "==> Waiting for Letta to be ready..." |
| 14 | +MAX_WAIT=180 |
| 15 | +WAITED=0 |
| 16 | +until curl -sf http://localhost:8283/v1/health > /dev/null 2>&1; do |
| 17 | + if ! kill -0 "$LETTA_PID" 2>/dev/null; then |
| 18 | + echo "==> ERROR: Letta process exited unexpectedly" |
| 19 | + exit 1 |
| 20 | + fi |
| 21 | + if [ "$WAITED" -ge "$MAX_WAIT" ]; then |
| 22 | + echo "==> ERROR: Letta did not become healthy within ${MAX_WAIT}s" |
| 23 | + exit 1 |
| 24 | + fi |
| 25 | + sleep 3 |
| 26 | + WAITED=$((WAITED + 3)) |
| 27 | + echo " Still waiting... (${WAITED}s)" |
10 | 28 | done |
11 | 29 |
|
12 | | -echo "PostgreSQL is ready!" |
13 | | - |
14 | | -# Create schema using Letta ORM |
15 | | -python3 << 'EOF' |
16 | | -import os |
17 | | -from letta.orm import Base |
18 | | -from sqlalchemy import create_engine, inspect |
19 | | -
|
20 | | -# Get connection string from environment |
21 | | -pg_uri = os.environ.get('LETTA_PG_URI') |
22 | | -print(f"Creating schema with URI: {pg_uri}") |
| 30 | +echo "==> Letta is healthy (took ~${WAITED}s)" |
23 | 31 |
|
24 | | -# Create engine and tables |
25 | | -engine = create_engine(pg_uri, echo=True) |
26 | | -Base.metadata.create_all(engine) |
| 32 | +# Register the Thoth MCP server via the REST API. This avoids the |
| 33 | +# foreign-key issues that come from inserting directly into the DB |
| 34 | +# before Letta seeds its default organization. |
| 35 | +# THOTH_MCP_URL comes from docker-compose.letta.yml (default: http://thoth-mcp:8001). |
| 36 | +# The MCP endpoint path is /mcp under that base URL. |
| 37 | +THOTH_MCP_BASE="${THOTH_MCP_URL:-http://thoth-mcp:8001}" |
| 38 | +THOTH_MCP_ENDPOINT="${THOTH_MCP_BASE}/mcp" |
27 | 39 |
|
28 | | -# Verify tables were created |
29 | | -inspector = inspect(engine) |
30 | | -tables = inspector.get_table_names() |
31 | | -print(f"\n==> Created {len(tables)} tables:") |
32 | | -for table in sorted(tables): |
33 | | - print(f" ✓ {table}") |
| 40 | +echo "==> Registering Thoth MCP server via API..." |
34 | 41 |
|
35 | | -if 'organizations' in tables: |
36 | | - print("\n==> Schema initialization successful!") |
37 | | -else: |
38 | | - print("\n==> ERROR: organizations table not found!") |
39 | | - exit(1) |
40 | | -EOF |
| 42 | +# Check if already registered |
| 43 | +EXISTING=$(curl -sf http://localhost:8283/v1/tools/mcp/servers 2>/dev/null || echo "[]") |
| 44 | +if echo "$EXISTING" | grep -q '"thoth-research-tools"'; then |
| 45 | + echo "==> Thoth MCP server already registered" |
| 46 | +else |
| 47 | + # The /v1/tools/mcp/servers endpoint registers a new MCP server. |
| 48 | + # Letta 0.16+ uses streamable_http transport. |
| 49 | + RESPONSE=$(curl -sf -X POST http://localhost:8283/v1/tools/mcp/servers \ |
| 50 | + -H "Content-Type: application/json" \ |
| 51 | + -d "{ |
| 52 | + \"server_name\": \"thoth-research-tools\", |
| 53 | + \"server_type\": \"streamable_http\", |
| 54 | + \"server_url\": \"${THOTH_MCP_ENDPOINT}\" |
| 55 | + }" 2>&1) || true |
41 | 56 |
|
42 | | -# Register Thoth MCP server in database |
43 | | -echo "==> Registering Thoth MCP server in database..." |
44 | | -python3 << 'EOF' |
45 | | -import os |
46 | | -import uuid |
47 | | -from sqlalchemy import create_engine, text |
| 57 | + if echo "$RESPONSE" | grep -q '"id"'; then |
| 58 | + echo "==> Thoth MCP server registered successfully" |
| 59 | + else |
| 60 | + # Non-fatal: the MCP server can be registered later via the UI |
| 61 | + echo "==> Warning: Could not register MCP server via API." |
| 62 | + echo " Response: ${RESPONSE}" |
| 63 | + echo " You can register it manually in the Letta ADE." |
| 64 | + fi |
| 65 | +fi |
48 | 66 |
|
49 | | -pg_uri = os.environ.get('LETTA_PG_URI') |
50 | | -engine = create_engine(pg_uri) |
| 67 | +echo "==> Letta initialization complete, server running (pid $LETTA_PID)" |
51 | 68 |
|
52 | | -# Check if MCP server already registered |
53 | | -with engine.connect() as conn: |
54 | | - result = conn.execute(text("SELECT COUNT(*) FROM mcp_server WHERE server_name = 'thoth-research-tools'")) |
55 | | - count = result.scalar() |
56 | | -
|
57 | | - if count == 0: |
58 | | - # Insert MCP server registration |
59 | | - # Note: Letta requires 'mcp_server-' prefix (underscore, not hyphen) |
60 | | - mcp_server_id = f"mcp_server-{uuid.uuid4()}" |
61 | | - conn.execute(text(""" |
62 | | - INSERT INTO mcp_server ( |
63 | | - id, server_name, server_type, server_url, |
64 | | - organization_id, is_deleted, |
65 | | - _created_by_id, _last_updated_by_id |
66 | | - ) VALUES ( |
67 | | - :id, :name, :type, :url, |
68 | | - 'org-00000000-0000-4000-8000-000000000000', false, |
69 | | - 'user-00000000-0000-4000-8000-000000000000', |
70 | | - 'user-00000000-0000-4000-8000-000000000000' |
71 | | - ) |
72 | | - """), { |
73 | | - "id": mcp_server_id, |
74 | | - "name": "thoth-research-tools", |
75 | | - "type": "streamable_http", |
76 | | - "url": "http://thoth-mcp:8000/mcp" |
77 | | - }) |
78 | | - conn.commit() |
79 | | - print(f"✓ Registered Thoth MCP server: {mcp_server_id}") |
80 | | - else: |
81 | | - print("✓ Thoth MCP server already registered") |
82 | | -EOF |
83 | | - |
84 | | -echo "==> Starting Letta server..." |
85 | | -exec letta server --host 0.0.0.0 --port 8283 --ade |
| 69 | +# Bring the Letta process to the foreground so Docker can track it |
| 70 | +wait "$LETTA_PID" |
0 commit comments