Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions mcp-client-python/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from anthropic import Anthropic
from dotenv import load_dotenv
from pathlib import Path

load_dotenv() # load environment variables from .env

Expand All @@ -30,14 +31,19 @@ async def connect_to_server(self, server_script_path: str):
is_js = server_script_path.endswith('.js')
if not (is_python or is_js):
raise ValueError("Server script must be a .py or .js file")

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


if is_python:
path = Path(server_script_path).resolve()
server_params = StdioServerParameters(
command="uv",
args=["--directory", str(path.parent), "run", path.name],
env=None,
)
else:
server_params = StdioServerParameters(
command="node", args=[server_script_path], env=None
)

stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
self.stdio, self.write = stdio_transport
self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
Expand Down