Python Environment Setup
Relevant source files
The following files were used as context for generating this wiki page:
The Python environment within the MCP Games monorepo specifically supports the narrative-ai service. This service is a FastAPI-based application responsible for AI-powered narrative generation, character modeling, and plot branching using Large Language Models (LLMs) like OpenAI and Anthropic.
Sources: README.md:46-51, apps/narrative-ai/README.md:3-10
Prerequisites and Core Requirements
The project specifies exact version requirements for the Python runtime to ensure compatibility with the narrative-ai service dependencies.
| Component | Requirement | Description |
|---|---|---|
| Python | >= 3.11 | Minimum runtime version required for the narrative-ai application. |
| Virtual Environment | venv | Standard Python module used for dependency isolation. |
| Package Manager | pip | Used to install requirements defined in requirements.txt. |
Sources: README.md:104, apps/narrative-ai/README.md:15
Installation and Configuration
Setting up the Python environment involves creating a virtual isolation layer and installing necessary libraries for FastAPI and LLM integration.
Initialization Workflow
The diagram above illustrates the standard sequence for preparing the Python-based Narrative AI service.
Sources: README.md:115-120, apps/narrative-ai/README.md:15-18
Environment Variables
The narrative-ai service relies on specific environment variables for API authentication and model configuration. These are loaded using python-dotenv.
| Variable | Default / Example | Purpose |
|---|---|---|
OPENAI_API_KEY | your_key_here | Authentication for OpenAI LLM services. |
ANTHROPIC_API_KEY | your_key_here | Authentication for Anthropic LLM services. |
DEFAULT_MODEL | gpt-4-turbo-preview | The default LLM used for narrative generation. |
PORT | 8000 | The local port the FastAPI server listens on. |
Sources: apps/narrative-ai/README.md:33-37, apps/narrative-ai/main.py:9
Execution and Development
The service is executed using uvicorn, an ASGI server implementation for Python. In a development context, it is typically run with the --reload flag to allow for hot-reloading during code changes.
# Activation and Execution
source venv/bin/activate
uvicorn main:app --reload
Sources: README.md:143-145, apps/narrative-ai/README.md:21-24
Service Integration Logic
The Python environment interacts with the broader monorepo through RESTful API endpoints. The cyoa-engine (Next.js) refers to this environment via the NARRATIVE_AI_URL environment variable.
This sequence shows how the Python environment facilitates the narrative generation flow.
Sources: apps/cyoa-engine/next.config.js:7, apps/narrative-ai/main.py:46-55
Summary
The Python environment setup is a critical step for enabling the AI capabilities of the MCP Games ecosystem. By utilizing a dedicated virtual environment and FastAPI, the project ensures a modular and scalable approach to narrative generation that remains isolated from the TypeScript-based frontend and integration layers.