Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 62 additions & 4 deletions packages/backend/embedding_atlas/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""Command line interface."""

import importlib
import logging
import pathlib
import socket
from pathlib import Path
from typing import Literal

import click
import inquirer
Expand Down Expand Up @@ -255,6 +257,13 @@ def import_modules(names: list[str]):
default=True,
help="Automatically find an available port if the specified port is in use.",
)
@click.option(
"--cors",
default=None,
is_flag=False,
flag_value="",
help="Allow cross-origin requests. Use --cors to allow all origins, or --cors http://example.com for a specific domain (or a comma-separated list of domains).",
)
@click.option(
"--static", type=str, help="Custom path to frontend static files directory."
)
Expand Down Expand Up @@ -288,6 +297,12 @@ def import_modules(names: list[str]):
default=None,
help="Path to a file containing labels for the embedding view. The file should be a table with columns 'x', 'y', 'text', and optionally 'level' and 'priority'",
)
@click.option(
"--mcp/--no-mcp",
"enable_mcp",
default=False,
help="Enable MCP (Model Context Protocol) server endpoints for external tool integration.",
)
@click.version_option(version=__version__, package_name="embedding_atlas")
def main(
inputs,
Expand All @@ -299,7 +314,7 @@ def main(
model: str | None,
trust_remote_code: bool,
batch_size: int | None,
text_projector: str,
text_projector: Literal["sentence_transformers", "litellm"],
api_key: str | None,
api_base: str | None,
dimensions: int | None,
Expand All @@ -318,11 +333,13 @@ def main(
host: str,
port: int,
enable_auto_port: bool,
cors: str | None,
export_application: str | None,
with_modules: list[str] | None,
point_size: float | None,
stop_words: str | None,
labels: str | None,
enable_mcp: bool,
):
apply_logging_config()

Expand Down Expand Up @@ -392,7 +409,7 @@ def main(
y=y_column,
neighbors=new_neighbors_column,
model=model,
text_projector=text_projector, # type: ignore
text_projector=text_projector,
trust_remote_code=trust_remote_code,
batch_size=batch_size,
umap_args=umap_args,
Expand Down Expand Up @@ -457,15 +474,56 @@ def main(
f.write(dataset.make_archive(static))
exit(0)

app = make_server(dataset, static_path=static, duckdb_uri=duckdb)
# Parse CORS configuration
cors_config = False
if cors is not None:
if cors == "":
# --cors flag without value means allow all origins
cors_config = True
else:
# --cors=domain1.com,domain2.com means specific domains
cors_config = [
domain.strip() for domain in cors.split(",") if domain.strip()
]

app = make_server(
dataset, static_path=static, duckdb_uri=duckdb, mcp=enable_mcp, cors=cors_config
)

if enable_auto_port:
new_port = find_available_port(port, max_attempts=10, host=host)
if new_port != port:
logger.info(f"Port {port} is not available, using {new_port}")
else:
new_port = port
uvicorn.run(app, port=new_port, host=host, access_log=False)

print()
print(click.style("-" * 79, dim=True))
print()
print(
f" {click.style('🚀 Embedding Atlas', fg='green', bold=True)} {click.style('v' + __version__, fg='green')}"
)
print()
print(f" ➜ URL: {click.style(f'http://{host}:{new_port}', fg='cyan', bold=True)}")
print(
click.style(
" ➜ Network: use --host to expose, use --cors to enable cross-origin requests",
dim=True,
)
)
if enable_mcp:
print(
f" ➜ MCP server: {click.style(f'http://{host}:{new_port}/mcp', fg='blue')}"
)
else:
print(click.style(" ➜ MCP server: use --mcp to enable", dim=True))
print(click.style(" ➜ Press CTRL+C to quit", dim=True))
print()
print(click.style("-" * 79, dim=True))

uvicorn.run(
app, port=new_port, host=host, access_log=False, log_level=logging.ERROR
)


if __name__ == "__main__":
Expand Down
162 changes: 147 additions & 15 deletions packages/backend/embedding_atlas/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Callable

import duckdb
from fastapi import FastAPI, Request, Response
from fastapi import FastAPI, HTTPException, Request, Response, WebSocket
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
Expand All @@ -21,19 +21,33 @@

def make_server(
data_source: DataSource,
*,
static_path: str,
mcp: bool = False,
cors: bool | list[str] = False,
duckdb_uri: str | None = None,
):
"""Creates a server for hosting Embedding Atlas"""

app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)

if cors is not None:
if isinstance(cors, bool) and cors:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)
elif isinstance(cors, list):
app.add_middleware(
CORSMiddleware,
allow_origins=cors,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)

mount_bytes(
app,
Expand All @@ -44,24 +58,34 @@ def make_server(

@app.get("/data/metadata.json")
async def get_metadata():
meta = {}
# Database
if duckdb_uri is None or duckdb_uri == "wasm":
db_meta = {"database": {"type": "wasm", "load": True}}
meta["database"] = {"type": "wasm", "load": True}
elif duckdb_uri == "server":
# Point to the server itself.
db_meta = {"database": {"type": "rest"}}
meta["database"] = {"type": "rest"}
else:
# Point to the given uri.
if duckdb_uri.startswith("http"):
db_meta = {
"database": {"type": "rest", "uri": duckdb_uri, "load": True}
meta["database"] = {
"type": "rest",
"uri": duckdb_uri,
"load": True,
}
elif duckdb_uri.startswith("ws"):
db_meta = {
"database": {"type": "socket", "uri": duckdb_uri, "load": True}
meta["database"] = {
"type": "socket",
"uri": duckdb_uri,
"load": True,
}
else:
raise ValueError("invalid DuckDB uri")
return data_source.metadata | db_meta
# MCP
if mcp:
meta["mcp"] = {"type": "websocket"}

return data_source.metadata | meta

@app.post("/data/cache/{name}")
async def post_cache(request: Request, name: str):
Expand Down Expand Up @@ -163,16 +187,124 @@ async def post_selection(req: Request):
executor, lambda: handle_selection(data)
)

if mcp:
make_mcp_proxy(app)

# Static files for the frontend
app.mount("/", StaticFiles(directory=static_path, html=True))

return app


class WebSocketHandler:
def __init__(self, websocket: WebSocket):
self.websocket = websocket
self.pending_requests: dict[str, asyncio.Future] = {}
self.is_connected = True

async def handle_connection(self):
try:
while self.is_connected:
data = await self.websocket.receive_text()
await self._handle_message(data)
except Exception as _:
pass
finally:
await self._cleanup()

async def _handle_message(self, data: str):
try:
response = json.loads(data)
request_id = response.get("id")
if request_id and request_id in self.pending_requests:
future = self.pending_requests.pop(request_id)
if not future.done():
future.set_result(response.get("response"))
except json.JSONDecodeError:
pass
except Exception as e:
print(f"Error processing WebSocket message: {e}")

async def _cleanup(self):
self.is_connected = False
for future in self.pending_requests.values():
if not future.done():
future.cancel()
self.pending_requests.clear()

async def send_request(self, request: dict) -> dict:
"""Send a request to the WebSocket and wait for response"""
if not self.is_connected:
raise HTTPException(status_code=503, detail="WebSocket disconnected")

request_id = str(uuid.uuid4())
payload = {"id": request_id, "request": request}

future = asyncio.Future()
self.pending_requests[request_id] = future

try:
await self.websocket.send_text(json.dumps(payload))
response = await asyncio.wait_for(future, timeout=30.0)
return response

except asyncio.TimeoutError:
self.pending_requests.pop(request_id, None)
raise HTTPException(status_code=408, detail="Request timeout")
except Exception as e:
self.pending_requests.pop(request_id, None)
if not self.is_connected:
raise HTTPException(status_code=503, detail="WebSocket disconnected")
else:
raise HTTPException(
status_code=500, detail=f"Internal server error: {str(e)}"
)

async def send_close(self):
try:
await self.websocket.send_text(json.dumps({"control": "close"}))
except Exception:
pass


def make_mcp_proxy(app: FastAPI):
# Registry to track the last connected WebSocket handler
last_handler: dict[str, WebSocketHandler | None] = {"handler": None}

@app.websocket("/data/mcp_websocket")
async def websocket_mcp_ws(websocket: WebSocket):
await websocket.accept()

# Create a new handler for this WebSocket connection
handler = WebSocketHandler(websocket)
if last_handler["handler"] is not None:
# Tell the existing client to close.
await last_handler["handler"].send_close()
last_handler["handler"] = handler

# Handle the connection (this will block until disconnection)
await handler.handle_connection()

# Clear the handler if it was the last one
if last_handler["handler"] == handler:
last_handler["handler"] = None

@app.post("/mcp")
async def post_mcp(request: Request):
# Check if we have a connected WebSocket handler
handler = last_handler["handler"]
if handler is None or not handler.is_connected:
raise HTTPException(status_code=503, detail="No MCP WebSocket connected")

return await handler.send_request(await request.json())


def make_duckdb_connection(df):
con = duckdb.connect(":memory:")
_ = df # used in the query
con.sql("CREATE TABLE dataset AS (SELECT * FROM df)")
con.sql("SET enable_external_access = false")
con.sql("SET lock_configuration = true")
return con


Expand Down
1 change: 1 addition & 0 deletions packages/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies = [
"accelerate >= 1.5.0",
"tqdm >= 4.60.0",
"litellm >= 1.70.0",
"websockets >= 15.0.1",
]

[project.scripts]
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

uv run embedding-atlas spawn99/wine-reviews --text description --split train --static ../viewer/dist "$@"
uv run embedding-atlas spawn99/wine-reviews --text description --split train --static ../viewer/dist --cors http://localhost:5173 --mcp "$@"
Loading