Skip to content

Commit 87adb39

Browse files
committed
fix: Improve session management in mcp_tools.py by adding error handling and cleanup for client sessions
1 parent 575555b commit 87adb39

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/upsonicai/server/tools/server/mcp_tools.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,31 @@ class ListToolsRequest(BaseRequestMCP):
2626
pass
2727

2828

29+
# Global variable to store the session
30+
session_store = None
31+
2932
async def get_session(command: str, args: list, env: dict):
3033
print("env", env)
3134
print("args", args)
3235
print("command", command)
3336

34-
if env:
35-
env = get_default_environment()
36-
env.update(env) if env else None
37-
3837
server_params = StdioServerParameters(
3938
command=command, # Executable
4039
args=args, # Optional command line arguments
4140
env=env, # Environment variables
4241
)
4342
async with stdio_client(server_params) as (read, write):
4443
async with ClientSession(read, write) as session:
45-
await session.initialize()
46-
yield session
47-
await session.close()
44+
try:
45+
print("Initializing session...")
46+
await session.initialize()
47+
print("Session initialized.")
48+
yield session
49+
except Exception as e:
50+
print(f"Error in session: {e}")
51+
raise
52+
finally:
53+
print("Session cleanup.")
4854

4955

5056
@app.post(f"{prefix}/tools")

0 commit comments

Comments
 (0)