From 9b1b399565098eaeb37dfc0a44b44028bbdd3e3c Mon Sep 17 00:00:00 2001 From: Dharshana-Belraj Date: Fri, 18 Jul 2025 22:48:08 +0530 Subject: [PATCH] Add .env file support for local development - Add python-dotenv dependency for loading environment variables from .env files - Create .env.example template with all required and optional variables - Update README.md with local development section and .env usage instructions - Maintains full backward compatibility with existing environment variable setup - Improves developer experience for local development and testing --- README.md | 19 +++++++++++++++++++ requirements.txt | 1 + src/mysql_mcp_server/.env.example | 8 ++++++++ src/mysql_mcp_server/server.py | 3 +++ 4 files changed, 31 insertions(+) create mode 100644 src/mysql_mcp_server/.env.example diff --git a/README.md b/README.md index f517802..5ab536f 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,25 @@ MYSQL_PASSWORD=your_password MYSQL_DATABASE=your_database ``` +## Local Development with .env Files +For local development, you can use a .env file instead of setting environment variables manually: + +Copy .env.example to .env: +``` bash + cp .env.example .env + +# Edit .env with your database credentials: +MYSQL_HOST=localhost +MYSQL_PORT=3306 +MYSQL_USER=your_username +MYSQL_PASSWORD=your_password +MYSQL_DATABASE=your_database +``` +The server will automatically load these variables when starting + + +Note: The .env file is for local development convenience. In production or when using Smithery, continue to use environment variables as documented above. + ## Usage ### With Claude Desktop Add this to your `claude_desktop_config.json`: diff --git a/requirements.txt b/requirements.txt index 604e871..2310240 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ mcp>=1.0.0 mysql-connector-python>=9.1.0 +python-dotenv>=1.0.0 \ No newline at end of file diff --git a/src/mysql_mcp_server/.env.example b/src/mysql_mcp_server/.env.example new file mode 100644 index 0000000..08233a7 --- /dev/null +++ b/src/mysql_mcp_server/.env.example @@ -0,0 +1,8 @@ +MYSQL_HOST=localhost +MYSQL_PORT=3306 +MYSQL_USER=your_username +MYSQL_PASSWORD=your_password +MYSQL_DATABASE=your_database +MYSQL_CHARSET=utf8mb4 +MYSQL_COLLATION=utf8mb4_unicode_ci +MYSQL_SQL_MODE=TRADITIONAL \ No newline at end of file diff --git a/src/mysql_mcp_server/server.py b/src/mysql_mcp_server/server.py index 15fcdbd..3b23a26 100644 --- a/src/mysql_mcp_server/server.py +++ b/src/mysql_mcp_server/server.py @@ -6,6 +6,9 @@ from mcp.server import Server from mcp.types import Resource, Tool, TextContent from pydantic import AnyUrl +from dotenv import load_dotenv + +load_dotenv() # Configure logging logging.basicConfig(