Skip to content

Requesting the mcp server through the mcp stdio method cannot correctly read the environment variables #1064

Closed
@Jasper-zh

Description

@Jasper-zh

Question

Currently, only the second load generated when starting the main process is normal. Each request seems to re-execute the mcp server, and the environment variables cannot be reached.

main process

@app.on_event("startup")
async def startup_event():
    global agent
    agent = await Agent.create()

    process = Popen(
        [sys.executable, "mcp_server/mcp_server_stdio.py"],
        env=os.environ.copy(),
        stdin=PIPE,
        stdout=PIPE,
        stderr=PIPE
    )

mcp server

def get_backend_server():
        return os.getenv("BACKEND_SERVER_URL", "http://192.168.100.25:20484")

@mcp.tool()
def select_bot_event(bot_name=None, status: int=None, sortType: int=None):
    """
    ...
    """

    logging.info(f"backend_server:{get_backend_server()}")

if __name__ == "__main__":
    if "BACKEND_SERVER_URL" in os.environ:
        backend_server = os.environ["BACKEND_SERVER_URL"]
        logger.info(f"env: {backend_server}")
    else:
        logger.warning("NOT FOUND BACKEND_SERVER_URL")
    
    mcp.run(transport="stdio")

logs

2025-07-01 15:58:03,406 - __main__ - WARNING - NOT FOUND BACKEND_SERVER_URL
2025-07-01 15:58:03,419 - mcp.server.lowlevel.server - INFO - Processing request of type ListToolsRequest
2025-07-01 15:58:04,235 - __main__ - INFO - env: http://127.0.0.1:8010
2025-07-01 15:58:30,216 - __main__ - WARNING -  NOT FOUND BACKEND_SERVER_URL2025-07-01 15:58:30,231 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
2025-07-01 15:58:30,232 - root - INFO - backend_server:http://192.168.100.25:20484

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions