Commit
·
1c0c480
1
Parent(s):
ef0ee7c
fix: pass env variables to mcp server
Browse files- langchain_mcp_client.py +36 -1
- postgre_mcp_server.py +3 -0
langchain_mcp_client.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os.path
|
| 2 |
import json
|
| 3 |
from typing import Tuple, Any
|
|
@@ -55,6 +56,8 @@ async def lc_mcp_exec(request: str, history=None) -> Tuple[str, list]:
|
|
| 55 |
async with stdio_client(server_params) as (read, write):
|
| 56 |
async with ClientSession(read, write) as session:
|
| 57 |
await session.initialize()
|
|
|
|
|
|
|
| 58 |
tools = await load_and_enrich_tools(session)
|
| 59 |
agent = create_react_agent(llm, tools)
|
| 60 |
|
|
@@ -103,13 +106,45 @@ def load_table_summary(path: str) -> str:
|
|
| 103 |
return file.read()
|
| 104 |
|
| 105 |
|
|
|
|
| 106 |
def get_server_params() -> StdioServerParameters:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return StdioServerParameters(
|
| 108 |
command="python",
|
| 109 |
-
args=[os.environ["MCP_SERVER_PATH"]],
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
|
|
|
| 113 |
async def load_and_enrich_tools(session: ClientSession):
|
| 114 |
tools = await load_mcp_tools(session)
|
| 115 |
return tools
|
|
|
|
| 1 |
+
import os
|
| 2 |
import os.path
|
| 3 |
import json
|
| 4 |
from typing import Tuple, Any
|
|
|
|
| 56 |
async with stdio_client(server_params) as (read, write):
|
| 57 |
async with ClientSession(read, write) as session:
|
| 58 |
await session.initialize()
|
| 59 |
+
|
| 60 |
+
# Load tools and create the agent
|
| 61 |
tools = await load_and_enrich_tools(session)
|
| 62 |
agent = create_react_agent(llm, tools)
|
| 63 |
|
|
|
|
| 106 |
return file.read()
|
| 107 |
|
| 108 |
|
| 109 |
+
|
| 110 |
def get_server_params() -> StdioServerParameters:
|
| 111 |
+
load_dotenv()
|
| 112 |
+
|
| 113 |
+
# Prepare the environment dictionary to pass to the subprocess
|
| 114 |
+
subprocess_env = {}
|
| 115 |
+
|
| 116 |
+
# List of environment variables that the postgre_mcp_server.py needs
|
| 117 |
+
required_vars_for_server = [
|
| 118 |
+
# "TABLE_SUMMARY_PATH",
|
| 119 |
+
"DB_URL",
|
| 120 |
+
"DB_SCHEMA",
|
| 121 |
+
"PANDAS_KEY",
|
| 122 |
+
"PANDAS_EXPORTS_PATH",
|
| 123 |
+
# "GEMINI_API_KEY",
|
| 124 |
+
# "GEMINI_MODEL",
|
| 125 |
+
# "GEMINI_MODEL_PROVIDER",
|
| 126 |
+
# "OPENAI_MODEL_PROVIDER",
|
| 127 |
+
# "OPENAI_MODEL",
|
| 128 |
+
# "OPENAI_API_KEY",
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
for var_name in required_vars_for_server:
|
| 132 |
+
value = os.getenv(var_name)
|
| 133 |
+
if value is not None:
|
| 134 |
+
subprocess_env[var_name] = value
|
| 135 |
+
else:
|
| 136 |
+
logger.warning(f"Environment variable {var_name} not found for passing to MCP server subprocess.")
|
| 137 |
+
|
| 138 |
+
logger.info(f"Passing environment to MCP server subprocess: {subprocess_env.keys()}")
|
| 139 |
+
|
| 140 |
return StdioServerParameters(
|
| 141 |
command="python",
|
| 142 |
+
args=[os.environ["MCP_SERVER_PATH"]], # MCP_SERVER_PATH itself must be available to this client
|
| 143 |
+
env=subprocess_env
|
| 144 |
)
|
| 145 |
|
| 146 |
|
| 147 |
+
|
| 148 |
async def load_and_enrich_tools(session: ClientSession):
|
| 149 |
tools = await load_mcp_tools(session)
|
| 150 |
return tools
|
postgre_mcp_server.py
CHANGED
|
@@ -33,6 +33,9 @@ class DbContext:
|
|
| 33 |
pool: asyncpg.Pool
|
| 34 |
schema: str
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Database connection lifecycle manager
|
| 38 |
@asynccontextmanager
|
|
|
|
| 33 |
pool: asyncpg.Pool
|
| 34 |
schema: str
|
| 35 |
|
| 36 |
+
# Load environment variables
|
| 37 |
+
load_dotenv()
|
| 38 |
+
|
| 39 |
|
| 40 |
# Database connection lifecycle manager
|
| 41 |
@asynccontextmanager
|