python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

MCP Python client fails to load the server

Open rchaganti opened this issue 1 year ago • 14 comments

Describe the bug I am following the documentation that shows building a Python client. I already have a sample server that works well with Claude Desktop. This server sample is https://github.com/rchaganti/azure-mcp-server. However, when I try to run the client using uv run client.py C:\\GitHub\\azure-mcp-server\\src\\azure_mcp_server\\server.py, it fails with a message:

Traceback (most recent call last): File "C:\GitHub\azure-mcp-server\src\azure_mcp_server\server.py", line 3, in from mcp.server.models import InitializationOptions ModuleNotFoundError: No module named 'mcp'

To Reproduce Steps to reproduce the behavior:

  1. Go to https://modelcontextprotocol.io/quickstart/client
  2. Follow the procedure to create an MCP client locally
  3. Download the Azure MCP Server sample from https://github.com/rchaganti/azure-mcp-server
  4. Run the client, uv run client.py C:\\GitHub\\azure-mcp-server\\src\\azure_mcp_server\\server.py

Expected behavior Client starts and a chatbot starts with a query prompt.

Desktop:

  • OS: Windows 11
  • Browser Edge

rchaganti avatar Mar 15 '25 15:03 rchaganti

Yeah I have this bug too, unfortunately I don't know why it happens, but I hope there will be an answer soon. I will tell you once I know why it happens 🫡

gdlambda avatar Mar 20 '25 14:03 gdlambda

Actually found a solution: server_params = StdioServerParameters( command="uv", # Executable args=["run", "../server/server.py"], # Optional command line arguments env=None, # Optional environment variables ) Use uv run as command instead of python.

gdlambda avatar Mar 20 '25 14:03 gdlambda

Atleast thats the case for me since I was new to uv and kinda missed that :|

gdlambda avatar Mar 20 '25 14:03 gdlambda

Interesting. @gdilillo Where are you adding the StdioServerParameters()? can you point me to your example?

rchaganti avatar Mar 20 '25 15:03 rchaganti

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client


# Create server parameters for stdio connection
server_params = StdioServerParameters(
    command="uv",  # Executable
    args=["run", "../server/server.py"],  # Optional command line arguments
    env=None,  # Optional environment variables
)

async def run():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(
            read, write
        ) as session:
            # Initialize the connection
            await session.initialize()

            # List available tools
            tools = await session.list_tools()
            print(tools)


if __name__ == "__main__":
    import asyncio

    asyncio.run(run())

Here is my client script, its simplified for testing reasons, but in the quickstart example its this line:

command = "python" if is_python else "node"
    server_params = StdioServerParameters(
        command=command,
        args=[server_script_path],
        env=None
    )

In the async def connect_to_server(self, server_script_path: str) method. Hope this helps :)

gdlambda avatar Mar 20 '25 15:03 gdlambda

Thank you. For me, this does not solve, but the error moved from mcp to azure!

File "C:\Github\azure-mcp-server\src\azure_mcp_server\server.py", line 11, in from azure.identity import EnvironmentCredential ModuleNotFoundError: No module named 'azure'

rchaganti avatar Mar 20 '25 15:03 rchaganti

Maybe you forgot to add azure in the server modules. Try doing uv add azure in the server environment

gdlambda avatar Mar 20 '25 16:03 gdlambda

Nah! Server works with Claude Desktop! So, there is something wrong in the way the client is trying to load. Seems like it is not getting the environment.

rchaganti avatar Mar 20 '25 16:03 rchaganti

If I don't activate the client venv and run:

uv run .\client.py c:\github\azure-mcp-server\src\azure_mcp_server\server.py warning: VIRTUAL_ENV=C:\GitHub\azure-mcp-server\.venv does not match the project environment path .venv and will be ignored; use --active to target the active environment instead Traceback (most recent call last): File "c:\github\azure-mcp-server\src\azure_mcp_server\server.py", line 3, in from mcp.server.models import InitializationOptions ModuleNotFoundError: No module named 'mcp'

The Python client documentation is certainly missing some step.

rchaganti avatar Mar 20 '25 16:03 rchaganti

Idk if this helps, but I found out, that you just need all dependencies installed in your client environment, atleast that worked for me.

gdlambda avatar Mar 21 '25 14:03 gdlambda

@gdilillo unfortunately, no! Even if it worked, I'd still question that design.

rchaganti avatar Mar 21 '25 14:03 rchaganti

Actually found a solution: server_params = StdioServerParameters( command="uv", # Executable args=["run", "../server/server.py"], # Optional command line arguments env=None, # Optional environment variables ) Use uv run as command instead of python.

It works for me. Thank you so much!

IndifferentYk avatar Mar 22 '25 09:03 IndifferentYk

You will need to specify the full path for uv command. Try the command to get the full executable path which uv, then replace that in the configuration. uv run mcp run client.py C:\\GitHub\\azure-mcp-server\\src\\azure_mcp_server\\server.

glanzz avatar Apr 10 '25 05:04 glanzz

If I don't activate the client venv and run:

uv run .\client.py c:\github\azure-mcp-server\src\azure_mcp_server\server.py warning: VIRTUAL_ENV=C:\GitHub\azure-mcp-server\.venv does not match the project environment path .venv and will be ignored; use --active to target the active environment instead Traceback (most recent call last): File "c:\github\azure-mcp-server\src\azure_mcp_server\server.py", line 3, in from mcp.server.models import InitializationOptions ModuleNotFoundError: No module named 'mcp'

The Python client documentation is certainly missing some step.

same issues

XIAOke8698 avatar Apr 28 '25 00:04 XIAOke8698