Skip to content

Commit 91637b1

Browse files
committed
TR updates, first round
1 parent 237a973 commit 91637b1

File tree

6 files changed

+162
-140
lines changed

6 files changed

+162
-140
lines changed

python-mcp-client/source-code-final/mcp_client/__main__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@
44
from mcp_client.mcp_client import MCPClient
55

66

7-
def main():
8-
"""Entry point for the mcp-client CLI app."""
7+
async def main() -> None:
8+
"""Run the MCP client with the specified options."""
99
args = parse_args()
1010
if not args.server_path.exists():
1111
print(f"Error: Server script '{args.server_path}' not found")
1212
return
1313

14-
async def run():
15-
try:
16-
async with MCPClient(args.server_path) as client:
17-
if args.members:
18-
await client.list_all_members()
19-
elif args.chat:
20-
await client.run_chat()
21-
except RuntimeError as e:
22-
print(e)
14+
try:
15+
async with MCPClient(str(args.server_path)) as client:
16+
if args.members:
17+
await client.list_all_members()
18+
elif args.chat:
19+
await client.run_chat()
20+
except RuntimeError as e:
21+
print(e)
22+
2323

24-
asyncio.run(run())
24+
def cli_main():
25+
"""Entry point for the mcp-client CLI app."""
26+
asyncio.run(main())
2527

2628

2729
if __name__ == "__main__":
28-
main()
30+
asyncio.run(main())

python-mcp-client/source-code-final/mcp_client/handlers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ async def _execute_tool(self, tool_call) -> dict:
8989
tool_args = json.loads(tool_call.function.arguments or "{}")
9090

9191
try:
92-
result = await self.client_session.call_tool(tool_name, tool_args)
92+
result = await self.client_session.call_tool(
93+
tool_name,
94+
tool_args,
95+
)
9396
content = result.content[0].text if result.content else ""
9497
log = f"[Used {tool_name}({tool_args})]"
9598
except Exception as e:
Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from contextlib import AsyncExitStack
3-
from typing import Self
3+
from typing import Any, Awaitable, Callable, Self
44

55
from mcp import ClientSession, StdioServerParameters
66
from mcp.client.stdio import stdio_client
@@ -17,12 +17,39 @@ class MCPClient:
1717
# Call client methods here...
1818
"""
1919

20-
client_session: ClientSession
21-
2220
def __init__(self, server_path: str):
2321
self.server_path = server_path
2422
self.exit_stack = AsyncExitStack()
2523

24+
async def __aenter__(self) -> Self:
25+
self.client_session = await self._connect_to_server()
26+
return self
27+
28+
async def __aexit__(self, *_) -> None:
29+
await self.exit_stack.aclose()
30+
31+
async def _connect_to_server(self) -> ClientSession:
32+
try:
33+
read, write = await self.exit_stack.enter_async_context(
34+
stdio_client(
35+
server=StdioServerParameters(
36+
command="sh",
37+
args=[
38+
"-c",
39+
f"{sys.executable} {self.server_path} 2>/dev/null",
40+
],
41+
env=None,
42+
)
43+
)
44+
)
45+
client_session = await self.exit_stack.enter_async_context(
46+
ClientSession(read, write)
47+
)
48+
await client_session.initialize()
49+
return client_session
50+
except Exception:
51+
raise RuntimeError("Error: Failed to connect to server")
52+
2653
async def list_all_members(self) -> None:
2754
"""List all available tools, prompts, and resources."""
2855
print("MCP Server Members")
@@ -38,28 +65,13 @@ async def list_all_members(self) -> None:
3865

3966
print("\n" + "=" * 50)
4067

41-
async def run_chat(self) -> None:
42-
"""Start interactive chat with MCP server using OpenAI."""
43-
try:
44-
handler = OpenAIQueryHandler(self.client_session)
45-
await chat.run_chat(handler)
46-
except RuntimeError as e:
47-
print(e)
48-
49-
async def __aenter__(self) -> Self:
50-
self.client_session = await self._connect_to_server()
51-
return self
52-
53-
async def __aexit__(self, *_) -> None:
54-
await self.exit_stack.aclose()
55-
5668
async def _list_section(
5769
self,
5870
section: str,
59-
list_func,
71+
list_method: Callable[[], Awaitable[Any]],
6072
) -> None:
6173
try:
62-
items = getattr(await list_func(), section)
74+
items = getattr(await list_method(), section)
6375
if items:
6476
print(f"\n{section.upper()} ({len(items)}):")
6577
print("-" * 30)
@@ -71,24 +83,10 @@ async def _list_section(
7183
except Exception as e:
7284
print(f"\n{section.upper()}: Error - {e}")
7385

74-
async def _connect_to_server(self) -> ClientSession:
86+
async def run_chat(self) -> None:
87+
"""Start interactive chat with MCP server using OpenAI."""
7588
try:
76-
stdio, write = await self.exit_stack.enter_async_context(
77-
stdio_client(
78-
server=StdioServerParameters(
79-
command="sh",
80-
args=[
81-
"-c",
82-
f"{sys.executable} {self.server_path} 2>/dev/null",
83-
],
84-
env=None,
85-
)
86-
)
87-
)
88-
client_session = await self.exit_stack.enter_async_context(
89-
ClientSession(stdio, write)
90-
)
91-
await client_session.initialize()
92-
return client_session
93-
except Exception:
94-
raise RuntimeError("Error: Failed to connect to server")
89+
handler = OpenAIQueryHandler(self.client_session)
90+
await chat.run_chat(handler)
91+
except RuntimeError as e:
92+
print(e)

python-mcp-client/source-code-final/mcp_server/mcp_server.py

100755100644
File mode changed.

python-mcp-client/source-code-final/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies = [
1010
]
1111

1212
[project.scripts]
13-
mcp-client = "mcp_client.__main__:main"
13+
mcp-client = "mcp_client.__main__:cli_main"
1414

1515
[build-system]
1616
requires = ["setuptools>=61.0", "wheel"]

0 commit comments

Comments
 (0)