sandbox-notebookllamalm/src/notebookllama/server.py
DJP 2292f8a511 Transform NotebookLlaMa to enterprise multi-user NotebookLM clone
Major Changes:
- Implemented notebook-first architecture (multi-document collections)
- Added user authentication and multi-tenancy
- Created comprehensive database schema (7 tables)
- Built complete notebook management system
- Implemented notebook-level chat across multiple documents
- Added podcast generation from notebooks
- Implemented notebook sharing with permissions
- Fixed MCP server crashes by bypassing for document processing
- Added all enterprise features requested

New Features:
- User login/signup with bcrypt password hashing
- Create/edit/delete notebooks
- Upload multiple PDFs to notebooks
- Add documents to existing notebooks
- Chat across all documents in a notebook
- Generate podcasts from entire notebooks
- Share notebooks with other users
- View shared notebooks
- Persistent chat history per notebook
- Document summaries, Q&A, and highlights

Technical Improvements:
- PostgreSQL database with SQLAlchemy ORM
- Connection pooling and proper cleanup
- Bypassed buggy MCP server for document processing
- Direct LlamaCloud API calls for reliability
- Proper CASCADE deletes
- Session management
- Error handling and logging

Documentation:
- ENTERPRISE_SETUP.md - Setup guide
- IMPLEMENTATION_SUMMARY.md - Technical details
- SIMPLIFIED_PLAN.md - Architecture overview
- CURRENT_STATUS.md - Status and known issues
- FINAL_README.md - User guide

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-01 17:28:06 -04:00

88 lines
3.1 KiB
Python

from utils import get_mind_map, process_file, query_index
from fastmcp import FastMCP
from typing import List, Union, Literal
import os
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
mcp: FastMCP = FastMCP(name="MCP For NotebookLM")
# Check if all required environment variables are set
REQUIRED_ENV_VARS = [
"LLAMACLOUD_API_KEY",
"EXTRACT_AGENT_ID",
"LLAMACLOUD_PIPELINE_ID",
"OPENAI_API_KEY"
]
missing_vars = [var for var in REQUIRED_ENV_VARS if not os.getenv(var)]
if missing_vars:
logger.error(f"Missing required environment variables: {', '.join(missing_vars)}")
SERVICES_AVAILABLE = False
else:
SERVICES_AVAILABLE = True
@mcp.tool(
name="process_file_tool",
description="This tool is useful to process files and produce summaries, question-answers and highlights.",
)
async def process_file_tool(
filename: str,
) -> Union[str, Literal["Sorry, your file could not be processed."]]:
if not SERVICES_AVAILABLE:
logger.error("Services not available due to missing environment variables")
return "Sorry, your file could not be processed."
try:
notebook_model, text = await process_file(filename=filename)
if notebook_model is None:
return "Sorry, your file could not be processed."
if text is None:
text = ""
return notebook_model + "\n%separator%\n" + text
except Exception as e:
logger.error(f"Error processing file: {e}", exc_info=True)
return "Sorry, your file could not be processed."
@mcp.tool(name="get_mind_map_tool", description="This tool is useful to get a mind map")
async def get_mind_map_tool(
summary: str, highlights: List[str]
) -> Union[str, Literal["Sorry, mind map creation failed."]]:
if not SERVICES_AVAILABLE:
logger.error("Services not available due to missing environment variables")
return "Sorry, mind map creation failed."
try:
mind_map_fl = await get_mind_map(summary=summary, highlights=highlights)
if mind_map_fl is None:
return "Sorry, mind map creation failed."
return mind_map_fl
except Exception as e:
logger.error(f"Error creating mind map: {e}", exc_info=True)
return "Sorry, mind map creation failed."
@mcp.tool(name="query_index_tool", description="Query a LlamaCloud index.")
async def query_index_tool(question: str) -> str:
if not SERVICES_AVAILABLE:
logger.error("Services not available due to missing environment variables")
return "Sorry, I was unable to find an answer to your question."
try:
response = await query_index(question=question)
if response is None:
return "Sorry, I was unable to find an answer to your question."
return response
except Exception as e:
logger.error(f"Error querying index: {e}", exc_info=True)
return "Sorry, I was unable to find an answer to your question."
if __name__ == "__main__":
if not SERVICES_AVAILABLE:
logger.warning("Starting MCP server with limited functionality due to missing environment variables")
mcp.run(transport="streamable-http")