Configure cognee-cli -ui mcp to connect to cognee backend

This commit is contained in:
Daulet Amirkhanov 2025-10-09 14:01:28 +01:00
parent 3e23c96595
commit bd49dd7bb5
4 changed files with 98 additions and 44 deletions

View file

@ -65,6 +65,9 @@ ENV PYTHONUNBUFFERED=1
ENV MCP_LOG_LEVEL=DEBUG ENV MCP_LOG_LEVEL=DEBUG
ENV PYTHONPATH=/app ENV PYTHONPATH=/app
# Add labels for API mode usage
LABEL org.opencontainers.image.description="Cognee MCP Server with API mode support"
# Use the application name from pyproject.toml for normal operation # Use the application name from pyproject.toml for normal operation
# For testing, we'll override this with a direct command # For testing, we'll override this with a direct command
ENTRYPOINT ["/app/entrypoint.sh"] ENTRYPOINT ["/app/entrypoint.sh"]

View file

@ -14,61 +14,94 @@ HTTP_PORT=${HTTP_PORT:-8000}
echo "Debug port: $DEBUG_PORT" echo "Debug port: $DEBUG_PORT"
echo "HTTP port: $HTTP_PORT" echo "HTTP port: $HTTP_PORT"
# Run Alembic migrations with proper error handling. # Check if API mode is enabled
# Note on UserAlreadyExists error handling: if [ -n "$API_URL" ]; then
# During database migrations, we attempt to create a default user. If this user echo "API mode enabled: $API_URL"
# already exists (e.g., from a previous deployment or migration), it's not a echo "Skipping database migrations (API server handles its own database)"
# critical error and shouldn't prevent the application from starting. This is else
# different from other migration errors which could indicate database schema echo "Direct mode: Using local cognee instance"
# inconsistencies and should cause the startup to fail. This check allows for # Run Alembic migrations with proper error handling.
# smooth redeployments and container restarts while maintaining data integrity. # Note on UserAlreadyExists error handling:
echo "Running database migrations..." # During database migrations, we attempt to create a default user. If this user
# already exists (e.g., from a previous deployment or migration), it's not a
# critical error and shouldn't prevent the application from starting. This is
# different from other migration errors which could indicate database schema
# inconsistencies and should cause the startup to fail. This check allows for
# smooth redeployments and container restarts while maintaining data integrity.
echo "Running database migrations..."
MIGRATION_OUTPUT=$(alembic upgrade head) MIGRATION_OUTPUT=$(alembic upgrade head)
MIGRATION_EXIT_CODE=$? MIGRATION_EXIT_CODE=$?
if [[ $MIGRATION_EXIT_CODE -ne 0 ]]; then if [[ $MIGRATION_EXIT_CODE -ne 0 ]]; then
if [[ "$MIGRATION_OUTPUT" == *"UserAlreadyExists"* ]] || [[ "$MIGRATION_OUTPUT" == *"User default_user@example.com already exists"* ]]; then if [[ "$MIGRATION_OUTPUT" == *"UserAlreadyExists"* ]] || [[ "$MIGRATION_OUTPUT" == *"User default_user@example.com already exists"* ]]; then
echo "Warning: Default user already exists, continuing startup..." echo "Warning: Default user already exists, continuing startup..."
else else
echo "Migration failed with unexpected error." echo "Migration failed with unexpected error."
exit 1 exit 1
fi
fi fi
fi
echo "Database migrations done." echo "Database migrations done."
fi
echo "Starting Cognee MCP Server with transport mode: $TRANSPORT_MODE" echo "Starting Cognee MCP Server with transport mode: $TRANSPORT_MODE"
# Add startup delay to ensure DB is ready # Add startup delay to ensure DB is ready
sleep 2 sleep 2
# Build API arguments if API_URL is set
API_ARGS=""
if [ -n "$API_URL" ]; then
# Handle localhost in API_URL - convert to host-accessible address
if echo "$API_URL" | grep -q "localhost" || echo "$API_URL" | grep -q "127.0.0.1"; then
echo "⚠️ Warning: API_URL contains localhost/127.0.0.1"
echo " Original: $API_URL"
# Try to use host.docker.internal (works on Mac/Windows and recent Linux with Docker Desktop)
FIXED_API_URL=$(echo "$API_URL" | sed 's/localhost/host.docker.internal/g' | sed 's/127\.0\.0\.1/host.docker.internal/g')
echo " Converted to: $FIXED_API_URL"
echo " This will work on Mac/Windows/Docker Desktop."
echo " On Linux without Docker Desktop, you may need to:"
echo " - Use --network host, OR"
echo " - Set API_URL=http://172.17.0.1:8000 (Docker bridge IP)"
API_URL="$FIXED_API_URL"
fi
API_ARGS="--api-url $API_URL"
if [ -n "$API_TOKEN" ]; then
API_ARGS="$API_ARGS --api-token $API_TOKEN"
fi
fi
# Modified startup with transport mode selection and error handling # Modified startup with transport mode selection and error handling
if [ "$ENVIRONMENT" = "dev" ] || [ "$ENVIRONMENT" = "local" ]; then if [ "$ENVIRONMENT" = "dev" ] || [ "$ENVIRONMENT" = "local" ]; then
if [ "$DEBUG" = "true" ]; then if [ "$DEBUG" = "true" ]; then
echo "Waiting for the debugger to attach..." echo "Waiting for the debugger to attach..."
if [ "$TRANSPORT_MODE" = "sse" ]; then if [ "$TRANSPORT_MODE" = "sse" ]; then
exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
elif [ "$TRANSPORT_MODE" = "http" ]; then elif [ "$TRANSPORT_MODE" = "http" ]; then
exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
else else
exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport stdio --no-migration exec python -m debugpy --wait-for-client --listen 0.0.0.0:$DEBUG_PORT -m cognee-mcp --transport stdio --no-migration $API_ARGS
fi fi
else else
if [ "$TRANSPORT_MODE" = "sse" ]; then if [ "$TRANSPORT_MODE" = "sse" ]; then
exec cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration exec cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
elif [ "$TRANSPORT_MODE" = "http" ]; then elif [ "$TRANSPORT_MODE" = "http" ]; then
exec cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration exec cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
else else
exec cognee-mcp --transport stdio --no-migration exec cognee-mcp --transport stdio --no-migration $API_ARGS
fi fi
fi fi
else else
if [ "$TRANSPORT_MODE" = "sse" ]; then if [ "$TRANSPORT_MODE" = "sse" ]; then
exec cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration exec cognee-mcp --transport sse --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
elif [ "$TRANSPORT_MODE" = "http" ]; then elif [ "$TRANSPORT_MODE" = "http" ]; then
exec cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration exec cognee-mcp --transport http --host 0.0.0.0 --port $HTTP_PORT --no-migration $API_ARGS
else else
exec cognee-mcp --transport stdio --no-migration exec cognee-mcp --transport stdio --no-migration $API_ARGS
fi fi
fi fi

View file

@ -18,8 +18,7 @@ from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware from starlette.middleware.cors import CORSMiddleware
import uvicorn import uvicorn
# Import the new CogneeClient abstraction from .cognee_client import CogneeClient
from cognee_client import CogneeClient
try: try:
@ -38,7 +37,6 @@ mcp = FastMCP("Cognee")
logger = get_logger() logger = get_logger()
# Global CogneeClient instance (will be initialized in main())
cognee_client: Optional[CogneeClient] = None cognee_client: Optional[CogneeClient] = None

View file

@ -507,19 +507,35 @@ def start_ui(
try: try:
image = "cognee/cognee-mcp:main" image = "cognee/cognee-mcp:main"
subprocess.run(["docker", "pull", image], check=True) subprocess.run(["docker", "pull", image], check=True)
docker_cmd = [
"docker",
"run",
"-p",
f"{mcp_port}:8000",
"--rm",
"--env-file",
env_file,
"-e",
"TRANSPORT_MODE=sse",
]
if start_backend:
docker_cmd.extend(
[
"-e",
f"API_URL=http://localhost:{backend_port}",
]
)
logger.info(
f"Configuring MCP to connect to backend API at http://localhost:{backend_port}"
)
logger.info("(localhost will be auto-converted to host.docker.internal)")
docker_cmd.append("cognee/cognee-mcp:daulet-dev")
mcp_process = subprocess.Popen( mcp_process = subprocess.Popen(
[ docker_cmd,
"docker",
"run",
"-p",
f"{mcp_port}:8000",
"--rm",
"--env-file",
env_file,
"-e",
"TRANSPORT_MODE=sse",
"cognee/cognee-mcp:main",
],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
preexec_fn=os.setsid if hasattr(os, "setsid") else None, preexec_fn=os.setsid if hasattr(os, "setsid") else None,
@ -529,7 +545,11 @@ def start_ui(
_stream_process_output(mcp_process, "stderr", "[MCP]", "\033[34m") # Blue _stream_process_output(mcp_process, "stderr", "[MCP]", "\033[34m") # Blue
pid_callback(mcp_process.pid) pid_callback(mcp_process.pid)
logger.info(f"✓ Cognee MCP server starting on http://127.0.0.1:{mcp_port}/sse")
mode_info = "API mode" if start_backend else "direct mode"
logger.info(
f"✓ Cognee MCP server starting on http://127.0.0.1:{mcp_port}/sse ({mode_info})"
)
except Exception as e: except Exception as e:
logger.error(f"Failed to start MCP server with Docker: {str(e)}") logger.error(f"Failed to start MCP server with Docker: {str(e)}")
# Start backend server if requested # Start backend server if requested